diff options
| author | jsing <> | 2022-08-17 07:39:19 +0000 |
|---|---|---|
| committer | jsing <> | 2022-08-17 07:39:19 +0000 |
| commit | 5f133a78eec6f3a2549c066b9a561d6350d6e07a (patch) | |
| tree | d4b208572f46a7c773aecb3e2d410aeaae5e817a /src/lib/libssl/tls_lib.c | |
| parent | 726478d55d7f47f50feb22b91bfcb268950310ac (diff) | |
| download | openbsd-5f133a78eec6f3a2549c066b9a561d6350d6e07a.tar.gz openbsd-5f133a78eec6f3a2549c066b9a561d6350d6e07a.tar.bz2 openbsd-5f133a78eec6f3a2549c066b9a561d6350d6e07a.zip | |
Deduplicate peer certificate chain processing code.
Rather than reimplement this in each TLS client and server, deduplicate it
into a single function. Furthermore, rather than dealing with the API
hazard that is SSL_get_peer_cert_chain() in this code, simply produce two
chains - one that has the leaf and one that does not.
SSL_get_peer_cert_chain() can then return the appropriate one.
This also moves the peer cert chain from the SSL_SESSION to the
SSL_HANDSHAKE, which makes more sense since it is not available on
resumption.
ok tb@
Diffstat (limited to 'src/lib/libssl/tls_lib.c')
| -rw-r--r-- | src/lib/libssl/tls_lib.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/lib/libssl/tls_lib.c b/src/lib/libssl/tls_lib.c new file mode 100644 index 0000000000..48db071803 --- /dev/null +++ b/src/lib/libssl/tls_lib.c | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | /* $OpenBSD: tls_lib.c,v 1.1 2022/08/17 07:39:19 jsing Exp $ */ | ||
| 2 | /* | ||
| 3 | * Copyright (c) 2019, 2021 Joel Sing <jsing@openbsd.org> | ||
| 4 | * | ||
| 5 | * Permission to use, copy, modify, and distribute this software for any | ||
| 6 | * purpose with or without fee is hereby granted, provided that the above | ||
| 7 | * copyright notice and this permission notice appear in all copies. | ||
| 8 | * | ||
| 9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
| 10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
| 11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
| 12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
| 13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include "ssl_locl.h" | ||
| 19 | |||
| 20 | int | ||
| 21 | tls_process_peer_certs(SSL *s, STACK_OF(X509) *peer_certs) | ||
| 22 | { | ||
| 23 | STACK_OF(X509) *peer_certs_no_leaf; | ||
| 24 | X509 *peer_cert = NULL; | ||
| 25 | EVP_PKEY *pkey; | ||
| 26 | int cert_type; | ||
| 27 | int ret = 0; | ||
| 28 | |||
| 29 | if (sk_X509_num(peer_certs) < 1) | ||
| 30 | goto err; | ||
| 31 | peer_cert = sk_X509_value(peer_certs, 0); | ||
| 32 | X509_up_ref(peer_cert); | ||
| 33 | |||
| 34 | if ((pkey = X509_get0_pubkey(peer_cert)) == NULL) { | ||
| 35 | SSLerror(s, SSL_R_NO_PUBLICKEY); | ||
| 36 | goto err; | ||
| 37 | } | ||
| 38 | if (EVP_PKEY_missing_parameters(pkey)) { | ||
| 39 | SSLerror(s, SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); | ||
| 40 | goto err; | ||
| 41 | } | ||
| 42 | if ((cert_type = ssl_cert_type(pkey)) < 0) { | ||
| 43 | SSLerror(s, SSL_R_UNKNOWN_CERTIFICATE_TYPE); | ||
| 44 | goto err; | ||
| 45 | } | ||
| 46 | |||
| 47 | s->session->peer_cert_type = cert_type; | ||
| 48 | |||
| 49 | X509_free(s->session->peer_cert); | ||
| 50 | s->session->peer_cert = peer_cert; | ||
| 51 | peer_cert = NULL; | ||
| 52 | |||
| 53 | sk_X509_pop_free(s->s3->hs.peer_certs, X509_free); | ||
| 54 | if ((s->s3->hs.peer_certs = X509_chain_up_ref(peer_certs)) == NULL) | ||
| 55 | goto err; | ||
| 56 | |||
| 57 | if ((peer_certs_no_leaf = X509_chain_up_ref(peer_certs)) == NULL) | ||
| 58 | goto err; | ||
| 59 | X509_free(sk_X509_shift(peer_certs_no_leaf)); | ||
| 60 | sk_X509_pop_free(s->s3->hs.peer_certs_no_leaf, X509_free); | ||
| 61 | s->s3->hs.peer_certs_no_leaf = peer_certs_no_leaf; | ||
| 62 | |||
| 63 | ret = 1; | ||
| 64 | err: | ||
| 65 | X509_free(peer_cert); | ||
| 66 | |||
| 67 | return ret; | ||
| 68 | } | ||
