Home | History | Annotate | Line # | Download | only in ssl
      1      1.1  christos /*
      2      1.1  christos  * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
      4      1.1  christos  *
      5      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      6      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      7      1.1  christos  * in the file LICENSE in the source distribution or at
      8      1.1  christos  * https://www.openssl.org/source/license.html
      9      1.1  christos  */
     10      1.1  christos 
     11      1.1  christos #include "internal/e_os.h"
     12      1.1  christos 
     13      1.1  christos #include <stdio.h>
     14      1.1  christos #include <sys/types.h>
     15      1.1  christos 
     16      1.1  christos #include "internal/nelem.h"
     17      1.1  christos #include "internal/o_dir.h"
     18      1.1  christos #include <openssl/bio.h>
     19      1.1  christos #include <openssl/pem.h>
     20      1.1  christos #include <openssl/store.h>
     21      1.1  christos #include <openssl/x509v3.h>
     22      1.1  christos #include <openssl/dh.h>
     23      1.1  christos #include <openssl/bn.h>
     24      1.1  christos #include <openssl/crypto.h>
     25      1.1  christos #include "internal/refcount.h"
     26      1.1  christos #include "ssl_local.h"
     27      1.1  christos #include "ssl_cert_table.h"
     28      1.1  christos #include "internal/thread_once.h"
     29      1.1  christos #include "internal/ssl_unwrap.h"
     30      1.1  christos #ifndef OPENSSL_NO_POSIX_IO
     31  1.1.1.2  christos #include <sys/stat.h>
     32  1.1.1.2  christos #ifdef _WIN32
     33  1.1.1.2  christos #define stat _stat
     34  1.1.1.2  christos #endif
     35  1.1.1.2  christos #ifndef S_ISDIR
     36  1.1.1.2  christos #define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
     37  1.1.1.2  christos #endif
     38      1.1  christos #endif
     39      1.1  christos 
     40      1.1  christos static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
     41  1.1.1.2  christos     int op, int bits, int nid, void *other,
     42  1.1.1.2  christos     void *ex);
     43      1.1  christos 
     44      1.1  christos static CRYPTO_ONCE ssl_x509_store_ctx_once = CRYPTO_ONCE_STATIC_INIT;
     45      1.1  christos static volatile int ssl_x509_store_ctx_idx = -1;
     46      1.1  christos 
     47      1.1  christos DEFINE_RUN_ONCE_STATIC(ssl_x509_store_ctx_init)
     48      1.1  christos {
     49      1.1  christos     ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(0,
     50  1.1.1.2  christos         "SSL for verify callback",
     51  1.1.1.2  christos         NULL, NULL, NULL);
     52      1.1  christos     return ssl_x509_store_ctx_idx >= 0;
     53      1.1  christos }
     54      1.1  christos 
     55      1.1  christos int SSL_get_ex_data_X509_STORE_CTX_idx(void)
     56      1.1  christos {
     57      1.1  christos 
     58      1.1  christos     if (!RUN_ONCE(&ssl_x509_store_ctx_once, ssl_x509_store_ctx_init))
     59      1.1  christos         return -1;
     60      1.1  christos     return ssl_x509_store_ctx_idx;
     61      1.1  christos }
     62      1.1  christos 
     63      1.1  christos CERT *ssl_cert_new(size_t ssl_pkey_num)
     64      1.1  christos {
     65      1.1  christos     CERT *ret = NULL;
     66      1.1  christos 
     67      1.1  christos     /* Should never happen */
     68      1.1  christos     if (!ossl_assert(ssl_pkey_num >= SSL_PKEY_NUM))
     69      1.1  christos         return NULL;
     70      1.1  christos 
     71      1.1  christos     ret = OPENSSL_zalloc(sizeof(*ret));
     72      1.1  christos     if (ret == NULL)
     73      1.1  christos         return NULL;
     74      1.1  christos 
     75      1.1  christos     ret->ssl_pkey_num = ssl_pkey_num;
     76      1.1  christos     ret->pkeys = OPENSSL_zalloc(ret->ssl_pkey_num * sizeof(CERT_PKEY));
     77      1.1  christos     if (ret->pkeys == NULL) {
     78      1.1  christos         OPENSSL_free(ret);
     79      1.1  christos         return NULL;
     80      1.1  christos     }
     81      1.1  christos 
     82      1.1  christos     ret->key = &(ret->pkeys[SSL_PKEY_RSA]);
     83      1.1  christos     ret->sec_cb = ssl_security_default_callback;
     84      1.1  christos     ret->sec_level = OPENSSL_TLS_SECURITY_LEVEL;
     85      1.1  christos     ret->sec_ex = NULL;
     86      1.1  christos     if (!CRYPTO_NEW_REF(&ret->references, 1)) {
     87      1.1  christos         OPENSSL_free(ret->pkeys);
     88      1.1  christos         OPENSSL_free(ret);
     89      1.1  christos         return NULL;
     90      1.1  christos     }
     91      1.1  christos 
     92      1.1  christos     return ret;
     93      1.1  christos }
     94      1.1  christos 
     95      1.1  christos CERT *ssl_cert_dup(CERT *cert)
     96      1.1  christos {
     97      1.1  christos     CERT *ret = OPENSSL_zalloc(sizeof(*ret));
     98      1.1  christos     size_t i;
     99      1.1  christos #ifndef OPENSSL_NO_COMP_ALG
    100      1.1  christos     int j;
    101      1.1  christos #endif
    102      1.1  christos 
    103      1.1  christos     if (ret == NULL)
    104      1.1  christos         return NULL;
    105      1.1  christos 
    106      1.1  christos     ret->ssl_pkey_num = cert->ssl_pkey_num;
    107      1.1  christos     ret->pkeys = OPENSSL_zalloc(ret->ssl_pkey_num * sizeof(CERT_PKEY));
    108      1.1  christos     if (ret->pkeys == NULL) {
    109      1.1  christos         OPENSSL_free(ret);
    110      1.1  christos         return NULL;
    111      1.1  christos     }
    112      1.1  christos 
    113      1.1  christos     ret->key = &ret->pkeys[cert->key - cert->pkeys];
    114      1.1  christos     if (!CRYPTO_NEW_REF(&ret->references, 1)) {
    115      1.1  christos         OPENSSL_free(ret->pkeys);
    116      1.1  christos         OPENSSL_free(ret);
    117      1.1  christos         return NULL;
    118      1.1  christos     }
    119      1.1  christos 
    120      1.1  christos     if (cert->dh_tmp != NULL) {
    121      1.1  christos         if (!EVP_PKEY_up_ref(cert->dh_tmp))
    122      1.1  christos             goto err;
    123      1.1  christos         ret->dh_tmp = cert->dh_tmp;
    124      1.1  christos     }
    125      1.1  christos 
    126      1.1  christos     ret->dh_tmp_cb = cert->dh_tmp_cb;
    127      1.1  christos     ret->dh_tmp_auto = cert->dh_tmp_auto;
    128      1.1  christos 
    129      1.1  christos     for (i = 0; i < ret->ssl_pkey_num; i++) {
    130      1.1  christos         CERT_PKEY *cpk = cert->pkeys + i;
    131      1.1  christos         CERT_PKEY *rpk = ret->pkeys + i;
    132      1.1  christos 
    133      1.1  christos         if (cpk->x509 != NULL) {
    134      1.1  christos             if (!X509_up_ref(cpk->x509))
    135      1.1  christos                 goto err;
    136      1.1  christos             rpk->x509 = cpk->x509;
    137      1.1  christos         }
    138      1.1  christos 
    139      1.1  christos         if (cpk->privatekey != NULL) {
    140      1.1  christos             if (!EVP_PKEY_up_ref(cpk->privatekey))
    141      1.1  christos                 goto err;
    142      1.1  christos             rpk->privatekey = cpk->privatekey;
    143      1.1  christos         }
    144      1.1  christos 
    145      1.1  christos         if (cpk->chain) {
    146      1.1  christos             rpk->chain = X509_chain_up_ref(cpk->chain);
    147      1.1  christos             if (!rpk->chain) {
    148      1.1  christos                 ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
    149      1.1  christos                 goto err;
    150      1.1  christos             }
    151      1.1  christos         }
    152      1.1  christos         if (cpk->serverinfo != NULL) {
    153      1.1  christos             /* Just copy everything. */
    154      1.1  christos             rpk->serverinfo = OPENSSL_memdup(cpk->serverinfo, cpk->serverinfo_length);
    155      1.1  christos             if (rpk->serverinfo == NULL)
    156      1.1  christos                 goto err;
    157      1.1  christos             rpk->serverinfo_length = cpk->serverinfo_length;
    158      1.1  christos         }
    159      1.1  christos #ifndef OPENSSL_NO_COMP_ALG
    160      1.1  christos         for (j = TLSEXT_comp_cert_none; j < TLSEXT_comp_cert_limit; j++) {
    161      1.1  christos             if (cpk->comp_cert[j] != NULL) {
    162      1.1  christos                 if (!OSSL_COMP_CERT_up_ref(cpk->comp_cert[j]))
    163      1.1  christos                     goto err;
    164      1.1  christos                 rpk->comp_cert[j] = cpk->comp_cert[j];
    165      1.1  christos             }
    166      1.1  christos         }
    167      1.1  christos #endif
    168      1.1  christos     }
    169      1.1  christos 
    170      1.1  christos     /* Configured sigalgs copied across */
    171      1.1  christos     if (cert->conf_sigalgs) {
    172      1.1  christos         ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen
    173  1.1.1.2  christos             * sizeof(*cert->conf_sigalgs));
    174      1.1  christos         if (ret->conf_sigalgs == NULL)
    175      1.1  christos             goto err;
    176      1.1  christos         memcpy(ret->conf_sigalgs, cert->conf_sigalgs,
    177  1.1.1.2  christos             cert->conf_sigalgslen * sizeof(*cert->conf_sigalgs));
    178      1.1  christos         ret->conf_sigalgslen = cert->conf_sigalgslen;
    179      1.1  christos     } else
    180      1.1  christos         ret->conf_sigalgs = NULL;
    181      1.1  christos 
    182      1.1  christos     if (cert->client_sigalgs) {
    183      1.1  christos         ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen
    184  1.1.1.2  christos             * sizeof(*cert->client_sigalgs));
    185      1.1  christos         if (ret->client_sigalgs == NULL)
    186      1.1  christos             goto err;
    187      1.1  christos         memcpy(ret->client_sigalgs, cert->client_sigalgs,
    188  1.1.1.2  christos             cert->client_sigalgslen * sizeof(*cert->client_sigalgs));
    189      1.1  christos         ret->client_sigalgslen = cert->client_sigalgslen;
    190      1.1  christos     } else
    191      1.1  christos         ret->client_sigalgs = NULL;
    192      1.1  christos     /* Copy any custom client certificate types */
    193      1.1  christos     if (cert->ctype) {
    194      1.1  christos         ret->ctype = OPENSSL_memdup(cert->ctype, cert->ctype_len);
    195      1.1  christos         if (ret->ctype == NULL)
    196      1.1  christos             goto err;
    197      1.1  christos         ret->ctype_len = cert->ctype_len;
    198      1.1  christos     }
    199      1.1  christos 
    200      1.1  christos     ret->cert_flags = cert->cert_flags;
    201      1.1  christos 
    202      1.1  christos     ret->cert_cb = cert->cert_cb;
    203      1.1  christos     ret->cert_cb_arg = cert->cert_cb_arg;
    204      1.1  christos 
    205      1.1  christos     if (cert->verify_store) {
    206      1.1  christos         if (!X509_STORE_up_ref(cert->verify_store))
    207      1.1  christos             goto err;
    208      1.1  christos         ret->verify_store = cert->verify_store;
    209      1.1  christos     }
    210      1.1  christos 
    211      1.1  christos     if (cert->chain_store) {
    212      1.1  christos         if (!X509_STORE_up_ref(cert->chain_store))
    213      1.1  christos             goto err;
    214      1.1  christos         ret->chain_store = cert->chain_store;
    215      1.1  christos     }
    216      1.1  christos 
    217      1.1  christos     ret->sec_cb = cert->sec_cb;
    218      1.1  christos     ret->sec_level = cert->sec_level;
    219      1.1  christos     ret->sec_ex = cert->sec_ex;
    220      1.1  christos 
    221      1.1  christos     if (!custom_exts_copy(&ret->custext, &cert->custext))
    222      1.1  christos         goto err;
    223      1.1  christos #ifndef OPENSSL_NO_PSK
    224      1.1  christos     if (cert->psk_identity_hint) {
    225      1.1  christos         ret->psk_identity_hint = OPENSSL_strdup(cert->psk_identity_hint);
    226      1.1  christos         if (ret->psk_identity_hint == NULL)
    227      1.1  christos             goto err;
    228      1.1  christos     }
    229      1.1  christos #endif
    230      1.1  christos     return ret;
    231      1.1  christos 
    232  1.1.1.2  christos err:
    233      1.1  christos     ssl_cert_free(ret);
    234      1.1  christos 
    235      1.1  christos     return NULL;
    236      1.1  christos }
    237      1.1  christos 
    238      1.1  christos /* Free up and clear all certificates and chains */
    239      1.1  christos 
    240      1.1  christos void ssl_cert_clear_certs(CERT *c)
    241      1.1  christos {
    242      1.1  christos     size_t i;
    243      1.1  christos #ifndef OPENSSL_NO_COMP_ALG
    244      1.1  christos     int j;
    245      1.1  christos #endif
    246      1.1  christos 
    247      1.1  christos     if (c == NULL)
    248      1.1  christos         return;
    249      1.1  christos     for (i = 0; i < c->ssl_pkey_num; i++) {
    250      1.1  christos         CERT_PKEY *cpk = c->pkeys + i;
    251      1.1  christos         X509_free(cpk->x509);
    252      1.1  christos         cpk->x509 = NULL;
    253      1.1  christos         EVP_PKEY_free(cpk->privatekey);
    254      1.1  christos         cpk->privatekey = NULL;
    255      1.1  christos         OSSL_STACK_OF_X509_free(cpk->chain);
    256      1.1  christos         cpk->chain = NULL;
    257      1.1  christos         OPENSSL_free(cpk->serverinfo);
    258      1.1  christos         cpk->serverinfo = NULL;
    259      1.1  christos         cpk->serverinfo_length = 0;
    260      1.1  christos #ifndef OPENSSL_NO_COMP_ALG
    261      1.1  christos         for (j = 0; j < TLSEXT_comp_cert_limit; j++) {
    262      1.1  christos             OSSL_COMP_CERT_free(cpk->comp_cert[j]);
    263      1.1  christos             cpk->comp_cert[j] = NULL;
    264      1.1  christos             cpk->cert_comp_used = 0;
    265      1.1  christos         }
    266      1.1  christos #endif
    267      1.1  christos     }
    268      1.1  christos }
    269      1.1  christos 
    270      1.1  christos void ssl_cert_free(CERT *c)
    271      1.1  christos {
    272      1.1  christos     int i;
    273      1.1  christos 
    274      1.1  christos     if (c == NULL)
    275      1.1  christos         return;
    276      1.1  christos     CRYPTO_DOWN_REF(&c->references, &i);
    277      1.1  christos     REF_PRINT_COUNT("CERT", i, c);
    278      1.1  christos     if (i > 0)
    279      1.1  christos         return;
    280      1.1  christos     REF_ASSERT_ISNT(i < 0);
    281      1.1  christos 
    282      1.1  christos     EVP_PKEY_free(c->dh_tmp);
    283      1.1  christos 
    284      1.1  christos     ssl_cert_clear_certs(c);
    285      1.1  christos     OPENSSL_free(c->conf_sigalgs);
    286      1.1  christos     OPENSSL_free(c->client_sigalgs);
    287      1.1  christos     OPENSSL_free(c->ctype);
    288      1.1  christos     X509_STORE_free(c->verify_store);
    289      1.1  christos     X509_STORE_free(c->chain_store);
    290      1.1  christos     custom_exts_free(&c->custext);
    291      1.1  christos #ifndef OPENSSL_NO_PSK
    292      1.1  christos     OPENSSL_free(c->psk_identity_hint);
    293      1.1  christos #endif
    294      1.1  christos     OPENSSL_free(c->pkeys);
    295      1.1  christos     CRYPTO_FREE_REF(&c->references);
    296      1.1  christos     OPENSSL_free(c);
    297      1.1  christos }
    298      1.1  christos 
    299      1.1  christos int ssl_cert_set0_chain(SSL_CONNECTION *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
    300      1.1  christos {
    301      1.1  christos     int i, r;
    302      1.1  christos     CERT_PKEY *cpk = s != NULL ? s->cert->key : ctx->cert->key;
    303      1.1  christos 
    304      1.1  christos     if (!cpk)
    305      1.1  christos         return 0;
    306      1.1  christos     for (i = 0; i < sk_X509_num(chain); i++) {
    307      1.1  christos         X509 *x = sk_X509_value(chain, i);
    308      1.1  christos 
    309      1.1  christos         r = ssl_security_cert(s, ctx, x, 0, 0);
    310      1.1  christos         if (r != 1) {
    311      1.1  christos             ERR_raise(ERR_LIB_SSL, r);
    312      1.1  christos             return 0;
    313      1.1  christos         }
    314      1.1  christos     }
    315      1.1  christos     OSSL_STACK_OF_X509_free(cpk->chain);
    316      1.1  christos     cpk->chain = chain;
    317      1.1  christos     return 1;
    318      1.1  christos }
    319      1.1  christos 
    320      1.1  christos int ssl_cert_set1_chain(SSL_CONNECTION *s, SSL_CTX *ctx, STACK_OF(X509) *chain)
    321      1.1  christos {
    322      1.1  christos     STACK_OF(X509) *dchain;
    323      1.1  christos 
    324      1.1  christos     if (!chain)
    325      1.1  christos         return ssl_cert_set0_chain(s, ctx, NULL);
    326      1.1  christos     dchain = X509_chain_up_ref(chain);
    327      1.1  christos     if (!dchain)
    328      1.1  christos         return 0;
    329      1.1  christos     if (!ssl_cert_set0_chain(s, ctx, dchain)) {
    330      1.1  christos         OSSL_STACK_OF_X509_free(dchain);
    331      1.1  christos         return 0;
    332      1.1  christos     }
    333      1.1  christos     return 1;
    334      1.1  christos }
    335      1.1  christos 
    336      1.1  christos int ssl_cert_add0_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x)
    337      1.1  christos {
    338      1.1  christos     int r;
    339      1.1  christos     CERT_PKEY *cpk = s ? s->cert->key : ctx->cert->key;
    340      1.1  christos 
    341      1.1  christos     if (!cpk)
    342      1.1  christos         return 0;
    343      1.1  christos     r = ssl_security_cert(s, ctx, x, 0, 0);
    344      1.1  christos     if (r != 1) {
    345      1.1  christos         ERR_raise(ERR_LIB_SSL, r);
    346      1.1  christos         return 0;
    347      1.1  christos     }
    348      1.1  christos     if (!cpk->chain)
    349      1.1  christos         cpk->chain = sk_X509_new_null();
    350      1.1  christos     if (!cpk->chain || !sk_X509_push(cpk->chain, x))
    351      1.1  christos         return 0;
    352      1.1  christos     return 1;
    353      1.1  christos }
    354      1.1  christos 
    355      1.1  christos int ssl_cert_add1_chain_cert(SSL_CONNECTION *s, SSL_CTX *ctx, X509 *x)
    356      1.1  christos {
    357      1.1  christos     if (!X509_up_ref(x))
    358      1.1  christos         return 0;
    359      1.1  christos     if (!ssl_cert_add0_chain_cert(s, ctx, x)) {
    360      1.1  christos         X509_free(x);
    361      1.1  christos         return 0;
    362      1.1  christos     }
    363      1.1  christos     return 1;
    364      1.1  christos }
    365      1.1  christos 
    366      1.1  christos int ssl_cert_select_current(CERT *c, X509 *x)
    367      1.1  christos {
    368      1.1  christos     size_t i;
    369      1.1  christos 
    370      1.1  christos     if (x == NULL)
    371      1.1  christos         return 0;
    372      1.1  christos     for (i = 0; i < c->ssl_pkey_num; i++) {
    373      1.1  christos         CERT_PKEY *cpk = c->pkeys + i;
    374      1.1  christos         if (cpk->x509 == x && cpk->privatekey) {
    375      1.1  christos             c->key = cpk;
    376      1.1  christos             return 1;
    377      1.1  christos         }
    378      1.1  christos     }
    379      1.1  christos 
    380      1.1  christos     for (i = 0; i < c->ssl_pkey_num; i++) {
    381      1.1  christos         CERT_PKEY *cpk = c->pkeys + i;
    382      1.1  christos         if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) {
    383      1.1  christos             c->key = cpk;
    384      1.1  christos             return 1;
    385      1.1  christos         }
    386      1.1  christos     }
    387      1.1  christos     return 0;
    388      1.1  christos }
    389      1.1  christos 
    390      1.1  christos int ssl_cert_set_current(CERT *c, long op)
    391      1.1  christos {
    392      1.1  christos     size_t i, idx;
    393      1.1  christos 
    394      1.1  christos     if (!c)
    395      1.1  christos         return 0;
    396      1.1  christos     if (op == SSL_CERT_SET_FIRST)
    397      1.1  christos         idx = 0;
    398      1.1  christos     else if (op == SSL_CERT_SET_NEXT) {
    399      1.1  christos         idx = (size_t)(c->key - c->pkeys + 1);
    400      1.1  christos         if (idx >= c->ssl_pkey_num)
    401      1.1  christos             return 0;
    402      1.1  christos     } else
    403      1.1  christos         return 0;
    404      1.1  christos     for (i = idx; i < c->ssl_pkey_num; i++) {
    405      1.1  christos         CERT_PKEY *cpk = c->pkeys + i;
    406      1.1  christos         if (cpk->x509 && cpk->privatekey) {
    407      1.1  christos             c->key = cpk;
    408      1.1  christos             return 1;
    409      1.1  christos         }
    410      1.1  christos     }
    411      1.1  christos     return 0;
    412      1.1  christos }
    413      1.1  christos 
    414  1.1.1.2  christos void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg)
    415      1.1  christos {
    416      1.1  christos     c->cert_cb = cb;
    417      1.1  christos     c->cert_cb_arg = arg;
    418      1.1  christos }
    419      1.1  christos 
    420      1.1  christos /*
    421      1.1  christos  * Verify a certificate chain/raw public key
    422      1.1  christos  * Return codes:
    423      1.1  christos  *  1: Verify success
    424      1.1  christos  *  0: Verify failure or error
    425      1.1  christos  * -1: Retry required
    426      1.1  christos  */
    427      1.1  christos static int ssl_verify_internal(SSL_CONNECTION *s, STACK_OF(X509) *sk, EVP_PKEY *rpk)
    428      1.1  christos {
    429      1.1  christos     X509 *x;
    430      1.1  christos     int i = 0;
    431      1.1  christos     X509_STORE *verify_store;
    432      1.1  christos     X509_STORE_CTX *ctx = NULL;
    433      1.1  christos     X509_VERIFY_PARAM *param;
    434      1.1  christos     SSL_CTX *sctx;
    435      1.1  christos 
    436      1.1  christos     /* Something must be passed in */
    437      1.1  christos     if ((sk == NULL || sk_X509_num(sk) == 0) && rpk == NULL)
    438      1.1  christos         return 0;
    439      1.1  christos 
    440      1.1  christos     /* Only one can be set */
    441      1.1  christos     if (sk != NULL && rpk != NULL)
    442      1.1  christos         return 0;
    443      1.1  christos 
    444      1.1  christos     sctx = SSL_CONNECTION_GET_CTX(s);
    445      1.1  christos     if (s->cert->verify_store)
    446      1.1  christos         verify_store = s->cert->verify_store;
    447      1.1  christos     else
    448      1.1  christos         verify_store = sctx->cert_store;
    449      1.1  christos 
    450      1.1  christos     ctx = X509_STORE_CTX_new_ex(sctx->libctx, sctx->propq);
    451      1.1  christos     if (ctx == NULL) {
    452      1.1  christos         ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
    453      1.1  christos         return 0;
    454      1.1  christos     }
    455      1.1  christos 
    456      1.1  christos     if (sk != NULL) {
    457      1.1  christos         x = sk_X509_value(sk, 0);
    458      1.1  christos         if (!X509_STORE_CTX_init(ctx, verify_store, x, sk)) {
    459      1.1  christos             ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
    460      1.1  christos             goto end;
    461      1.1  christos         }
    462      1.1  christos     } else {
    463      1.1  christos         if (!X509_STORE_CTX_init_rpk(ctx, verify_store, rpk)) {
    464      1.1  christos             ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
    465      1.1  christos             goto end;
    466      1.1  christos         }
    467      1.1  christos     }
    468      1.1  christos     param = X509_STORE_CTX_get0_param(ctx);
    469      1.1  christos     /*
    470      1.1  christos      * XXX: Separate @AUTHSECLEVEL and @TLSSECLEVEL would be useful at some
    471      1.1  christos      * point, for now a single @SECLEVEL sets the same policy for TLS crypto
    472      1.1  christos      * and PKI authentication.
    473      1.1  christos      */
    474      1.1  christos     X509_VERIFY_PARAM_set_auth_level(param,
    475      1.1  christos         SSL_get_security_level(SSL_CONNECTION_GET_SSL(s)));
    476      1.1  christos 
    477      1.1  christos     /* Set suite B flags if needed */
    478      1.1  christos     X509_STORE_CTX_set_flags(ctx, tls1_suiteb(s));
    479      1.1  christos     if (!X509_STORE_CTX_set_ex_data(ctx,
    480      1.1  christos             SSL_get_ex_data_X509_STORE_CTX_idx(),
    481      1.1  christos             SSL_CONNECTION_GET_USER_SSL(s)))
    482      1.1  christos         goto end;
    483      1.1  christos 
    484      1.1  christos     /* Verify via DANE if enabled */
    485      1.1  christos     if (DANETLS_ENABLED(&s->dane))
    486      1.1  christos         X509_STORE_CTX_set0_dane(ctx, &s->dane);
    487      1.1  christos 
    488      1.1  christos     /*
    489      1.1  christos      * We need to inherit the verify parameters. These can be determined by
    490      1.1  christos      * the context: if its a server it will verify SSL client certificates or
    491      1.1  christos      * vice versa.
    492      1.1  christos      */
    493      1.1  christos 
    494      1.1  christos     X509_STORE_CTX_set_default(ctx, s->server ? "ssl_client" : "ssl_server");
    495      1.1  christos     /*
    496      1.1  christos      * Anything non-default in "s->param" should overwrite anything in the ctx.
    497      1.1  christos      */
    498      1.1  christos     X509_VERIFY_PARAM_set1(param, s->param);
    499      1.1  christos 
    500      1.1  christos     if (s->verify_callback)
    501      1.1  christos         X509_STORE_CTX_set_verify_cb(ctx, s->verify_callback);
    502      1.1  christos 
    503      1.1  christos     if (sctx->app_verify_callback != NULL) {
    504      1.1  christos         i = sctx->app_verify_callback(ctx, sctx->app_verify_arg);
    505      1.1  christos     } else {
    506      1.1  christos         i = X509_verify_cert(ctx);
    507      1.1  christos         /* We treat an error in the same way as a failure to verify */
    508      1.1  christos         if (i < 0)
    509      1.1  christos             i = 0;
    510      1.1  christos     }
    511      1.1  christos 
    512      1.1  christos     s->verify_result = X509_STORE_CTX_get_error(ctx);
    513      1.1  christos     OSSL_STACK_OF_X509_free(s->verified_chain);
    514      1.1  christos     s->verified_chain = NULL;
    515      1.1  christos 
    516      1.1  christos     if (sk != NULL && X509_STORE_CTX_get0_chain(ctx) != NULL) {
    517      1.1  christos         s->verified_chain = X509_STORE_CTX_get1_chain(ctx);
    518      1.1  christos         if (s->verified_chain == NULL) {
    519      1.1  christos             ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
    520      1.1  christos             i = 0;
    521      1.1  christos         }
    522      1.1  christos     }
    523      1.1  christos 
    524      1.1  christos     /* Move peername from the store context params to the SSL handle's */
    525      1.1  christos     X509_VERIFY_PARAM_move_peername(s->param, param);
    526      1.1  christos 
    527  1.1.1.2  christos end:
    528      1.1  christos     X509_STORE_CTX_free(ctx);
    529      1.1  christos     return i;
    530      1.1  christos }
    531      1.1  christos 
    532      1.1  christos /*
    533      1.1  christos  * Verify a raw public key
    534      1.1  christos  * Return codes:
    535      1.1  christos  *  1: Verify success
    536      1.1  christos  *  0: Verify failure or error
    537      1.1  christos  * -1: Retry required
    538      1.1  christos  */
    539      1.1  christos int ssl_verify_rpk(SSL_CONNECTION *s, EVP_PKEY *rpk)
    540      1.1  christos {
    541      1.1  christos     return ssl_verify_internal(s, NULL, rpk);
    542      1.1  christos }
    543      1.1  christos 
    544      1.1  christos /*
    545      1.1  christos  * Verify a certificate chain
    546      1.1  christos  * Return codes:
    547      1.1  christos  *  1: Verify success
    548      1.1  christos  *  0: Verify failure or error
    549      1.1  christos  * -1: Retry required
    550      1.1  christos  */
    551      1.1  christos int ssl_verify_cert_chain(SSL_CONNECTION *s, STACK_OF(X509) *sk)
    552      1.1  christos {
    553      1.1  christos     return ssl_verify_internal(s, sk, NULL);
    554      1.1  christos }
    555      1.1  christos 
    556      1.1  christos static void set0_CA_list(STACK_OF(X509_NAME) **ca_list,
    557  1.1.1.2  christos     STACK_OF(X509_NAME) *name_list)
    558      1.1  christos {
    559      1.1  christos     sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
    560      1.1  christos     *ca_list = name_list;
    561      1.1  christos }
    562      1.1  christos 
    563      1.1  christos STACK_OF(X509_NAME) *SSL_dup_CA_list(const STACK_OF(X509_NAME) *sk)
    564      1.1  christos {
    565      1.1  christos     int i;
    566      1.1  christos     const int num = sk_X509_NAME_num(sk);
    567      1.1  christos     STACK_OF(X509_NAME) *ret;
    568      1.1  christos     X509_NAME *name;
    569      1.1  christos 
    570      1.1  christos     ret = sk_X509_NAME_new_reserve(NULL, num);
    571      1.1  christos     if (ret == NULL) {
    572      1.1  christos         ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
    573      1.1  christos         return NULL;
    574      1.1  christos     }
    575      1.1  christos     for (i = 0; i < num; i++) {
    576      1.1  christos         name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
    577      1.1  christos         if (name == NULL) {
    578      1.1  christos             ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
    579      1.1  christos             sk_X509_NAME_pop_free(ret, X509_NAME_free);
    580      1.1  christos             return NULL;
    581      1.1  christos         }
    582  1.1.1.2  christos         sk_X509_NAME_push(ret, name); /* Cannot fail after reserve call */
    583      1.1  christos     }
    584      1.1  christos     return ret;
    585      1.1  christos }
    586      1.1  christos 
    587      1.1  christos void SSL_set0_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
    588      1.1  christos {
    589      1.1  christos     SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
    590      1.1  christos 
    591      1.1  christos     if (sc == NULL)
    592      1.1  christos         return;
    593      1.1  christos 
    594      1.1  christos     set0_CA_list(&sc->ca_names, name_list);
    595      1.1  christos }
    596      1.1  christos 
    597      1.1  christos void SSL_CTX_set0_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
    598      1.1  christos {
    599      1.1  christos     set0_CA_list(&ctx->ca_names, name_list);
    600      1.1  christos }
    601      1.1  christos 
    602      1.1  christos const STACK_OF(X509_NAME) *SSL_CTX_get0_CA_list(const SSL_CTX *ctx)
    603      1.1  christos {
    604      1.1  christos     return ctx->ca_names;
    605      1.1  christos }
    606      1.1  christos 
    607      1.1  christos const STACK_OF(X509_NAME) *SSL_get0_CA_list(const SSL *s)
    608      1.1  christos {
    609      1.1  christos     const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
    610      1.1  christos 
    611      1.1  christos     if (sc == NULL)
    612      1.1  christos         return NULL;
    613      1.1  christos 
    614      1.1  christos     return sc->ca_names != NULL ? sc->ca_names : s->ctx->ca_names;
    615      1.1  christos }
    616      1.1  christos 
    617      1.1  christos void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
    618      1.1  christos {
    619      1.1  christos     set0_CA_list(&ctx->client_ca_names, name_list);
    620      1.1  christos }
    621      1.1  christos 
    622      1.1  christos STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
    623      1.1  christos {
    624      1.1  christos     return ctx->client_ca_names;
    625      1.1  christos }
    626      1.1  christos 
    627      1.1  christos void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
    628      1.1  christos {
    629      1.1  christos     SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
    630      1.1  christos 
    631      1.1  christos     if (sc == NULL)
    632      1.1  christos         return;
    633      1.1  christos 
    634      1.1  christos     set0_CA_list(&sc->client_ca_names, name_list);
    635      1.1  christos }
    636      1.1  christos 
    637      1.1  christos const STACK_OF(X509_NAME) *SSL_get0_peer_CA_list(const SSL *s)
    638      1.1  christos {
    639      1.1  christos     const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
    640      1.1  christos 
    641      1.1  christos     if (sc == NULL)
    642      1.1  christos         return NULL;
    643      1.1  christos 
    644      1.1  christos     return sc->s3.tmp.peer_ca_names;
    645      1.1  christos }
    646      1.1  christos 
    647      1.1  christos STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
    648      1.1  christos {
    649      1.1  christos     const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
    650      1.1  christos 
    651      1.1  christos     if (sc == NULL)
    652      1.1  christos         return NULL;
    653      1.1  christos 
    654      1.1  christos     if (!sc->server)
    655      1.1  christos         return sc->s3.tmp.peer_ca_names;
    656      1.1  christos     return sc->client_ca_names != NULL ? sc->client_ca_names
    657      1.1  christos                                        : s->ctx->client_ca_names;
    658      1.1  christos }
    659      1.1  christos 
    660      1.1  christos static int add_ca_name(STACK_OF(X509_NAME) **sk, const X509 *x)
    661      1.1  christos {
    662      1.1  christos     X509_NAME *name;
    663      1.1  christos 
    664      1.1  christos     if (x == NULL)
    665      1.1  christos         return 0;
    666      1.1  christos     if (*sk == NULL && ((*sk = sk_X509_NAME_new_null()) == NULL))
    667      1.1  christos         return 0;
    668      1.1  christos 
    669      1.1  christos     if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
    670      1.1  christos         return 0;
    671      1.1  christos 
    672      1.1  christos     if (!sk_X509_NAME_push(*sk, name)) {
    673      1.1  christos         X509_NAME_free(name);
    674      1.1  christos         return 0;
    675      1.1  christos     }
    676      1.1  christos     return 1;
    677      1.1  christos }
    678      1.1  christos 
    679      1.1  christos int SSL_add1_to_CA_list(SSL *ssl, const X509 *x)
    680      1.1  christos {
    681      1.1  christos     SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
    682      1.1  christos 
    683      1.1  christos     if (sc == NULL)
    684      1.1  christos         return 0;
    685      1.1  christos 
    686      1.1  christos     return add_ca_name(&sc->ca_names, x);
    687      1.1  christos }
    688      1.1  christos 
    689      1.1  christos int SSL_CTX_add1_to_CA_list(SSL_CTX *ctx, const X509 *x)
    690      1.1  christos {
    691      1.1  christos     return add_ca_name(&ctx->ca_names, x);
    692      1.1  christos }
    693      1.1  christos 
    694      1.1  christos /*
    695      1.1  christos  * The following two are older names are to be replaced with
    696      1.1  christos  * SSL(_CTX)_add1_to_CA_list
    697      1.1  christos  */
    698      1.1  christos int SSL_add_client_CA(SSL *ssl, X509 *x)
    699      1.1  christos {
    700      1.1  christos     SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(ssl);
    701      1.1  christos 
    702      1.1  christos     if (sc == NULL)
    703      1.1  christos         return 0;
    704      1.1  christos 
    705      1.1  christos     return add_ca_name(&sc->client_ca_names, x);
    706      1.1  christos }
    707      1.1  christos 
    708      1.1  christos int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
    709      1.1  christos {
    710      1.1  christos     return add_ca_name(&ctx->client_ca_names, x);
    711      1.1  christos }
    712      1.1  christos 
    713      1.1  christos static int xname_cmp(const X509_NAME *a, const X509_NAME *b)
    714      1.1  christos {
    715      1.1  christos     unsigned char *abuf = NULL, *bbuf = NULL;
    716      1.1  christos     int alen, blen, ret;
    717      1.1  christos 
    718      1.1  christos     /* X509_NAME_cmp() itself casts away constness in this way, so
    719      1.1  christos      * assume it's safe:
    720      1.1  christos      */
    721      1.1  christos     alen = i2d_X509_NAME((X509_NAME *)a, &abuf);
    722      1.1  christos     blen = i2d_X509_NAME((X509_NAME *)b, &bbuf);
    723      1.1  christos 
    724      1.1  christos     if (alen < 0 || blen < 0)
    725      1.1  christos         ret = -2;
    726      1.1  christos     else if (alen != blen)
    727      1.1  christos         ret = alen - blen;
    728      1.1  christos     else /* alen == blen */
    729      1.1  christos         ret = memcmp(abuf, bbuf, alen);
    730      1.1  christos 
    731      1.1  christos     OPENSSL_free(abuf);
    732      1.1  christos     OPENSSL_free(bbuf);
    733      1.1  christos 
    734      1.1  christos     return ret;
    735      1.1  christos }
    736      1.1  christos 
    737      1.1  christos static int xname_sk_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
    738      1.1  christos {
    739      1.1  christos     return xname_cmp(*a, *b);
    740      1.1  christos }
    741      1.1  christos 
    742      1.1  christos static unsigned long xname_hash(const X509_NAME *a)
    743      1.1  christos {
    744      1.1  christos     /* This returns 0 also if SHA1 is not available */
    745      1.1  christos     return X509_NAME_hash_ex((X509_NAME *)a, NULL, NULL, NULL);
    746      1.1  christos }
    747      1.1  christos 
    748      1.1  christos STACK_OF(X509_NAME) *SSL_load_client_CA_file_ex(const char *file,
    749  1.1.1.2  christos     OSSL_LIB_CTX *libctx,
    750  1.1.1.2  christos     const char *propq)
    751      1.1  christos {
    752      1.1  christos     BIO *in = BIO_new(BIO_s_file());
    753      1.1  christos     X509 *x = NULL;
    754      1.1  christos     X509_NAME *xn = NULL;
    755      1.1  christos     STACK_OF(X509_NAME) *ret = NULL;
    756      1.1  christos     LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);
    757      1.1  christos     OSSL_LIB_CTX *prev_libctx = NULL;
    758      1.1  christos 
    759      1.1  christos     if (file == NULL) {
    760      1.1  christos         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
    761      1.1  christos         goto err;
    762      1.1  christos     }
    763      1.1  christos     if (name_hash == NULL) {
    764      1.1  christos         ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
    765      1.1  christos         goto err;
    766      1.1  christos     }
    767      1.1  christos     if (in == NULL) {
    768      1.1  christos         ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB);
    769      1.1  christos         goto err;
    770      1.1  christos     }
    771      1.1  christos 
    772      1.1  christos     x = X509_new_ex(libctx, propq);
    773      1.1  christos     if (x == NULL) {
    774      1.1  christos         ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
    775      1.1  christos         goto err;
    776      1.1  christos     }
    777      1.1  christos     if (BIO_read_filename(in, file) <= 0)
    778      1.1  christos         goto err;
    779      1.1  christos 
    780      1.1  christos     /* Internally lh_X509_NAME_retrieve() needs the libctx to retrieve SHA1 */
    781      1.1  christos     prev_libctx = OSSL_LIB_CTX_set0_default(libctx);
    782      1.1  christos     for (;;) {
    783      1.1  christos         if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
    784      1.1  christos             break;
    785      1.1  christos         if (ret == NULL) {
    786      1.1  christos             ret = sk_X509_NAME_new_null();
    787      1.1  christos             if (ret == NULL) {
    788      1.1  christos                 ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
    789      1.1  christos                 goto err;
    790      1.1  christos             }
    791      1.1  christos         }
    792      1.1  christos         if ((xn = X509_get_subject_name(x)) == NULL)
    793      1.1  christos             goto err;
    794      1.1  christos         /* check for duplicates */
    795      1.1  christos         xn = X509_NAME_dup(xn);
    796      1.1  christos         if (xn == NULL)
    797      1.1  christos             goto err;
    798      1.1  christos         if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {
    799      1.1  christos             /* Duplicate. */
    800      1.1  christos             X509_NAME_free(xn);
    801      1.1  christos             xn = NULL;
    802      1.1  christos         } else {
    803      1.1  christos             lh_X509_NAME_insert(name_hash, xn);
    804      1.1  christos             if (!sk_X509_NAME_push(ret, xn))
    805      1.1  christos                 goto err;
    806      1.1  christos         }
    807      1.1  christos     }
    808      1.1  christos     goto done;
    809      1.1  christos 
    810  1.1.1.2  christos err:
    811      1.1  christos     X509_NAME_free(xn);
    812      1.1  christos     sk_X509_NAME_pop_free(ret, X509_NAME_free);
    813      1.1  christos     ret = NULL;
    814  1.1.1.2  christos done:
    815      1.1  christos     /* restore the old libctx */
    816      1.1  christos     OSSL_LIB_CTX_set0_default(prev_libctx);
    817      1.1  christos     BIO_free(in);
    818      1.1  christos     X509_free(x);
    819      1.1  christos     lh_X509_NAME_free(name_hash);
    820      1.1  christos     if (ret != NULL)
    821      1.1  christos         ERR_clear_error();
    822      1.1  christos     return ret;
    823      1.1  christos }
    824      1.1  christos 
    825      1.1  christos STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
    826      1.1  christos {
    827      1.1  christos     return SSL_load_client_CA_file_ex(file, NULL, NULL);
    828      1.1  christos }
    829      1.1  christos 
    830      1.1  christos static int add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
    831  1.1.1.2  christos     const char *file,
    832  1.1.1.2  christos     LHASH_OF(X509_NAME) *name_hash)
    833      1.1  christos {
    834      1.1  christos     BIO *in;
    835      1.1  christos     X509 *x = NULL;
    836      1.1  christos     X509_NAME *xn = NULL;
    837      1.1  christos     int ret = 1;
    838      1.1  christos 
    839      1.1  christos     in = BIO_new(BIO_s_file());
    840      1.1  christos 
    841      1.1  christos     if (in == NULL) {
    842      1.1  christos         ERR_raise(ERR_LIB_SSL, ERR_R_BIO_LIB);
    843      1.1  christos         goto err;
    844      1.1  christos     }
    845      1.1  christos 
    846      1.1  christos     if (BIO_read_filename(in, file) <= 0)
    847      1.1  christos         goto err;
    848      1.1  christos 
    849      1.1  christos     for (;;) {
    850      1.1  christos         if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
    851      1.1  christos             break;
    852      1.1  christos         if ((xn = X509_get_subject_name(x)) == NULL)
    853      1.1  christos             goto err;
    854      1.1  christos         xn = X509_NAME_dup(xn);
    855      1.1  christos         if (xn == NULL)
    856      1.1  christos             goto err;
    857      1.1  christos         if (lh_X509_NAME_retrieve(name_hash, xn) != NULL) {
    858      1.1  christos             /* Duplicate. */
    859      1.1  christos             X509_NAME_free(xn);
    860      1.1  christos         } else if (!sk_X509_NAME_push(stack, xn)) {
    861      1.1  christos             X509_NAME_free(xn);
    862      1.1  christos             goto err;
    863      1.1  christos         } else {
    864      1.1  christos             /* Successful insert, add to hash table */
    865      1.1  christos             lh_X509_NAME_insert(name_hash, xn);
    866      1.1  christos         }
    867      1.1  christos     }
    868      1.1  christos 
    869      1.1  christos     ERR_clear_error();
    870      1.1  christos     goto done;
    871      1.1  christos 
    872  1.1.1.2  christos err:
    873      1.1  christos     ret = 0;
    874  1.1.1.2  christos done:
    875      1.1  christos     BIO_free(in);
    876      1.1  christos     X509_free(x);
    877      1.1  christos     return ret;
    878      1.1  christos }
    879      1.1  christos 
    880      1.1  christos int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
    881  1.1.1.2  christos     const char *file)
    882      1.1  christos {
    883      1.1  christos     X509_NAME *xn = NULL;
    884      1.1  christos     int ret = 1;
    885      1.1  christos     int idx = 0;
    886      1.1  christos     int num = 0;
    887      1.1  christos     LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);
    888      1.1  christos 
    889      1.1  christos     if (file == NULL) {
    890      1.1  christos         ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
    891      1.1  christos         goto err;
    892      1.1  christos     }
    893      1.1  christos 
    894      1.1  christos     if (name_hash == NULL) {
    895      1.1  christos         ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
    896      1.1  christos         goto err;
    897      1.1  christos     }
    898      1.1  christos 
    899      1.1  christos     /*
    900      1.1  christos      * Pre-populate the lhash with the existing entries of the stack, since
    901      1.1  christos      * using the LHASH_OF is much faster for duplicate checking. That's because
    902      1.1  christos      * xname_cmp converts the X509_NAMEs to DER involving a memory allocation
    903      1.1  christos      * for every single invocation of the comparison function.
    904      1.1  christos      */
    905      1.1  christos     num = sk_X509_NAME_num(stack);
    906      1.1  christos     for (idx = 0; idx < num; idx++) {
    907      1.1  christos         xn = sk_X509_NAME_value(stack, idx);
    908      1.1  christos         lh_X509_NAME_insert(name_hash, xn);
    909      1.1  christos     }
    910      1.1  christos 
    911      1.1  christos     ret = add_file_cert_subjects_to_stack(stack, file, name_hash);
    912      1.1  christos     goto done;
    913      1.1  christos 
    914  1.1.1.2  christos err:
    915      1.1  christos     ret = 0;
    916  1.1.1.2  christos done:
    917      1.1  christos     lh_X509_NAME_free(name_hash);
    918      1.1  christos     return ret;
    919      1.1  christos }
    920      1.1  christos 
    921      1.1  christos int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
    922  1.1.1.2  christos     const char *dir)
    923      1.1  christos {
    924      1.1  christos     OPENSSL_DIR_CTX *d = NULL;
    925      1.1  christos     const char *filename;
    926      1.1  christos     int ret = 0;
    927      1.1  christos     X509_NAME *xn = NULL;
    928      1.1  christos     int idx = 0;
    929      1.1  christos     int num = 0;
    930      1.1  christos     LHASH_OF(X509_NAME) *name_hash = lh_X509_NAME_new(xname_hash, xname_cmp);
    931      1.1  christos 
    932      1.1  christos     if (name_hash == NULL) {
    933      1.1  christos         ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
    934      1.1  christos         goto err;
    935      1.1  christos     }
    936      1.1  christos 
    937      1.1  christos     /*
    938      1.1  christos      * Pre-populate the lhash with the existing entries of the stack, since
    939      1.1  christos      * using the LHASH_OF is much faster for duplicate checking. That's because
    940      1.1  christos      * xname_cmp converts the X509_NAMEs to DER involving a memory allocation
    941      1.1  christos      * for every single invocation of the comparison function.
    942      1.1  christos      */
    943      1.1  christos     num = sk_X509_NAME_num(stack);
    944      1.1  christos     for (idx = 0; idx < num; idx++) {
    945      1.1  christos         xn = sk_X509_NAME_value(stack, idx);
    946      1.1  christos         lh_X509_NAME_insert(name_hash, xn);
    947      1.1  christos     }
    948      1.1  christos 
    949      1.1  christos     while ((filename = OPENSSL_DIR_read(&d, dir))) {
    950      1.1  christos         char buf[1024];
    951      1.1  christos         int r;
    952      1.1  christos #ifndef OPENSSL_NO_POSIX_IO
    953      1.1  christos         struct stat st;
    954      1.1  christos 
    955      1.1  christos #else
    956      1.1  christos         /* Cannot use stat so just skip current and parent directories */
    957      1.1  christos         if (strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0)
    958      1.1  christos             continue;
    959      1.1  christos #endif
    960      1.1  christos         if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
    961      1.1  christos             ERR_raise(ERR_LIB_SSL, SSL_R_PATH_TOO_LONG);
    962      1.1  christos             goto err;
    963      1.1  christos         }
    964      1.1  christos #ifdef OPENSSL_SYS_VMS
    965      1.1  christos         r = BIO_snprintf(buf, sizeof(buf), "%s%s", dir, filename);
    966      1.1  christos #else
    967      1.1  christos         r = BIO_snprintf(buf, sizeof(buf), "%s/%s", dir, filename);
    968      1.1  christos #endif
    969      1.1  christos #ifndef OPENSSL_NO_POSIX_IO
    970      1.1  christos         /* Skip subdirectories */
    971      1.1  christos         if (!stat(buf, &st) && S_ISDIR(st.st_mode))
    972      1.1  christos             continue;
    973      1.1  christos #endif
    974      1.1  christos         if (r <= 0 || r >= (int)sizeof(buf))
    975      1.1  christos             goto err;
    976      1.1  christos         if (!add_file_cert_subjects_to_stack(stack, buf, name_hash))
    977      1.1  christos             goto err;
    978      1.1  christos     }
    979      1.1  christos 
    980      1.1  christos     if (errno) {
    981      1.1  christos         ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
    982  1.1.1.2  christos             "calling OPENSSL_dir_read(%s)", dir);
    983      1.1  christos         ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
    984      1.1  christos         goto err;
    985      1.1  christos     }
    986      1.1  christos 
    987      1.1  christos     ret = 1;
    988      1.1  christos 
    989  1.1.1.2  christos err:
    990      1.1  christos     if (d)
    991      1.1  christos         OPENSSL_DIR_end(&d);
    992      1.1  christos     lh_X509_NAME_free(name_hash);
    993      1.1  christos 
    994      1.1  christos     return ret;
    995      1.1  christos }
    996      1.1  christos 
    997      1.1  christos static int add_uris_recursive(STACK_OF(X509_NAME) *stack,
    998  1.1.1.2  christos     const char *uri, int depth)
    999      1.1  christos {
   1000      1.1  christos     int ok = 1;
   1001      1.1  christos     OSSL_STORE_CTX *ctx = NULL;
   1002      1.1  christos     X509 *x = NULL;
   1003      1.1  christos     X509_NAME *xn = NULL;
   1004      1.1  christos     OSSL_STORE_INFO *info = NULL;
   1005      1.1  christos 
   1006      1.1  christos     if ((ctx = OSSL_STORE_open(uri, NULL, NULL, NULL, NULL)) == NULL)
   1007      1.1  christos         goto err;
   1008      1.1  christos 
   1009      1.1  christos     while (!OSSL_STORE_eof(ctx) && !OSSL_STORE_error(ctx)) {
   1010      1.1  christos         int infotype;
   1011      1.1  christos 
   1012      1.1  christos         if ((info = OSSL_STORE_load(ctx)) == NULL)
   1013      1.1  christos             continue;
   1014      1.1  christos         infotype = OSSL_STORE_INFO_get_type(info);
   1015      1.1  christos 
   1016      1.1  christos         if (infotype == OSSL_STORE_INFO_NAME) {
   1017      1.1  christos             /*
   1018      1.1  christos              * This is an entry in the "directory" represented by the current
   1019      1.1  christos              * uri.  if |depth| allows, dive into it.
   1020      1.1  christos              */
   1021      1.1  christos             if (depth > 0)
   1022      1.1  christos                 ok = add_uris_recursive(stack, OSSL_STORE_INFO_get0_NAME(info),
   1023  1.1.1.2  christos                     depth - 1);
   1024      1.1  christos         } else if (infotype == OSSL_STORE_INFO_CERT) {
   1025      1.1  christos             if ((x = OSSL_STORE_INFO_get0_CERT(info)) == NULL
   1026      1.1  christos                 || (xn = X509_get_subject_name(x)) == NULL
   1027      1.1  christos                 || (xn = X509_NAME_dup(xn)) == NULL)
   1028      1.1  christos                 goto err;
   1029      1.1  christos             if (sk_X509_NAME_find(stack, xn) >= 0) {
   1030      1.1  christos                 /* Duplicate. */
   1031      1.1  christos                 X509_NAME_free(xn);
   1032      1.1  christos             } else if (!sk_X509_NAME_push(stack, xn)) {
   1033      1.1  christos                 X509_NAME_free(xn);
   1034      1.1  christos                 goto err;
   1035      1.1  christos             }
   1036      1.1  christos         }
   1037      1.1  christos 
   1038      1.1  christos         OSSL_STORE_INFO_free(info);
   1039      1.1  christos         info = NULL;
   1040      1.1  christos     }
   1041      1.1  christos 
   1042      1.1  christos     ERR_clear_error();
   1043      1.1  christos     goto done;
   1044      1.1  christos 
   1045  1.1.1.2  christos err:
   1046      1.1  christos     ok = 0;
   1047      1.1  christos     OSSL_STORE_INFO_free(info);
   1048  1.1.1.2  christos done:
   1049      1.1  christos     OSSL_STORE_close(ctx);
   1050      1.1  christos 
   1051      1.1  christos     return ok;
   1052      1.1  christos }
   1053      1.1  christos 
   1054      1.1  christos int SSL_add_store_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
   1055  1.1.1.2  christos     const char *store)
   1056      1.1  christos {
   1057  1.1.1.2  christos     int (*oldcmp)(const X509_NAME *const *a, const X509_NAME *const *b)
   1058      1.1  christos         = sk_X509_NAME_set_cmp_func(stack, xname_sk_cmp);
   1059      1.1  christos     int ret = add_uris_recursive(stack, store, 1);
   1060      1.1  christos 
   1061      1.1  christos     (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
   1062      1.1  christos     return ret;
   1063      1.1  christos }
   1064      1.1  christos 
   1065      1.1  christos /* Build a certificate chain for current certificate */
   1066      1.1  christos int ssl_build_cert_chain(SSL_CONNECTION *s, SSL_CTX *ctx, int flags)
   1067      1.1  christos {
   1068      1.1  christos     CERT *c = s != NULL ? s->cert : ctx->cert;
   1069      1.1  christos     CERT_PKEY *cpk = c->key;
   1070      1.1  christos     X509_STORE *chain_store = NULL;
   1071      1.1  christos     X509_STORE_CTX *xs_ctx = NULL;
   1072      1.1  christos     STACK_OF(X509) *chain = NULL, *untrusted = NULL;
   1073      1.1  christos     X509 *x;
   1074      1.1  christos     SSL_CTX *real_ctx = (s == NULL) ? ctx : SSL_CONNECTION_GET_CTX(s);
   1075      1.1  christos     int i, rv = 0;
   1076      1.1  christos 
   1077      1.1  christos     if (cpk->x509 == NULL) {
   1078      1.1  christos         ERR_raise(ERR_LIB_SSL, SSL_R_NO_CERTIFICATE_SET);
   1079      1.1  christos         goto err;
   1080      1.1  christos     }
   1081      1.1  christos     /* Rearranging and check the chain: add everything to a store */
   1082      1.1  christos     if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
   1083      1.1  christos         chain_store = X509_STORE_new();
   1084      1.1  christos         if (chain_store == NULL)
   1085      1.1  christos             goto err;
   1086      1.1  christos         for (i = 0; i < sk_X509_num(cpk->chain); i++) {
   1087      1.1  christos             x = sk_X509_value(cpk->chain, i);
   1088      1.1  christos             if (!X509_STORE_add_cert(chain_store, x))
   1089      1.1  christos                 goto err;
   1090      1.1  christos         }
   1091      1.1  christos         /* Add EE cert too: it might be self signed */
   1092      1.1  christos         if (!X509_STORE_add_cert(chain_store, cpk->x509))
   1093      1.1  christos             goto err;
   1094      1.1  christos     } else {
   1095      1.1  christos         if (c->chain_store != NULL)
   1096      1.1  christos             chain_store = c->chain_store;
   1097      1.1  christos         else
   1098      1.1  christos             chain_store = real_ctx->cert_store;
   1099      1.1  christos 
   1100      1.1  christos         if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
   1101      1.1  christos             untrusted = cpk->chain;
   1102      1.1  christos     }
   1103      1.1  christos 
   1104      1.1  christos     xs_ctx = X509_STORE_CTX_new_ex(real_ctx->libctx, real_ctx->propq);
   1105      1.1  christos     if (xs_ctx == NULL) {
   1106      1.1  christos         ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
   1107      1.1  christos         goto err;
   1108      1.1  christos     }
   1109      1.1  christos     if (!X509_STORE_CTX_init(xs_ctx, chain_store, cpk->x509, untrusted)) {
   1110      1.1  christos         ERR_raise(ERR_LIB_SSL, ERR_R_X509_LIB);
   1111      1.1  christos         goto err;
   1112      1.1  christos     }
   1113      1.1  christos     /* Set suite B flags if needed */
   1114      1.1  christos     X509_STORE_CTX_set_flags(xs_ctx,
   1115  1.1.1.2  christos         c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
   1116      1.1  christos 
   1117      1.1  christos     i = X509_verify_cert(xs_ctx);
   1118      1.1  christos     if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
   1119      1.1  christos         if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
   1120      1.1  christos             ERR_clear_error();
   1121      1.1  christos         i = 1;
   1122      1.1  christos         rv = 2;
   1123      1.1  christos     }
   1124      1.1  christos     if (i > 0)
   1125      1.1  christos         chain = X509_STORE_CTX_get1_chain(xs_ctx);
   1126      1.1  christos     if (i <= 0) {
   1127      1.1  christos         i = X509_STORE_CTX_get_error(xs_ctx);
   1128      1.1  christos         ERR_raise_data(ERR_LIB_SSL, SSL_R_CERTIFICATE_VERIFY_FAILED,
   1129  1.1.1.2  christos             "Verify error:%s", X509_verify_cert_error_string(i));
   1130      1.1  christos 
   1131      1.1  christos         goto err;
   1132      1.1  christos     }
   1133      1.1  christos     /* Remove EE certificate from chain */
   1134      1.1  christos     x = sk_X509_shift(chain);
   1135      1.1  christos     X509_free(x);
   1136      1.1  christos     if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
   1137      1.1  christos         if (sk_X509_num(chain) > 0) {
   1138      1.1  christos             /* See if last cert is self signed */
   1139      1.1  christos             x = sk_X509_value(chain, sk_X509_num(chain) - 1);
   1140      1.1  christos             if (X509_get_extension_flags(x) & EXFLAG_SS) {
   1141      1.1  christos                 x = sk_X509_pop(chain);
   1142      1.1  christos                 X509_free(x);
   1143      1.1  christos             }
   1144      1.1  christos         }
   1145      1.1  christos     }
   1146      1.1  christos     /*
   1147      1.1  christos      * Check security level of all CA certificates: EE will have been checked
   1148      1.1  christos      * already.
   1149      1.1  christos      */
   1150      1.1  christos     for (i = 0; i < sk_X509_num(chain); i++) {
   1151      1.1  christos         x = sk_X509_value(chain, i);
   1152      1.1  christos         rv = ssl_security_cert(s, ctx, x, 0, 0);
   1153      1.1  christos         if (rv != 1) {
   1154      1.1  christos             ERR_raise(ERR_LIB_SSL, rv);
   1155      1.1  christos             OSSL_STACK_OF_X509_free(chain);
   1156      1.1  christos             rv = 0;
   1157      1.1  christos             goto err;
   1158      1.1  christos         }
   1159      1.1  christos     }
   1160      1.1  christos     OSSL_STACK_OF_X509_free(cpk->chain);
   1161      1.1  christos     cpk->chain = chain;
   1162      1.1  christos     if (rv == 0)
   1163      1.1  christos         rv = 1;
   1164  1.1.1.2  christos err:
   1165      1.1  christos     if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
   1166      1.1  christos         X509_STORE_free(chain_store);
   1167      1.1  christos     X509_STORE_CTX_free(xs_ctx);
   1168      1.1  christos 
   1169      1.1  christos     return rv;
   1170      1.1  christos }
   1171      1.1  christos 
   1172      1.1  christos int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
   1173      1.1  christos {
   1174      1.1  christos     X509_STORE **pstore;
   1175      1.1  christos 
   1176      1.1  christos     if (ref && store && !X509_STORE_up_ref(store))
   1177      1.1  christos         return 0;
   1178      1.1  christos 
   1179      1.1  christos     if (chain)
   1180      1.1  christos         pstore = &c->chain_store;
   1181      1.1  christos     else
   1182      1.1  christos         pstore = &c->verify_store;
   1183      1.1  christos     X509_STORE_free(*pstore);
   1184      1.1  christos     *pstore = store;
   1185      1.1  christos 
   1186      1.1  christos     return 1;
   1187      1.1  christos }
   1188      1.1  christos 
   1189      1.1  christos int ssl_cert_get_cert_store(CERT *c, X509_STORE **pstore, int chain)
   1190      1.1  christos {
   1191      1.1  christos     *pstore = (chain ? c->chain_store : c->verify_store);
   1192      1.1  christos     return 1;
   1193      1.1  christos }
   1194      1.1  christos 
   1195      1.1  christos int ssl_get_security_level_bits(const SSL *s, const SSL_CTX *ctx, int *levelp)
   1196      1.1  christos {
   1197      1.1  christos     int level;
   1198      1.1  christos     /*
   1199      1.1  christos      * note that there's a corresponding minbits_table
   1200      1.1  christos      * in crypto/x509/x509_vfy.c that's used for checking the security level
   1201      1.1  christos      * of RSA and DSA keys
   1202      1.1  christos      */
   1203      1.1  christos     static const int minbits_table[5 + 1] = { 0, 80, 112, 128, 192, 256 };
   1204      1.1  christos 
   1205      1.1  christos     if (ctx != NULL)
   1206      1.1  christos         level = SSL_CTX_get_security_level(ctx);
   1207      1.1  christos     else
   1208      1.1  christos         level = SSL_get_security_level(s);
   1209      1.1  christos 
   1210      1.1  christos     if (level > 5)
   1211      1.1  christos         level = 5;
   1212      1.1  christos     else if (level < 0)
   1213      1.1  christos         level = 0;
   1214      1.1  christos 
   1215      1.1  christos     if (levelp != NULL)
   1216      1.1  christos         *levelp = level;
   1217      1.1  christos 
   1218      1.1  christos     return minbits_table[level];
   1219      1.1  christos }
   1220      1.1  christos 
   1221      1.1  christos static int ssl_security_default_callback(const SSL *s, const SSL_CTX *ctx,
   1222  1.1.1.2  christos     int op, int bits, int nid, void *other,
   1223  1.1.1.2  christos     void *ex)
   1224      1.1  christos {
   1225      1.1  christos     int level, minbits, pfs_mask;
   1226      1.1  christos     const SSL_CONNECTION *sc;
   1227      1.1  christos 
   1228      1.1  christos     minbits = ssl_get_security_level_bits(s, ctx, &level);
   1229      1.1  christos 
   1230      1.1  christos     if (level == 0) {
   1231      1.1  christos         /*
   1232      1.1  christos          * No EDH keys weaker than 1024-bits even at level 0, otherwise,
   1233      1.1  christos          * anything goes.
   1234      1.1  christos          */
   1235      1.1  christos         if (op == SSL_SECOP_TMP_DH && bits < 80)
   1236      1.1  christos             return 0;
   1237      1.1  christos         return 1;
   1238      1.1  christos     }
   1239      1.1  christos     switch (op) {
   1240      1.1  christos     case SSL_SECOP_CIPHER_SUPPORTED:
   1241      1.1  christos     case SSL_SECOP_CIPHER_SHARED:
   1242  1.1.1.2  christos     case SSL_SECOP_CIPHER_CHECK: {
   1243  1.1.1.2  christos         const SSL_CIPHER *c = other;
   1244  1.1.1.2  christos         /* No ciphers below security level */
   1245  1.1.1.2  christos         if (bits < minbits)
   1246  1.1.1.2  christos             return 0;
   1247  1.1.1.2  christos         /* No unauthenticated ciphersuites */
   1248  1.1.1.2  christos         if (c->algorithm_auth & SSL_aNULL)
   1249  1.1.1.2  christos             return 0;
   1250  1.1.1.2  christos         /* No MD5 mac ciphersuites */
   1251  1.1.1.2  christos         if (c->algorithm_mac & SSL_MD5)
   1252  1.1.1.2  christos             return 0;
   1253  1.1.1.2  christos         /* SHA1 HMAC is 160 bits of security */
   1254  1.1.1.2  christos         if (minbits > 160 && c->algorithm_mac & SSL_SHA1)
   1255  1.1.1.2  christos             return 0;
   1256  1.1.1.2  christos         /* Level 3: forward secure ciphersuites only */
   1257  1.1.1.2  christos         pfs_mask = SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK;
   1258  1.1.1.2  christos         if (level >= 3 && c->min_tls != TLS1_3_VERSION && !(c->algorithm_mkey & pfs_mask))
   1259  1.1.1.2  christos             return 0;
   1260  1.1.1.2  christos         break;
   1261  1.1.1.2  christos     }
   1262      1.1  christos     case SSL_SECOP_VERSION:
   1263      1.1  christos         if ((sc = SSL_CONNECTION_FROM_CONST_SSL(s)) == NULL)
   1264      1.1  christos             return 0;
   1265      1.1  christos         if (!SSL_CONNECTION_IS_DTLS(sc)) {
   1266      1.1  christos             /* SSLv3, TLS v1.0 and TLS v1.1 only allowed at level 0 */
   1267      1.1  christos             if (nid <= TLS1_1_VERSION && level > 0)
   1268      1.1  christos                 return 0;
   1269      1.1  christos         } else {
   1270      1.1  christos             /* DTLS v1.0 only allowed at level 0 */
   1271      1.1  christos             if (DTLS_VERSION_LT(nid, DTLS1_2_VERSION) && level > 0)
   1272      1.1  christos                 return 0;
   1273      1.1  christos         }
   1274      1.1  christos         break;
   1275      1.1  christos 
   1276      1.1  christos     case SSL_SECOP_COMPRESSION:
   1277      1.1  christos         if (level >= 2)
   1278      1.1  christos             return 0;
   1279      1.1  christos         break;
   1280      1.1  christos     case SSL_SECOP_TICKET:
   1281      1.1  christos         if (level >= 3)
   1282      1.1  christos             return 0;
   1283      1.1  christos         break;
   1284      1.1  christos     default:
   1285      1.1  christos         if (bits < minbits)
   1286      1.1  christos             return 0;
   1287      1.1  christos     }
   1288      1.1  christos     return 1;
   1289      1.1  christos }
   1290      1.1  christos 
   1291      1.1  christos int ssl_security(const SSL_CONNECTION *s, int op, int bits, int nid, void *other)
   1292      1.1  christos {
   1293      1.1  christos     return s->cert->sec_cb(SSL_CONNECTION_GET_USER_SSL(s), NULL, op, bits, nid,
   1294  1.1.1.2  christos         other, s->cert->sec_ex);
   1295      1.1  christos }
   1296      1.1  christos 
   1297      1.1  christos int ssl_ctx_security(const SSL_CTX *ctx, int op, int bits, int nid, void *other)
   1298      1.1  christos {
   1299      1.1  christos     return ctx->cert->sec_cb(NULL, ctx, op, bits, nid, other,
   1300  1.1.1.2  christos         ctx->cert->sec_ex);
   1301      1.1  christos }
   1302      1.1  christos 
   1303      1.1  christos int ssl_cert_lookup_by_nid(int nid, size_t *pidx, SSL_CTX *ctx)
   1304      1.1  christos {
   1305      1.1  christos     size_t i;
   1306      1.1  christos 
   1307      1.1  christos     for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
   1308  1.1.1.2  christos         if (ssl_cert_info[i].pkey_nid == nid) {
   1309      1.1  christos             *pidx = i;
   1310      1.1  christos             return 1;
   1311      1.1  christos         }
   1312      1.1  christos     }
   1313      1.1  christos     for (i = 0; i < ctx->sigalg_list_len; i++) {
   1314  1.1.1.2  christos         if (ctx->ssl_cert_info[i].pkey_nid == nid) {
   1315      1.1  christos             *pidx = SSL_PKEY_NUM + i;
   1316      1.1  christos             return 1;
   1317      1.1  christos         }
   1318      1.1  christos     }
   1319      1.1  christos     return 0;
   1320      1.1  christos }
   1321      1.1  christos 
   1322      1.1  christos const SSL_CERT_LOOKUP *ssl_cert_lookup_by_pkey(const EVP_PKEY *pk, size_t *pidx, SSL_CTX *ctx)
   1323      1.1  christos {
   1324      1.1  christos     size_t i;
   1325      1.1  christos 
   1326      1.1  christos     /* check classic pk types */
   1327      1.1  christos     for (i = 0; i < OSSL_NELEM(ssl_cert_info); i++) {
   1328      1.1  christos         const SSL_CERT_LOOKUP *tmp_lu = &ssl_cert_info[i];
   1329      1.1  christos 
   1330  1.1.1.2  christos         if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->pkey_nid))
   1331  1.1.1.2  christos             || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->pkey_nid))) {
   1332      1.1  christos             if (pidx != NULL)
   1333      1.1  christos                 *pidx = i;
   1334      1.1  christos             return tmp_lu;
   1335      1.1  christos         }
   1336      1.1  christos     }
   1337      1.1  christos     /* check provider-loaded pk types */
   1338      1.1  christos     for (i = 0; i < ctx->sigalg_list_len; i++) {
   1339      1.1  christos         SSL_CERT_LOOKUP *tmp_lu = &(ctx->ssl_cert_info[i]);
   1340      1.1  christos 
   1341  1.1.1.2  christos         if (EVP_PKEY_is_a(pk, OBJ_nid2sn(tmp_lu->pkey_nid))
   1342  1.1.1.2  christos             || EVP_PKEY_is_a(pk, OBJ_nid2ln(tmp_lu->pkey_nid))) {
   1343      1.1  christos             if (pidx != NULL)
   1344      1.1  christos                 *pidx = SSL_PKEY_NUM + i;
   1345      1.1  christos             return &ctx->ssl_cert_info[i];
   1346      1.1  christos         }
   1347      1.1  christos     }
   1348      1.1  christos 
   1349      1.1  christos     return NULL;
   1350      1.1  christos }
   1351      1.1  christos 
   1352      1.1  christos const SSL_CERT_LOOKUP *ssl_cert_lookup_by_idx(size_t idx, SSL_CTX *ctx)
   1353      1.1  christos {
   1354      1.1  christos     if (idx >= (OSSL_NELEM(ssl_cert_info) + ctx->sigalg_list_len))
   1355      1.1  christos         return NULL;
   1356      1.1  christos     else if (idx >= (OSSL_NELEM(ssl_cert_info)))
   1357      1.1  christos         return &(ctx->ssl_cert_info[idx - SSL_PKEY_NUM]);
   1358      1.1  christos     return &ssl_cert_info[idx];
   1359      1.1  christos }
   1360