Add OPENSSL_NO_ECDH guards
[openssl.git] / apps / ocsp.c
index 6f36edcc527a55c504e0931562adab4355b9cea3..8883afc9565b010379f4f19b06efac1ce60c6c3d 100644 (file)
  */
 #ifndef OPENSSL_NO_OCSP
 
+#ifdef OPENSSL_SYS_VMS
 #define _XOPEN_SOURCE_EXTENDED /* So fd_set and friends get properly defined
                                   on OpenVMS */
+#endif
+
+#define USE_SOCKETS
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -71,6 +75,7 @@
 #include <openssl/ssl.h>
 #include <openssl/evp.h>
 #include <openssl/bn.h>
+#include <openssl/x509v3.h>
 
 #if defined(NETWARE_CLIB)
 #  ifdef NETWARE_BSDSOCK
@@ -95,7 +100,7 @@ static int add_ocsp_cert(OCSP_REQUEST **req, X509 *cert, const EVP_MD *cert_id_m
 static int add_ocsp_serial(OCSP_REQUEST **req, char *serial, const EVP_MD * cert_id_md, X509 *issuer,
                                STACK_OF(OCSP_CERTID) *ids);
 static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
-                             STACK_OF(STRING) *names,
+                             STACK_OF(OPENSSL_STRING) *names,
                              STACK_OF(OCSP_CERTID) *ids, long nsec,
                              long maxage);
 
@@ -109,6 +114,7 @@ static BIO *init_responder(char *port);
 static int do_responder(OCSP_REQUEST **preq, BIO **pcbio, BIO *acbio, char *port);
 static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp);
 static OCSP_RESPONSE *query_responder(BIO *err, BIO *cbio, char *path,
+                               STACK_OF(CONF_VALUE) *headers,
                                OCSP_REQUEST *req, int req_timeout);
 
 #undef PROG
@@ -121,12 +127,14 @@ int MAIN(int argc, char **argv)
        ENGINE *e = NULL;
        char **args;
        char *host = NULL, *port = NULL, *path = "/";
+       char *thost = NULL, *tport = NULL, *tpath = NULL;
        char *reqin = NULL, *respin = NULL;
        char *reqout = NULL, *respout = NULL;
        char *signfile = NULL, *keyfile = NULL;
        char *rsignfile = NULL, *rkeyfile = NULL;
        char *outfile = NULL;
        int add_nonce = 1, noverify = 0, use_ssl = -1;
+       STACK_OF(CONF_VALUE) *headers = NULL;
        OCSP_REQUEST *req = NULL;
        OCSP_RESPONSE *resp = NULL;
        OCSP_BASICRESP *bs = NULL;
@@ -149,7 +157,7 @@ int MAIN(int argc, char **argv)
        int badarg = 0;
        int i;
        int ignore_err = 0;
-       STACK_OF(STRING) *reqnames = NULL;
+       STACK_OF(OPENSSL_STRING) *reqnames = NULL;
        STACK_OF(OCSP_CERTID) *ids = NULL;
 
        X509 *rca_cert = NULL;
@@ -166,7 +174,7 @@ int MAIN(int argc, char **argv)
        SSL_load_error_strings();
        OpenSSL_add_ssl_algorithms();
        args = argv + 1;
-       reqnames = sk_STRING_new_null();
+       reqnames = sk_OPENSSL_STRING_new_null();
        ids = sk_OCSP_CERTID_new_null();
        while (!badarg && *args && *args[0] == '-')
                {
@@ -197,6 +205,12 @@ int MAIN(int argc, char **argv)
                        }
                else if (!strcmp(*args, "-url"))
                        {
+                       if (thost)
+                               OPENSSL_free(thost);
+                       if (tport)
+                               OPENSSL_free(tport);
+                       if (tpath)
+                               OPENSSL_free(tpath);
                        if (args[1])
                                {
                                args++;
@@ -205,6 +219,9 @@ int MAIN(int argc, char **argv)
                                        BIO_printf(bio_err, "Error parsing URL\n");
                                        badarg = 1;
                                        }
+                               thost = host;
+                               tport = port;
+                               tpath = path;
                                }
                        else badarg = 1;
                        }
@@ -226,6 +243,16 @@ int MAIN(int argc, char **argv)
                                }
                        else badarg = 1;
                        }
+               else if (!strcmp(*args, "-header"))
+                       {
+                       if (args[1] && args[2])
+                               {
+                               if (!X509V3_add_value(args[1], args[2], &headers))
+                                       goto end;
+                               args += 2;
+                               }
+                       else badarg = 1;
+                       }
                else if (!strcmp(*args, "-ignore_err"))
                        ignore_err = 1;
                else if (!strcmp(*args, "-noverify"))
@@ -428,7 +455,7 @@ int MAIN(int argc, char **argv)
                                if (!cert_id_md) cert_id_md = EVP_sha1();
                                if(!add_ocsp_cert(&req, cert, cert_id_md, issuer, ids))
                                        goto end;
-                               if(!sk_STRING_push(reqnames, *args))
+                               if(!sk_OPENSSL_STRING_push(reqnames, *args))
                                        goto end;
                                }
                        else badarg = 1;
@@ -441,7 +468,7 @@ int MAIN(int argc, char **argv)
                                if (!cert_id_md) cert_id_md = EVP_sha1();
                                if(!add_ocsp_serial(&req, *args, cert_id_md, issuer, ids))
                                        goto end;
-                               if(!sk_STRING_push(reqnames, *args))
+                               if(!sk_OPENSSL_STRING_push(reqnames, *args))
                                        goto end;
                                }
                        else badarg = 1;
@@ -556,51 +583,52 @@ int MAIN(int argc, char **argv)
                BIO_printf (bio_err, "OCSP utility\n");
                BIO_printf (bio_err, "Usage ocsp [options]\n");
                BIO_printf (bio_err, "where options are\n");
-               BIO_printf (bio_err, "-out file          output filename\n");
-               BIO_printf (bio_err, "-issuer file       issuer certificate\n");
-               BIO_printf (bio_err, "-cert file         certificate to check\n");
-               BIO_printf (bio_err, "-serial n          serial number to check\n");
-               BIO_printf (bio_err, "-signer file       certificate to sign OCSP request with\n");
-               BIO_printf (bio_err, "-signkey file      private key to sign OCSP request with\n");
-               BIO_printf (bio_err, "-sign_other file   additional certificates to include in signed request\n");
-               BIO_printf (bio_err, "-no_certs          don't include any certificates in signed request\n");
-               BIO_printf (bio_err, "-req_text          print text form of request\n");
-               BIO_printf (bio_err, "-resp_text         print text form of response\n");
-               BIO_printf (bio_err, "-text              print text form of request and response\n");
-               BIO_printf (bio_err, "-reqout file       write DER encoded OCSP request to \"file\"\n");
-               BIO_printf (bio_err, "-respout file      write DER encoded OCSP reponse to \"file\"\n");
-               BIO_printf (bio_err, "-reqin file        read DER encoded OCSP request from \"file\"\n");
-               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_other file additional certificates to search for signer\n");
-               BIO_printf (bio_err, "-trust_other       don't verify additional certificates\n");
-               BIO_printf (bio_err, "-no_intern         don't search certificates contained in response for signer\n");
+               BIO_printf (bio_err, "-out file            output filename\n");
+               BIO_printf (bio_err, "-issuer file         issuer certificate\n");
+               BIO_printf (bio_err, "-cert file           certificate to check\n");
+               BIO_printf (bio_err, "-serial n            serial number to check\n");
+               BIO_printf (bio_err, "-signer file         certificate to sign OCSP request with\n");
+               BIO_printf (bio_err, "-signkey file        private key to sign OCSP request with\n");
+               BIO_printf (bio_err, "-sign_other file     additional certificates to include in signed request\n");
+               BIO_printf (bio_err, "-no_certs            don't include any certificates in signed request\n");
+               BIO_printf (bio_err, "-req_text            print text form of request\n");
+               BIO_printf (bio_err, "-resp_text           print text form of response\n");
+               BIO_printf (bio_err, "-text                print text form of request and response\n");
+               BIO_printf (bio_err, "-reqout file         write DER encoded OCSP request to \"file\"\n");
+               BIO_printf (bio_err, "-respout file        write DER encoded OCSP reponse to \"file\"\n");
+               BIO_printf (bio_err, "-reqin file          read DER encoded OCSP request from \"file\"\n");
+               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_other file   additional certificates to search for signer\n");
+               BIO_printf (bio_err, "-trust_other         don't verify additional certificates\n");
+               BIO_printf (bio_err, "-no_intern           don't search certificates contained in response for signer\n");
                BIO_printf (bio_err, "-no_signature_verify don't check signature on response\n");
-               BIO_printf (bio_err, "-no_cert_verify    don't check signing certificate\n");
-               BIO_printf (bio_err, "-no_chain          don't chain verify response\n");
-               BIO_printf (bio_err, "-no_cert_checks    don't do additional checks on signing certificate\n");
-               BIO_printf (bio_err, "-port num          port to run responder on\n");
-               BIO_printf (bio_err, "-index file        certificate status index file\n");
-               BIO_printf (bio_err, "-CA file           CA certificate\n");
-               BIO_printf (bio_err, "-rsigner file      responder certificate to sign responses with\n");
-               BIO_printf (bio_err, "-rkey file         responder key to sign responses with\n");
-               BIO_printf (bio_err, "-rother file       other certificates to include in response\n");
-               BIO_printf (bio_err, "-resp_no_certs     don't include any certificates in response\n");
-               BIO_printf (bio_err, "-nmin n            number of minutes before next update\n");
-               BIO_printf (bio_err, "-ndays n           number of days before next update\n");
-               BIO_printf (bio_err, "-resp_key_id       identify reponse by signing certificate key ID\n");
-               BIO_printf (bio_err, "-nrequest n        number of requests to accept (default unlimited)\n");
-               BIO_printf (bio_err, "-<dgst alg>     use specified digest in the request");
+               BIO_printf (bio_err, "-no_cert_verify      don't check signing certificate\n");
+               BIO_printf (bio_err, "-no_chain            don't chain verify response\n");
+               BIO_printf (bio_err, "-no_cert_checks      don't do additional checks on signing certificate\n");
+               BIO_printf (bio_err, "-port num            port to run responder on\n");
+               BIO_printf (bio_err, "-index file          certificate status index file\n");
+               BIO_printf (bio_err, "-CA file             CA certificate\n");
+               BIO_printf (bio_err, "-rsigner file        responder certificate to sign responses with\n");
+               BIO_printf (bio_err, "-rkey file           responder key to sign responses with\n");
+               BIO_printf (bio_err, "-rother file         other certificates to include in response\n");
+               BIO_printf (bio_err, "-resp_no_certs       don't include any certificates in response\n");
+               BIO_printf (bio_err, "-nmin n              number of minutes before next update\n");
+               BIO_printf (bio_err, "-ndays n             number of days before next update\n");
+               BIO_printf (bio_err, "-resp_key_id         identify reponse by signing certificate key ID\n");
+               BIO_printf (bio_err, "-nrequest n          number of requests to accept (default unlimited)\n");
+               BIO_printf (bio_err, "-<dgst alg>          use specified digest in the request\n");
+               BIO_printf (bio_err, "-timeout n           timeout connection to OCSP responder after n seconds\n");
                goto end;
                }
 
@@ -752,7 +780,7 @@ int MAIN(int argc, char **argv)
                {
 #ifndef OPENSSL_NO_SOCK
                resp = process_responder(bio_err, req, host, path,
-                                               port, use_ssl, req_timeout);
+                                       port, use_ssl, headers, req_timeout);
                if (!resp)
                        goto end;
 #else
@@ -897,17 +925,18 @@ end:
        OCSP_REQUEST_free(req);
        OCSP_RESPONSE_free(resp);
        OCSP_BASICRESP_free(bs);
-       sk_STRING_free(reqnames);
+       sk_OPENSSL_STRING_free(reqnames);
        sk_OCSP_CERTID_free(ids);
        sk_X509_pop_free(sign_other, X509_free);
        sk_X509_pop_free(verify_other, X509_free);
+       sk_CONF_VALUE_pop_free(headers, X509V3_conf_free);
 
-       if (use_ssl != -1)
-               {
-               OPENSSL_free(host);
-               OPENSSL_free(port);
-               OPENSSL_free(path);
-               }
+       if (thost)
+               OPENSSL_free(thost);
+       if (tport)
+               OPENSSL_free(tport);
+       if (tpath)
+               OPENSSL_free(tpath);
 
        OPENSSL_EXIT(ret);
 }
@@ -967,7 +996,7 @@ static int add_ocsp_serial(OCSP_REQUEST **req, char *serial,const EVP_MD *cert_i
        }
 
 static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
-                             STACK_OF(STRING) *names,
+                             STACK_OF(OPENSSL_STRING) *names,
                              STACK_OF(OCSP_CERTID) *ids, long nsec,
                              long maxage)
        {
@@ -979,13 +1008,13 @@ static int print_ocsp_summary(BIO *out, OCSP_BASICRESP *bs, OCSP_REQUEST *req,
 
        ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd;
 
-       if (!bs || !req || !sk_STRING_num(names) || !sk_OCSP_CERTID_num(ids))
+       if (!bs || !req || !sk_OPENSSL_STRING_num(names) || !sk_OCSP_CERTID_num(ids))
                return 1;
 
        for (i = 0; i < sk_OCSP_CERTID_num(ids); i++)
                {
                id = sk_OCSP_CERTID_value(ids, i);
-               name = sk_STRING_value(names, i);
+               name = sk_OPENSSL_STRING_value(names, i);
                BIO_printf(out, "%s: ", name);
 
                if(!OCSP_resp_find_status(bs, id, &status, &reason,
@@ -1256,10 +1285,12 @@ static int send_ocsp_response(BIO *cbio, OCSP_RESPONSE *resp)
        }
 
 static OCSP_RESPONSE *query_responder(BIO *err, BIO *cbio, char *path,
+                               STACK_OF(CONF_VALUE) *headers,
                                OCSP_REQUEST *req, int req_timeout)
        {
        int fd;
        int rv;
+       int i;
        OCSP_REQ_CTX *ctx = NULL;
        OCSP_RESPONSE *rsp = NULL;
        fd_set confds;
@@ -1276,16 +1307,13 @@ static OCSP_RESPONSE *query_responder(BIO *err, BIO *cbio, char *path,
                return NULL;
                }
 
-       if (req_timeout == -1)
-               return OCSP_sendreq_bio(cbio, path, req);
-
        if (BIO_get_fd(cbio, &fd) <= 0)
                {
                BIO_puts(err, "Can't get connection fd\n");
                goto err;
                }
 
-       if (rv <= 0)
+       if (req_timeout != -1 && rv <= 0)
                {
                FD_ZERO(&confds);
                openssl_fdset(fd, &confds);
@@ -1300,15 +1328,27 @@ static OCSP_RESPONSE *query_responder(BIO *err, BIO *cbio, char *path,
                }
 
 
-       ctx = OCSP_sendreq_new(cbio, path, req, -1);
+       ctx = OCSP_sendreq_new(cbio, path, NULL, -1);
        if (!ctx)
                return NULL;
+
+       for (i = 0; i < sk_CONF_VALUE_num(headers); i++)
+               {
+               CONF_VALUE *hdr = sk_CONF_VALUE_value(headers, i);
+               if (!OCSP_REQ_CTX_add1_header(ctx, hdr->name, hdr->value))
+                       goto err;
+               }
+
+       if (!OCSP_REQ_CTX_set1_req(ctx, req))
+               goto err;
        
        for (;;)
                {
                rv = OCSP_sendreq_nbio(&rsp, ctx);
                if (rv != -1)
                        break;
+               if (req_timeout == -1)
+                       continue;
                FD_ZERO(&confds);
                openssl_fdset(fd, &confds);
                tv.tv_usec = 0;
@@ -1332,7 +1372,7 @@ static OCSP_RESPONSE *query_responder(BIO *err, BIO *cbio, char *path,
                        BIO_puts(err, "Select error\n");
                        break;
                        }
-                       
+
                }
        err:
        if (ctx)
@@ -1343,6 +1383,7 @@ static OCSP_RESPONSE *query_responder(BIO *err, BIO *cbio, char *path,
 
 OCSP_RESPONSE *process_responder(BIO *err, OCSP_REQUEST *req,
                        char *host, char *path, char *port, int use_ssl,
+                       STACK_OF(CONF_VALUE) *headers,
                        int req_timeout)
        {
        BIO *cbio = NULL;
@@ -1358,16 +1399,7 @@ OCSP_RESPONSE *process_responder(BIO *err, OCSP_REQUEST *req,
        if (use_ssl == 1)
                {
                BIO *sbio;
-#if !defined(OPENSSL_NO_SSL2) && !defined(OPENSSL_NO_SSL3)
                ctx = SSL_CTX_new(SSLv23_client_method());
-#elif !defined(OPENSSL_NO_SSL3)
-               ctx = SSL_CTX_new(SSLv3_client_method());
-#elif !defined(OPENSSL_NO_SSL2)
-               ctx = SSL_CTX_new(SSLv2_client_method());
-#else
-               BIO_printf(err, "SSL is disabled\n");
-                       goto end;
-#endif
                if (ctx == NULL)
                        {
                        BIO_printf(err, "Error creating SSL context.\n");
@@ -1377,14 +1409,14 @@ OCSP_RESPONSE *process_responder(BIO *err, OCSP_REQUEST *req,
                sbio = BIO_new_ssl(ctx, 1);
                cbio = BIO_push(sbio, cbio);
                }
-       resp = query_responder(err, cbio, path, req, req_timeout);
+       resp = query_responder(err, cbio, path, headers, req, req_timeout);
        if (!resp)
-               BIO_printf(bio_err, "Error querying OCSP responsder\n");
+               BIO_printf(bio_err, "Error querying OCSP responder\n");
        end:
-       if (ctx)
-               SSL_CTX_free(ctx);
        if (cbio)
                BIO_free_all(cbio);
+       if (ctx)
+               SSL_CTX_free(ctx);
        return resp;
        }