free cleanup almost the finale
[openssl.git] / ssl / ssl_cert.c
1 /*
2  * ! \file ssl/ssl_cert.c
3  */
4 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5  * All rights reserved.
6  *
7  * This package is an SSL implementation written
8  * by Eric Young (eay@cryptsoft.com).
9  * The implementation was written so as to conform with Netscapes SSL.
10  *
11  * This library is free for commercial and non-commercial use as long as
12  * the following conditions are aheared to.  The following conditions
13  * apply to all code found in this distribution, be it the RC4, RSA,
14  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
15  * included with this distribution is covered by the same copyright terms
16  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
17  *
18  * Copyright remains Eric Young's, and as such any Copyright notices in
19  * the code are not to be removed.
20  * If this package is used in a product, Eric Young should be given attribution
21  * as the author of the parts of the library used.
22  * This can be in the form of a textual message at program startup or
23  * in documentation (online or textual) provided with the package.
24  *
25  * Redistribution and use in source and binary forms, with or without
26  * modification, are permitted provided that the following conditions
27  * are met:
28  * 1. Redistributions of source code must retain the copyright
29  *    notice, this list of conditions and the following disclaimer.
30  * 2. Redistributions in binary form must reproduce the above copyright
31  *    notice, this list of conditions and the following disclaimer in the
32  *    documentation and/or other materials provided with the distribution.
33  * 3. All advertising materials mentioning features or use of this software
34  *    must display the following acknowledgement:
35  *    "This product includes cryptographic software written by
36  *     Eric Young (eay@cryptsoft.com)"
37  *    The word 'cryptographic' can be left out if the rouines from the library
38  *    being used are not cryptographic related :-).
39  * 4. If you include any Windows specific code (or a derivative thereof) from
40  *    the apps directory (application code) you must include an acknowledgement:
41  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
42  *
43  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  *
55  * The licence and distribution terms for any publically available version or
56  * derivative of this code cannot be changed.  i.e. this code cannot simply be
57  * copied and put under another distribution licence
58  * [including the GNU Public Licence.]
59  */
60 /* ====================================================================
61  * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
62  *
63  * Redistribution and use in source and binary forms, with or without
64  * modification, are permitted provided that the following conditions
65  * are met:
66  *
67  * 1. Redistributions of source code must retain the above copyright
68  *    notice, this list of conditions and the following disclaimer.
69  *
70  * 2. Redistributions in binary form must reproduce the above copyright
71  *    notice, this list of conditions and the following disclaimer in
72  *    the documentation and/or other materials provided with the
73  *    distribution.
74  *
75  * 3. All advertising materials mentioning features or use of this
76  *    software must display the following acknowledgment:
77  *    "This product includes software developed by the OpenSSL Project
78  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
79  *
80  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
81  *    endorse or promote products derived from this software without
82  *    prior written permission. For written permission, please contact
83  *    openssl-core@openssl.org.
84  *
85  * 5. Products derived from this software may not be called "OpenSSL"
86  *    nor may "OpenSSL" appear in their names without prior written
87  *    permission of the OpenSSL Project.
88  *
89  * 6. Redistributions of any form whatsoever must retain the following
90  *    acknowledgment:
91  *    "This product includes software developed by the OpenSSL Project
92  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
93  *
94  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
95  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
96  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
97  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
98  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
99  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
100  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
101  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
103  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
104  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
105  * OF THE POSSIBILITY OF SUCH DAMAGE.
106  * ====================================================================
107  *
108  * This product includes cryptographic software written by Eric Young
109  * (eay@cryptsoft.com).  This product includes software written by Tim
110  * Hudson (tjh@cryptsoft.com).
111  *
112  */
113 /* ====================================================================
114  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
115  * ECC cipher suite support in OpenSSL originally developed by
116  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
117  */
118
119 #include <stdio.h>
120
121 #include "e_os.h"
122 #ifndef NO_SYS_TYPES_H
123 # include <sys/types.h>
124 #endif
125
126 #include "o_dir.h"
127 #include <openssl/objects.h>
128 #include <openssl/bio.h>
129 #include <openssl/pem.h>
130 #include <openssl/x509v3.h>
131 #ifndef OPENSSL_NO_DH
132 # include <openssl/dh.h>
133 #endif
134 #include <openssl/bn.h>
135 #include "ssl_locl.h"
136
137 static int ssl_security_default_callback(SSL *s, SSL_CTX *ctx, int op,
138                                          int bits, int nid, void *other,
139                                          void *ex);
140
141 int SSL_get_ex_data_X509_STORE_CTX_idx(void)
142 {
143     static volatile int ssl_x509_store_ctx_idx = -1;
144     int got_write_lock = 0;
145
146     CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
147
148     if (ssl_x509_store_ctx_idx < 0) {
149         CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
150         CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
151         got_write_lock = 1;
152
153         if (ssl_x509_store_ctx_idx < 0) {
154             ssl_x509_store_ctx_idx =
155                 X509_STORE_CTX_get_ex_new_index(0, "SSL for verify callback",
156                                                 NULL, NULL, NULL);
157         }
158     }
159
160     if (got_write_lock)
161         CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
162     else
163         CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
164
165     return ssl_x509_store_ctx_idx;
166 }
167
168 void ssl_cert_set_default_md(CERT *cert)
169 {
170     /* Set digest values to defaults */
171 #ifndef OPENSSL_NO_DSA
172     cert->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
173 #endif
174 #ifndef OPENSSL_NO_RSA
175     cert->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
176     cert->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
177 #endif
178 #ifndef OPENSSL_NO_EC
179     cert->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
180 #endif
181 }
182
183 CERT *ssl_cert_new(void)
184 {
185     CERT *ret;
186
187     ret = OPENSSL_malloc(sizeof(CERT));
188     if (ret == NULL) {
189         SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE);
190         return (NULL);
191     }
192     memset(ret, 0, sizeof(CERT));
193
194     ret->key = &(ret->pkeys[SSL_PKEY_RSA_ENC]);
195     ret->references = 1;
196     ssl_cert_set_default_md(ret);
197     ret->sec_cb = ssl_security_default_callback;
198     ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL;
199     ret->sec_ex = NULL;
200     return (ret);
201 }
202
203 CERT *ssl_cert_dup(CERT *cert)
204 {
205     CERT *ret;
206     int i;
207
208     ret = OPENSSL_malloc(sizeof(CERT));
209     if (ret == NULL) {
210         SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
211         return (NULL);
212     }
213
214     memset(ret, 0, sizeof(CERT));
215
216     ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
217     /*
218      * or ret->key = ret->pkeys + (cert->key - cert->pkeys), if you find that
219      * more readable
220      */
221
222     ret->valid = cert->valid;
223     ret->mask_k = cert->mask_k;
224     ret->mask_a = cert->mask_a;
225     ret->export_mask_k = cert->export_mask_k;
226     ret->export_mask_a = cert->export_mask_a;
227
228 #ifndef OPENSSL_NO_RSA
229     if (cert->rsa_tmp != NULL) {
230         RSA_up_ref(cert->rsa_tmp);
231         ret->rsa_tmp = cert->rsa_tmp;
232     }
233     ret->rsa_tmp_cb = cert->rsa_tmp_cb;
234 #endif
235
236 #ifndef OPENSSL_NO_DH
237     if (cert->dh_tmp != NULL) {
238         ret->dh_tmp = DHparams_dup(cert->dh_tmp);
239         if (ret->dh_tmp == NULL) {
240             SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_DH_LIB);
241             goto err;
242         }
243         if (cert->dh_tmp->priv_key) {
244             BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
245             if (!b) {
246                 SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
247                 goto err;
248             }
249             ret->dh_tmp->priv_key = b;
250         }
251         if (cert->dh_tmp->pub_key) {
252             BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
253             if (!b) {
254                 SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
255                 goto err;
256             }
257             ret->dh_tmp->pub_key = b;
258         }
259     }
260     ret->dh_tmp_cb = cert->dh_tmp_cb;
261     ret->dh_tmp_auto = cert->dh_tmp_auto;
262 #endif
263
264 #ifndef OPENSSL_NO_EC
265     if (cert->ecdh_tmp) {
266         ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp);
267         if (ret->ecdh_tmp == NULL) {
268             SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_EC_LIB);
269             goto err;
270         }
271     }
272     ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
273     ret->ecdh_tmp_auto = cert->ecdh_tmp_auto;
274 #endif
275
276     for (i = 0; i < SSL_PKEY_NUM; i++) {
277         CERT_PKEY *cpk = cert->pkeys + i;
278         CERT_PKEY *rpk = ret->pkeys + i;
279         if (cpk->x509 != NULL) {
280             rpk->x509 = cpk->x509;
281             CRYPTO_add(&rpk->x509->references, 1, CRYPTO_LOCK_X509);
282         }
283
284         if (cpk->privatekey != NULL) {
285             rpk->privatekey = cpk->privatekey;
286             CRYPTO_add(&cpk->privatekey->references, 1, CRYPTO_LOCK_EVP_PKEY);
287         }
288
289         if (cpk->chain) {
290             rpk->chain = X509_chain_up_ref(cpk->chain);
291             if (!rpk->chain) {
292                 SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
293                 goto err;
294             }
295         }
296         rpk->valid_flags = 0;
297 #ifndef OPENSSL_NO_TLSEXT
298         if (cert->pkeys[i].serverinfo != NULL) {
299             /* Just copy everything. */
300             ret->pkeys[i].serverinfo =
301                 OPENSSL_malloc(cert->pkeys[i].serverinfo_length);
302             if (ret->pkeys[i].serverinfo == NULL) {
303                 SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
304                 goto err;
305             }
306             ret->pkeys[i].serverinfo_length =
307                 cert->pkeys[i].serverinfo_length;
308             memcpy(ret->pkeys[i].serverinfo,
309                    cert->pkeys[i].serverinfo,
310                    cert->pkeys[i].serverinfo_length);
311         }
312 #endif
313     }
314
315     ret->references = 1;
316     /*
317      * Set digests to defaults. NB: we don't copy existing values as they
318      * will be set during handshake.
319      */
320     ssl_cert_set_default_md(ret);
321     /* Peer sigalgs set to NULL as we get these from handshake too */
322     ret->peer_sigalgs = NULL;
323     ret->peer_sigalgslen = 0;
324     /* Configured sigalgs however we copy across */
325
326     if (cert->conf_sigalgs) {
327         ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen);
328         if (!ret->conf_sigalgs)
329             goto err;
330         memcpy(ret->conf_sigalgs, cert->conf_sigalgs, cert->conf_sigalgslen);
331         ret->conf_sigalgslen = cert->conf_sigalgslen;
332     } else
333         ret->conf_sigalgs = NULL;
334
335     if (cert->client_sigalgs) {
336         ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen);
337         if (!ret->client_sigalgs)
338             goto err;
339         memcpy(ret->client_sigalgs, cert->client_sigalgs,
340                cert->client_sigalgslen);
341         ret->client_sigalgslen = cert->client_sigalgslen;
342     } else
343         ret->client_sigalgs = NULL;
344     /* Shared sigalgs also NULL */
345     ret->shared_sigalgs = NULL;
346     /* Copy any custom client certificate types */
347     if (cert->ctypes) {
348         ret->ctypes = OPENSSL_malloc(cert->ctype_num);
349         if (!ret->ctypes)
350             goto err;
351         memcpy(ret->ctypes, cert->ctypes, cert->ctype_num);
352         ret->ctype_num = cert->ctype_num;
353     }
354
355     ret->cert_flags = cert->cert_flags;
356
357     ret->cert_cb = cert->cert_cb;
358     ret->cert_cb_arg = cert->cert_cb_arg;
359
360     if (cert->verify_store) {
361         CRYPTO_add(&cert->verify_store->references, 1,
362                    CRYPTO_LOCK_X509_STORE);
363         ret->verify_store = cert->verify_store;
364     }
365
366     if (cert->chain_store) {
367         CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE);
368         ret->chain_store = cert->chain_store;
369     }
370
371     ret->ciphers_raw = NULL;
372
373     ret->sec_cb = cert->sec_cb;
374     ret->sec_level = cert->sec_level;
375     ret->sec_ex = cert->sec_ex;
376
377 #ifndef OPENSSL_NO_TLSEXT
378     if (!custom_exts_copy(&ret->cli_ext, &cert->cli_ext))
379         goto err;
380     if (!custom_exts_copy(&ret->srv_ext, &cert->srv_ext))
381         goto err;
382 #endif
383
384     return (ret);
385
386  err:
387     ssl_cert_free(ret);
388
389     return NULL;
390 }
391
392 /* Free up and clear all certificates and chains */
393
394 void ssl_cert_clear_certs(CERT *c)
395 {
396     int i;
397     if (c == NULL)
398         return;
399     for (i = 0; i < SSL_PKEY_NUM; i++) {
400         CERT_PKEY *cpk = c->pkeys + i;
401         X509_free(cpk->x509);
402         cpk->x509 = NULL;
403         EVP_PKEY_free(cpk->privatekey);
404         cpk->privatekey = NULL;
405         sk_X509_pop_free(cpk->chain, X509_free);
406         cpk->chain = NULL;
407 #ifndef OPENSSL_NO_TLSEXT
408         if (cpk->serverinfo) {
409             OPENSSL_free(cpk->serverinfo);
410             cpk->serverinfo = NULL;
411             cpk->serverinfo_length = 0;
412         }
413 #endif
414         /* Clear all flags apart from explicit sign */
415         cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
416     }
417 }
418
419 void ssl_cert_free(CERT *c)
420 {
421     int i;
422
423     if (c == NULL)
424         return;
425
426     i = CRYPTO_add(&c->references, -1, CRYPTO_LOCK_SSL_CERT);
427 #ifdef REF_PRINT
428     REF_PRINT("CERT", c);
429 #endif
430     if (i > 0)
431         return;
432 #ifdef REF_CHECK
433     if (i < 0) {
434         fprintf(stderr, "ssl_cert_free, bad reference count\n");
435         abort();                /* ok */
436     }
437 #endif
438
439 #ifndef OPENSSL_NO_RSA
440     RSA_free(c->rsa_tmp);
441 #endif
442 #ifndef OPENSSL_NO_DH
443     DH_free(c->dh_tmp);
444 #endif
445 #ifndef OPENSSL_NO_EC
446     EC_KEY_free(c->ecdh_tmp);
447 #endif
448
449     ssl_cert_clear_certs(c);
450     if (c->peer_sigalgs)
451         OPENSSL_free(c->peer_sigalgs);
452     if (c->conf_sigalgs)
453         OPENSSL_free(c->conf_sigalgs);
454     if (c->client_sigalgs)
455         OPENSSL_free(c->client_sigalgs);
456     if (c->shared_sigalgs)
457         OPENSSL_free(c->shared_sigalgs);
458     if (c->ctypes)
459         OPENSSL_free(c->ctypes);
460     X509_STORE_free(c->verify_store);
461     X509_STORE_free(c->chain_store);
462     if (c->ciphers_raw)
463         OPENSSL_free(c->ciphers_raw);
464 #ifndef OPENSSL_NO_TLSEXT
465     custom_exts_free(&c->cli_ext);
466     custom_exts_free(&c->srv_ext);
467 #endif
468     OPENSSL_clear_free(c->pms, c->pmslen);
469     c->pms = NULL;
470     OPENSSL_free(c);
471 }
472
473 int ssl_cert_set0_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
474 {
475     int i, r;
476     CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key;
477     if (!cpk)
478         return 0;
479     sk_X509_pop_free(cpk->chain, X509_free);
480     for (i = 0; i < sk_X509_num(chain); i++) {
481         r = ssl_security_cert(s, ctx, sk_X509_value(chain, i), 0, 0);
482         if (r != 1) {
483             SSLerr(SSL_F_SSL_CERT_SET0_CHAIN, r);
484             return 0;
485         }
486     }
487     cpk->chain = chain;
488     return 1;
489 }
490
491 int ssl_cert_set1_chain(SSL *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
492 {
493     STACK_OF(X509) *dchain;
494     if (!chain)
495         return ssl_cert_set0_chain(s, ctx, NULL);
496     dchain = X509_chain_up_ref(chain);
497     if (!dchain)
498         return 0;
499     if (!ssl_cert_set0_chain(s, ctx, dchain)) {
500         sk_X509_pop_free(dchain, X509_free);
501         return 0;
502     }
503     return 1;
504 }
505
506 int ssl_cert_add0_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x)
507 {
508     int r;
509     CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key;
510     if (!cpk)
511         return 0;
512     r = ssl_security_cert(s, ctx, x, 0, 0);
513     if (r != 1) {
514         SSLerr(SSL_F_SSL_CERT_ADD0_CHAIN_CERT, r);
515         return 0;
516     }
517     if (!cpk->chain)
518         cpk->chain = sk_X509_new_null();
519     if (!cpk->chain || !sk_X509_push(cpk->chain, x))
520         return 0;
521     return 1;
522 }
523
524 int ssl_cert_add1_chain_cert(SSL *s, SSL_CTX *ctx, X509 *x)
525 {
526     if (!ssl_cert_add0_chain_cert(s, ctx, x))
527         return 0;
528     CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
529     return 1;
530 }
531
532 int ssl_cert_select_current(CERT *c, X509 *x)
533 {
534     int i;
535     if (x == NULL)
536         return 0;
537     for (i = 0; i < SSL_PKEY_NUM; i++) {
538         CERT_PKEY *cpk = c->pkeys + i;
539         if (cpk->x509 == x && cpk->privatekey) {
540             c->key = cpk;
541             return 1;
542         }
543     }
544
545     for (i = 0; i < SSL_PKEY_NUM; i++) {
546         CERT_PKEY *cpk = c->pkeys + i;
547         if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) {
548             c->key = cpk;
549             return 1;
550         }
551     }
552     return 0;
553 }
554
555 int ssl_cert_set_current(CERT *c, long op)
556 {
557     int i, idx;
558     if (!c)
559         return 0;
560     if (op == SSL_CERT_SET_FIRST)
561         idx = 0;
562     else if (op == SSL_CERT_SET_NEXT) {
563         idx = (int)(c->key - c->pkeys + 1);
564         if (idx >= SSL_PKEY_NUM)
565             return 0;
566     } else
567         return 0;
568     for (i = idx; i < SSL_PKEY_NUM; i++) {
569         CERT_PKEY *cpk = c->pkeys + i;
570         if (cpk->x509 && cpk->privatekey) {
571             c->key = cpk;
572             return 1;
573         }
574     }
575     return 0;
576 }
577
578 void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg)
579 {
580     c->cert_cb = cb;
581     c->cert_cb_arg = arg;
582 }
583
584 SESS_CERT *ssl_sess_cert_new(void)
585 {
586     SESS_CERT *ret;
587
588     ret = OPENSSL_malloc(sizeof *ret);
589     if (ret == NULL) {
590         SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE);
591         return NULL;
592     }
593
594     memset(ret, 0, sizeof *ret);
595     ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
596     ret->references = 1;
597
598     return ret;
599 }
600
601 void ssl_sess_cert_free(SESS_CERT *sc)
602 {
603     int i;
604
605     if (sc == NULL)
606         return;
607
608     i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT);
609 #ifdef REF_PRINT
610     REF_PRINT("SESS_CERT", sc);
611 #endif
612     if (i > 0)
613         return;
614 #ifdef REF_CHECK
615     if (i < 0) {
616         fprintf(stderr, "ssl_sess_cert_free, bad reference count\n");
617         abort();                /* ok */
618     }
619 #endif
620
621     /* i == 0 */
622     sk_X509_pop_free(sc->cert_chain, X509_free);
623     for (i = 0; i < SSL_PKEY_NUM; i++) {
624         X509_free(sc->peer_pkeys[i].x509);
625 #if 0
626         /*
627          * We don't have the peer's private key. These lines are just
628          * here as a reminder that we're still using a not-quite-appropriate
629          *  data structure.
630          */
631         if (sc->peer_pkeys[i].privatekey != NULL)
632             EVP_PKEY_free(sc->peer_pkeys[i].privatekey);
633 #endif
634     }
635
636 #ifndef OPENSSL_NO_RSA
637     RSA_free(sc->peer_rsa_tmp);
638 #endif
639 #ifndef OPENSSL_NO_DH
640     DH_free(sc->peer_dh_tmp);
641 #endif
642 #ifndef OPENSSL_NO_EC
643     EC_KEY_free(sc->peer_ecdh_tmp);
644 #endif
645
646     OPENSSL_free(sc);
647 }
648
649 int ssl_set_peer_cert_type(SESS_CERT *sc, int type)
650 {
651     sc->peer_cert_type = type;
652     return (1);
653 }
654
655 int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk)
656 {
657     X509 *x;
658     int i;
659     X509_STORE *verify_store;
660     X509_STORE_CTX ctx;
661
662     if (s->cert->verify_store)
663         verify_store = s->cert->verify_store;
664     else
665         verify_store = s->ctx->cert_store;
666
667     if ((sk == NULL) || (sk_X509_num(sk) == 0))
668         return (0);
669
670     x = sk_X509_value(sk, 0);
671     if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) {
672         SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_X509_LIB);
673         return (0);
674     }
675     /* Set suite B flags if needed */
676     X509_STORE_CTX_set_flags(&ctx, tls1_suiteb(s));
677     X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s);
678
679     /*
680      * We need to inherit the verify parameters. These can be determined by
681      * the context: if its a server it will verify SSL client certificates or
682      * vice versa.
683      */
684
685     X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server");
686     /*
687      * Anything non-default in "param" should overwrite anything in the ctx.
688      */
689     X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
690
691     if (s->verify_callback)
692         X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
693
694     if (s->ctx->app_verify_callback != NULL)
695         i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
696     else {
697         i = X509_verify_cert(&ctx);
698 # if 0
699         /* Dummy error calls so mkerr generates them */
700         SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);
701         SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);
702         SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);
703 # endif
704         if (i > 0)
705             i = ssl_security_cert_chain(s, ctx.chain, NULL, 1);
706     }
707
708     s->verify_result = ctx.error;
709     X509_STORE_CTX_cleanup(&ctx);
710
711     return (i);
712 }
713
714 static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
715                                STACK_OF(X509_NAME) *name_list)
716 {
717     sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
718     *ca_list = name_list;
719 }
720
721 STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk)
722 {
723     int i;
724     STACK_OF(X509_NAME) *ret;
725     X509_NAME *name;
726
727     ret = sk_X509_NAME_new_null();
728     for (i = 0; i < sk_X509_NAME_num(sk); i++) {
729         name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
730         if ((name == NULL) || !sk_X509_NAME_push(ret, name)) {
731             sk_X509_NAME_pop_free(ret, X509_NAME_free);
732             return (NULL);
733         }
734     }
735     return (ret);
736 }
737
738 void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
739 {
740     set_client_CA_list(&(s->client_CA), name_list);
741 }
742
743 void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
744 {
745     set_client_CA_list(&(ctx->client_CA), name_list);
746 }
747
748 STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
749 {
750     return (ctx->client_CA);
751 }
752
753 STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
754 {
755     if (s->type == SSL_ST_CONNECT) { /* we are in the client */
756         if (((s->version >> 8) == SSL3_VERSION_MAJOR) && (s->s3 != NULL))
757             return (s->s3->tmp.ca_names);
758         else
759             return (NULL);
760     } else {
761         if (s->client_CA != NULL)
762             return (s->client_CA);
763         else
764             return (s->ctx->client_CA);
765     }
766 }
767
768 static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x)
769 {
770     X509_NAME *name;
771
772     if (x == NULL)
773         return (0);
774     if ((*sk == NULL) && ((*sk = sk_X509_NAME_new_null()) == NULL))
775         return (0);
776
777     if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
778         return (0);
779
780     if (!sk_X509_NAME_push(*sk, name)) {
781         X509_NAME_free(name);
782         return (0);
783     }
784     return (1);
785 }
786
787 int SSL_add_client_CA(SSL *ssl, X509 *x)
788 {
789     return (add_client_CA(&(ssl->client_CA), x));
790 }
791
792 int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
793 {
794     return (add_client_CA(&(ctx->client_CA), x));
795 }
796
797 static int xname_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
798 {
799     return (X509_NAME_cmp(*a, *b));
800 }
801
802 #ifndef OPENSSL_NO_STDIO
803 /**
804  * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
805  * it doesn't really have anything to do with clients (except that a common use
806  * for a stack of CAs is to send it to the client). Actually, it doesn't have
807  * much to do with CAs, either, since it will load any old cert.
808  * \param file the file containing one or more certs.
809  * \return a ::STACK containing the certs.
810  */
811 STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
812 {
813     BIO *in;
814     X509 *x = NULL;
815     X509_NAME *xn = NULL;
816     STACK_OF(X509_NAME) *ret = NULL, *sk;
817
818     sk = sk_X509_NAME_new(xname_cmp);
819
820     in = BIO_new(BIO_s_file_internal());
821
822     if ((sk == NULL) || (in == NULL)) {
823         SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
824         goto err;
825     }
826
827     if (!BIO_read_filename(in, file))
828         goto err;
829
830     for (;;) {
831         if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
832             break;
833         if (ret == NULL) {
834             ret = sk_X509_NAME_new_null();
835             if (ret == NULL) {
836                 SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
837                 goto err;
838             }
839         }
840         if ((xn = X509_get_subject_name(x)) == NULL)
841             goto err;
842         /* check for duplicates */
843         xn = X509_NAME_dup(xn);
844         if (xn == NULL)
845             goto err;
846         if (sk_X509_NAME_find(sk, xn) >= 0)
847             X509_NAME_free(xn);
848         else {
849             sk_X509_NAME_push(sk, xn);
850             sk_X509_NAME_push(ret, xn);
851         }
852     }
853
854     if (0) {
855  err:
856         sk_X509_NAME_pop_free(ret, X509_NAME_free);
857         ret = NULL;
858     }
859     sk_X509_NAME_free(sk);
860     BIO_free(in);
861     X509_free(x);
862     if (ret != NULL)
863         ERR_clear_error();
864     return (ret);
865 }
866 #endif
867
868 /**
869  * Add a file of certs to a stack.
870  * \param stack the stack to add to.
871  * \param file the file to add from. All certs in this file that are not
872  * already in the stack will be added.
873  * \return 1 for success, 0 for failure. Note that in the case of failure some
874  * certs may have been added to \c stack.
875  */
876
877 int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
878                                         const char *file)
879 {
880     BIO *in;
881     X509 *x = NULL;
882     X509_NAME *xn = NULL;
883     int ret = 1;
884     int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
885
886     oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
887
888     in = BIO_new(BIO_s_file_internal());
889
890     if (in == NULL) {
891         SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK,
892                ERR_R_MALLOC_FAILURE);
893         goto err;
894     }
895
896     if (!BIO_read_filename(in, file))
897         goto err;
898
899     for (;;) {
900         if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
901             break;
902         if ((xn = X509_get_subject_name(x)) == NULL)
903             goto err;
904         xn = X509_NAME_dup(xn);
905         if (xn == NULL)
906             goto err;
907         if (sk_X509_NAME_find(stack, xn) >= 0)
908             X509_NAME_free(xn);
909         else
910             sk_X509_NAME_push(stack, xn);
911     }
912
913     ERR_clear_error();
914
915     if (0) {
916  err:
917         ret = 0;
918     }
919     BIO_free(in);
920     if (x != NULL)
921         X509_free(x);
922
923     (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
924
925     return ret;
926 }
927
928 /**
929  * Add a directory of certs to a stack.
930  * \param stack the stack to append to.
931  * \param dir the directory to append from. All files in this directory will be
932  * examined as potential certs. Any that are acceptable to
933  * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
934  * included.
935  * \return 1 for success, 0 for failure. Note that in the case of failure some
936  * certs may have been added to \c stack.
937  */
938
939 int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
940                                        const char *dir)
941 {
942     OPENSSL_DIR_CTX *d = NULL;
943     const char *filename;
944     int ret = 0;
945
946     CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
947
948     /* Note that a side effect is that the CAs will be sorted by name */
949
950     while ((filename = OPENSSL_DIR_read(&d, dir))) {
951         char buf[1024];
952         int r;
953
954         if (strlen(dir) + strlen(filename) + 2 > sizeof buf) {
955             SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,
956                    SSL_R_PATH_TOO_LONG);
957             goto err;
958         }
959 #ifdef OPENSSL_SYS_VMS
960         r = BIO_snprintf(buf, sizeof buf, "%s%s", dir, filename);
961 #else
962         r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename);
963 #endif
964         if (r <= 0 || r >= (int)sizeof(buf))
965             goto err;
966         if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
967             goto err;
968     }
969
970     if (errno) {
971         SYSerr(SYS_F_OPENDIR, get_last_sys_error());
972         ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
973         SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);
974         goto err;
975     }
976
977     ret = 1;
978
979  err:
980     if (d)
981         OPENSSL_DIR_end(&d);
982     CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
983     return ret;
984 }
985
986 /* Add a certificate to a BUF_MEM structure */
987
988 static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
989 {
990     int n;
991     unsigned char *p;
992
993     n = i2d_X509(x, NULL);
994     if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
995         SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);
996         return 0;
997     }
998     p = (unsigned char *)&(buf->data[*l]);
999     l2n3(n, p);
1000     i2d_X509(x, &p);
1001     *l += n + 3;
1002
1003     return 1;
1004 }
1005
1006 /* Add certificate chain to internal SSL BUF_MEM strcuture */
1007 int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l)
1008 {
1009     BUF_MEM *buf = s->init_buf;
1010     int i;
1011
1012     X509 *x;
1013     STACK_OF(X509) *extra_certs;
1014     X509_STORE *chain_store;
1015
1016     /* TLSv1 sends a chain with nothing in it, instead of an alert */
1017     if (!BUF_MEM_grow_clean(buf, 10)) {
1018         SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_BUF_LIB);
1019         return 0;
1020     }
1021
1022     if (!cpk || !cpk->x509)
1023         return 1;
1024
1025     x = cpk->x509;
1026
1027     /*
1028      * If we have a certificate specific chain use it, else use parent ctx.
1029      */
1030     if (cpk->chain)
1031         extra_certs = cpk->chain;
1032     else
1033         extra_certs = s->ctx->extra_certs;
1034
1035     if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
1036         chain_store = NULL;
1037     else if (s->cert->chain_store)
1038         chain_store = s->cert->chain_store;
1039     else
1040         chain_store = s->ctx->cert_store;
1041
1042     if (chain_store) {
1043         X509_STORE_CTX xs_ctx;
1044
1045         if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) {
1046             SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);
1047             return (0);
1048         }
1049         X509_verify_cert(&xs_ctx);
1050         /* Don't leave errors in the queue */
1051         ERR_clear_error();
1052         i = ssl_security_cert_chain(s, xs_ctx.chain, NULL, 0);
1053         if (i != 1) {
1054             X509_STORE_CTX_cleanup(&xs_ctx);
1055             SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);
1056             return 0;
1057         }
1058         for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
1059             x = sk_X509_value(xs_ctx.chain, i);
1060
1061             if (!ssl_add_cert_to_buf(buf, l, x)) {
1062                 X509_STORE_CTX_cleanup(&xs_ctx);
1063                 return 0;
1064             }
1065         }
1066         X509_STORE_CTX_cleanup(&xs_ctx);
1067     } else {
1068         i = ssl_security_cert_chain(s, extra_certs, x, 0);
1069         if (i != 1) {
1070             SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, i);
1071             return 0;
1072         }
1073         if (!ssl_add_cert_to_buf(buf, l, x))
1074             return 0;
1075         for (i = 0; i < sk_X509_num(extra_certs); i++) {
1076             x = sk_X509_value(extra_certs, i);
1077             if (!ssl_add_cert_to_buf(buf, l, x))
1078                 return 0;
1079         }
1080     }
1081     return 1;
1082 }
1083
1084 /* Build a certificate chain for current certificate */
1085 int ssl_build_cert_chain(SSL *s, SSL_CTX *ctx, int flags)
1086 {
1087     CERT *c = s ? s->cert : ctx->cert;
1088     CERT_PKEY *cpk = c->key;
1089     X509_STORE *chain_store = NULL;
1090     X509_STORE_CTX xs_ctx;
1091     STACK_OF(X509) *chain = NULL, *untrusted = NULL;
1092     X509 *x;
1093     int i, rv = 0;
1094     unsigned long error;
1095
1096     if (!cpk->x509) {
1097         SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_NO_CERTIFICATE_SET);
1098         goto err;
1099     }
1100     /* Rearranging and check the chain: add everything to a store */
1101     if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
1102         chain_store = X509_STORE_new();
1103         if (!chain_store)
1104             goto err;
1105         for (i = 0; i < sk_X509_num(cpk->chain); i++) {
1106             x = sk_X509_value(cpk->chain, i);
1107             if (!X509_STORE_add_cert(chain_store, x)) {
1108                 error = ERR_peek_last_error();
1109                 if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
1110                     ERR_GET_REASON(error) !=
1111                     X509_R_CERT_ALREADY_IN_HASH_TABLE)
1112                     goto err;
1113                 ERR_clear_error();
1114             }
1115         }
1116         /* Add EE cert too: it might be self signed */
1117         if (!X509_STORE_add_cert(chain_store, cpk->x509)) {
1118             error = ERR_peek_last_error();
1119             if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
1120                 ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
1121                 goto err;
1122             ERR_clear_error();
1123         }
1124     } else {
1125         if (c->chain_store)
1126             chain_store = c->chain_store;
1127         else if (s)
1128             chain_store = s->ctx->cert_store;
1129         else
1130             chain_store = ctx->cert_store;
1131
1132         if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
1133             untrusted = cpk->chain;
1134     }
1135
1136     if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) {
1137         SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_X509_LIB);
1138         goto err;
1139     }
1140     /* Set suite B flags if needed */
1141     X509_STORE_CTX_set_flags(&xs_ctx,
1142                              c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
1143
1144     i = X509_verify_cert(&xs_ctx);
1145     if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
1146         if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
1147             ERR_clear_error();
1148         i = 1;
1149         rv = 2;
1150     }
1151     if (i > 0)
1152         chain = X509_STORE_CTX_get1_chain(&xs_ctx);
1153     if (i <= 0) {
1154         SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_CERTIFICATE_VERIFY_FAILED);
1155         i = X509_STORE_CTX_get_error(&xs_ctx);
1156         ERR_add_error_data(2, "Verify error:",
1157                            X509_verify_cert_error_string(i));
1158
1159         X509_STORE_CTX_cleanup(&xs_ctx);
1160         goto err;
1161     }
1162     X509_STORE_CTX_cleanup(&xs_ctx);
1163     /* Remove EE certificate from chain */
1164     x = sk_X509_shift(chain);
1165     X509_free(x);
1166     if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
1167         if (sk_X509_num(chain) > 0) {
1168             /* See if last cert is self signed */
1169             x = sk_X509_value(chain, sk_X509_num(chain) - 1);
1170             X509_check_purpose(x, -1, 0);
1171             if (x->ex_flags & EXFLAG_SS) {
1172                 x = sk_X509_pop(chain);
1173                 X509_free(x);
1174             }
1175         }
1176     }
1177     /*
1178      * Check security level of all CA certificates: EE will have been checked
1179      * already.
1180      */
1181     for (i = 0; i < sk_X509_num(chain); i++) {
1182         x = sk_X509_value(chain, i);
1183         rv = ssl_security_cert(s, ctx, x, 0, 0);
1184         if (rv != 1) {
1185             SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, rv);
1186             sk_X509_pop_free(chain, X509_free);
1187             rv = 0;
1188             goto err;
1189         }
1190     }
1191     sk_X509_pop_free(cpk->chain, X509_free);
1192     cpk->chain = chain;
1193     if (rv == 0)
1194         rv = 1;
1195  err:
1196     if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
1197         X509_STORE_free(chain_store);
1198
1199     return rv;
1200 }
1201
1202 int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
1203 {
1204     X509_STORE **pstore;
1205     if (chain)
1206         pstore = &c->chain_store;
1207     else
1208         pstore = &c->verify_store;
1209     X509_STORE_free(*pstore);
1210     *pstore = store;
1211     if (ref && store)
1212         CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE);
1213     return 1;
1214 }
1215
1216 static int ssl_security_default_callback(SSL *s, SSL_CTX *ctx, int op,
1217                                          int bits, int nid, void *other,
1218                                          void *ex)
1219 {
1220     int level, minbits;
1221     static const int minbits_table[5] = { 80, 112, 128, 192, 256 };
1222     if (ctx)
1223         level = SSL_CTX_get_security_level(ctx);
1224     else
1225         level = SSL_get_security_level(s);
1226     /* Level 0: anything goes */
1227     if (level <= 0)
1228         return 1;
1229     if (level > 5)
1230         level = 5;
1231     minbits = minbits_table[level - 1];
1232     switch (op) {
1233     case SSL_SECOP_CIPHER_SUPPORTED:
1234     case SSL_SECOP_CIPHER_SHARED:
1235     case SSL_SECOP_CIPHER_CHECK:
1236         {
1237             const SSL_CIPHER *c = other;
1238             /* No ciphers below security level */
1239             if (bits < minbits)
1240                 return 0;
1241             /* No unauthenticated ciphersuites */
1242             if (c->algorithm_auth & SSL_aNULL)
1243                 return 0;
1244             /* No MD5 mac ciphersuites */
1245             if (c->algorithm_mac & SSL_MD5)
1246                 return 0;
1247             /* SHA1 HMAC is 160 bits of security */
1248             if (minbits > 160 && c->algorithm_mac & SSL_SHA1)
1249                 return 0;
1250             /* Level 2: no RC4 */
1251             if (level >= 2 && c->algorithm_enc == SSL_RC4)
1252                 return 0;
1253             /* Level 3: forward secure ciphersuites only */
1254             if (level >= 3 && !(c->algorithm_mkey & (SSL_kEDH | SSL_kEECDH)))
1255                 return 0;
1256             break;
1257         }
1258     case SSL_SECOP_VERSION:
1259         /* SSLv3 not allowed on level 2 */
1260         if (nid <= SSL3_VERSION && level >= 2)
1261             return 0;
1262         /* TLS v1.1 and above only for level 3 */
1263         if (nid <= TLS1_VERSION && level >= 3)
1264             return 0;
1265         /* TLS v1.2 only for level 4 and above */
1266         if (nid <= TLS1_1_VERSION && level >= 4)
1267             return 0;
1268         break;
1269
1270     case SSL_SECOP_COMPRESSION:
1271         if (level >= 2)
1272             return 0;
1273         break;
1274     case SSL_SECOP_TICKET:
1275         if (level >= 3)
1276             return 0;
1277         break;
1278     default:
1279         if (bits < minbits)
1280             return 0;
1281     }
1282     return 1;
1283 }
1284
1285 int ssl_security(SSL *s, int op, int bits, int nid, void *other)
1286 {
1287     return s->cert->sec_cb(s, NULL, op, bits, nid, other, s->cert->sec_ex);
1288 }
1289
1290 int ssl_ctx_security(SSL_CTX *ctx, int op, int bits, int nid, void *other)
1291 {
1292     return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other,
1293                              ctx->cert->sec_ex);
1294 }