xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/ssl/ssl_x509.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young ([email protected]).
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 ([email protected]).
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 ([email protected])"
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 ([email protected])"
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  *    [email protected].
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  * ([email protected]).  This product includes software written by Tim
107  * Hudson ([email protected]).
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 "internal.h"
153 #include "../crypto/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->default_credential->ClearIntermediateCerts();
202   for (X509 *x509 : chain) {
203     UniquePtr<CRYPTO_BUFFER> buffer = x509_to_buffer(x509);
204     if (!buffer ||
205         !cert->default_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(ctx.get(),
348                                   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(ctx.get(),
353                                   ssl->server ? "ssl_client" : "ssl_server") ||
354       // Anything non-default in "param" should overwrite anything in the ctx.
355       !X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(ctx.get()),
356                               hs->config->param) ||
357       // ClientHelloOuter connections use a different name.
358       (name_len != 0 &&
359        !X509_VERIFY_PARAM_set1_host(X509_STORE_CTX_get0_param(ctx.get()), name,
360                                     name_len))) {
361     OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
362     return false;
363   }
364 
365   if (hs->config->verify_callback) {
366     X509_STORE_CTX_set_verify_cb(ctx.get(), hs->config->verify_callback);
367   }
368 
369   int verify_ret;
370   if (ssl_ctx->app_verify_callback != nullptr) {
371     verify_ret =
372         ssl_ctx->app_verify_callback(ctx.get(), ssl_ctx->app_verify_arg);
373   } else {
374     verify_ret = X509_verify_cert(ctx.get());
375   }
376 
377   session->verify_result = X509_STORE_CTX_get_error(ctx.get());
378 
379   // If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result.
380   if (verify_ret <= 0 && hs->config->verify_mode != SSL_VERIFY_NONE) {
381     *out_alert = SSL_alert_from_verify_result(session->verify_result);
382     return false;
383   }
384 
385   ERR_clear_error();
386   return true;
387 }
388 
ssl_crypto_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE * hs)389 static void ssl_crypto_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {
390   sk_X509_NAME_pop_free(hs->cached_x509_ca_names, X509_NAME_free);
391   hs->cached_x509_ca_names = nullptr;
392 }
393 
ssl_crypto_x509_ssl_new(SSL_HANDSHAKE * hs)394 static bool ssl_crypto_x509_ssl_new(SSL_HANDSHAKE *hs) {
395   hs->config->param = X509_VERIFY_PARAM_new();
396   if (hs->config->param == nullptr) {
397     return false;
398   }
399   X509_VERIFY_PARAM_inherit(hs->config->param, hs->ssl->ctx->param);
400   return true;
401 }
402 
ssl_crypto_x509_ssl_flush_cached_client_CA(SSL_CONFIG * cfg)403 static void ssl_crypto_x509_ssl_flush_cached_client_CA(SSL_CONFIG *cfg) {
404   sk_X509_NAME_pop_free(cfg->cached_x509_client_CA, X509_NAME_free);
405   cfg->cached_x509_client_CA = nullptr;
406 }
407 
ssl_crypto_x509_ssl_config_free(SSL_CONFIG * cfg)408 static void ssl_crypto_x509_ssl_config_free(SSL_CONFIG *cfg) {
409   sk_X509_NAME_pop_free(cfg->cached_x509_client_CA, X509_NAME_free);
410   cfg->cached_x509_client_CA = nullptr;
411   X509_VERIFY_PARAM_free(cfg->param);
412 }
413 
ssl_crypto_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE * hs)414 static bool ssl_crypto_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE *hs) {
415   // Only build a chain if the feature isn't disabled, the default credential
416   // exists but has no intermediates configured.
417   SSL *ssl = hs->ssl;
418   SSL_CREDENTIAL *cred = hs->config->cert->default_credential.get();
419   if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || !cred->IsComplete() ||
420       sk_CRYPTO_BUFFER_num(cred->chain.get()) != 1) {
421     return true;
422   }
423 
424   UniquePtr<X509> leaf(
425       X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(cred->chain.get(), 0)));
426   if (!leaf) {
427     OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
428     return false;
429   }
430 
431   UniquePtr<X509_STORE_CTX> ctx(X509_STORE_CTX_new());
432   if (!ctx || !X509_STORE_CTX_init(ctx.get(), ssl->ctx->cert_store, leaf.get(),
433                                    nullptr)) {
434     OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
435     return false;
436   }
437 
438   // Attempt to build a chain, ignoring the result.
439   X509_verify_cert(ctx.get());
440   ERR_clear_error();
441 
442   // Remove the leaf from the generated chain.
443   UniquePtr<STACK_OF(X509)> chain(X509_STORE_CTX_get1_chain(ctx.get()));
444   if (!chain) {
445     return false;
446   }
447   X509_free(sk_X509_shift(chain.get()));
448 
449   return SSL_set1_chain(ssl, chain.get());
450 }
451 
ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX * ctx)452 static void ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {
453   sk_X509_NAME_pop_free(ctx->cached_x509_client_CA, X509_NAME_free);
454   ctx->cached_x509_client_CA = nullptr;
455 }
456 
ssl_crypto_x509_ssl_ctx_new(SSL_CTX * ctx)457 static bool ssl_crypto_x509_ssl_ctx_new(SSL_CTX *ctx) {
458   ctx->cert_store = X509_STORE_new();
459   ctx->param = X509_VERIFY_PARAM_new();
460   return (ctx->cert_store != nullptr && ctx->param != nullptr);
461 }
462 
ssl_crypto_x509_ssl_ctx_free(SSL_CTX * ctx)463 static void ssl_crypto_x509_ssl_ctx_free(SSL_CTX *ctx) {
464   ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx);
465   X509_VERIFY_PARAM_free(ctx->param);
466   X509_STORE_free(ctx->cert_store);
467 }
468 
469 const SSL_X509_METHOD ssl_crypto_x509_method = {
470   ssl_crypto_x509_check_client_CA_list,
471   ssl_crypto_x509_cert_clear,
472   ssl_crypto_x509_cert_free,
473   ssl_crypto_x509_cert_dup,
474   ssl_crypto_x509_cert_flush_cached_chain,
475   ssl_crypto_x509_cert_flush_cached_leaf,
476   ssl_crypto_x509_session_cache_objects,
477   ssl_crypto_x509_session_dup,
478   ssl_crypto_x509_session_clear,
479   ssl_crypto_x509_session_verify_cert_chain,
480   ssl_crypto_x509_hs_flush_cached_ca_names,
481   ssl_crypto_x509_ssl_new,
482   ssl_crypto_x509_ssl_config_free,
483   ssl_crypto_x509_ssl_flush_cached_client_CA,
484   ssl_crypto_x509_ssl_auto_chain_if_needed,
485   ssl_crypto_x509_ssl_ctx_new,
486   ssl_crypto_x509_ssl_ctx_free,
487   ssl_crypto_x509_ssl_ctx_flush_cached_client_CA,
488 };
489 
490 BSSL_NAMESPACE_END
491 
492 using namespace bssl;
493 
SSL_get_peer_certificate(const SSL * ssl)494 X509 *SSL_get_peer_certificate(const SSL *ssl) {
495   check_ssl_x509_method(ssl);
496   if (ssl == NULL) {
497     return NULL;
498   }
499   SSL_SESSION *session = SSL_get_session(ssl);
500   if (session == NULL || session->x509_peer == NULL) {
501     return NULL;
502   }
503   X509_up_ref(session->x509_peer);
504   return session->x509_peer;
505 }
506 
STACK_OF(X509)507 STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
508   check_ssl_x509_method(ssl);
509   if (ssl == nullptr) {
510     return nullptr;
511   }
512   SSL_SESSION *session = SSL_get_session(ssl);
513   if (session == nullptr) {
514     return nullptr;
515   }
516 
517   // OpenSSL historically didn't include the leaf certificate in the returned
518   // certificate chain, but only for servers.
519   return ssl->server ? session->x509_chain_without_leaf : session->x509_chain;
520 }
521 
STACK_OF(X509)522 STACK_OF(X509) *SSL_get_peer_full_cert_chain(const SSL *ssl) {
523   check_ssl_x509_method(ssl);
524   SSL_SESSION *session = SSL_get_session(ssl);
525   if (session == NULL) {
526     return NULL;
527   }
528 
529   return session->x509_chain;
530 }
531 
SSL_CTX_set_purpose(SSL_CTX * ctx,int purpose)532 int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
533   check_ssl_ctx_x509_method(ctx);
534   return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
535 }
536 
SSL_set_purpose(SSL * ssl,int purpose)537 int SSL_set_purpose(SSL *ssl, int purpose) {
538   check_ssl_x509_method(ssl);
539   if (!ssl->config) {
540     return 0;
541   }
542   return X509_VERIFY_PARAM_set_purpose(ssl->config->param, purpose);
543 }
544 
SSL_CTX_set_trust(SSL_CTX * ctx,int trust)545 int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
546   check_ssl_ctx_x509_method(ctx);
547   return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
548 }
549 
SSL_set_trust(SSL * ssl,int trust)550 int SSL_set_trust(SSL *ssl, int trust) {
551   check_ssl_x509_method(ssl);
552   if (!ssl->config) {
553     return 0;
554   }
555   return X509_VERIFY_PARAM_set_trust(ssl->config->param, trust);
556 }
557 
SSL_CTX_set1_param(SSL_CTX * ctx,const X509_VERIFY_PARAM * param)558 int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
559   check_ssl_ctx_x509_method(ctx);
560   return X509_VERIFY_PARAM_set1(ctx->param, param);
561 }
562 
SSL_set1_param(SSL * ssl,const X509_VERIFY_PARAM * param)563 int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
564   check_ssl_x509_method(ssl);
565   if (!ssl->config) {
566     return 0;
567   }
568   return X509_VERIFY_PARAM_set1(ssl->config->param, param);
569 }
570 
SSL_CTX_get0_param(SSL_CTX * ctx)571 X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) {
572   check_ssl_ctx_x509_method(ctx);
573   return ctx->param;
574 }
575 
SSL_get0_param(SSL * ssl)576 X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) {
577   check_ssl_x509_method(ssl);
578   if (!ssl->config) {
579     assert(ssl->config);
580     return 0;
581   }
582   return ssl->config->param;
583 }
584 
SSL_get_verify_depth(const SSL * ssl)585 int SSL_get_verify_depth(const SSL *ssl) {
586   check_ssl_x509_method(ssl);
587   if (!ssl->config) {
588     assert(ssl->config);
589     return 0;
590   }
591   return X509_VERIFY_PARAM_get_depth(ssl->config->param);
592 }
593 
SSL_get_verify_callback(const SSL * ssl)594 int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
595   check_ssl_x509_method(ssl);
596   if (!ssl->config) {
597     assert(ssl->config);
598     return 0;
599   }
600   return ssl->config->verify_callback;
601 }
602 
SSL_CTX_get_verify_mode(const SSL_CTX * ctx)603 int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) {
604   check_ssl_ctx_x509_method(ctx);
605   return ctx->verify_mode;
606 }
607 
SSL_CTX_get_verify_depth(const SSL_CTX * ctx)608 int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
609   check_ssl_ctx_x509_method(ctx);
610   return X509_VERIFY_PARAM_get_depth(ctx->param);
611 }
612 
SSL_CTX_get_verify_callback(const SSL_CTX * ctx)613 int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
614     int ok, X509_STORE_CTX *store_ctx) {
615   check_ssl_ctx_x509_method(ctx);
616   return ctx->default_verify_callback;
617 }
618 
SSL_set_verify(SSL * ssl,int mode,int (* callback)(int ok,X509_STORE_CTX * store_ctx))619 void SSL_set_verify(SSL *ssl, int mode,
620                     int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
621   check_ssl_x509_method(ssl);
622   if (!ssl->config) {
623     return;
624   }
625   ssl->config->verify_mode = mode;
626   if (callback != NULL) {
627     ssl->config->verify_callback = callback;
628   }
629 }
630 
SSL_set_verify_depth(SSL * ssl,int depth)631 void SSL_set_verify_depth(SSL *ssl, int depth) {
632   check_ssl_x509_method(ssl);
633   if (!ssl->config) {
634     return;
635   }
636   X509_VERIFY_PARAM_set_depth(ssl->config->param, depth);
637 }
638 
SSL_CTX_set_cert_verify_callback(SSL_CTX * ctx,int (* cb)(X509_STORE_CTX * store_ctx,void * arg),void * arg)639 void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
640                                       int (*cb)(X509_STORE_CTX *store_ctx,
641                                                 void *arg),
642                                       void *arg) {
643   check_ssl_ctx_x509_method(ctx);
644   ctx->app_verify_callback = cb;
645   ctx->app_verify_arg = arg;
646 }
647 
SSL_CTX_set_verify(SSL_CTX * ctx,int mode,int (* cb)(int,X509_STORE_CTX *))648 void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
649                         int (*cb)(int, X509_STORE_CTX *)) {
650   check_ssl_ctx_x509_method(ctx);
651   ctx->verify_mode = mode;
652   ctx->default_verify_callback = cb;
653 }
654 
SSL_CTX_set_verify_depth(SSL_CTX * ctx,int depth)655 void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
656   check_ssl_ctx_x509_method(ctx);
657   X509_VERIFY_PARAM_set_depth(ctx->param, depth);
658 }
659 
SSL_CTX_set_default_verify_paths(SSL_CTX * ctx)660 int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
661   check_ssl_ctx_x509_method(ctx);
662   return X509_STORE_set_default_paths(ctx->cert_store);
663 }
664 
SSL_CTX_load_verify_locations(SSL_CTX * ctx,const char * ca_file,const char * ca_dir)665 int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
666                                   const char *ca_dir) {
667   check_ssl_ctx_x509_method(ctx);
668   return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
669 }
670 
SSL_get_verify_result(const SSL * ssl)671 long SSL_get_verify_result(const SSL *ssl) {
672   check_ssl_x509_method(ssl);
673   SSL_SESSION *session = SSL_get_session(ssl);
674   if (session == NULL) {
675     return X509_V_ERR_INVALID_CALL;
676   }
677   return session->verify_result;
678 }
679 
SSL_CTX_get_cert_store(const SSL_CTX * ctx)680 X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
681   check_ssl_ctx_x509_method(ctx);
682   return ctx->cert_store;
683 }
684 
SSL_CTX_set_cert_store(SSL_CTX * ctx,X509_STORE * store)685 void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
686   check_ssl_ctx_x509_method(ctx);
687   X509_STORE_free(ctx->cert_store);
688   ctx->cert_store = store;
689 }
690 
ssl_use_certificate(CERT * cert,X509 * x)691 static int ssl_use_certificate(CERT *cert, X509 *x) {
692   if (x == NULL) {
693     OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
694     return 0;
695   }
696 
697   UniquePtr<CRYPTO_BUFFER> buffer = x509_to_buffer(x);
698   if (!buffer) {
699     return 0;
700   }
701 
702   return ssl_set_cert(cert, std::move(buffer));
703 }
704 
SSL_use_certificate(SSL * ssl,X509 * x)705 int SSL_use_certificate(SSL *ssl, X509 *x) {
706   check_ssl_x509_method(ssl);
707   if (!ssl->config) {
708     return 0;
709   }
710   return ssl_use_certificate(ssl->config->cert.get(), x);
711 }
712 
SSL_CTX_use_certificate(SSL_CTX * ctx,X509 * x)713 int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) {
714   check_ssl_ctx_x509_method(ctx);
715   return ssl_use_certificate(ctx->cert.get(), x);
716 }
717 
718 // ssl_cert_cache_leaf_cert sets |cert->x509_leaf|, if currently NULL, from the
719 // first element of |cert->chain|.
ssl_cert_cache_leaf_cert(CERT * cert)720 static int ssl_cert_cache_leaf_cert(CERT *cert) {
721   assert(cert->x509_method);
722 
723   const SSL_CREDENTIAL *cred = cert->default_credential.get();
724   if (cert->x509_leaf != NULL ||
725       cred->chain == NULL) {
726     return 1;
727   }
728 
729   CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cred->chain.get(), 0);
730   if (!leaf) {
731     return 1;
732   }
733 
734   cert->x509_leaf = X509_parse_from_buffer(leaf);
735   return cert->x509_leaf != NULL;
736 }
737 
ssl_cert_get0_leaf(CERT * cert)738 static X509 *ssl_cert_get0_leaf(CERT *cert) {
739   if (cert->x509_leaf == NULL &&
740       !ssl_cert_cache_leaf_cert(cert)) {
741     return NULL;
742   }
743 
744   return cert->x509_leaf;
745 }
746 
SSL_get_certificate(const SSL * ssl)747 X509 *SSL_get_certificate(const SSL *ssl) {
748   check_ssl_x509_method(ssl);
749   if (!ssl->config) {
750     assert(ssl->config);
751     return 0;
752   }
753   return ssl_cert_get0_leaf(ssl->config->cert.get());
754 }
755 
SSL_CTX_get0_certificate(const SSL_CTX * ctx)756 X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
757   check_ssl_ctx_x509_method(ctx);
758   MutexWriteLock lock(const_cast<CRYPTO_MUTEX*>(&ctx->lock));
759   return ssl_cert_get0_leaf(ctx->cert.get());
760 }
761 
ssl_cert_add1_chain_cert(CERT * cert,X509 * x509)762 static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
763   assert(cert->x509_method);
764 
765   UniquePtr<CRYPTO_BUFFER> buffer = x509_to_buffer(x509);
766   if (!buffer ||
767       !cert->default_credential->AppendIntermediateCert(std::move(buffer))) {
768     return 0;
769   }
770 
771   ssl_crypto_x509_cert_flush_cached_chain(cert);
772   return 1;
773 }
774 
ssl_cert_add0_chain_cert(CERT * cert,X509 * x509)775 static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
776   if (!ssl_cert_add1_chain_cert(cert, x509)) {
777     return 0;
778   }
779 
780   X509_free(cert->x509_stash);
781   cert->x509_stash = x509;
782   return 1;
783 }
784 
SSL_CTX_set0_chain(SSL_CTX * ctx,STACK_OF (X509)* chain)785 int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
786   check_ssl_ctx_x509_method(ctx);
787   if (!ssl_cert_set1_chain(ctx->cert.get(), chain)) {
788     return 0;
789   }
790   sk_X509_pop_free(chain, X509_free);
791   return 1;
792 }
793 
SSL_CTX_set1_chain(SSL_CTX * ctx,STACK_OF (X509)* chain)794 int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
795   check_ssl_ctx_x509_method(ctx);
796   return ssl_cert_set1_chain(ctx->cert.get(), chain);
797 }
798 
SSL_set0_chain(SSL * ssl,STACK_OF (X509)* chain)799 int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
800   check_ssl_x509_method(ssl);
801   if (!ssl->config) {
802     return 0;
803   }
804   if (!ssl_cert_set1_chain(ssl->config->cert.get(), chain)) {
805     return 0;
806   }
807   sk_X509_pop_free(chain, X509_free);
808   return 1;
809 }
810 
SSL_set1_chain(SSL * ssl,STACK_OF (X509)* chain)811 int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
812   check_ssl_x509_method(ssl);
813   if (!ssl->config) {
814     return 0;
815   }
816   return ssl_cert_set1_chain(ssl->config->cert.get(), chain);
817 }
818 
SSL_CTX_add0_chain_cert(SSL_CTX * ctx,X509 * x509)819 int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
820   check_ssl_ctx_x509_method(ctx);
821   return ssl_cert_add0_chain_cert(ctx->cert.get(), x509);
822 }
823 
SSL_CTX_add1_chain_cert(SSL_CTX * ctx,X509 * x509)824 int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
825   check_ssl_ctx_x509_method(ctx);
826   return ssl_cert_add1_chain_cert(ctx->cert.get(), x509);
827 }
828 
SSL_CTX_add_extra_chain_cert(SSL_CTX * ctx,X509 * x509)829 int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
830   check_ssl_ctx_x509_method(ctx);
831   return SSL_CTX_add0_chain_cert(ctx, x509);
832 }
833 
SSL_add0_chain_cert(SSL * ssl,X509 * x509)834 int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
835   check_ssl_x509_method(ssl);
836   if (!ssl->config) {
837     return 0;
838   }
839   return ssl_cert_add0_chain_cert(ssl->config->cert.get(), x509);
840 }
841 
SSL_add1_chain_cert(SSL * ssl,X509 * x509)842 int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
843   check_ssl_x509_method(ssl);
844   if (!ssl->config) {
845     return 0;
846   }
847   return ssl_cert_add1_chain_cert(ssl->config->cert.get(), x509);
848 }
849 
SSL_CTX_clear_chain_certs(SSL_CTX * ctx)850 int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
851   check_ssl_ctx_x509_method(ctx);
852   return SSL_CTX_set0_chain(ctx, NULL);
853 }
854 
SSL_CTX_clear_extra_chain_certs(SSL_CTX * ctx)855 int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
856   check_ssl_ctx_x509_method(ctx);
857   return SSL_CTX_clear_chain_certs(ctx);
858 }
859 
SSL_clear_chain_certs(SSL * ssl)860 int SSL_clear_chain_certs(SSL *ssl) {
861   check_ssl_x509_method(ssl);
862   return SSL_set0_chain(ssl, NULL);
863 }
864 
865 // ssl_cert_cache_chain_certs fills in |cert->x509_chain| from elements 1.. of
866 // |cert->chain|.
ssl_cert_cache_chain_certs(CERT * cert)867 static int ssl_cert_cache_chain_certs(CERT *cert) {
868   assert(cert->x509_method);
869 
870   const SSL_CREDENTIAL *cred = cert->default_credential.get();
871   if (cert->x509_chain != nullptr ||
872       cred->chain == nullptr ||
873       sk_CRYPTO_BUFFER_num(cred->chain.get()) < 2) {
874     return 1;
875   }
876 
877   UniquePtr<STACK_OF(X509)> chain(sk_X509_new_null());
878   if (!chain) {
879     return 0;
880   }
881 
882   for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cred->chain.get()); i++) {
883     CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(cred->chain.get(), i);
884     UniquePtr<X509> x509(X509_parse_from_buffer(buffer));
885     if (!x509 ||
886         !PushToStack(chain.get(), std::move(x509))) {
887       return 0;
888     }
889   }
890 
891   cert->x509_chain = chain.release();
892   return 1;
893 }
894 
SSL_CTX_get0_chain_certs(const SSL_CTX * ctx,STACK_OF (X509)** out_chain)895 int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
896   check_ssl_ctx_x509_method(ctx);
897   MutexWriteLock lock(const_cast<CRYPTO_MUTEX*>(&ctx->lock));
898   if (!ssl_cert_cache_chain_certs(ctx->cert.get())) {
899     *out_chain = NULL;
900     return 0;
901   }
902 
903   *out_chain = ctx->cert->x509_chain;
904   return 1;
905 }
906 
SSL_CTX_get_extra_chain_certs(const SSL_CTX * ctx,STACK_OF (X509)** out_chain)907 int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
908                                   STACK_OF(X509) **out_chain) {
909   return SSL_CTX_get0_chain_certs(ctx, out_chain);
910 }
911 
SSL_get0_chain_certs(const SSL * ssl,STACK_OF (X509)** out_chain)912 int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
913   check_ssl_x509_method(ssl);
914   if (!ssl->config) {
915     assert(ssl->config);
916     return 0;
917   }
918   if (!ssl_cert_cache_chain_certs(ssl->config->cert.get())) {
919     *out_chain = NULL;
920     return 0;
921   }
922 
923   *out_chain = ssl->config->cert->x509_chain;
924   return 1;
925 }
926 
d2i_SSL_SESSION_bio(BIO * bio,SSL_SESSION ** out)927 SSL_SESSION *d2i_SSL_SESSION_bio(BIO *bio, SSL_SESSION **out) {
928   uint8_t *data;
929   size_t len;
930   if (!BIO_read_asn1(bio, &data, &len, 1024 * 1024)) {
931     return 0;
932   }
933   bssl::UniquePtr<uint8_t> free_data(data);
934   const uint8_t *ptr = data;
935   return d2i_SSL_SESSION(out, &ptr, static_cast<long>(len));
936 }
937 
i2d_SSL_SESSION_bio(BIO * bio,const SSL_SESSION * session)938 int i2d_SSL_SESSION_bio(BIO *bio, const SSL_SESSION *session) {
939   uint8_t *data;
940   size_t len;
941   if (!SSL_SESSION_to_bytes(session, &data, &len)) {
942     return 0;
943   }
944   bssl::UniquePtr<uint8_t> free_data(data);
945   return BIO_write_all(bio, data, len);
946 }
947 
IMPLEMENT_PEM_rw(SSL_SESSION,SSL_SESSION,PEM_STRING_SSL_SESSION,SSL_SESSION)948 IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)
949 
950 SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
951   if (length < 0) {
952     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
953     return NULL;
954   }
955 
956   CBS cbs;
957   CBS_init(&cbs, *pp, length);
958 
959   UniquePtr<SSL_SESSION> ret = SSL_SESSION_parse(&cbs, &ssl_crypto_x509_method,
960                                                  NULL /* no buffer pool */);
961   if (!ret) {
962     return NULL;
963   }
964 
965   if (a) {
966     SSL_SESSION_free(*a);
967     *a = ret.get();
968   }
969   *pp = CBS_data(&cbs);
970   return ret.release();
971 }
972 
STACK_OF(X509_NAME)973 STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
974   // TODO(https://crbug.com/boringssl/407): |X509_NAME_dup| should be const.
975   auto name_dup = [](const X509_NAME *name) {
976     return X509_NAME_dup(const_cast<X509_NAME *>(name));
977   };
978   return sk_X509_NAME_deep_copy(list, name_dup, X509_NAME_free);
979 }
980 
set_client_CA_list(UniquePtr<STACK_OF (CRYPTO_BUFFER)> * ca_list,const STACK_OF (X509_NAME)* name_list,CRYPTO_BUFFER_POOL * pool)981 static void set_client_CA_list(UniquePtr<STACK_OF(CRYPTO_BUFFER)> *ca_list,
982                                const STACK_OF(X509_NAME) *name_list,
983                                CRYPTO_BUFFER_POOL *pool) {
984   UniquePtr<STACK_OF(CRYPTO_BUFFER)> buffers(sk_CRYPTO_BUFFER_new_null());
985   if (!buffers) {
986     return;
987   }
988 
989   for (X509_NAME *name : name_list) {
990     uint8_t *outp = NULL;
991     int len = i2d_X509_NAME(name, &outp);
992     if (len < 0) {
993       return;
994     }
995 
996     UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(outp, len, pool));
997     OPENSSL_free(outp);
998     if (!buffer ||
999         !PushToStack(buffers.get(), std::move(buffer))) {
1000       return;
1001     }
1002   }
1003 
1004   *ca_list = std::move(buffers);
1005 }
1006 
SSL_set_client_CA_list(SSL * ssl,STACK_OF (X509_NAME)* name_list)1007 void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
1008   check_ssl_x509_method(ssl);
1009   if (!ssl->config) {
1010     return;
1011   }
1012   ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl->config.get());
1013   set_client_CA_list(&ssl->config->client_CA, name_list, ssl->ctx->pool);
1014   sk_X509_NAME_pop_free(name_list, X509_NAME_free);
1015 }
1016 
SSL_CTX_set_client_CA_list(SSL_CTX * ctx,STACK_OF (X509_NAME)* name_list)1017 void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
1018   check_ssl_ctx_x509_method(ctx);
1019   ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx);
1020   set_client_CA_list(&ctx->client_CA, name_list, ctx->pool);
1021   sk_X509_NAME_pop_free(name_list, X509_NAME_free);
1022 }
1023 
STACK_OF(X509_NAME)1024 static STACK_OF(X509_NAME) *
1025     buffer_names_to_x509(const STACK_OF(CRYPTO_BUFFER) *names,
1026                          STACK_OF(X509_NAME) **cached) {
1027   if (names == NULL) {
1028     return NULL;
1029   }
1030 
1031   if (*cached != NULL) {
1032     return *cached;
1033   }
1034 
1035   UniquePtr<STACK_OF(X509_NAME)> new_cache(sk_X509_NAME_new_null());
1036   if (!new_cache) {
1037     return NULL;
1038   }
1039 
1040   for (const CRYPTO_BUFFER *buffer : names) {
1041     const uint8_t *inp = CRYPTO_BUFFER_data(buffer);
1042     UniquePtr<X509_NAME> name(
1043         d2i_X509_NAME(nullptr, &inp, CRYPTO_BUFFER_len(buffer)));
1044     if (!name ||
1045         inp != CRYPTO_BUFFER_data(buffer) + CRYPTO_BUFFER_len(buffer) ||
1046         !PushToStack(new_cache.get(), std::move(name))) {
1047       return NULL;
1048     }
1049   }
1050 
1051   *cached = new_cache.release();
1052   return *cached;
1053 }
1054 
STACK_OF(X509_NAME)1055 STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
1056   check_ssl_x509_method(ssl);
1057   if (!ssl->config) {
1058     assert(ssl->config);
1059     return NULL;
1060   }
1061   // For historical reasons, this function is used both to query configuration
1062   // state on a server as well as handshake state on a client. However, whether
1063   // |ssl| is a client or server is not known until explicitly configured with
1064   // |SSL_set_connect_state|. If |do_handshake| is NULL, |ssl| is in an
1065   // indeterminate mode and |ssl->server| is unset.
1066   if (ssl->do_handshake != NULL && !ssl->server) {
1067     if (ssl->s3->hs != NULL) {
1068       return buffer_names_to_x509(ssl->s3->hs->ca_names.get(),
1069                                   &ssl->s3->hs->cached_x509_ca_names);
1070     }
1071 
1072     return NULL;
1073   }
1074 
1075   if (ssl->config->client_CA != NULL) {
1076     return buffer_names_to_x509(
1077         ssl->config->client_CA.get(),
1078         (STACK_OF(X509_NAME) **)&ssl->config->cached_x509_client_CA);
1079   }
1080   return SSL_CTX_get_client_CA_list(ssl->ctx.get());
1081 }
1082 
STACK_OF(X509_NAME)1083 STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
1084   check_ssl_ctx_x509_method(ctx);
1085   // This is a logically const operation that may be called on multiple threads,
1086   // so it needs to lock around updating |cached_x509_client_CA|.
1087   MutexWriteLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock));
1088   return buffer_names_to_x509(
1089       ctx->client_CA.get(),
1090       const_cast<STACK_OF(X509_NAME) **>(&ctx->cached_x509_client_CA));
1091 }
1092 
add_client_CA(UniquePtr<STACK_OF (CRYPTO_BUFFER)> * names,X509 * x509,CRYPTO_BUFFER_POOL * pool)1093 static int add_client_CA(UniquePtr<STACK_OF(CRYPTO_BUFFER)> *names, X509 *x509,
1094                          CRYPTO_BUFFER_POOL *pool) {
1095   if (x509 == NULL) {
1096     return 0;
1097   }
1098 
1099   uint8_t *outp = NULL;
1100   int len = i2d_X509_NAME(X509_get_subject_name(x509), &outp);
1101   if (len < 0) {
1102     return 0;
1103   }
1104 
1105   UniquePtr<CRYPTO_BUFFER> buffer(CRYPTO_BUFFER_new(outp, len, pool));
1106   OPENSSL_free(outp);
1107   if (!buffer) {
1108     return 0;
1109   }
1110 
1111   int alloced = 0;
1112   if (*names == nullptr) {
1113     names->reset(sk_CRYPTO_BUFFER_new_null());
1114     alloced = 1;
1115 
1116     if (*names == NULL) {
1117       return 0;
1118     }
1119   }
1120 
1121   if (!PushToStack(names->get(), std::move(buffer))) {
1122     if (alloced) {
1123       names->reset();
1124     }
1125     return 0;
1126   }
1127 
1128   return 1;
1129 }
1130 
SSL_add_client_CA(SSL * ssl,X509 * x509)1131 int SSL_add_client_CA(SSL *ssl, X509 *x509) {
1132   check_ssl_x509_method(ssl);
1133   if (!ssl->config) {
1134     return 0;
1135   }
1136   if (!add_client_CA(&ssl->config->client_CA, x509, ssl->ctx->pool)) {
1137     return 0;
1138   }
1139 
1140   ssl_crypto_x509_ssl_flush_cached_client_CA(ssl->config.get());
1141   return 1;
1142 }
1143 
SSL_CTX_add_client_CA(SSL_CTX * ctx,X509 * x509)1144 int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
1145   check_ssl_ctx_x509_method(ctx);
1146   if (!add_client_CA(&ctx->client_CA, x509, ctx->pool)) {
1147     return 0;
1148   }
1149 
1150   ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx);
1151   return 1;
1152 }
1153 
do_client_cert_cb(SSL * ssl,void * arg)1154 static int do_client_cert_cb(SSL *ssl, void *arg) {
1155   // Should only be called during handshake, but check to be sure.
1156   BSSL_CHECK(ssl->config);
1157 
1158   if (ssl->config->cert->default_credential->IsComplete() ||
1159       ssl->ctx->client_cert_cb == nullptr) {
1160     return 1;
1161   }
1162 
1163   X509 *x509 = NULL;
1164   EVP_PKEY *pkey = NULL;
1165   int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
1166   if (ret < 0) {
1167     return -1;
1168   }
1169   UniquePtr<X509> free_x509(x509);
1170   UniquePtr<EVP_PKEY> free_pkey(pkey);
1171 
1172   if (ret != 0) {
1173     if (!SSL_use_certificate(ssl, x509) ||
1174         !SSL_use_PrivateKey(ssl, pkey)) {
1175       return 0;
1176     }
1177   }
1178 
1179   return 1;
1180 }
1181 
SSL_CTX_set_client_cert_cb(SSL_CTX * ctx,int (* cb)(SSL * ssl,X509 ** out_x509,EVP_PKEY ** out_pkey))1182 void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl,
1183                                                         X509 **out_x509,
1184                                                         EVP_PKEY **out_pkey)) {
1185   check_ssl_ctx_x509_method(ctx);
1186   // Emulate the old client certificate callback with the new one.
1187   SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL);
1188   ctx->client_cert_cb = cb;
1189 }
1190 
set_cert_store(X509_STORE ** store_ptr,X509_STORE * new_store,int take_ref)1191 static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store,
1192                           int take_ref) {
1193   X509_STORE_free(*store_ptr);
1194   *store_ptr = new_store;
1195 
1196   if (new_store != NULL && take_ref) {
1197     X509_STORE_up_ref(new_store);
1198   }
1199 
1200   return 1;
1201 }
1202 
SSL_get_ex_data_X509_STORE_CTX_idx(void)1203 int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
1204   // The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the
1205   // reserved app_data slot. Before ex_data was introduced, app_data was used.
1206   // Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data|
1207   // works.
1208   return 0;
1209 }
1210 
SSL_CTX_set0_verify_cert_store(SSL_CTX * ctx,X509_STORE * store)1211 int SSL_CTX_set0_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, 0);
1214 }
1215 
SSL_CTX_set1_verify_cert_store(SSL_CTX * ctx,X509_STORE * store)1216 int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
1217   check_ssl_ctx_x509_method(ctx);
1218   return set_cert_store(&ctx->cert->verify_store, store, 1);
1219 }
1220 
SSL_set0_verify_cert_store(SSL * ssl,X509_STORE * store)1221 int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
1222   check_ssl_x509_method(ssl);
1223   if (!ssl->config) {
1224     return 0;
1225   }
1226   return set_cert_store(&ssl->config->cert->verify_store, store, 0);
1227 }
1228 
SSL_set1_verify_cert_store(SSL * ssl,X509_STORE * store)1229 int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
1230   check_ssl_x509_method(ssl);
1231   if (!ssl->config) {
1232     return 0;
1233   }
1234   return set_cert_store(&ssl->config->cert->verify_store, store, 1);
1235 }
1236 
SSL_set1_host(SSL * ssl,const char * hostname)1237 int SSL_set1_host(SSL *ssl, const char *hostname) {
1238   check_ssl_x509_method(ssl);
1239   if (!ssl->config) {
1240     return 0;
1241   }
1242   return X509_VERIFY_PARAM_set1_host(ssl->config->param, hostname,
1243                                      strlen(hostname));
1244 }
1245 
SSL_set_hostflags(SSL * ssl,unsigned flags)1246 void SSL_set_hostflags(SSL *ssl, unsigned flags) {
1247   check_ssl_x509_method(ssl);
1248   if (!ssl->config) {
1249     return;
1250   }
1251   X509_VERIFY_PARAM_set_hostflags(ssl->config->param, flags);
1252 }
1253 
SSL_alert_from_verify_result(long result)1254 int SSL_alert_from_verify_result(long result) {
1255   switch (result) {
1256     case X509_V_ERR_CERT_CHAIN_TOO_LONG:
1257     case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1258     case X509_V_ERR_INVALID_CA:
1259     case X509_V_ERR_PATH_LENGTH_EXCEEDED:
1260     case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1261     case X509_V_ERR_UNABLE_TO_GET_CRL:
1262     case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
1263     case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1264     case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1265     case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1266       return SSL_AD_UNKNOWN_CA;
1267 
1268     case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
1269     case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
1270     case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
1271     case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
1272     case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
1273     case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
1274     case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
1275     case X509_V_ERR_CERT_UNTRUSTED:
1276     case X509_V_ERR_CERT_REJECTED:
1277     case X509_V_ERR_HOSTNAME_MISMATCH:
1278     case X509_V_ERR_EMAIL_MISMATCH:
1279     case X509_V_ERR_IP_ADDRESS_MISMATCH:
1280       return SSL_AD_BAD_CERTIFICATE;
1281 
1282     case X509_V_ERR_CERT_SIGNATURE_FAILURE:
1283     case X509_V_ERR_CRL_SIGNATURE_FAILURE:
1284       return SSL_AD_DECRYPT_ERROR;
1285 
1286     case X509_V_ERR_CERT_HAS_EXPIRED:
1287     case X509_V_ERR_CERT_NOT_YET_VALID:
1288     case X509_V_ERR_CRL_HAS_EXPIRED:
1289     case X509_V_ERR_CRL_NOT_YET_VALID:
1290       return SSL_AD_CERTIFICATE_EXPIRED;
1291 
1292     case X509_V_ERR_CERT_REVOKED:
1293       return SSL_AD_CERTIFICATE_REVOKED;
1294 
1295     case X509_V_ERR_UNSPECIFIED:
1296     case X509_V_ERR_OUT_OF_MEM:
1297     case X509_V_ERR_INVALID_CALL:
1298     case X509_V_ERR_STORE_LOOKUP:
1299       return SSL_AD_INTERNAL_ERROR;
1300 
1301     case X509_V_ERR_APPLICATION_VERIFICATION:
1302       return SSL_AD_HANDSHAKE_FAILURE;
1303 
1304     case X509_V_ERR_INVALID_PURPOSE:
1305       return SSL_AD_UNSUPPORTED_CERTIFICATE;
1306 
1307     default:
1308       return SSL_AD_CERTIFICATE_UNKNOWN;
1309   }
1310 }
1311