User OPENSSL_UNISTD instead of <unistd.h>.
[openssl.git] / apps / ocsp.c
index 32506a37cb4d731b286fbdfa7493a1e36538412c..ba456fc58faffebbf22b7850b29aa346ed2ff2c4 100644 (file)
 #include <openssl/pem.h>
 #include <openssl/ocsp.h>
 #include <openssl/err.h>
+#include <openssl/ssl.h>
 #include "apps.h"
 
+/* Maximum leeway in validity period: default 5 minutes */
+#define MAX_VALIDITY_PERIOD    (5 * 60)
+
 static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, X509 *issuer,
                                STACK_OF(OCSP_CERTID) *ids);
 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, X509 *issuer,
                                STACK_OF(OCSP_CERTID) *ids);
 static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
-                               STACK *names, STACK_OF(OCSP_CERTID) *ids);
+                               STACK *names, STACK_OF(OCSP_CERTID) *ids,
+                               long nsec, long maxage);
 
 #undef PROG
 #define PROG ocsp_main
@@ -78,12 +83,12 @@ int MAIN(int, char **);
 int MAIN(int argc, char **argv)
        {
        char **args;
-       char *host = NULL, *path = "/";
+       char *host = NULL, *port = NULL, *path = "/";
        char *reqin = NULL, *respin = NULL;
        char *reqout = NULL, *respout = NULL;
        char *signfile = NULL, *keyfile = NULL;
        char *outfile = NULL;
-       int add_nonce = 1, noverify = 0;
+       int add_nonce = 1, noverify = 0, use_ssl = -1;
        OCSP_REQUEST *req = NULL;
        OCSP_RESPONSE *resp = NULL;
        OCSP_BASICRESP *bs = NULL;
@@ -93,8 +98,10 @@ int MAIN(int argc, char **argv)
        BIO *cbio = NULL, *derbio = NULL;
        BIO *out = NULL;
        int req_text = 0, resp_text = 0;
+       long nsec = MAX_VALIDITY_PERIOD, maxage = -1;
        char *CAfile = NULL, *CApath = NULL;
        X509_STORE *store = NULL;
+       SSL_CTX *ctx = NULL;
        STACK_OF(X509) *sign_other = NULL, *verify_other = NULL;
        char *sign_certfile = NULL, *verify_certfile = NULL;
        unsigned long sign_flags = 0, verify_flags = 0;
@@ -104,7 +111,7 @@ int MAIN(int argc, char **argv)
        STACK *reqnames = NULL;
        STACK_OF(OCSP_CERTID) *ids = NULL;
        if (bio_err == NULL) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
-       ERR_load_crypto_strings();
+       SSL_load_error_strings();
        args = argv + 1;
        reqnames = sk_new_null();
        ids = sk_OCSP_CERTID_new_null();
@@ -119,6 +126,19 @@ int MAIN(int argc, char **argv)
                                }
                        else badarg = 1;
                        }
+               else if (!strcmp(*args, "-url"))
+                       {
+                       if (args[1])
+                               {
+                               args++;
+                               if (!OCSP_parse_url(*args, &host, &port, &path, &use_ssl))
+                                       {
+                                       BIO_printf(bio_err, "Error parsing URL\n");
+                                       badarg = 1;
+                                       }
+                               }
+                       else badarg = 1;
+                       }
                else if (!strcmp(*args, "-host"))
                        {
                        if (args[1])
@@ -232,6 +252,38 @@ int MAIN(int argc, char **argv)
                                }
                        else badarg = 1;
                        }
+               else if (!strcmp (*args, "-validity_period"))
+                       {
+                       if (args[1])
+                               {
+                               args++;
+                               nsec = atol(*args);
+                               if (nsec < 0)
+                                       {
+                                       BIO_printf(bio_err,
+                                               "Illegal validity period %s\n",
+                                               *args);
+                                       badarg = 1;
+                                       }
+                               }
+                       else badarg = 1;
+                       }
+               else if (!strcmp (*args, "-status_age"))
+                       {
+                       if (args[1])
+                               {
+                               args++;
+                               maxage = atol(*args);
+                               if (maxage < 0)
+                                       {
+                                       BIO_printf(bio_err,
+                                               "Illegal validity age %s\n",
+                                               *args);
+                                       badarg = 1;
+                                       }
+                               }
+                       else badarg = 1;
+                       }
                 else if (!strcmp(*args, "-signkey"))
                        {
                        if (args[1])
@@ -335,11 +387,14 @@ int MAIN(int argc, char **argv)
                BIO_printf (bio_err, "-respin file       read DER encoded OCSP reponse from \"file\"\n");
                BIO_printf (bio_err, "-nonce             add OCSP nonce to request\n");
                BIO_printf (bio_err, "-no_nonce          don't add OCSP nonce to request\n");
+               BIO_printf (bio_err, "-url URL           OCSP responder URL\n");
                BIO_printf (bio_err, "-host host:n       send OCSP request to host on port n\n");
                BIO_printf (bio_err, "-path              path to use in OCSP request\n");
                BIO_printf (bio_err, "-CApath dir        trusted certificates directory\n");
                BIO_printf (bio_err, "-CAfile file       trusted certificates file\n");
                BIO_printf (bio_err, "-VAfile file       validator certificates file\n");
+               BIO_printf (bio_err, "-validity_period n maximum validity discrepancy in seconds\n");
+               BIO_printf (bio_err, "-status_age n      maximum status age in seconds\n");
                BIO_printf (bio_err, "-noverify          don't verify response at all\n");
                BIO_printf (bio_err, "-verify_certs file additional certificates to search for signer\n");
                BIO_printf (bio_err, "-trust_other       don't verify additional certificates\n");
@@ -436,13 +491,22 @@ int MAIN(int argc, char **argv)
                        BIO_printf(bio_err, "Error creating connect BIO\n");
                        goto end;
                        }
+               if (port) BIO_set_conn_port(cbio, port);
+               if (use_ssl == 1)
+                       {
+                       BIO *sbio;
+                       ctx = SSL_CTX_new(SSLv23_client_method());
+                       SSL_CTX_set_mode(ctx, SSL_MODE_AUTO_RETRY);
+                       sbio = BIO_new_ssl(ctx, 1);
+                       cbio = BIO_push(sbio, cbio);
+                       }
                if (BIO_do_connect(cbio) <= 0)
                        {
                        BIO_printf(bio_err, "Error connecting BIO\n");
                        goto end;
                        }
                resp = OCSP_sendreq_bio(cbio, path, req);
-               BIO_free(cbio);
+               BIO_free_all(cbio);
                cbio = NULL;
                if (!resp)
                        {
@@ -515,10 +579,15 @@ int MAIN(int argc, char **argv)
 
        if (!noverify)
                {
-               if (req && (OCSP_check_nonce(req, bs) <= 0))
+               if (req && ((i = OCSP_check_nonce(req, bs)) <= 0))
                        {
-                       BIO_printf(bio_err, "Nonce Verify error\n");
-                       goto end;
+                       if (i == -1)
+                               BIO_printf(bio_err, "WARNING: no nonce in response\n");
+                       else
+                               {
+                               BIO_printf(bio_err, "Nonce Verify error\n");
+                               goto end;
+                               }
                        }
 
                i = OCSP_basic_verify(bs, verify_other, store, verify_flags);
@@ -534,7 +603,7 @@ int MAIN(int argc, char **argv)
 
                }
 
-       if (!print_ocsp_summary(out, bs, req, reqnames, ids))
+       if (!print_ocsp_summary(out, bs, req, reqnames, ids, nsec, maxage))
                goto end;
 
        ret = 0;
@@ -546,7 +615,7 @@ end:
        EVP_PKEY_free(key);
        X509_free(issuer);
        X509_free(cert);
-       BIO_free(cbio);
+       BIO_free_all(cbio);
        BIO_free(out);
        OCSP_REQUEST_free(req);
        OCSP_RESPONSE_free(resp);
@@ -556,6 +625,14 @@ end:
        sk_X509_pop_free(sign_other, X509_free);
        sk_X509_pop_free(verify_other, X509_free);
 
+       if (use_ssl != -1)
+               {
+               OPENSSL_free(host);
+               OPENSSL_free(port);
+               OPENSSL_free(path);
+               SSL_CTX_free(ctx);
+               }
+
        EXIT(ret);
 }
 
@@ -614,7 +691,8 @@ static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, X509 *issuer,
        }
 
 static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
-                                       STACK *names, STACK_OF(OCSP_CERTID) *ids)
+                                       STACK *names, STACK_OF(OCSP_CERTID) *ids,
+                                       long nsec, long maxage)
        {
        OCSP_CERTID *id;
        char *name;
@@ -639,6 +717,15 @@ static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
                        BIO_puts(out, "ERROR: No Status found.\n");
                        continue;
                        }
+
+               /* Check validity: if invalid write to output BIO so we
+                * know which response this refers to.
+                */
+               if (!OCSP_check_validity(thisupd, nextupd, nsec, maxage))
+                       {
+                       BIO_puts(out, "WARNING: Status times invalid.\n");
+                       ERR_print_errors(out);
+                       }
                BIO_printf(out, "%s\n", OCSP_cert_status_str(status));
 
                BIO_puts(out, "\tThis Update: ");
@@ -648,7 +735,7 @@ static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
                if(nextupd)
                        {
                        BIO_puts(out, "\tNext Update: ");
-                       ASN1_GENERALIZEDTIME_print(out, thisupd);
+                       ASN1_GENERALIZEDTIME_print(out, nextupd);
                        BIO_puts(out, "\n");
                        }