summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2016-12-06 13:38:11 +0000
committerjsing <>2016-12-06 13:38:11 +0000
commit95a901d22cb8e548a73bd42d95b1bdf70996f7f2 (patch)
tree637189bfb9f1969f946ec330944d4038db2c5cb9 /src/lib
parent21ff89ebbb4bdd4d2a5dee38cb8d4960c200234c (diff)
downloadopenbsd-95a901d22cb8e548a73bd42d95b1bdf70996f7f2.tar.gz
openbsd-95a901d22cb8e548a73bd42d95b1bdf70996f7f2.tar.bz2
openbsd-95a901d22cb8e548a73bd42d95b1bdf70996f7f2.zip
Now that ssl3_send_{client,server}_certificate() are using the common
handshake functions, we can remove more copied code from DTLS.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libssl/d1_both.c76
-rw-r--r--src/lib/libssl/d1_clnt.c69
-rw-r--r--src/lib/libssl/d1_srvr.c31
-rw-r--r--src/lib/libssl/ssl_locl.h6
4 files changed, 6 insertions, 176 deletions
diff --git a/src/lib/libssl/d1_both.c b/src/lib/libssl/d1_both.c
index bce084f1ee..7f9d5af4ce 100644
--- a/src/lib/libssl/d1_both.c
+++ b/src/lib/libssl/d1_both.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: d1_both.c,v 1.39 2016/03/06 14:52:15 beck Exp $ */ 1/* $OpenBSD: d1_both.c,v 1.40 2016/12/06 13:38:11 jsing Exp $ */
2/* 2/*
3 * DTLS implementation written by Nagendra Modadugu 3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. 4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
@@ -936,80 +936,6 @@ dtls1_send_change_cipher_spec(SSL *s, int a, int b)
936 return (dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC)); 936 return (dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC));
937} 937}
938 938
939static int
940dtls1_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
941{
942 int n;
943 unsigned char *p;
944
945 n = i2d_X509(x, NULL);
946 if (!BUF_MEM_grow_clean(buf, n + (*l) + 3)) {
947 SSLerr(SSL_F_DTLS1_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);
948 return 0;
949 }
950 p = (unsigned char *)&(buf->data[*l]);
951 l2n3(n, p);
952 i2d_X509(x, &p);
953 *l += n + 3;
954
955 return 1;
956}
957
958unsigned long
959dtls1_output_cert_chain(SSL *s, X509 *x)
960{
961 unsigned char *p;
962 int i;
963 unsigned long l = 3 + DTLS1_HM_HEADER_LENGTH;
964 BUF_MEM *buf;
965
966 /* TLSv1 sends a chain with nothing in it, instead of an alert */
967 buf = s->init_buf;
968 if (!BUF_MEM_grow_clean(buf, 10)) {
969 SSLerr(SSL_F_DTLS1_OUTPUT_CERT_CHAIN, ERR_R_BUF_LIB);
970 return (0);
971 }
972 if (x != NULL) {
973 X509_STORE_CTX xs_ctx;
974
975 if (!X509_STORE_CTX_init(&xs_ctx, s->ctx->cert_store,
976 x, NULL)) {
977 SSLerr(SSL_F_DTLS1_OUTPUT_CERT_CHAIN, ERR_R_X509_LIB);
978 return (0);
979 }
980
981 X509_verify_cert(&xs_ctx);
982 /* Don't leave errors in the queue */
983 ERR_clear_error();
984 for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
985 x = sk_X509_value(xs_ctx.chain, i);
986
987 if (!dtls1_add_cert_to_buf(buf, &l, x)) {
988 X509_STORE_CTX_cleanup(&xs_ctx);
989 return 0;
990 }
991 }
992 X509_STORE_CTX_cleanup(&xs_ctx);
993 }
994 /* Thawte special :-) */
995 for (i = 0; i < sk_X509_num(s->ctx->extra_certs); i++) {
996 x = sk_X509_value(s->ctx->extra_certs, i);
997 if (!dtls1_add_cert_to_buf(buf, &l, x))
998 return 0;
999 }
1000
1001 l -= (3 + DTLS1_HM_HEADER_LENGTH);
1002
1003 p = (unsigned char *)&(buf->data[DTLS1_HM_HEADER_LENGTH]);
1004 l2n3(l, p);
1005 l += 3;
1006 p = (unsigned char *)&(buf->data[0]);
1007 p = dtls1_set_message_header(s, p, SSL3_MT_CERTIFICATE, l, 0, l);
1008
1009 l += DTLS1_HM_HEADER_LENGTH;
1010 return (l);
1011}
1012
1013int 939int
1014dtls1_read_failed(SSL *s, int code) 940dtls1_read_failed(SSL *s, int code)
1015{ 941{
diff --git a/src/lib/libssl/d1_clnt.c b/src/lib/libssl/d1_clnt.c
index 07ae92f4c9..42e149f864 100644
--- a/src/lib/libssl/d1_clnt.c
+++ b/src/lib/libssl/d1_clnt.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: d1_clnt.c,v 1.58 2016/11/04 19:11:43 jsing Exp $ */ 1/* $OpenBSD: d1_clnt.c,v 1.59 2016/12/06 13:38:11 jsing Exp $ */
2/* 2/*
3 * DTLS implementation written by Nagendra Modadugu 3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. 4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
@@ -384,7 +384,7 @@ dtls1_connect(SSL *s)
384 case SSL3_ST_CW_CERT_C: 384 case SSL3_ST_CW_CERT_C:
385 case SSL3_ST_CW_CERT_D: 385 case SSL3_ST_CW_CERT_D:
386 dtls1_start_timer(s); 386 dtls1_start_timer(s);
387 ret = dtls1_send_client_certificate(s); 387 ret = ssl3_send_client_certificate(s);
388 if (ret <= 0) 388 if (ret <= 0)
389 goto end; 389 goto end;
390 s->state = SSL3_ST_CW_KEY_EXCH_A; 390 s->state = SSL3_ST_CW_KEY_EXCH_A;
@@ -657,68 +657,3 @@ f_err:
657 ssl3_send_alert(s, SSL3_AL_FATAL, al); 657 ssl3_send_alert(s, SSL3_AL_FATAL, al);
658 return -1; 658 return -1;
659} 659}
660
661int
662dtls1_send_client_certificate(SSL *s)
663{
664 X509 *x509 = NULL;
665 EVP_PKEY *pkey = NULL;
666 int i;
667 unsigned long l;
668
669 if (s->state == SSL3_ST_CW_CERT_A) {
670 if ((s->cert == NULL) || (s->cert->key->x509 == NULL) ||
671 (s->cert->key->privatekey == NULL))
672 s->state = SSL3_ST_CW_CERT_B;
673 else
674 s->state = SSL3_ST_CW_CERT_C;
675 }
676
677 /* We need to get a client cert */
678 if (s->state == SSL3_ST_CW_CERT_B) {
679 /* If we get an error, we need to
680 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
681 * We then get retied later */
682 i = 0;
683 i = ssl_do_client_cert_cb(s, &x509, &pkey);
684 if (i < 0) {
685 s->rwstate = SSL_X509_LOOKUP;
686 return (-1);
687 }
688 s->rwstate = SSL_NOTHING;
689 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
690 s->state = SSL3_ST_CW_CERT_B;
691 if (!SSL_use_certificate(s, x509) ||
692 !SSL_use_PrivateKey(s, pkey))
693 i = 0;
694 } else if (i == 1) {
695 i = 0;
696 SSLerr(SSL_F_DTLS1_SEND_CLIENT_CERTIFICATE,
697 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
698 }
699
700 X509_free(x509);
701 EVP_PKEY_free(pkey);
702 if (i == 0)
703 s->s3->tmp.cert_req = 2;
704
705 /* Ok, we have a cert */
706 s->state = SSL3_ST_CW_CERT_C;
707 }
708
709 if (s->state == SSL3_ST_CW_CERT_C) {
710 s->state = SSL3_ST_CW_CERT_D;
711 l = dtls1_output_cert_chain(s,
712 (s->s3->tmp.cert_req == 2) ? NULL : s->cert->key->x509);
713 s->init_num = (int)l;
714 s->init_off = 0;
715
716 /* set header called by dtls1_output_cert_chain() */
717
718 /* buffer the message to handle re-xmits */
719 dtls1_buffer_message(s, 0);
720 }
721
722 /* SSL3_ST_CW_CERT_D */
723 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
724}
diff --git a/src/lib/libssl/d1_srvr.c b/src/lib/libssl/d1_srvr.c
index 8027e44123..472d0de9dd 100644
--- a/src/lib/libssl/d1_srvr.c
+++ b/src/lib/libssl/d1_srvr.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: d1_srvr.c,v 1.68 2016/11/04 18:30:21 guenther Exp $ */ 1/* $OpenBSD: d1_srvr.c,v 1.69 2016/12/06 13:38:11 jsing Exp $ */
2/* 2/*
3 * DTLS implementation written by Nagendra Modadugu 3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005. 4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
@@ -368,7 +368,7 @@ dtls1_accept(SSL *s)
368 if (!(s->s3->tmp.new_cipher->algorithm_auth & 368 if (!(s->s3->tmp.new_cipher->algorithm_auth &
369 SSL_aNULL)) { 369 SSL_aNULL)) {
370 dtls1_start_timer(s); 370 dtls1_start_timer(s);
371 ret = dtls1_send_server_certificate(s); 371 ret = ssl3_send_server_certificate(s);
372 if (ret <= 0) 372 if (ret <= 0)
373 goto end; 373 goto end;
374 if (s->tlsext_status_expected) 374 if (s->tlsext_status_expected)
@@ -722,30 +722,3 @@ dtls1_send_hello_verify_request(SSL *s)
722 /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */ 722 /* s->state = DTLS1_ST_SW_HELLO_VERIFY_REQUEST_B */
723 return (ssl3_handshake_write(s)); 723 return (ssl3_handshake_write(s));
724} 724}
725
726int
727dtls1_send_server_certificate(SSL *s)
728{
729 unsigned long l;
730 X509 *x;
731
732 if (s->state == SSL3_ST_SW_CERT_A) {
733 x = ssl_get_server_send_cert(s);
734 if (x == NULL) {
735 SSLerr(SSL_F_DTLS1_SEND_SERVER_CERTIFICATE,
736 ERR_R_INTERNAL_ERROR);
737 return (0);
738 }
739
740 l = dtls1_output_cert_chain(s, x);
741 s->state = SSL3_ST_SW_CERT_B;
742 s->init_num = (int)l;
743 s->init_off = 0;
744
745 /* buffer the message to handle re-xmits */
746 dtls1_buffer_message(s, 0);
747 }
748
749 /* SSL3_ST_SW_CERT_B */
750 return (dtls1_do_write(s, SSL3_RT_HANDSHAKE));
751}
diff --git a/src/lib/libssl/ssl_locl.h b/src/lib/libssl/ssl_locl.h
index 89fb83eb9a..3de5571985 100644
--- a/src/lib/libssl/ssl_locl.h
+++ b/src/lib/libssl/ssl_locl.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssl_locl.h,v 1.138 2016/12/06 13:17:52 jsing Exp $ */ 1/* $OpenBSD: ssl_locl.h,v 1.139 2016/12/06 13:38:11 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -715,8 +715,6 @@ int ssl3_check_cert_and_algorithm(SSL *s);
715int ssl3_check_finished(SSL *s); 715int ssl3_check_finished(SSL *s);
716int ssl3_send_next_proto(SSL *s); 716int ssl3_send_next_proto(SSL *s);
717 717
718int dtls1_send_client_certificate(SSL *s);
719
720/* some server-only functions */ 718/* some server-only functions */
721int ssl3_get_client_hello(SSL *s); 719int ssl3_get_client_hello(SSL *s);
722int ssl3_send_server_hello(SSL *s); 720int ssl3_send_server_hello(SSL *s);
@@ -729,8 +727,6 @@ int ssl3_get_client_key_exchange(SSL *s);
729int ssl3_get_cert_verify(SSL *s); 727int ssl3_get_cert_verify(SSL *s);
730int ssl3_get_next_proto(SSL *s); 728int ssl3_get_next_proto(SSL *s);
731 729
732int dtls1_send_server_certificate(SSL *s);
733
734int ssl23_accept(SSL *s); 730int ssl23_accept(SSL *s);
735int ssl23_connect(SSL *s); 731int ssl23_connect(SSL *s);
736int ssl23_read_bytes(SSL *s, int n); 732int ssl23_read_bytes(SSL *s, int n);