~ubuntu-branches/ubuntu/lucid/openssl/lucid-proposed

« back to all changes in this revision

Viewing changes to apps/s_server.c

  • Committer: Bazaar Package Importer
  • Author(s): Kurt Roeckx
  • Date: 2009-06-13 18:15:46 UTC
  • mto: (11.1.5 squeeze)
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: james.westby@ubuntu.com-20090613181546-vbfntai3b009dl1u
Tags: upstream-0.9.8k
ImportĀ upstreamĀ versionĀ 0.9.8k

Show diffs side-by-side

added added

removed removed

Lines of Context:
153
153
#include <openssl/x509.h>
154
154
#include <openssl/ssl.h>
155
155
#include <openssl/rand.h>
 
156
#include <openssl/ocsp.h>
156
157
#ifndef OPENSSL_NO_DH
157
158
#include <openssl/dh.h>
158
159
#endif
269
270
static int s_debug=0;
270
271
#ifndef OPENSSL_NO_TLSEXT
271
272
static int s_tlsextdebug=0;
 
273
static int s_tlsextstatus=0;
 
274
static int cert_status_cb(SSL *s, void *arg);
272
275
#endif
273
276
static int s_msg=0;
274
277
static int s_quiet=0;
330
333
        BIO_printf(bio_err," -Verify arg   - turn on peer certificate verification, must have a cert.\n");
331
334
        BIO_printf(bio_err," -cert arg     - certificate file to use\n");
332
335
        BIO_printf(bio_err,"                 (default is %s)\n",TEST_CERT);
 
336
        BIO_printf(bio_err," -crl_check    - check the peer certificate has not been revoked by its CA.\n" \
 
337
                           "                 The CRL(s) are appended to the certificate file\n");
 
338
        BIO_printf(bio_err," -crl_check_all - check the peer certificate has not been revoked by its CA\n" \
 
339
                           "                 or any other CRL in the CA chain. CRL(s) are appened to the\n" \
 
340
                           "                 the certificate file.\n");
333
341
        BIO_printf(bio_err," -certform arg - certificate format (PEM or DER) PEM default\n");
334
342
        BIO_printf(bio_err," -key arg      - Private Key file to use, in cert file if\n");
335
343
        BIO_printf(bio_err,"                 not specified (default is %s)\n",TEST_CERT);
585
593
                }
586
594
        return SSL_TLSEXT_ERR_OK;
587
595
}
 
596
 
 
597
/* Structure passed to cert status callback */
 
598
 
 
599
typedef struct tlsextstatusctx_st {
 
600
   /* Default responder to use */
 
601
   char *host, *path, *port;
 
602
   int use_ssl;
 
603
   int timeout;
 
604
   BIO *err;
 
605
   int verbose;
 
606
} tlsextstatusctx;
 
607
 
 
608
static tlsextstatusctx tlscstatp = {NULL, NULL, NULL, 0, -1, NULL, 0};
 
609
 
 
610
/* Certificate Status callback. This is called when a client includes a
 
611
 * certificate status request extension.
 
612
 *
 
613
 * This is a simplified version. It examines certificates each time and
 
614
 * makes one OCSP responder query for each request.
 
615
 *
 
616
 * A full version would store details such as the OCSP certificate IDs and
 
617
 * minimise the number of OCSP responses by caching them until they were
 
618
 * considered "expired".
 
619
 */
 
620
 
 
621
static int cert_status_cb(SSL *s, void *arg)
 
622
        {
 
623
        tlsextstatusctx *srctx = arg;
 
624
        BIO *err = srctx->err;
 
625
        char *host, *port, *path;
 
626
        int use_ssl;
 
627
        unsigned char *rspder = NULL;
 
628
        int rspderlen;
 
629
        STACK *aia = NULL;
 
630
        X509 *x = NULL;
 
631
        X509_STORE_CTX inctx;
 
632
        X509_OBJECT obj;
 
633
        OCSP_REQUEST *req = NULL;
 
634
        OCSP_RESPONSE *resp = NULL;
 
635
        OCSP_CERTID *id = NULL;
 
636
        STACK_OF(X509_EXTENSION) *exts;
 
637
        int ret = SSL_TLSEXT_ERR_NOACK;
 
638
        int i;
 
639
#if 0
 
640
STACK_OF(OCSP_RESPID) *ids;
 
641
SSL_get_tlsext_status_ids(s, &ids);
 
642
BIO_printf(err, "cert_status: received %d ids\n", sk_OCSP_RESPID_num(ids));
 
643
#endif
 
644
        if (srctx->verbose)
 
645
                BIO_puts(err, "cert_status: callback called\n");
 
646
        /* Build up OCSP query from server certificate */
 
647
        x = SSL_get_certificate(s);
 
648
        aia = X509_get1_ocsp(x);
 
649
        if (aia)
 
650
                {
 
651
                if (!OCSP_parse_url(sk_value(aia, 0),
 
652
                        &host, &port, &path, &use_ssl))
 
653
                        {
 
654
                        BIO_puts(err, "cert_status: can't parse AIA URL\n");
 
655
                        goto err;
 
656
                        }
 
657
                if (srctx->verbose)
 
658
                        BIO_printf(err, "cert_status: AIA URL: %s\n",
 
659
                                        sk_value(aia, 0));
 
660
                }
 
661
        else
 
662
                {
 
663
                if (!srctx->host)
 
664
                        {
 
665
                        BIO_puts(srctx->err, "cert_status: no AIA and no default responder URL\n");
 
666
                        goto done;
 
667
                        }
 
668
                host = srctx->host;
 
669
                path = srctx->path;
 
670
                port = srctx->port;
 
671
                use_ssl = srctx->use_ssl;
 
672
                }
 
673
                
 
674
        if (!X509_STORE_CTX_init(&inctx,
 
675
                                SSL_CTX_get_cert_store(SSL_get_SSL_CTX(s)),
 
676
                                NULL, NULL))
 
677
                goto err;
 
678
        if (X509_STORE_get_by_subject(&inctx,X509_LU_X509,
 
679
                                X509_get_issuer_name(x),&obj) <= 0)
 
680
                {
 
681
                BIO_puts(err, "cert_status: Can't retrieve issuer certificate.\n");
 
682
                X509_STORE_CTX_cleanup(&inctx);
 
683
                goto done;
 
684
                }
 
685
        req = OCSP_REQUEST_new();
 
686
        if (!req)
 
687
                goto err;
 
688
        id = OCSP_cert_to_id(NULL, x, obj.data.x509);
 
689
        X509_free(obj.data.x509);
 
690
        X509_STORE_CTX_cleanup(&inctx);
 
691
        if (!id)
 
692
                goto err;
 
693
        if (!OCSP_request_add0_id(req, id))
 
694
                goto err;
 
695
        id = NULL;
 
696
        /* Add any extensions to the request */
 
697
        SSL_get_tlsext_status_exts(s, &exts);
 
698
        for (i = 0; i < sk_X509_EXTENSION_num(exts); i++)
 
699
                {
 
700
                X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i);
 
701
                if (!OCSP_REQUEST_add_ext(req, ext, -1))
 
702
                        goto err;
 
703
                }
 
704
        resp = process_responder(err, req, host, path, port, use_ssl,
 
705
                                        srctx->timeout);
 
706
        if (!resp)
 
707
                {
 
708
                BIO_puts(err, "cert_status: error querying responder\n");
 
709
                goto done;
 
710
                }
 
711
        rspderlen = i2d_OCSP_RESPONSE(resp, &rspder);
 
712
        if (rspderlen <= 0)
 
713
                goto err;
 
714
        SSL_set_tlsext_status_ocsp_resp(s, rspder, rspderlen);
 
715
        if (srctx->verbose)
 
716
                {
 
717
                BIO_puts(err, "cert_status: ocsp response sent:\n");
 
718
                OCSP_RESPONSE_print(err, resp, 2);
 
719
                }
 
720
        ret = SSL_TLSEXT_ERR_OK;
 
721
        done:
 
722
        if (ret != SSL_TLSEXT_ERR_OK)
 
723
                ERR_print_errors(err);
 
724
        if (aia)
 
725
                {
 
726
                OPENSSL_free(host);
 
727
                OPENSSL_free(path);
 
728
                OPENSSL_free(port);
 
729
                X509_email_free(aia);
 
730
                }
 
731
        if (id)
 
732
                OCSP_CERTID_free(id);
 
733
        if (req)
 
734
                OCSP_REQUEST_free(req);
 
735
        if (resp)
 
736
                OCSP_RESPONSE_free(resp);
 
737
        return ret;
 
738
        err:
 
739
        ret = SSL_TLSEXT_ERR_ALERT_FATAL;
 
740
        goto done;
 
741
        }
588
742
#endif
589
743
int MAIN(int, char **);
590
744
 
 
745
#ifndef OPENSSL_NO_JPAKE
 
746
static char *jpake_secret = NULL;
 
747
#endif
 
748
 
591
749
int MAIN(int argc, char *argv[])
592
750
        {
593
751
        X509_STORE *store = NULL;
606
764
        int state=0;
607
765
        SSL_METHOD *meth=NULL;
608
766
        int socket_type=SOCK_STREAM;
609
 
#ifndef OPENSSL_NO_ENGINE
610
767
        ENGINE *e=NULL;
611
 
#endif
612
768
        char *inrand=NULL;
613
769
        int s_cert_format = FORMAT_PEM, s_key_format = FORMAT_PEM;
614
770
        char *passarg = NULL, *pass = NULL;
620
776
        EVP_PKEY *s_key2 = NULL;
621
777
        X509 *s_cert2 = NULL;
622
778
#endif
623
 
 
624
779
#ifndef OPENSSL_NO_TLSEXT
625
780
        tlsextctx tlsextcbp = {NULL, NULL, SSL_TLSEXT_ERR_ALERT_WARNING};
626
781
#endif
760
915
                        {
761
916
                        vflags |= X509_V_FLAG_CRL_CHECK;
762
917
                        }
763
 
                else if (strcmp(*argv,"-crl_check") == 0)
 
918
                else if (strcmp(*argv,"-crl_check_all") == 0)
764
919
                        {
765
920
                        vflags |= X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
766
921
                        }
792
947
#ifndef OPENSSL_NO_TLSEXT
793
948
                else if (strcmp(*argv,"-tlsextdebug") == 0)
794
949
                        s_tlsextdebug=1;
 
950
                else if (strcmp(*argv,"-status") == 0)
 
951
                        s_tlsextstatus=1;
 
952
                else if (strcmp(*argv,"-status_verbose") == 0)
 
953
                        {
 
954
                        s_tlsextstatus=1;
 
955
                        tlscstatp.verbose = 1;
 
956
                        }
 
957
                else if (!strcmp(*argv, "-status_timeout"))
 
958
                        {
 
959
                        s_tlsextstatus=1;
 
960
                        if (--argc < 1) goto bad;
 
961
                        tlscstatp.timeout = atoi(*(++argv));
 
962
                        }
 
963
                else if (!strcmp(*argv, "-status_url"))
 
964
                        {
 
965
                        s_tlsextstatus=1;
 
966
                        if (--argc < 1) goto bad;
 
967
                        if (!OCSP_parse_url(*(++argv),
 
968
                                        &tlscstatp.host,
 
969
                                        &tlscstatp.port,
 
970
                                        &tlscstatp.path,
 
971
                                        &tlscstatp.use_ssl))
 
972
                                {
 
973
                                BIO_printf(bio_err, "Error parsing URL\n");
 
974
                                goto bad;
 
975
                                }
 
976
                        }
795
977
#endif
796
978
                else if (strcmp(*argv,"-msg") == 0)
797
979
                        { s_msg=1; }
890
1072
                        if (--argc < 1) goto bad;
891
1073
                        s_key_file2= *(++argv);
892
1074
                        }
 
1075
                        
 
1076
#endif
 
1077
#ifndef OPENSSL_NO_JPAKE
 
1078
                else if (strcmp(*argv,"-jpake") == 0)
 
1079
                        {
 
1080
                        if (--argc < 1) goto bad;
 
1081
                        jpake_secret = *(++argv);
 
1082
                        }
893
1083
#endif
894
1084
                else
895
1085
                        {
1430
1620
                SSL_set_tlsext_debug_callback(con, tlsext_cb);
1431
1621
                SSL_set_tlsext_debug_arg(con, bio_s_out);
1432
1622
                }
 
1623
        if (s_tlsextstatus)
 
1624
                {
 
1625
                SSL_CTX_set_tlsext_status_cb(ctx, cert_status_cb);
 
1626
                tlscstatp.err = bio_err;
 
1627
                SSL_CTX_set_tlsext_status_arg(ctx, &tlscstatp);
 
1628
                }
1433
1629
#endif
1434
1630
#ifndef OPENSSL_NO_KRB5
1435
1631
                if ((con->kssl_ctx = kssl_ctx_new()) != NULL)
1486
1682
                test=BIO_new(BIO_f_nbio_test());
1487
1683
                sbio=BIO_push(test,sbio);
1488
1684
                }
 
1685
#ifndef OPENSSL_NO_JPAKE
 
1686
        if(jpake_secret)
 
1687
                jpake_server_auth(bio_s_out, sbio, jpake_secret);
 
1688
#endif
 
1689
 
1489
1690
        SSL_set_bio(con,sbio,sbio);
1490
1691
        SSL_set_accept_state(con);
1491
1692
        /* SSL_set_fd(con,s); */