• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57 /* ====================================================================
58  * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  *    notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  *    notice, this list of conditions and the following disclaimer in
69  *    the documentation and/or other materials provided with the
70  *    distribution.
71  *
72  * 3. All advertising materials mentioning features or use of this
73  *    software must display the following acknowledgment:
74  *    "This product includes software developed by the OpenSSL Project
75  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76  *
77  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78  *    endorse or promote products derived from this software without
79  *    prior written permission. For written permission, please contact
80  *    openssl-core@openssl.org.
81  *
82  * 5. Products derived from this software may not be called "OpenSSL"
83  *    nor may "OpenSSL" appear in their names without prior written
84  *    permission of the OpenSSL Project.
85  *
86  * 6. Redistributions of any form whatsoever must retain the following
87  *    acknowledgment:
88  *    "This product includes software developed by the OpenSSL Project
89  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90  *
91  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
95  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102  * OF THE POSSIBILITY OF SUCH DAMAGE.
103  * ====================================================================
104  *
105  * This product includes cryptographic software written by Eric Young
106  * (eay@cryptsoft.com).  This product includes software written by Tim
107  * Hudson (tjh@cryptsoft.com).
108  *
109  */
110 /* ====================================================================
111  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
112  * ECC cipher suite support in OpenSSL originally developed by
113  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
114  */
115 /* ====================================================================
116  * Copyright 2005 Nokia. All rights reserved.
117  *
118  * The portions of the attached software ("Contribution") is developed by
119  * Nokia Corporation and is licensed pursuant to the OpenSSL open source
120  * license.
121  *
122  * The Contribution, originally written by Mika Kousa and Pasi Eronen of
123  * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
124  * support (see RFC 4279) to OpenSSL.
125  *
126  * No patent licenses or other rights except those expressly stated in
127  * the OpenSSL open source license shall be deemed granted or received
128  * expressly, by implication, estoppel, or otherwise.
129  *
130  * No assurances are provided by Nokia that the Contribution does not
131  * infringe the patent or other intellectual property rights of any third
132  * party or that the license provides you with all the necessary rights
133  * to make use of the Contribution.
134  *
135  * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
136  * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
137  * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
138  * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
139  * OTHERWISE. */
140 
141 #include <openssl/ssl.h>
142 
143 #include <assert.h>
144 
145 #include <openssl/asn1.h>
146 #include <openssl/bytestring.h>
147 #include <openssl/err.h>
148 #include <openssl/pem.h>
149 #include <openssl/stack.h>
150 #include <openssl/x509.h>
151 
152 #include "../crypto/internal.h"
153 #include "internal.h"
154 
155 
156 BSSL_NAMESPACE_BEGIN
157 
158 // check_ssl_x509_method asserts that |ssl| has the X509-based method
159 // installed. Calling an X509-based method on an |ssl| with a different method
160 // will likely misbehave and possibly crash or leak memory.
check_ssl_x509_method(const SSL * ssl)161 static void check_ssl_x509_method(const SSL *ssl) {
162   assert(ssl == NULL || ssl->ctx->x509_method == &ssl_crypto_x509_method);
163 }
164 
165 // check_ssl_ctx_x509_method acts like |check_ssl_x509_method|, but for an
166 // |SSL_CTX|.
check_ssl_ctx_x509_method(const SSL_CTX * ctx)167 static void check_ssl_ctx_x509_method(const SSL_CTX *ctx) {
168   assert(ctx == NULL || ctx->x509_method == &ssl_crypto_x509_method);
169 }
170 
171 // x509_to_buffer returns a |CRYPTO_BUFFER| that contains the serialised
172 // contents of |x509|.
x509_to_buffer(X509 * x509)173 static UniquePtr<CRYPTO_BUFFER> x509_to_buffer(X509 *x509) {
174   uint8_t *buf = NULL;
175   int cert_len = i2d_X509(x509, &buf);
176   if (cert_len <= 0) {
177     return 0;
178   }
179 
180   UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(buf, cert_len, NULL));
181   OPENSSL_free(buf);
182 
183   return buffer;
184 }
185 
ssl_crypto_x509_cert_flush_cached_leaf(CERT * cert)186 static void ssl_crypto_x509_cert_flush_cached_leaf(CERT *cert) {
187   X509_free(cert->x509_leaf);
188   cert->x509_leaf = nullptr;
189 }
190 
ssl_crypto_x509_cert_flush_cached_chain(CERT * cert)191 static void ssl_crypto_x509_cert_flush_cached_chain(CERT *cert) {
192   sk_X509_pop_free(cert->x509_chain, X509_free);
193   cert->x509_chain = nullptr;
194 }
195 
196 // ssl_cert_set1_chain sets elements 1.. of |cert->chain| to the serialised
197 // forms of elements of |chain|. It returns one on success or zero on error, in
198 // which case no change to |cert->chain| is made. It preverses the existing
199 // leaf from |cert->chain|, if any.
ssl_cert_set1_chain(CERT * cert,STACK_OF (X509)* chain)200 static bool ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
201   cert->legacy_credential->ClearIntermediateCerts();
202   for (X509 *x509 : chain) {
203     UniquePtr<CRYPTO_BUFFER> buffer = x509_to_buffer(x509);
204     if (!buffer ||
205         !cert->legacy_credential->AppendIntermediateCert(std::move(buffer))) {
206       return false;
207     }
208   }
209 
210   ssl_crypto_x509_cert_flush_cached_chain(cert);
211   return true;
212 }
213 
ssl_crypto_x509_check_client_CA_list(STACK_OF (CRYPTO_BUFFER)* names)214 static bool ssl_crypto_x509_check_client_CA_list(
215     STACK_OF(CRYPTO_BUFFER) *names) {
216   for (const CRYPTO_BUFFER *buffer : names) {
217     const uint8_t *inp = CRYPTO_BUFFER_data(buffer);
218     UniquePtr<X509_NAME> name(
219         d2i_X509_NAME(nullptr, &inp, CRYPTO_BUFFER_len(buffer)));
220     if (name == nullptr ||
221         inp != CRYPTO_BUFFER_data(buffer) + CRYPTO_BUFFER_len(buffer)) {
222       return false;
223     }
224   }
225 
226   return true;
227 }
228 
ssl_crypto_x509_cert_clear(CERT * cert)229 static void ssl_crypto_x509_cert_clear(CERT *cert) {
230   ssl_crypto_x509_cert_flush_cached_leaf(cert);
231   ssl_crypto_x509_cert_flush_cached_chain(cert);
232 
233   X509_free(cert->x509_stash);
234   cert->x509_stash = nullptr;
235 }
236 
ssl_crypto_x509_cert_free(CERT * cert)237 static void ssl_crypto_x509_cert_free(CERT *cert) {
238   ssl_crypto_x509_cert_clear(cert);
239   X509_STORE_free(cert->verify_store);
240 }
241 
ssl_crypto_x509_cert_dup(CERT * new_cert,const CERT * cert)242 static void ssl_crypto_x509_cert_dup(CERT *new_cert, const CERT *cert) {
243   if (cert->verify_store != nullptr) {
244     X509_STORE_up_ref(cert->verify_store);
245     new_cert->verify_store = cert->verify_store;
246   }
247 }
248 
ssl_crypto_x509_session_cache_objects(SSL_SESSION * sess)249 static bool ssl_crypto_x509_session_cache_objects(SSL_SESSION *sess) {
250   bssl::UniquePtr<STACK_OF(X509)> chain, chain_without_leaf;
251   if (sk_CRYPTO_BUFFER_num(sess->certs.get()) > 0) {
252     chain.reset(sk_X509_new_null());
253     if (!chain) {
254       return false;
255     }
256     if (sess->is_server) {
257       // chain_without_leaf is only needed for server sessions. See
258       // |SSL_get_peer_cert_chain|.
259       chain_without_leaf.reset(sk_X509_new_null());
260       if (!chain_without_leaf) {
261         return false;
262       }
263     }
264   }
265 
266   bssl::UniquePtr<X509> leaf;
267   for (CRYPTO_BUFFER *cert : sess->certs.get()) {
268     UniquePtr<X509> x509(X509_parse_from_buffer(cert));
269     if (!x509) {
270       OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
271       return false;
272     }
273     if (leaf == nullptr) {
274       leaf = UpRef(x509);
275     } else if (chain_without_leaf &&
276                !PushToStack(chain_without_leaf.get(), UpRef(x509))) {
277       return false;
278     }
279     if (!PushToStack(chain.get(), std::move(x509))) {
280       return false;
281     }
282   }
283 
284   sk_X509_pop_free(sess->x509_chain, X509_free);
285   sess->x509_chain = chain.release();
286 
287   sk_X509_pop_free(sess->x509_chain_without_leaf, X509_free);
288   sess->x509_chain_without_leaf = chain_without_leaf.release();
289 
290   X509_free(sess->x509_peer);
291   sess->x509_peer = leaf.release();
292   return true;
293 }
294 
ssl_crypto_x509_session_dup(SSL_SESSION * new_session,const SSL_SESSION * session)295 static bool ssl_crypto_x509_session_dup(SSL_SESSION *new_session,
296                                         const SSL_SESSION *session) {
297   new_session->x509_peer = UpRef(session->x509_peer).release();
298   if (session->x509_chain != nullptr) {
299     new_session->x509_chain = X509_chain_up_ref(session->x509_chain);
300     if (new_session->x509_chain == nullptr) {
301       return false;
302     }
303   }
304   if (session->x509_chain_without_leaf != nullptr) {
305     new_session->x509_chain_without_leaf =
306         X509_chain_up_ref(session->x509_chain_without_leaf);
307     if (new_session->x509_chain_without_leaf == nullptr) {
308       return false;
309     }
310   }
311 
312   return true;
313 }
314 
ssl_crypto_x509_session_clear(SSL_SESSION * session)315 static void ssl_crypto_x509_session_clear(SSL_SESSION *session) {
316   X509_free(session->x509_peer);
317   session->x509_peer = nullptr;
318   sk_X509_pop_free(session->x509_chain, X509_free);
319   session->x509_chain = nullptr;
320   sk_X509_pop_free(session->x509_chain_without_leaf, X509_free);
321   session->x509_chain_without_leaf = nullptr;
322 }
323 
ssl_crypto_x509_session_verify_cert_chain(SSL_SESSION * session,SSL_HANDSHAKE * hs,uint8_t * out_alert)324 static bool ssl_crypto_x509_session_verify_cert_chain(SSL_SESSION *session,
325                                                       SSL_HANDSHAKE *hs,
326                                                       uint8_t *out_alert) {
327   *out_alert = SSL_AD_INTERNAL_ERROR;
328   STACK_OF(X509) *const cert_chain = session->x509_chain;
329   if (cert_chain == nullptr || sk_X509_num(cert_chain) == 0) {
330     return false;
331   }
332 
333   SSL *const ssl = hs->ssl;
334   SSL_CTX *ssl_ctx = ssl->ctx.get();
335   X509_STORE *verify_store = ssl_ctx->cert_store;
336   if (hs->config->cert->verify_store != nullptr) {
337     verify_store = hs->config->cert->verify_store;
338   }
339 
340   X509 *leaf = sk_X509_value(cert_chain, 0);
341   const char *name;
342   size_t name_len;
343   SSL_get0_ech_name_override(ssl, &name, &name_len);
344   UniquePtr<X509_STORE_CTX> ctx(X509_STORE_CTX_new());
345   if (!ctx ||                                                             //
346       !X509_STORE_CTX_init(ctx.get(), verify_store, leaf, cert_chain) ||  //
347       !X509_STORE_CTX_set_ex_data(
348           ctx.get(), SSL_get_ex_data_X509_STORE_CTX_idx(), ssl) ||  //
349       // We need to inherit the verify parameters. These can be determined by
350       // the context: if its a server it will verify SSL client certificates or
351       // vice versa.
352       !X509_STORE_CTX_set_default(
353           ctx.get(),
354           ssl->server ? "ssl_client" : "ssl_server") ||  //
355       // Anything non-default in "param" should overwrite anything in the ctx.
356       !X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(ctx.get()),
357                               hs->config->param) ||  //
358       // ClientHelloOuter connections use a different name.
359       (name_len != 0 &&  //
360        !X509_VERIFY_PARAM_set1_host(X509_STORE_CTX_get0_param(ctx.get()), name,
361                                     name_len))) {
362     OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
363     return false;
364   }
365 
366   if (hs->config->verify_callback) {
367     X509_STORE_CTX_set_verify_cb(ctx.get(), hs->config->verify_callback);
368   }
369 
370   int verify_ret;
371   if (ssl_ctx->app_verify_callback != nullptr) {
372     verify_ret =
373         ssl_ctx->app_verify_callback(ctx.get(), ssl_ctx->app_verify_arg);
374   } else {
375     verify_ret = X509_verify_cert(ctx.get());
376   }
377 
378   session->verify_result = X509_STORE_CTX_get_error(ctx.get());
379 
380   // If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result.
381   if (verify_ret <= 0 && hs->config->verify_mode != SSL_VERIFY_NONE) {
382     *out_alert = SSL_alert_from_verify_result(session->verify_result);
383     return false;
384   }
385 
386   ERR_clear_error();
387   return true;
388 }
389 
ssl_crypto_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE * hs)390 static void ssl_crypto_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {
391   sk_X509_NAME_pop_free(hs->cached_x509_ca_names, X509_NAME_free);
392   hs->cached_x509_ca_names = nullptr;
393 }
394 
ssl_crypto_x509_ssl_new(SSL_HANDSHAKE * hs)395 static bool ssl_crypto_x509_ssl_new(SSL_HANDSHAKE *hs) {
396   hs->config->param = X509_VERIFY_PARAM_new();
397   if (hs->config->param == nullptr) {
398     return false;
399   }
400   X509_VERIFY_PARAM_inherit(hs->config->param, hs->ssl->ctx->param);
401   return true;
402 }
403 
ssl_crypto_x509_ssl_flush_cached_client_CA(SSL_CONFIG * cfg)404 static void ssl_crypto_x509_ssl_flush_cached_client_CA(SSL_CONFIG *cfg) {
405   sk_X509_NAME_pop_free(cfg->cached_x509_client_CA, X509_NAME_free);
406   cfg->cached_x509_client_CA = nullptr;
407 }
408 
ssl_crypto_x509_ssl_config_free(SSL_CONFIG * cfg)409 static void ssl_crypto_x509_ssl_config_free(SSL_CONFIG *cfg) {
410   sk_X509_NAME_pop_free(cfg->cached_x509_client_CA, X509_NAME_free);
411   cfg->cached_x509_client_CA = nullptr;
412   X509_VERIFY_PARAM_free(cfg->param);
413 }
414 
ssl_crypto_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE * hs)415 static bool ssl_crypto_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE *hs) {
416   // Only build a chain if the feature isn't disabled, the legacy credential
417   // exists but has no intermediates configured.
418   SSL *ssl = hs->ssl;
419   SSL_CREDENTIAL *cred = hs->config->cert->legacy_credential.get();
420   if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || !cred->IsComplete() ||
421       sk_CRYPTO_BUFFER_num(cred->chain.get()) != 1) {
422     return true;
423   }
424 
425   UniquePtr<X509> leaf(
426       X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(cred->chain.get(), 0)));
427   if (!leaf) {
428     OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
429     return false;
430   }
431 
432   UniquePtr<X509_STORE_CTX> ctx(X509_STORE_CTX_new());
433   if (!ctx || !X509_STORE_CTX_init(ctx.get(), ssl->ctx->cert_store, leaf.get(),
434                                    nullptr)) {
435     OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
436     return false;
437   }
438 
439   // Attempt to build a chain, ignoring the result.
440   X509_verify_cert(ctx.get());
441   ERR_clear_error();
442 
443   // Remove the leaf from the generated chain.
444   UniquePtr<STACK_OF(X509)> chain(X509_STORE_CTX_get1_chain(ctx.get()));
445   if (!chain) {
446     return false;
447   }
448   X509_free(sk_X509_shift(chain.get()));
449 
450   return SSL_set1_chain(ssl, chain.get());
451 }
452 
ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX * ctx)453 static void ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {
454   sk_X509_NAME_pop_free(ctx->cached_x509_client_CA, X509_NAME_free);
455   ctx->cached_x509_client_CA = nullptr;
456 }
457 
ssl_crypto_x509_ssl_ctx_new(SSL_CTX * ctx)458 static bool ssl_crypto_x509_ssl_ctx_new(SSL_CTX *ctx) {
459   ctx->cert_store = X509_STORE_new();
460   ctx->param = X509_VERIFY_PARAM_new();
461   return (ctx->cert_store != nullptr && ctx->param != nullptr);
462 }
463 
ssl_crypto_x509_ssl_ctx_free(SSL_CTX * ctx)464 static void ssl_crypto_x509_ssl_ctx_free(SSL_CTX *ctx) {
465   ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx);
466   X509_VERIFY_PARAM_free(ctx->param);
467   X509_STORE_free(ctx->cert_store);
468 }
469 
470 const SSL_X509_METHOD ssl_crypto_x509_method = {
471     ssl_crypto_x509_check_client_CA_list,
472     ssl_crypto_x509_cert_clear,
473     ssl_crypto_x509_cert_free,
474     ssl_crypto_x509_cert_dup,
475     ssl_crypto_x509_cert_flush_cached_chain,
476     ssl_crypto_x509_cert_flush_cached_leaf,
477     ssl_crypto_x509_session_cache_objects,
478     ssl_crypto_x509_session_dup,
479     ssl_crypto_x509_session_clear,
480     ssl_crypto_x509_session_verify_cert_chain,
481     ssl_crypto_x509_hs_flush_cached_ca_names,
482     ssl_crypto_x509_ssl_new,
483     ssl_crypto_x509_ssl_config_free,
484     ssl_crypto_x509_ssl_flush_cached_client_CA,
485     ssl_crypto_x509_ssl_auto_chain_if_needed,
486     ssl_crypto_x509_ssl_ctx_new,
487     ssl_crypto_x509_ssl_ctx_free,
488     ssl_crypto_x509_ssl_ctx_flush_cached_client_CA,
489 };
490 
491 BSSL_NAMESPACE_END
492 
493 using namespace bssl;
494 
SSL_get_peer_certificate(const SSL * ssl)495 X509 *SSL_get_peer_certificate(const SSL *ssl) {
496   check_ssl_x509_method(ssl);
497   if (ssl == NULL) {
498     return NULL;
499   }
500   SSL_SESSION *session = SSL_get_session(ssl);
501   if (session == NULL || session->x509_peer == NULL) {
502     return NULL;
503   }
504   X509_up_ref(session->x509_peer);
505   return session->x509_peer;
506 }
507 
STACK_OF(X509)508 STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
509   check_ssl_x509_method(ssl);
510   if (ssl == nullptr) {
511     return nullptr;
512   }
513   SSL_SESSION *session = SSL_get_session(ssl);
514   if (session == nullptr) {
515     return nullptr;
516   }
517 
518   // OpenSSL historically didn't include the leaf certificate in the returned
519   // certificate chain, but only for servers.
520   return ssl->server ? session->x509_chain_without_leaf : session->x509_chain;
521 }
522 
STACK_OF(X509)523 STACK_OF(X509) *SSL_get_peer_full_cert_chain(const SSL *ssl) {
524   check_ssl_x509_method(ssl);
525   SSL_SESSION *session = SSL_get_session(ssl);
526   if (session == NULL) {
527     return NULL;
528   }
529 
530   return session->x509_chain;
531 }
532 
SSL_CTX_set_purpose(SSL_CTX * ctx,int purpose)533 int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
534   check_ssl_ctx_x509_method(ctx);
535   return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
536 }
537 
SSL_set_purpose(SSL * ssl,int purpose)538 int SSL_set_purpose(SSL *ssl, int purpose) {
539   check_ssl_x509_method(ssl);
540   if (!ssl->config) {
541     return 0;
542   }
543   return X509_VERIFY_PARAM_set_purpose(ssl->config->param, purpose);
544 }
545 
SSL_CTX_set_trust(SSL_CTX * ctx,int trust)546 int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
547   check_ssl_ctx_x509_method(ctx);
548   return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
549 }
550 
SSL_set_trust(SSL * ssl,int trust)551 int SSL_set_trust(SSL *ssl, int trust) {
552   check_ssl_x509_method(ssl);
553   if (!ssl->config) {
554     return 0;
555   }
556   return X509_VERIFY_PARAM_set_trust(ssl->config->param, trust);
557 }
558 
SSL_CTX_set1_param(SSL_CTX * ctx,const X509_VERIFY_PARAM * param)559 int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
560   check_ssl_ctx_x509_method(ctx);
561   return X509_VERIFY_PARAM_set1(ctx->param, param);
562 }
563 
SSL_set1_param(SSL * ssl,const X509_VERIFY_PARAM * param)564 int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
565   check_ssl_x509_method(ssl);
566   if (!ssl->config) {
567     return 0;
568   }
569   return X509_VERIFY_PARAM_set1(ssl->config->param, param);
570 }
571 
SSL_CTX_get0_param(SSL_CTX * ctx)572 X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) {
573   check_ssl_ctx_x509_method(ctx);
574   return ctx->param;
575 }
576 
SSL_get0_param(SSL * ssl)577 X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) {
578   check_ssl_x509_method(ssl);
579   if (!ssl->config) {
580     assert(ssl->config);
581     return 0;
582   }
583   return ssl->config->param;
584 }
585 
SSL_get_verify_depth(const SSL * ssl)586 int SSL_get_verify_depth(const SSL *ssl) {
587   check_ssl_x509_method(ssl);
588   if (!ssl->config) {
589     assert(ssl->config);
590     return 0;
591   }
592   return X509_VERIFY_PARAM_get_depth(ssl->config->param);
593 }
594 
SSL_get_verify_callback(const SSL * ssl)595 int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
596   check_ssl_x509_method(ssl);
597   if (!ssl->config) {
598     assert(ssl->config);
599     return 0;
600   }
601   return ssl->config->verify_callback;
602 }
603 
SSL_CTX_get_verify_mode(const SSL_CTX * ctx)604 int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) {
605   check_ssl_ctx_x509_method(ctx);
606   return ctx->verify_mode;
607 }
608 
SSL_CTX_get_verify_depth(const SSL_CTX * ctx)609 int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
610   check_ssl_ctx_x509_method(ctx);
611   return X509_VERIFY_PARAM_get_depth(ctx->param);
612 }
613 
SSL_CTX_get_verify_callback(const SSL_CTX * ctx)614 int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
615     int ok, X509_STORE_CTX *store_ctx) {
616   check_ssl_ctx_x509_method(ctx);
617   return ctx->default_verify_callback;
618 }
619 
SSL_set_verify(SSL * ssl,int mode,int (* callback)(int ok,X509_STORE_CTX * store_ctx))620 void SSL_set_verify(SSL *ssl, int mode,
621                     int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
622   check_ssl_x509_method(ssl);
623   if (!ssl->config) {
624     return;
625   }
626   ssl->config->verify_mode = mode;
627   if (callback != NULL) {
628     ssl->config->verify_callback = callback;
629   }
630 }
631 
SSL_set_verify_depth(SSL * ssl,int depth)632 void SSL_set_verify_depth(SSL *ssl, int depth) {
633   check_ssl_x509_method(ssl);
634   if (!ssl->config) {
635     return;
636   }
637   X509_VERIFY_PARAM_set_depth(ssl->config->param, depth);
638 }
639 
SSL_CTX_set_cert_verify_callback(SSL_CTX * ctx,int (* cb)(X509_STORE_CTX * store_ctx,void * arg),void * arg)640 void SSL_CTX_set_cert_verify_callback(
641     SSL_CTX *ctx, int (*cb)(X509_STORE_CTX *store_ctx, void *arg), void *arg) {
642   check_ssl_ctx_x509_method(ctx);
643   ctx->app_verify_callback = cb;
644   ctx->app_verify_arg = arg;
645 }
646 
SSL_CTX_set_verify(SSL_CTX * ctx,int mode,int (* cb)(int,X509_STORE_CTX *))647 void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
648                         int (*cb)(int, X509_STORE_CTX *)) {
649   check_ssl_ctx_x509_method(ctx);
650   ctx->verify_mode = mode;
651   ctx->default_verify_callback = cb;
652 }
653 
SSL_CTX_set_verify_depth(SSL_CTX * ctx,int depth)654 void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
655   check_ssl_ctx_x509_method(ctx);
656   X509_VERIFY_PARAM_set_depth(ctx->param, depth);
657 }
658 
SSL_CTX_set_default_verify_paths(SSL_CTX * ctx)659 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
660   check_ssl_ctx_x509_method(ctx);
661   return X509_STORE_set_default_paths(ctx->cert_store);
662 }
663 
SSL_CTX_load_verify_locations(SSL_CTX * ctx,const char * ca_file,const char * ca_dir)664 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
665                                   const char *ca_dir) {
666   check_ssl_ctx_x509_method(ctx);
667   return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
668 }
669 
SSL_get_verify_result(const SSL * ssl)670 long SSL_get_verify_result(const SSL *ssl) {
671   check_ssl_x509_method(ssl);
672   SSL_SESSION *session = SSL_get_session(ssl);
673   if (session == NULL) {
674     return X509_V_ERR_INVALID_CALL;
675   }
676   return session->verify_result;
677 }
678 
SSL_CTX_get_cert_store(const SSL_CTX * ctx)679 X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
680   check_ssl_ctx_x509_method(ctx);
681   return ctx->cert_store;
682 }
683 
SSL_CTX_set_cert_store(SSL_CTX * ctx,X509_STORE * store)684 void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
685   check_ssl_ctx_x509_method(ctx);
686   X509_STORE_free(ctx->cert_store);
687   ctx->cert_store = store;
688 }
689 
ssl_use_certificate(CERT * cert,X509 * x)690 static int ssl_use_certificate(CERT *cert, X509 *x) {
691   if (x == NULL) {
692     OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
693     return 0;
694   }
695 
696   UniquePtr<CRYPTO_BUFFER> buffer = x509_to_buffer(x);
697   if (!buffer) {
698     return 0;
699   }
700 
701   return ssl_set_cert(cert, std::move(buffer));
702 }
703 
SSL_use_certificate(SSL * ssl,X509 * x)704 int SSL_use_certificate(SSL *ssl, X509 *x) {
705   check_ssl_x509_method(ssl);
706   if (!ssl->config) {
707     return 0;
708   }
709   return ssl_use_certificate(ssl->config->cert.get(), x);
710 }
711 
SSL_CTX_use_certificate(SSL_CTX * ctx,X509 * x)712 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) {
713   check_ssl_ctx_x509_method(ctx);
714   return ssl_use_certificate(ctx->cert.get(), x);
715 }
716 
717 // ssl_cert_cache_leaf_cert sets |cert->x509_leaf|, if currently NULL, from the
718 // first element of |cert->chain|.
ssl_cert_cache_leaf_cert(CERT * cert)719 static int ssl_cert_cache_leaf_cert(CERT *cert) {
720   assert(cert->x509_method);
721 
722   const SSL_CREDENTIAL *cred = cert->legacy_credential.get();
723   if (cert->x509_leaf != NULL || cred->chain == NULL) {
724     return 1;
725   }
726 
727   CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cred->chain.get(), 0);
728   if (!leaf) {
729     return 1;
730   }
731 
732   cert->x509_leaf = X509_parse_from_buffer(leaf);
733   return cert->x509_leaf != NULL;
734 }
735 
ssl_cert_get0_leaf(CERT * cert)736 static X509 *ssl_cert_get0_leaf(CERT *cert) {
737   if (cert->x509_leaf == NULL &&  //
738       !ssl_cert_cache_leaf_cert(cert)) {
739     return NULL;
740   }
741 
742   return cert->x509_leaf;
743 }
744 
SSL_get_certificate(const SSL * ssl)745 X509 *SSL_get_certificate(const SSL *ssl) {
746   check_ssl_x509_method(ssl);
747   if (!ssl->config) {
748     assert(ssl->config);
749     return 0;
750   }
751   return ssl_cert_get0_leaf(ssl->config->cert.get());
752 }
753 
SSL_CTX_get0_certificate(const SSL_CTX * ctx)754 X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
755   check_ssl_ctx_x509_method(ctx);
756   MutexWriteLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock));
757   return ssl_cert_get0_leaf(ctx->cert.get());
758 }
759 
ssl_cert_add1_chain_cert(CERT * cert,X509 * x509)760 static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
761   assert(cert->x509_method);
762 
763   UniquePtr<CRYPTO_BUFFER> buffer = x509_to_buffer(x509);
764   if (!buffer ||
765       !cert->legacy_credential->AppendIntermediateCert(std::move(buffer))) {
766     return 0;
767   }
768 
769   ssl_crypto_x509_cert_flush_cached_chain(cert);
770   return 1;
771 }
772 
ssl_cert_add0_chain_cert(CERT * cert,X509 * x509)773 static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
774   if (!ssl_cert_add1_chain_cert(cert, x509)) {
775     return 0;
776   }
777 
778   X509_free(cert->x509_stash);
779   cert->x509_stash = x509;
780   return 1;
781 }
782 
SSL_CTX_set0_chain(SSL_CTX * ctx,STACK_OF (X509)* chain)783 int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
784   check_ssl_ctx_x509_method(ctx);
785   if (!ssl_cert_set1_chain(ctx->cert.get(), chain)) {
786     return 0;
787   }
788   sk_X509_pop_free(chain, X509_free);
789   return 1;
790 }
791 
SSL_CTX_set1_chain(SSL_CTX * ctx,STACK_OF (X509)* chain)792 int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
793   check_ssl_ctx_x509_method(ctx);
794   return ssl_cert_set1_chain(ctx->cert.get(), chain);
795 }
796 
SSL_set0_chain(SSL * ssl,STACK_OF (X509)* chain)797 int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
798   check_ssl_x509_method(ssl);
799   if (!ssl->config) {
800     return 0;
801   }
802   if (!ssl_cert_set1_chain(ssl->config->cert.get(), chain)) {
803     return 0;
804   }
805   sk_X509_pop_free(chain, X509_free);
806   return 1;
807 }
808 
SSL_set1_chain(SSL * ssl,STACK_OF (X509)* chain)809 int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
810   check_ssl_x509_method(ssl);
811   if (!ssl->config) {
812     return 0;
813   }
814   return ssl_cert_set1_chain(ssl->config->cert.get(), chain);
815 }
816 
SSL_CTX_add0_chain_cert(SSL_CTX * ctx,X509 * x509)817 int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
818   check_ssl_ctx_x509_method(ctx);
819   return ssl_cert_add0_chain_cert(ctx->cert.get(), x509);
820 }
821 
SSL_CTX_add1_chain_cert(SSL_CTX * ctx,X509 * x509)822 int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
823   check_ssl_ctx_x509_method(ctx);
824   return ssl_cert_add1_chain_cert(ctx->cert.get(), x509);
825 }
826 
SSL_CTX_add_extra_chain_cert(SSL_CTX * ctx,X509 * x509)827 int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
828   check_ssl_ctx_x509_method(ctx);
829   return SSL_CTX_add0_chain_cert(ctx, x509);
830 }
831 
SSL_add0_chain_cert(SSL * ssl,X509 * x509)832 int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
833   check_ssl_x509_method(ssl);
834   if (!ssl->config) {
835     return 0;
836   }
837   return ssl_cert_add0_chain_cert(ssl->config->cert.get(), x509);
838 }
839 
SSL_add1_chain_cert(SSL * ssl,X509 * x509)840 int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
841   check_ssl_x509_method(ssl);
842   if (!ssl->config) {
843     return 0;
844   }
845   return ssl_cert_add1_chain_cert(ssl->config->cert.get(), x509);
846 }
847 
SSL_CTX_clear_chain_certs(SSL_CTX * ctx)848 int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
849   check_ssl_ctx_x509_method(ctx);
850   return SSL_CTX_set0_chain(ctx, NULL);
851 }
852 
SSL_CTX_clear_extra_chain_certs(SSL_CTX * ctx)853 int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
854   check_ssl_ctx_x509_method(ctx);
855   return SSL_CTX_clear_chain_certs(ctx);
856 }
857 
SSL_clear_chain_certs(SSL * ssl)858 int SSL_clear_chain_certs(SSL *ssl) {
859   check_ssl_x509_method(ssl);
860   return SSL_set0_chain(ssl, NULL);
861 }
862 
863 // ssl_cert_cache_chain_certs fills in |cert->x509_chain| from elements 1.. of
864 // |cert->chain|.
ssl_cert_cache_chain_certs(CERT * cert)865 static int ssl_cert_cache_chain_certs(CERT *cert) {
866   assert(cert->x509_method);
867 
868   const SSL_CREDENTIAL *cred = cert->legacy_credential.get();
869   if (cert->x509_chain != nullptr || cred->chain == nullptr ||
870       sk_CRYPTO_BUFFER_num(cred->chain.get()) < 2) {
871     return 1;
872   }
873 
874   UniquePtr<STACK_OF(X509)> chain(sk_X509_new_null());
875   if (!chain) {
876     return 0;
877   }
878 
879   for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cred->chain.get()); i++) {
880     CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(cred->chain.get(), i);
881     UniquePtr<X509> x509(X509_parse_from_buffer(buffer));
882     if (!x509 ||  //
883         !PushToStack(chain.get(), std::move(x509))) {
884       return 0;
885     }
886   }
887 
888   cert->x509_chain = chain.release();
889   return 1;
890 }
891 
SSL_CTX_get0_chain_certs(const SSL_CTX * ctx,STACK_OF (X509)** out_chain)892 int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
893   check_ssl_ctx_x509_method(ctx);
894   MutexWriteLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock));
895   if (!ssl_cert_cache_chain_certs(ctx->cert.get())) {
896     *out_chain = NULL;
897     return 0;
898   }
899 
900   *out_chain = ctx->cert->x509_chain;
901   return 1;
902 }
903 
SSL_CTX_get_extra_chain_certs(const SSL_CTX * ctx,STACK_OF (X509)** out_chain)904 int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
905                                   STACK_OF(X509) **out_chain) {
906   return SSL_CTX_get0_chain_certs(ctx, out_chain);
907 }
908 
SSL_get0_chain_certs(const SSL * ssl,STACK_OF (X509)** out_chain)909 int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
910   check_ssl_x509_method(ssl);
911   if (!ssl->config) {
912     assert(ssl->config);
913     return 0;
914   }
915   if (!ssl_cert_cache_chain_certs(ssl->config->cert.get())) {
916     *out_chain = NULL;
917     return 0;
918   }
919 
920   *out_chain = ssl->config->cert->x509_chain;
921   return 1;
922 }
923 
d2i_SSL_SESSION_bio(BIO * bio,SSL_SESSION ** out)924 SSL_SESSION *d2i_SSL_SESSION_bio(BIO *bio, SSL_SESSION **out) {
925   uint8_t *data;
926   size_t len;
927   if (!BIO_read_asn1(bio, &data, &len, 1024 * 1024)) {
928     return 0;
929   }
930   bssl::UniquePtr<uint8_t> free_data(data);
931   const uint8_t *ptr = data;
932   return d2i_SSL_SESSION(out, &ptr, static_cast<long>(len));
933 }
934 
i2d_SSL_SESSION_bio(BIO * bio,const SSL_SESSION * session)935 int i2d_SSL_SESSION_bio(BIO *bio, const SSL_SESSION *session) {
936   uint8_t *data;
937   size_t len;
938   if (!SSL_SESSION_to_bytes(session, &data, &len)) {
939     return 0;
940   }
941   bssl::UniquePtr<uint8_t> free_data(data);
942   return BIO_write_all(bio, data, len);
943 }
944 
IMPLEMENT_PEM_rw(SSL_SESSION,SSL_SESSION,PEM_STRING_SSL_SESSION,SSL_SESSION)945 IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)
946 
947 SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
948   if (length < 0) {
949     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
950     return NULL;
951   }
952 
953   CBS cbs;
954   CBS_init(&cbs, *pp, length);
955 
956   UniquePtr<SSL_SESSION> ret = SSL_SESSION_parse(&cbs, &ssl_crypto_x509_method,
957                                                  NULL /* no buffer pool */);
958   if (!ret) {
959     return NULL;
960   }
961 
962   if (a) {
963     SSL_SESSION_free(*a);
964     *a = ret.get();
965   }
966   *pp = CBS_data(&cbs);
967   return ret.release();
968 }
969 
STACK_OF(X509_NAME)970 STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
971   // TODO(https://crbug.com/boringssl/407): |X509_NAME_dup| should be const.
972   auto name_dup = [](const X509_NAME *name) {
973     return X509_NAME_dup(const_cast<X509_NAME *>(name));
974   };
975   return sk_X509_NAME_deep_copy(list, name_dup, X509_NAME_free);
976 }
977 
set_client_CA_list(UniquePtr<STACK_OF (CRYPTO_BUFFER)> * ca_list,const STACK_OF (X509_NAME)* name_list,CRYPTO_BUFFER_POOL * pool)978 static void set_client_CA_list(UniquePtr<STACK_OF(CRYPTO_BUFFER)> *ca_list,
979                                const STACK_OF(X509_NAME) *name_list,
980                                CRYPTO_BUFFER_POOL *pool) {
981   UniquePtr<STACK_OF(CRYPTO_BUFFER)> buffers(sk_CRYPTO_BUFFER_new_null());
982   if (!buffers) {
983     return;
984   }
985 
986   for (X509_NAME *name : name_list) {
987     uint8_t *outp = NULL;
988     int len = i2d_X509_NAME(name, &outp);
989     if (len < 0) {
990       return;
991     }
992 
993     UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(outp, len, pool));
994     OPENSSL_free(outp);
995     if (!buffer || !PushToStack(buffers.get(), std::move(buffer))) {
996       return;
997     }
998   }
999 
1000   *ca_list = std::move(buffers);
1001 }
1002 
SSL_set_client_CA_list(SSL * ssl,STACK_OF (X509_NAME)* name_list)1003 void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
1004   check_ssl_x509_method(ssl);
1005   if (!ssl->config) {
1006     return;
1007   }
1008   ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl->config.get());
1009   set_client_CA_list(&ssl->config->client_CA, name_list, ssl->ctx->pool);
1010   sk_X509_NAME_pop_free(name_list, X509_NAME_free);
1011 }
1012 
SSL_CTX_set_client_CA_list(SSL_CTX * ctx,STACK_OF (X509_NAME)* name_list)1013 void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
1014   check_ssl_ctx_x509_method(ctx);
1015   ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx);
1016   set_client_CA_list(&ctx->client_CA, name_list, ctx->pool);
1017   sk_X509_NAME_pop_free(name_list, X509_NAME_free);
1018 }
1019 
STACK_OF(X509_NAME)1020 static STACK_OF(X509_NAME) *buffer_names_to_x509(
1021     const STACK_OF(CRYPTO_BUFFER) *names, STACK_OF(X509_NAME) **cached) {
1022   if (names == NULL) {
1023     return NULL;
1024   }
1025 
1026   if (*cached != NULL) {
1027     return *cached;
1028   }
1029 
1030   UniquePtr<STACK_OF(X509_NAME)> new_cache(sk_X509_NAME_new_null());
1031   if (!new_cache) {
1032     return NULL;
1033   }
1034 
1035   for (const CRYPTO_BUFFER *buffer : names) {
1036     const uint8_t *inp = CRYPTO_BUFFER_data(buffer);
1037     UniquePtr<X509_NAME> name(
1038         d2i_X509_NAME(nullptr, &inp, CRYPTO_BUFFER_len(buffer)));
1039     if (!name ||
1040         inp != CRYPTO_BUFFER_data(buffer) + CRYPTO_BUFFER_len(buffer) ||
1041         !PushToStack(new_cache.get(), std::move(name))) {
1042       return NULL;
1043     }
1044   }
1045 
1046   *cached = new_cache.release();
1047   return *cached;
1048 }
1049 
STACK_OF(X509_NAME)1050 STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
1051   check_ssl_x509_method(ssl);
1052   if (!ssl->config) {
1053     assert(ssl->config);
1054     return NULL;
1055   }
1056   // For historical reasons, this function is used both to query configuration
1057   // state on a server as well as handshake state on a client. However, whether
1058   // |ssl| is a client or server is not known until explicitly configured with
1059   // |SSL_set_connect_state|. If |do_handshake| is NULL, |ssl| is in an
1060   // indeterminate mode and |ssl->server| is unset.
1061   if (ssl->do_handshake != NULL && !ssl->server) {
1062     if (ssl->s3->hs != NULL) {
1063       return buffer_names_to_x509(ssl->s3->hs->ca_names.get(),
1064                                   &ssl->s3->hs->cached_x509_ca_names);
1065     }
1066 
1067     return NULL;
1068   }
1069 
1070   if (ssl->config->client_CA != NULL) {
1071     return buffer_names_to_x509(
1072         ssl->config->client_CA.get(),
1073         (STACK_OF(X509_NAME) **)&ssl->config->cached_x509_client_CA);
1074   }
1075   return SSL_CTX_get_client_CA_list(ssl->ctx.get());
1076 }
1077 
STACK_OF(X509_NAME)1078 STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
1079   check_ssl_ctx_x509_method(ctx);
1080   // This is a logically const operation that may be called on multiple threads,
1081   // so it needs to lock around updating |cached_x509_client_CA|.
1082   MutexWriteLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock));
1083   return buffer_names_to_x509(
1084       ctx->client_CA.get(),
1085       const_cast<STACK_OF(X509_NAME) **>(&ctx->cached_x509_client_CA));
1086 }
1087 
add_client_CA(UniquePtr<STACK_OF (CRYPTO_BUFFER)> * names,X509 * x509,CRYPTO_BUFFER_POOL * pool)1088 static int add_client_CA(UniquePtr<STACK_OF(CRYPTO_BUFFER)> *names, X509 *x509,
1089                          CRYPTO_BUFFER_POOL *pool) {
1090   if (x509 == NULL) {
1091     return 0;
1092   }
1093 
1094   uint8_t *outp = NULL;
1095   int len = i2d_X509_NAME(X509_get_subject_name(x509), &outp);
1096   if (len < 0) {
1097     return 0;
1098   }
1099 
1100   UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(outp, len, pool));
1101   OPENSSL_free(outp);
1102   if (!buffer) {
1103     return 0;
1104   }
1105 
1106   int alloced = 0;
1107   if (*names == nullptr) {
1108     names->reset(sk_CRYPTO_BUFFER_new_null());
1109     alloced = 1;
1110 
1111     if (*names == NULL) {
1112       return 0;
1113     }
1114   }
1115 
1116   if (!PushToStack(names->get(), std::move(buffer))) {
1117     if (alloced) {
1118       names->reset();
1119     }
1120     return 0;
1121   }
1122 
1123   return 1;
1124 }
1125 
SSL_add_client_CA(SSL * ssl,X509 * x509)1126 int SSL_add_client_CA(SSL *ssl, X509 *x509) {
1127   check_ssl_x509_method(ssl);
1128   if (!ssl->config) {
1129     return 0;
1130   }
1131   if (!add_client_CA(&ssl->config->client_CA, x509, ssl->ctx->pool)) {
1132     return 0;
1133   }
1134 
1135   ssl_crypto_x509_ssl_flush_cached_client_CA(ssl->config.get());
1136   return 1;
1137 }
1138 
SSL_CTX_add_client_CA(SSL_CTX * ctx,X509 * x509)1139 int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
1140   check_ssl_ctx_x509_method(ctx);
1141   if (!add_client_CA(&ctx->client_CA, x509, ctx->pool)) {
1142     return 0;
1143   }
1144 
1145   ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx);
1146   return 1;
1147 }
1148 
do_client_cert_cb(SSL * ssl,void * arg)1149 static int do_client_cert_cb(SSL *ssl, void *arg) {
1150   // Should only be called during handshake, but check to be sure.
1151   BSSL_CHECK(ssl->config);
1152 
1153   if (ssl->config->cert->legacy_credential->IsComplete() ||
1154       ssl->ctx->client_cert_cb == nullptr) {
1155     return 1;
1156   }
1157 
1158   X509 *x509 = NULL;
1159   EVP_PKEY *pkey = NULL;
1160   int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
1161   if (ret < 0) {
1162     return -1;
1163   }
1164   UniquePtr<X509> free_x509(x509);
1165   UniquePtr<EVP_PKEY> free_pkey(pkey);
1166 
1167   if (ret != 0) {
1168     if (!SSL_use_certificate(ssl, x509) ||  //
1169         !SSL_use_PrivateKey(ssl, pkey)) {
1170       return 0;
1171     }
1172   }
1173 
1174   return 1;
1175 }
1176 
SSL_CTX_set_client_cert_cb(SSL_CTX * ctx,int (* cb)(SSL * ssl,X509 ** out_x509,EVP_PKEY ** out_pkey))1177 void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx,
1178                                 int (*cb)(SSL *ssl, X509 **out_x509,
1179                                           EVP_PKEY **out_pkey)) {
1180   check_ssl_ctx_x509_method(ctx);
1181   // Emulate the old client certificate callback with the new one.
1182   SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL);
1183   ctx->client_cert_cb = cb;
1184 }
1185 
set_cert_store(X509_STORE ** store_ptr,X509_STORE * new_store,int take_ref)1186 static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store,
1187                           int take_ref) {
1188   X509_STORE_free(*store_ptr);
1189   *store_ptr = new_store;
1190 
1191   if (new_store != NULL && take_ref) {
1192     X509_STORE_up_ref(new_store);
1193   }
1194 
1195   return 1;
1196 }
1197 
SSL_get_ex_data_X509_STORE_CTX_idx(void)1198 int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
1199   // The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the
1200   // reserved app_data slot. Before ex_data was introduced, app_data was used.
1201   // Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data|
1202   // works.
1203   return 0;
1204 }
1205 
SSL_CTX_set0_verify_cert_store(SSL_CTX * ctx,X509_STORE * store)1206 int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
1207   check_ssl_ctx_x509_method(ctx);
1208   return set_cert_store(&ctx->cert->verify_store, store, 0);
1209 }
1210 
SSL_CTX_set1_verify_cert_store(SSL_CTX * ctx,X509_STORE * store)1211 int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
1212   check_ssl_ctx_x509_method(ctx);
1213   return set_cert_store(&ctx->cert->verify_store, store, 1);
1214 }
1215 
SSL_set0_verify_cert_store(SSL * ssl,X509_STORE * store)1216 int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
1217   check_ssl_x509_method(ssl);
1218   if (!ssl->config) {
1219     return 0;
1220   }
1221   return set_cert_store(&ssl->config->cert->verify_store, store, 0);
1222 }
1223 
SSL_set1_verify_cert_store(SSL * ssl,X509_STORE * store)1224 int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
1225   check_ssl_x509_method(ssl);
1226   if (!ssl->config) {
1227     return 0;
1228   }
1229   return set_cert_store(&ssl->config->cert->verify_store, store, 1);
1230 }
1231 
SSL_set1_host(SSL * ssl,const char * hostname)1232 int SSL_set1_host(SSL *ssl, const char *hostname) {
1233   check_ssl_x509_method(ssl);
1234   if (!ssl->config) {
1235     return 0;
1236   }
1237   return X509_VERIFY_PARAM_set1_host(ssl->config->param, hostname,
1238                                      strlen(hostname));
1239 }
1240 
SSL_set_hostflags(SSL * ssl,unsigned flags)1241 void SSL_set_hostflags(SSL *ssl, unsigned flags) {
1242   check_ssl_x509_method(ssl);
1243   if (!ssl->config) {
1244     return;
1245   }
1246   X509_VERIFY_PARAM_set_hostflags(ssl->config->param, flags);
1247 }
1248 
SSL_alert_from_verify_result(long result)1249 int SSL_alert_from_verify_result(long result) {
1250   switch (result) {
1251     case X509_V_ERR_CERT_CHAIN_TOO_LONG:
1252     case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1253     case X509_V_ERR_INVALID_CA:
1254     case X509_V_ERR_PATH_LENGTH_EXCEEDED:
1255     case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1256     case X509_V_ERR_UNABLE_TO_GET_CRL:
1257     case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
1258     case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1259     case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1260     case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1261       return SSL_AD_UNKNOWN_CA;
1262 
1263     case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
1264     case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
1265     case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
1266     case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
1267     case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
1268     case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
1269     case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
1270     case X509_V_ERR_CERT_UNTRUSTED:
1271     case X509_V_ERR_CERT_REJECTED:
1272     case X509_V_ERR_HOSTNAME_MISMATCH:
1273     case X509_V_ERR_EMAIL_MISMATCH:
1274     case X509_V_ERR_IP_ADDRESS_MISMATCH:
1275       return SSL_AD_BAD_CERTIFICATE;
1276 
1277     case X509_V_ERR_CERT_SIGNATURE_FAILURE:
1278     case X509_V_ERR_CRL_SIGNATURE_FAILURE:
1279       return SSL_AD_DECRYPT_ERROR;
1280 
1281     case X509_V_ERR_CERT_HAS_EXPIRED:
1282     case X509_V_ERR_CERT_NOT_YET_VALID:
1283     case X509_V_ERR_CRL_HAS_EXPIRED:
1284     case X509_V_ERR_CRL_NOT_YET_VALID:
1285       return SSL_AD_CERTIFICATE_EXPIRED;
1286 
1287     case X509_V_ERR_CERT_REVOKED:
1288       return SSL_AD_CERTIFICATE_REVOKED;
1289 
1290     case X509_V_ERR_UNSPECIFIED:
1291     case X509_V_ERR_OUT_OF_MEM:
1292     case X509_V_ERR_INVALID_CALL:
1293     case X509_V_ERR_STORE_LOOKUP:
1294       return SSL_AD_INTERNAL_ERROR;
1295 
1296     case X509_V_ERR_APPLICATION_VERIFICATION:
1297       return SSL_AD_HANDSHAKE_FAILURE;
1298 
1299     case X509_V_ERR_INVALID_PURPOSE:
1300       return SSL_AD_UNSUPPORTED_CERTIFICATE;
1301 
1302     default:
1303       return SSL_AD_CERTIFICATE_UNKNOWN;
1304   }
1305 }
1306