2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
120 * In addition, Sun covenants to all licensees who provide a reciprocal
121 * covenant with respect to their own patents if any, not to sue under
122 * current and future patent claims necessarily infringed by the making,
123 * using, practicing, selling, offering for sale and/or otherwise
124 * disposing of the Contribution as delivered hereunder
125 * (or portions thereof), provided that such covenant shall not apply:
126 * 1) for code that a licensee deletes from the Contribution;
127 * 2) separates from the Contribution; or
128 * 3) for infringements caused by:
129 * i) the modification of the Contribution or
130 * ii) the combination of the Contribution with other software or
131 * devices where such combination causes the infringement.
133 * ECC cipher suite support in OpenSSL originally written by
134 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
138 #define REUSE_CIPHER_BUG
139 #define NETSCAPE_HANG_BUG
142 #include "ssl_locl.h"
143 #include "kssl_lcl.h"
144 #include <openssl/buffer.h>
145 #include <openssl/rand.h>
146 #include <openssl/objects.h>
147 #include <openssl/evp.h>
148 #include <openssl/x509.h>
149 #include <openssl/krb5_asn.h>
150 #include <openssl/md5.h>
151 #include "cryptlib.h"
153 static SSL_METHOD *ssl3_get_server_method(int ver);
154 static int ssl3_get_client_hello(SSL *s);
155 static int ssl3_check_client_hello(SSL *s);
156 static int ssl3_send_server_hello(SSL *s);
157 static int ssl3_send_server_key_exchange(SSL *s);
158 static int ssl3_send_certificate_request(SSL *s);
159 static int ssl3_send_server_done(SSL *s);
160 static int ssl3_get_client_key_exchange(SSL *s);
161 static int ssl3_get_client_certificate(SSL *s);
162 static int ssl3_get_cert_verify(SSL *s);
163 static int ssl3_send_hello_request(SSL *s);
165 #ifndef OPENSSL_NO_ECDH
166 static int nid2curve_id(int nid);
169 static SSL_METHOD *ssl3_get_server_method(int ver)
171 if (ver == SSL3_VERSION)
172 return(SSLv3_server_method());
177 SSL_METHOD *SSLv3_server_method(void)
180 static SSL_METHOD SSLv3_server_data;
184 CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
186 memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(),
188 SSLv3_server_data.ssl_accept=ssl3_accept;
189 SSLv3_server_data.get_ssl_method=ssl3_get_server_method;
192 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
194 return(&SSLv3_server_data);
197 int ssl3_accept(SSL *s)
200 unsigned long l,Time=time(NULL);
201 void (*cb)(const SSL *ssl,int type,int val)=NULL;
204 int new_state,state,skip=0;
206 RAND_add(&Time,sizeof(Time),0);
210 if (s->info_callback != NULL)
212 else if (s->ctx->info_callback != NULL)
213 cb=s->ctx->info_callback;
215 /* init things to blank */
217 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
221 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
231 case SSL_ST_RENEGOTIATE:
233 /* s->state=SSL_ST_ACCEPT; */
237 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
238 case SSL_ST_OK|SSL_ST_ACCEPT:
241 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
243 if ((s->version>>8) != 3)
245 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
248 s->type=SSL_ST_ACCEPT;
250 if (s->init_buf == NULL)
252 if ((buf=BUF_MEM_new()) == NULL)
257 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
265 if (!ssl3_setup_buffers(s))
273 if (s->state != SSL_ST_RENEGOTIATE)
275 /* Ok, we now need to push on a buffering BIO so that
276 * the output is sent in a way that TCP likes :-)
278 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
280 ssl3_init_finished_mac(s);
281 s->state=SSL3_ST_SR_CLNT_HELLO_A;
282 s->ctx->stats.sess_accept++;
286 /* s->state == SSL_ST_RENEGOTIATE,
287 * we will just send a HelloRequest */
288 s->ctx->stats.sess_accept_renegotiate++;
289 s->state=SSL3_ST_SW_HELLO_REQ_A;
293 case SSL3_ST_SW_HELLO_REQ_A:
294 case SSL3_ST_SW_HELLO_REQ_B:
297 ret=ssl3_send_hello_request(s);
298 if (ret <= 0) goto end;
299 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
300 s->state=SSL3_ST_SW_FLUSH;
303 ssl3_init_finished_mac(s);
306 case SSL3_ST_SW_HELLO_REQ_C:
310 case SSL3_ST_SR_CLNT_HELLO_A:
311 case SSL3_ST_SR_CLNT_HELLO_B:
312 case SSL3_ST_SR_CLNT_HELLO_C:
315 ret=ssl3_get_client_hello(s);
316 if (ret <= 0) goto end;
318 s->state=SSL3_ST_SW_SRVR_HELLO_A;
322 case SSL3_ST_SW_SRVR_HELLO_A:
323 case SSL3_ST_SW_SRVR_HELLO_B:
324 ret=ssl3_send_server_hello(s);
325 if (ret <= 0) goto end;
328 s->state=SSL3_ST_SW_CHANGE_A;
330 s->state=SSL3_ST_SW_CERT_A;
334 case SSL3_ST_SW_CERT_A:
335 case SSL3_ST_SW_CERT_B:
336 /* Check if it is anon DH or anon ECDH */
337 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
339 ret=ssl3_send_server_certificate(s);
340 if (ret <= 0) goto end;
344 s->state=SSL3_ST_SW_KEY_EXCH_A;
348 case SSL3_ST_SW_KEY_EXCH_A:
349 case SSL3_ST_SW_KEY_EXCH_B:
350 l=s->s3->tmp.new_cipher->algorithms;
352 /* clear this, it may get reset by
353 * send_server_key_exchange */
354 if ((s->options & SSL_OP_EPHEMERAL_RSA)
355 #ifndef OPENSSL_NO_KRB5
357 #endif /* OPENSSL_NO_KRB5 */
359 /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
360 * even when forbidden by protocol specs
361 * (handshake may fail as clients are not required to
362 * be able to handle this) */
363 s->s3->tmp.use_rsa_tmp=1;
365 s->s3->tmp.use_rsa_tmp=0;
368 /* only send if a DH key exchange, fortezza or
369 * RSA but we have a sign only certificate
371 * For ECC ciphersuites, we send a serverKeyExchange
372 * message only if the cipher suite is either
373 * ECDH-anon or ECDHE. In other cases, the
374 * server certificate contains the server's
375 * public key for key exchange.
377 if (s->s3->tmp.use_rsa_tmp
379 || (l & (SSL_DH|SSL_kFZA))
381 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
382 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
383 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
389 ret=ssl3_send_server_key_exchange(s);
390 if (ret <= 0) goto end;
395 s->state=SSL3_ST_SW_CERT_REQ_A;
399 case SSL3_ST_SW_CERT_REQ_A:
400 case SSL3_ST_SW_CERT_REQ_B:
401 if (/* don't request cert unless asked for it: */
402 !(s->verify_mode & SSL_VERIFY_PEER) ||
403 /* if SSL_VERIFY_CLIENT_ONCE is set,
404 * don't request cert during re-negotiation: */
405 ((s->session->peer != NULL) &&
406 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
407 /* never request cert in anonymous ciphersuites
408 * (see section "Certificate request" in SSL 3 drafts
409 * and in RFC 2246): */
410 ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
411 /* ... except when the application insists on verification
412 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
413 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
414 /* never request cert in Kerberos ciphersuites */
415 (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
417 /* no cert request */
419 s->s3->tmp.cert_request=0;
420 s->state=SSL3_ST_SW_SRVR_DONE_A;
424 s->s3->tmp.cert_request=1;
425 ret=ssl3_send_certificate_request(s);
426 if (ret <= 0) goto end;
427 #ifndef NETSCAPE_HANG_BUG
428 s->state=SSL3_ST_SW_SRVR_DONE_A;
430 s->state=SSL3_ST_SW_FLUSH;
431 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
437 case SSL3_ST_SW_SRVR_DONE_A:
438 case SSL3_ST_SW_SRVR_DONE_B:
439 ret=ssl3_send_server_done(s);
440 if (ret <= 0) goto end;
441 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
442 s->state=SSL3_ST_SW_FLUSH;
446 case SSL3_ST_SW_FLUSH:
447 /* number of bytes to be flushed */
448 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
451 s->rwstate=SSL_WRITING;
452 num1=BIO_flush(s->wbio);
453 if (num1 <= 0) { ret= -1; goto end; }
454 s->rwstate=SSL_NOTHING;
457 s->state=s->s3->tmp.next_state;
460 case SSL3_ST_SR_CERT_A:
461 case SSL3_ST_SR_CERT_B:
462 /* Check for second client hello (MS SGC) */
463 ret = ssl3_check_client_hello(s);
467 s->state = SSL3_ST_SR_CLNT_HELLO_C;
469 /* could be sent for a DH cert, even if we
470 * have not asked for it :-) */
471 ret=ssl3_get_client_certificate(s);
472 if (ret <= 0) goto end;
474 s->state=SSL3_ST_SR_KEY_EXCH_A;
478 case SSL3_ST_SR_KEY_EXCH_A:
479 case SSL3_ST_SR_KEY_EXCH_B:
480 ret=ssl3_get_client_key_exchange(s);
485 /* For the ECDH ciphersuites when
486 * the client sends its ECDH pub key in
487 * a certificate, the CertificateVerify
488 * message is not sent.
490 s->state=SSL3_ST_SR_FINISHED_A;
495 s->state=SSL3_ST_SR_CERT_VRFY_A;
498 /* We need to get hashes here so if there is
499 * a client cert, it can be verified
501 s->method->ssl3_enc->cert_verify_mac(s,
502 &(s->s3->finish_dgst1),
503 &(s->s3->tmp.cert_verify_md[0]));
504 s->method->ssl3_enc->cert_verify_mac(s,
505 &(s->s3->finish_dgst2),
506 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
510 case SSL3_ST_SR_CERT_VRFY_A:
511 case SSL3_ST_SR_CERT_VRFY_B:
513 /* we should decide if we expected this one */
514 ret=ssl3_get_cert_verify(s);
515 if (ret <= 0) goto end;
517 s->state=SSL3_ST_SR_FINISHED_A;
521 case SSL3_ST_SR_FINISHED_A:
522 case SSL3_ST_SR_FINISHED_B:
523 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
524 SSL3_ST_SR_FINISHED_B);
525 if (ret <= 0) goto end;
529 s->state=SSL3_ST_SW_CHANGE_A;
533 case SSL3_ST_SW_CHANGE_A:
534 case SSL3_ST_SW_CHANGE_B:
536 s->session->cipher=s->s3->tmp.new_cipher;
537 if (!s->method->ssl3_enc->setup_key_block(s))
538 { ret= -1; goto end; }
540 ret=ssl3_send_change_cipher_spec(s,
541 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
543 if (ret <= 0) goto end;
544 s->state=SSL3_ST_SW_FINISHED_A;
547 if (!s->method->ssl3_enc->change_cipher_state(s,
548 SSL3_CHANGE_CIPHER_SERVER_WRITE))
556 case SSL3_ST_SW_FINISHED_A:
557 case SSL3_ST_SW_FINISHED_B:
558 ret=ssl3_send_finished(s,
559 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
560 s->method->ssl3_enc->server_finished_label,
561 s->method->ssl3_enc->server_finished_label_len);
562 if (ret <= 0) goto end;
563 s->state=SSL3_ST_SW_FLUSH;
565 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
567 s->s3->tmp.next_state=SSL_ST_OK;
572 /* clean a few things up */
573 ssl3_cleanup_key_block(s);
575 BUF_MEM_free(s->init_buf);
578 /* remove buffering on output */
579 ssl_free_wbio_buffer(s);
583 if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
585 /* actually not necessarily a 'new' session unless
586 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
590 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
592 s->ctx->stats.sess_accept_good++;
594 s->handshake_func=ssl3_accept;
596 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
604 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
610 if (!s->s3->tmp.reuse_message && !skip)
614 if ((ret=BIO_flush(s->wbio)) <= 0)
619 if ((cb != NULL) && (s->state != state))
623 cb(s,SSL_CB_ACCEPT_LOOP,1);
630 /* BIO_flush(s->wbio); */
634 cb(s,SSL_CB_ACCEPT_EXIT,ret);
638 static int ssl3_send_hello_request(SSL *s)
642 if (s->state == SSL3_ST_SW_HELLO_REQ_A)
644 p=(unsigned char *)s->init_buf->data;
645 *(p++)=SSL3_MT_HELLO_REQUEST;
650 s->state=SSL3_ST_SW_HELLO_REQ_B;
651 /* number of bytes to write */
656 /* SSL3_ST_SW_HELLO_REQ_B */
657 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
660 static int ssl3_check_client_hello(SSL *s)
665 /* this function is called when we really expect a Certificate message,
666 * so permit appropriate message length */
667 n=ssl3_get_message(s,
673 if (!ok) return((int)n);
674 s->s3->tmp.reuse_message = 1;
675 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
677 /* Throw away what we have done so far in the current handshake,
678 * which will now be aborted. (A full SSL_clear would be too much.)
679 * I hope that tmp.dh is the only thing that may need to be cleared
680 * when a handshake is not completed ... */
681 #ifndef OPENSSL_NO_DH
682 if (s->s3->tmp.dh != NULL)
684 DH_free(s->s3->tmp.dh);
685 s->s3->tmp.dh = NULL;
693 static int ssl3_get_client_hello(SSL *s)
695 int i,j,ok,al,ret= -1;
698 unsigned char *p,*d,*q;
701 STACK_OF(SSL_CIPHER) *ciphers=NULL;
703 /* We do this so that we will respond with our native type.
704 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
705 * This down switching should be handled by a different method.
706 * If we are SSLv3, we will respond with SSLv3, even if prompted with
709 if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
712 s->state=SSL3_ST_SR_CLNT_HELLO_B;
714 n=ssl3_get_message(s,
715 SSL3_ST_SR_CLNT_HELLO_B,
716 SSL3_ST_SR_CLNT_HELLO_C,
717 SSL3_MT_CLIENT_HELLO,
718 SSL3_RT_MAX_PLAIN_LENGTH,
721 if (!ok) return((int)n);
722 d=p=(unsigned char *)s->init_msg;
724 /* use version from inside client hello, not from record header
725 * (may differ: see RFC 2246, Appendix E, second paragraph) */
726 s->client_version=(((int)p[0])<<8)|(int)p[1];
729 if (s->client_version < s->version)
731 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
732 if ((s->client_version>>8) == SSL3_VERSION_MAJOR)
734 /* similar to ssl3_get_record, send alert using remote version number */
735 s->version = s->client_version;
737 al = SSL_AD_PROTOCOL_VERSION;
741 /* load the client random */
742 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
745 /* get the session-id */
749 /* Versions before 0.9.7 always allow session reuse during renegotiation
750 * (i.e. when s->new_session is true), option
751 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
752 * Maybe this optional behaviour should always have been the default,
753 * but we cannot safely change the default behaviour (or new applications
754 * might be written that become totally unsecure when compiled with
755 * an earlier library version)
757 if (j == 0 || (s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
759 if (!ssl_get_new_session(s,1))
764 i=ssl_get_prev_session(s,p,j);
766 { /* previous session */
773 if (!ssl_get_new_session(s,1))
780 if ((i == 0) && (j != 0))
782 /* we need a cipher if we are not resuming a session */
783 al=SSL_AD_ILLEGAL_PARAMETER;
784 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
789 /* not enough data */
790 al=SSL_AD_DECODE_ERROR;
791 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
794 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
801 /* If it is a hit, check that the cipher is in the list */
802 if ((s->hit) && (i > 0))
805 id=s->session->cipher->id;
808 printf("client sent %d ciphers\n",sk_num(ciphers));
810 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
812 c=sk_SSL_CIPHER_value(ciphers,i);
814 printf("client [%2d of %2d]:%s\n",
815 i,sk_num(ciphers),SSL_CIPHER_get_name(c));
825 if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
827 /* Very bad for multi-threading.... */
828 s->session->cipher=sk_SSL_CIPHER_value(ciphers,
833 /* we need to have the cipher in the cipher
834 * list if we are asked to reuse it */
835 al=SSL_AD_ILLEGAL_PARAMETER;
836 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
846 /* not enough data */
847 al=SSL_AD_DECODE_ERROR;
848 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
854 if (p[j] == 0) break;
861 al=SSL_AD_DECODE_ERROR;
862 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
866 /* Worst case, we will use the NULL compression, but if we have other
867 * options, we will now look for them. We have i-1 compression
868 * algorithms from the client, starting at q. */
869 s->s3->tmp.new_compression=NULL;
870 if (s->ctx->comp_methods != NULL)
871 { /* See if we have a match */
874 nn=sk_SSL_COMP_num(s->ctx->comp_methods);
877 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
890 s->s3->tmp.new_compression=comp;
895 /* TLS does not mind if there is extra stuff */
896 if (s->version == SSL3_VERSION)
900 /* wrong number of bytes,
901 * there could be more to follow */
902 al=SSL_AD_DECODE_ERROR;
903 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
908 /* Given s->session->ciphers and SSL_get_ciphers, we must
913 s->session->compress_meth=(comp == NULL)?0:comp->id;
914 if (s->session->ciphers != NULL)
915 sk_SSL_CIPHER_free(s->session->ciphers);
916 s->session->ciphers=ciphers;
919 al=SSL_AD_ILLEGAL_PARAMETER;
920 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
924 c=ssl3_choose_cipher(s,s->session->ciphers,
929 al=SSL_AD_HANDSHAKE_FAILURE;
930 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
933 s->s3->tmp.new_cipher=c;
937 /* Session-id reuse */
938 #ifdef REUSE_CIPHER_BUG
939 STACK_OF(SSL_CIPHER) *sk;
943 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
945 sk=s->session->ciphers;
946 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
948 c=sk_SSL_CIPHER_value(sk,i);
949 if (c->algorithms & SSL_eNULL)
951 if (SSL_C_IS_EXPORT(c))
955 s->s3->tmp.new_cipher=nc;
957 s->s3->tmp.new_cipher=ec;
959 s->s3->tmp.new_cipher=s->session->cipher;
963 s->s3->tmp.new_cipher=s->session->cipher;
966 /* we now have the following setup.
968 * cipher_list - our prefered list of ciphers
969 * ciphers - the clients prefered list of ciphers
970 * compression - basically ignored right now
971 * ssl version is set - sslv3
972 * s->session - The ssl session has been setup.
973 * s->hit - session reuse flag
974 * s->tmp.new_cipher - the new cipher to use.
981 ssl3_send_alert(s,SSL3_AL_FATAL,al);
984 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
988 static int ssl3_send_server_hello(SSL *s)
993 unsigned long l,Time;
995 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
997 buf=(unsigned char *)s->init_buf->data;
998 p=s->s3->server_random;
999 Time=time(NULL); /* Time */
1001 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
1002 /* Do the message type and length last */
1005 *(p++)=s->version>>8;
1006 *(p++)=s->version&0xff;
1009 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
1010 p+=SSL3_RANDOM_SIZE;
1012 /* now in theory we have 3 options to sending back the
1013 * session id. If it is a re-use, we send back the
1014 * old session-id, if it is a new session, we send
1015 * back the new session-id or we send back a 0 length
1016 * session-id if we want it to be single use.
1017 * Currently I will not implement the '0' length session-id
1018 * 12-Jan-98 - I'll now support the '0' length stuff.
1020 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
1021 s->session->session_id_length=0;
1023 sl=s->session->session_id_length;
1024 if (sl > sizeof s->session->session_id)
1026 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1030 memcpy(p,s->session->session_id,sl);
1033 /* put the cipher */
1034 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
1037 /* put the compression method */
1038 if (s->s3->tmp.new_compression == NULL)
1041 *(p++)=s->s3->tmp.new_compression->id;
1046 *(d++)=SSL3_MT_SERVER_HELLO;
1049 s->state=SSL3_ST_CW_CLNT_HELLO_B;
1050 /* number of bytes to write */
1055 /* SSL3_ST_CW_CLNT_HELLO_B */
1056 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1059 static int ssl3_send_server_done(SSL *s)
1063 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
1065 p=(unsigned char *)s->init_buf->data;
1068 *(p++)=SSL3_MT_SERVER_DONE;
1073 s->state=SSL3_ST_SW_SRVR_DONE_B;
1074 /* number of bytes to write */
1079 /* SSL3_ST_CW_CLNT_HELLO_B */
1080 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1083 static int ssl3_send_server_key_exchange(SSL *s)
1085 #ifndef OPENSSL_NO_RSA
1089 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1092 #ifndef OPENSSL_NO_DH
1095 #ifndef OPENSSL_NO_ECDH
1096 EC_KEY *ecdh=NULL, *ecdhp;
1097 unsigned char *encodedPoint = NULL;
1100 BN_CTX *bn_ctx = NULL;
1103 unsigned char *p,*d;
1113 EVP_MD_CTX_init(&md_ctx);
1114 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1116 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
1121 r[0]=r[1]=r[2]=r[3]=NULL;
1123 #ifndef OPENSSL_NO_RSA
1124 if (type & SSL_kRSA)
1127 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
1129 rsa=s->cert->rsa_tmp_cb(s,
1130 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1131 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1134 al=SSL_AD_HANDSHAKE_FAILURE;
1135 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1143 al=SSL_AD_HANDSHAKE_FAILURE;
1144 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
1149 s->s3->tmp.use_rsa_tmp=1;
1153 #ifndef OPENSSL_NO_DH
1154 if (type & SSL_kEDH)
1157 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1158 dhp=s->cert->dh_tmp_cb(s,
1159 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1160 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1163 al=SSL_AD_HANDSHAKE_FAILURE;
1164 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1168 if (s->s3->tmp.dh != NULL)
1171 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1175 if ((dh=DHparams_dup(dhp)) == NULL)
1177 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1182 if ((dhp->pub_key == NULL ||
1183 dhp->priv_key == NULL ||
1184 (s->options & SSL_OP_SINGLE_DH_USE)))
1186 if(!DH_generate_key(dh))
1188 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1195 dh->pub_key=BN_dup(dhp->pub_key);
1196 dh->priv_key=BN_dup(dhp->priv_key);
1197 if ((dh->pub_key == NULL) ||
1198 (dh->priv_key == NULL))
1200 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1210 #ifndef OPENSSL_NO_ECDH
1211 if (type & SSL_kECDHE)
1213 ecdhp=cert->ecdh_tmp;
1214 if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL))
1216 ecdhp=s->cert->ecdh_tmp_cb(s,
1217 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1218 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1222 al=SSL_AD_HANDSHAKE_FAILURE;
1223 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
1227 if (s->s3->tmp.ecdh != NULL)
1229 EC_KEY_free(s->s3->tmp.ecdh);
1230 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1234 /* Duplicate the ECDH structure. */
1237 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1240 if (!EC_KEY_up_ref(ecdhp))
1242 SSLerr(SSL_F_SSL3_CTRL,ERR_R_ECDH_LIB);
1247 s->s3->tmp.ecdh=ecdh;
1248 if ((ecdh->pub_key == NULL) ||
1249 (ecdh->priv_key == NULL) ||
1250 (s->options & SSL_OP_SINGLE_ECDH_USE))
1252 if(!EC_KEY_generate_key(ecdh))
1254 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1259 if ((ecdh->group == NULL) ||
1260 (ecdh->pub_key == NULL) ||
1261 (ecdh->priv_key == NULL))
1263 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1267 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1268 (EC_GROUP_get_degree(ecdh->group) > 163))
1270 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1274 /* XXX: For now, we only support ephemeral ECDH
1275 * keys over named (not generic) curves. For
1276 * supported named curves, curve_id is non-zero.
1279 nid2curve_id(EC_GROUP_get_nid(ecdh->group)))
1282 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1286 /* Encode the public key.
1287 * First check the size of encoding and
1288 * allocate memory accordingly.
1290 encodedlen = EC_POINT_point2oct(ecdh->group,
1292 POINT_CONVERSION_UNCOMPRESSED,
1295 encodedPoint = (unsigned char *)
1296 OPENSSL_malloc(encodedlen*sizeof(unsigned char));
1297 bn_ctx = BN_CTX_new();
1298 if ((encodedPoint == NULL) || (bn_ctx == NULL))
1300 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
1305 encodedlen = EC_POINT_point2oct(ecdh->group,
1307 POINT_CONVERSION_UNCOMPRESSED,
1308 encodedPoint, encodedlen, bn_ctx);
1310 if (encodedlen == 0)
1312 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_ECDH_LIB);
1316 BN_CTX_free(bn_ctx); bn_ctx=NULL;
1318 /* XXX: For now, we only support named (not
1319 * generic) curves in ECDH ephemeral key exchanges.
1320 * In this situation, we need three additional bytes
1321 * to encode the entire ServerECDHParams
1326 /* We'll generate the serverKeyExchange message
1327 * explicitly so we can set these to NULLs
1334 #endif /* !OPENSSL_NO_ECDH */
1336 al=SSL_AD_HANDSHAKE_FAILURE;
1337 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1340 for (i=0; r[i] != NULL; i++)
1342 nr[i]=BN_num_bytes(r[i]);
1346 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
1348 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1351 al=SSL_AD_DECODE_ERROR;
1354 kn=EVP_PKEY_size(pkey);
1362 if (!BUF_MEM_grow(buf,n+4+kn))
1364 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1367 d=(unsigned char *)s->init_buf->data;
1370 for (i=0; r[i] != NULL; i++)
1377 #ifndef OPENSSL_NO_ECDH
1378 if (type & SSL_kECDHE)
1380 /* XXX: For now, we only support named (not generic) curves.
1381 * In this situation, the serverKeyExchange message has:
1382 * [1 byte CurveType], [1 byte CurveName]
1383 * [1 byte length of encoded point], followed by
1384 * the actual encoded point itself
1386 *p = NAMED_CURVE_TYPE;
1392 memcpy((unsigned char*)p,
1393 (unsigned char *)encodedPoint,
1395 OPENSSL_free(encodedPoint);
1403 /* n is the length of the params, they start at &(d[4])
1404 * and p points to the space at the end. */
1405 #ifndef OPENSSL_NO_RSA
1406 if (pkey->type == EVP_PKEY_RSA)
1410 for (num=2; num > 0; num--)
1412 EVP_DigestInit_ex(&md_ctx,(num == 2)
1413 ?s->ctx->md5:s->ctx->sha1, NULL);
1414 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1415 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1416 EVP_DigestUpdate(&md_ctx,&(d[4]),n);
1417 EVP_DigestFinal_ex(&md_ctx,q,
1418 (unsigned int *)&i);
1422 if (RSA_sign(NID_md5_sha1, md_buf, j,
1423 &(p[2]), &u, pkey->pkey.rsa) <= 0)
1425 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1433 #if !defined(OPENSSL_NO_DSA)
1434 if (pkey->type == EVP_PKEY_DSA)
1437 EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
1438 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1439 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1440 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1441 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1442 (unsigned int *)&i,pkey))
1444 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
1452 #if !defined(OPENSSL_NO_ECDSA)
1453 if (pkey->type == EVP_PKEY_EC)
1455 /* let's do ECDSA */
1456 EVP_SignInit_ex(&md_ctx,EVP_ecdsa(), NULL);
1457 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1458 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1459 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1460 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1461 (unsigned int *)&i,pkey))
1463 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_ECDSA);
1472 /* Is this error check actually needed? */
1473 al=SSL_AD_HANDSHAKE_FAILURE;
1474 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
1479 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
1482 /* we should now have things packed up, so lets send
1488 s->state = SSL3_ST_SW_KEY_EXCH_B;
1489 EVP_MD_CTX_cleanup(&md_ctx);
1490 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1492 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1494 #ifndef OPENSSL_NO_ECDH
1495 if (encodedPoint != NULL) OPENSSL_free(encodedPoint);
1496 BN_CTX_free(bn_ctx);
1498 EVP_MD_CTX_cleanup(&md_ctx);
1502 static int ssl3_send_certificate_request(SSL *s)
1504 unsigned char *p,*d;
1506 STACK_OF(X509_NAME) *sk=NULL;
1510 if (s->state == SSL3_ST_SW_CERT_REQ_A)
1514 d=p=(unsigned char *)&(buf->data[4]);
1516 /* get the list of acceptable cert types */
1518 n=ssl3_get_req_cert_type(s,p);
1527 sk=SSL_get_client_CA_list(s);
1531 for (i=0; i<sk_X509_NAME_num(sk); i++)
1533 name=sk_X509_NAME_value(sk,i);
1534 j=i2d_X509_NAME(name,NULL);
1535 if (!BUF_MEM_grow(buf,4+n+j+2))
1537 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
1540 p=(unsigned char *)&(buf->data[4+n]);
1541 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1544 i2d_X509_NAME(name,&p);
1551 i2d_X509_NAME(name,&p);
1552 j-=2; s2n(j,d); j+=2;
1558 /* else no CA names */
1559 p=(unsigned char *)&(buf->data[4+off]);
1562 d=(unsigned char *)buf->data;
1563 *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
1566 /* we should now have things packed up, so lets send
1571 #ifdef NETSCAPE_HANG_BUG
1572 p=(unsigned char *)s->init_buf->data + s->init_num;
1575 *(p++)=SSL3_MT_SERVER_DONE;
1584 /* SSL3_ST_SW_CERT_REQ_B */
1585 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1590 static int ssl3_get_client_key_exchange(SSL *s)
1596 #ifndef OPENSSL_NO_RSA
1598 EVP_PKEY *pkey=NULL;
1600 #ifndef OPENSSL_NO_DH
1604 #ifndef OPENSSL_NO_KRB5
1606 #endif /* OPENSSL_NO_KRB5 */
1608 #ifndef OPENSSL_NO_ECDH
1609 EC_KEY *srvr_ecdh = NULL;
1610 EVP_PKEY *clnt_pub_pkey = NULL;
1611 EC_POINT *clnt_ecpoint = NULL;
1612 BN_CTX *bn_ctx = NULL;
1615 n=ssl3_get_message(s,
1616 SSL3_ST_SR_KEY_EXCH_A,
1617 SSL3_ST_SR_KEY_EXCH_B,
1618 SSL3_MT_CLIENT_KEY_EXCHANGE,
1622 if (!ok) return((int)n);
1623 p=(unsigned char *)s->init_msg;
1625 l=s->s3->tmp.new_cipher->algorithms;
1627 #ifndef OPENSSL_NO_RSA
1630 /* FIX THIS UP EAY EAY EAY EAY */
1631 if (s->s3->tmp.use_rsa_tmp)
1633 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1634 rsa=s->cert->rsa_tmp;
1635 /* Don't do a callback because rsa_tmp should
1636 * be sent already */
1639 al=SSL_AD_HANDSHAKE_FAILURE;
1640 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
1647 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1648 if ( (pkey == NULL) ||
1649 (pkey->type != EVP_PKEY_RSA) ||
1650 (pkey->pkey.rsa == NULL))
1652 al=SSL_AD_HANDSHAKE_FAILURE;
1653 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
1660 if (s->version > SSL3_VERSION)
1665 if (!(s->options & SSL_OP_TLS_D5_BUG))
1667 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1677 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
1681 if (i != SSL_MAX_MASTER_KEY_LENGTH)
1683 al=SSL_AD_DECODE_ERROR;
1684 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1687 if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
1689 /* The premaster secret must contain the same version number as the
1690 * ClientHello to detect version rollback attacks (strangely, the
1691 * protocol does not offer such protection for DH ciphersuites).
1692 * However, buggy clients exist that send the negotiated protocol
1693 * version instead if the server does not support the requested
1695 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
1696 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
1697 (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
1699 al=SSL_AD_DECODE_ERROR;
1700 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1710 /* Some decryption failure -- use random value instead as countermeasure
1711 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
1712 * (see RFC 2246, section 7.4.7.1).
1713 * But note that due to length and protocol version checking, the
1714 * attack is impractical anyway (see section 5 in D. Bleichenbacher:
1715 * "Chosen Ciphertext Attacks Against Protocols Based on the RSA
1716 * Encryption Standard PKCS #1", CRYPTO '98, LNCS 1462, pp. 1-12).
1719 i = SSL_MAX_MASTER_KEY_LENGTH;
1720 p[0] = s->client_version >> 8;
1721 p[1] = s->client_version & 0xff;
1722 RAND_pseudo_bytes(p+2, i-2); /* should be RAND_bytes, but we cannot work around a failure */
1726 s->session->master_key_length=
1727 s->method->ssl3_enc->generate_master_secret(s,
1728 s->session->master_key,
1734 #ifndef OPENSSL_NO_DH
1735 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1740 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
1742 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1752 if (n == 0L) /* the parameters are in the cert */
1754 al=SSL_AD_HANDSHAKE_FAILURE;
1755 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1760 if (s->s3->tmp.dh == NULL)
1762 al=SSL_AD_HANDSHAKE_FAILURE;
1763 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1767 dh_srvr=s->s3->tmp.dh;
1770 pub=BN_bin2bn(p,i,NULL);
1773 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
1777 i=DH_compute_key(p,pub,dh_srvr);
1781 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1785 DH_free(s->s3->tmp.dh);
1790 s->session->master_key_length=
1791 s->method->ssl3_enc->generate_master_secret(s,
1792 s->session->master_key,p,i);
1797 #ifndef OPENSSL_NO_KRB5
1800 krb5_error_code krb5rc;
1801 krb5_data enc_ticket;
1802 krb5_data authenticator;
1804 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1805 EVP_CIPHER_CTX ciph_ctx;
1806 EVP_CIPHER *enc = NULL;
1807 unsigned char iv[EVP_MAX_IV_LENGTH];
1808 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH
1809 + EVP_MAX_BLOCK_LENGTH];
1811 krb5_timestamp authtime = 0;
1812 krb5_ticket_times ttimes;
1814 EVP_CIPHER_CTX_init(&ciph_ctx);
1816 if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
1819 enc_ticket.length = i;
1820 enc_ticket.data = (char *)p;
1821 p+=enc_ticket.length;
1824 authenticator.length = i;
1825 authenticator.data = (char *)p;
1826 p+=authenticator.length;
1830 enc_pms.data = (char *)p;
1833 /* Note that the length is checked again below,
1836 if(enc_pms.length > sizeof pms)
1838 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1839 SSL_R_DATA_LENGTH_TOO_LONG);
1843 if (n != enc_ticket.length + authenticator.length +
1846 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1847 SSL_R_DATA_LENGTH_TOO_LONG);
1851 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
1855 printf("kssl_sget_tkt rtn %d [%d]\n",
1856 krb5rc, kssl_err.reason);
1858 printf("kssl_err text= %s\n", kssl_err.text);
1859 #endif /* KSSL_DEBUG */
1860 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1865 /* Note: no authenticator is not considered an error,
1866 ** but will return authtime == 0.
1868 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
1869 &authtime, &kssl_err)) != 0)
1872 printf("kssl_check_authent rtn %d [%d]\n",
1873 krb5rc, kssl_err.reason);
1875 printf("kssl_err text= %s\n", kssl_err.text);
1876 #endif /* KSSL_DEBUG */
1877 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1882 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
1884 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, krb5rc);
1889 kssl_ctx_show(kssl_ctx);
1890 #endif /* KSSL_DEBUG */
1892 enc = kssl_map_enc(kssl_ctx->enctype);
1896 memset(iv, 0, EVP_MAX_IV_LENGTH); /* per RFC 1510 */
1898 if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
1900 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1901 SSL_R_DECRYPTION_FAILED);
1904 if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
1905 (unsigned char *)enc_pms.data, enc_pms.length))
1907 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1908 SSL_R_DECRYPTION_FAILED);
1911 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
1913 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1914 SSL_R_DATA_LENGTH_TOO_LONG);
1917 if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
1919 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1920 SSL_R_DECRYPTION_FAILED);
1924 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
1926 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1927 SSL_R_DATA_LENGTH_TOO_LONG);
1930 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
1932 s->session->master_key_length=
1933 s->method->ssl3_enc->generate_master_secret(s,
1934 s->session->master_key, pms, outl);
1936 if (kssl_ctx->client_princ)
1938 int len = strlen(kssl_ctx->client_princ);
1939 if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
1941 s->session->krb5_client_princ_len = len;
1942 memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
1947 /* Was doing kssl_ctx_free() here,
1948 ** but it caused problems for apache.
1949 ** kssl_ctx = kssl_ctx_free(kssl_ctx);
1950 ** if (s->kssl_ctx) s->kssl_ctx = NULL;
1954 #endif /* OPENSSL_NO_KRB5 */
1956 #ifndef OPENSSL_NO_ECDH
1957 if ((l & SSL_kECDH) || (l & SSL_kECDHE))
1961 /* initialize structures for server's ECDH key pair */
1962 if ((srvr_ecdh = EC_KEY_new()) == NULL)
1964 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1965 ERR_R_MALLOC_FAILURE);
1969 /* Let's get server private key and group information */
1972 /* use the certificate */
1973 srvr_ecdh->group = s->cert->key->privatekey-> \
1975 srvr_ecdh->priv_key = s->cert->key->privatekey-> \
1976 pkey.eckey->priv_key;
1980 /* use the ephermeral values we saved when
1981 * generating the ServerKeyExchange msg.
1983 srvr_ecdh->group = s->s3->tmp.ecdh->group;
1984 srvr_ecdh->priv_key = s->s3->tmp.ecdh->priv_key;
1987 /* Let's get client's public key */
1988 if ((clnt_ecpoint = EC_POINT_new(srvr_ecdh->group))
1991 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1992 ERR_R_MALLOC_FAILURE);
1998 /* Client Publickey was in Client Certificate */
2002 al=SSL_AD_HANDSHAKE_FAILURE;
2003 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_ECDH_KEY);
2006 if (((clnt_pub_pkey=X509_get_pubkey(s->session->peer))
2008 (clnt_pub_pkey->type != EVP_PKEY_EC))
2010 /* XXX: For now, we do not support client
2011 * authentication using ECDH certificates
2012 * so this branch (n == 0L) of the code is
2013 * never executed. When that support is
2014 * added, we ought to ensure the key
2015 * received in the certificate is
2016 * authorized for key agreement.
2017 * ECDH_compute_key implicitly checks that
2018 * the two ECDH shares are for the same
2021 al=SSL_AD_HANDSHAKE_FAILURE;
2022 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2023 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2027 EC_POINT_copy(clnt_ecpoint,
2028 clnt_pub_pkey->pkey.eckey->pub_key);
2029 ret = 2; /* Skip certificate verify processing */
2033 /* Get client's public key from encoded point
2034 * in the ClientKeyExchange message.
2036 if ((bn_ctx = BN_CTX_new()) == NULL)
2038 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2039 ERR_R_MALLOC_FAILURE);
2043 /* Get encoded point length */
2046 if (EC_POINT_oct2point(srvr_ecdh->group,
2047 clnt_ecpoint, p, i, bn_ctx) == 0)
2049 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2053 /* p is pointing to somewhere in the buffer
2054 * currently, so set it to the start
2056 p=(unsigned char *)s->init_buf->data;
2059 /* Compute the shared pre-master secret */
2060 i = ECDH_compute_key(p, clnt_ecpoint, srvr_ecdh);
2063 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2068 EVP_PKEY_free(clnt_pub_pkey);
2069 EC_POINT_free(clnt_ecpoint);
2070 if (srvr_ecdh != NULL)
2072 srvr_ecdh->priv_key = NULL;
2073 srvr_ecdh->group = NULL;
2074 EC_KEY_free(srvr_ecdh);
2076 BN_CTX_free(bn_ctx);
2078 /* Compute the master secret */
2079 s->session->master_key_length = s->method->ssl3_enc-> \
2080 generate_master_secret(s, s->session->master_key, p, i);
2088 al=SSL_AD_HANDSHAKE_FAILURE;
2089 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2090 SSL_R_UNKNOWN_CIPHER_TYPE);
2096 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2097 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH)
2100 #ifndef NO_OPENSSL_ECDH
2101 EVP_PKEY_free(clnt_pub_pkey);
2102 EC_POINT_free(clnt_ecpoint);
2103 if (srvr_ecdh != NULL)
2105 srvr_ecdh->priv_key = NULL;
2106 srvr_ecdh->group = NULL;
2107 EC_KEY_free(srvr_ecdh);
2109 BN_CTX_free(bn_ctx);
2114 static int ssl3_get_cert_verify(SSL *s)
2116 EVP_PKEY *pkey=NULL;
2123 n=ssl3_get_message(s,
2124 SSL3_ST_SR_CERT_VRFY_A,
2125 SSL3_ST_SR_CERT_VRFY_B,
2130 if (!ok) return((int)n);
2132 if (s->session->peer != NULL)
2134 peer=s->session->peer;
2135 pkey=X509_get_pubkey(peer);
2136 type=X509_certificate_type(peer,pkey);
2144 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
2146 s->s3->tmp.reuse_message=1;
2147 if ((peer != NULL) && (type | EVP_PKT_SIGN))
2149 al=SSL_AD_UNEXPECTED_MESSAGE;
2150 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
2159 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
2160 al=SSL_AD_UNEXPECTED_MESSAGE;
2164 if (!(type & EVP_PKT_SIGN))
2166 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2167 al=SSL_AD_ILLEGAL_PARAMETER;
2171 if (s->s3->change_cipher_spec)
2173 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
2174 al=SSL_AD_UNEXPECTED_MESSAGE;
2178 /* we now have a signature that we need to verify */
2179 p=(unsigned char *)s->init_msg;
2184 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
2185 al=SSL_AD_DECODE_ERROR;
2189 j=EVP_PKEY_size(pkey);
2190 if ((i > j) || (n > j) || (n <= 0))
2192 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
2193 al=SSL_AD_DECODE_ERROR;
2197 #ifndef OPENSSL_NO_RSA
2198 if (pkey->type == EVP_PKEY_RSA)
2200 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
2201 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
2205 al=SSL_AD_DECRYPT_ERROR;
2206 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
2211 al=SSL_AD_DECRYPT_ERROR;
2212 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
2218 #ifndef OPENSSL_NO_DSA
2219 if (pkey->type == EVP_PKEY_DSA)
2221 j=DSA_verify(pkey->save_type,
2222 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2223 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
2227 al=SSL_AD_DECRYPT_ERROR;
2228 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
2234 #ifndef OPENSSL_NO_ECDSA
2235 if (pkey->type == EVP_PKEY_EC)
2237 j=ECDSA_verify(pkey->save_type,
2238 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
2239 SHA_DIGEST_LENGTH,p,i,pkey->pkey.eckey);
2243 al=SSL_AD_DECRYPT_ERROR;
2244 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2245 SSL_R_BAD_ECDSA_SIGNATURE);
2252 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
2253 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
2262 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2265 EVP_PKEY_free(pkey);
2269 static int ssl3_get_client_certificate(SSL *s)
2271 int i,ok,al,ret= -1;
2273 unsigned long l,nc,llen,n;
2274 unsigned char *p,*d,*q;
2275 STACK_OF(X509) *sk=NULL;
2277 n=ssl3_get_message(s,
2284 if (!ok) return((int)n);
2286 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
2288 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
2289 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
2291 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2292 al=SSL_AD_HANDSHAKE_FAILURE;
2295 /* If tls asked for a client cert, the client must return a 0 list */
2296 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
2298 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
2299 al=SSL_AD_UNEXPECTED_MESSAGE;
2302 s->s3->tmp.reuse_message=1;
2306 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
2308 al=SSL_AD_UNEXPECTED_MESSAGE;
2309 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
2312 d=p=(unsigned char *)s->init_msg;
2314 if ((sk=sk_X509_new_null()) == NULL)
2316 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
2323 al=SSL_AD_DECODE_ERROR;
2324 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
2327 for (nc=0; nc<llen; )
2330 if ((l+nc+3) > llen)
2332 al=SSL_AD_DECODE_ERROR;
2333 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
2338 x=d2i_X509(NULL,&p,l);
2341 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
2346 al=SSL_AD_DECODE_ERROR;
2347 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
2350 if (!sk_X509_push(sk,x))
2352 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
2359 if (sk_X509_num(sk) <= 0)
2361 /* TLS does not mind 0 certs returned */
2362 if (s->version == SSL3_VERSION)
2364 al=SSL_AD_HANDSHAKE_FAILURE;
2365 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
2368 /* Fail for TLS only if we required a certificate */
2369 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
2370 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
2372 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2373 al=SSL_AD_HANDSHAKE_FAILURE;
2379 i=ssl_verify_cert_chain(s,sk);
2382 al=ssl_verify_alarm_type(s->verify_result);
2383 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
2388 if (s->session->peer != NULL) /* This should not be needed */
2389 X509_free(s->session->peer);
2390 s->session->peer=sk_X509_shift(sk);
2391 s->session->verify_result = s->verify_result;
2393 /* With the current implementation, sess_cert will always be NULL
2394 * when we arrive here. */
2395 if (s->session->sess_cert == NULL)
2397 s->session->sess_cert = ssl_sess_cert_new();
2398 if (s->session->sess_cert == NULL)
2400 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2404 if (s->session->sess_cert->cert_chain != NULL)
2405 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2406 s->session->sess_cert->cert_chain=sk;
2407 /* Inconsistency alert: cert_chain does *not* include the
2408 * peer's own certificate, while we do include it in s3_clnt.c */
2416 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2419 if (x != NULL) X509_free(x);
2420 if (sk != NULL) sk_X509_pop_free(sk,X509_free);
2424 int ssl3_send_server_certificate(SSL *s)
2429 if (s->state == SSL3_ST_SW_CERT_A)
2431 x=ssl_get_server_send_cert(s);
2433 /* VRS: allow null cert if auth == KRB5 */
2434 (s->s3->tmp.new_cipher->algorithms
2435 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
2436 != (SSL_aKRB5|SSL_kKRB5))
2438 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
2442 l=ssl3_output_cert_chain(s,x);
2443 s->state=SSL3_ST_SW_CERT_B;
2448 /* SSL3_ST_SW_CERT_B */
2449 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2453 #ifndef OPENSSL_NO_ECDH
2454 /* This is the complement of curve_id2nid in s3_clnt.c. */
2455 static int nid2curve_id(int nid)
2457 /* ECC curves from draft-ietf-tls-ecc-01.txt (Mar 15, 2001) */
2459 case NID_sect163k1: /* sect163k1 (1) */
2461 case NID_sect163r1: /* sect163r1 (2) */
2463 case NID_sect163r2: /* sect163r2 (3) */
2465 case NID_sect193r1: /* sect193r1 (4) */
2467 case NID_sect193r2: /* sect193r2 (5) */
2469 case NID_sect233k1: /* sect233k1 (6) */
2471 case NID_sect233r1: /* sect233r1 (7) */
2473 case NID_sect239k1: /* sect239k1 (8) */
2475 case NID_sect283k1: /* sect283k1 (9) */
2477 case NID_sect283r1: /* sect283r1 (10) */
2479 case NID_sect409k1: /* sect409k1 (11) */
2481 case NID_sect409r1: /* sect409r1 (12) */
2483 case NID_sect571k1: /* sect571k1 (13) */
2485 case NID_sect571r1: /* sect571r1 (14) */
2487 case NID_secp160k1: /* secp160k1 (15) */
2489 case NID_secp160r1: /* secp160r1 (16) */
2491 case NID_secp160r2: /* secp160r2 (17) */
2493 case NID_secp192k1: /* secp192k1 (18) */
2495 case NID_X9_62_prime192v1: /* secp192r1 (19) */
2497 case NID_secp224k1: /* secp224k1 (20) */
2499 case NID_secp224r1: /* secp224r1 (21) */
2501 case NID_secp256k1: /* secp256k1 (22) */
2503 case NID_X9_62_prime256v1: /* secp256r1 (23) */
2505 case NID_secp384r1: /* secp384r1 (24) */
2507 case NID_secp521r1: /* secp521r1 (25) */