blob: 5796bf4748c095490d6c69b71da52e4533851e4e [file] [log] [blame]
David Benjaminc895d6b2016-08-11 13:26:41 -04001/* Copyright (c) 2016, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <openssl/ssl.h>
16
17#include <assert.h>
18#include <string.h>
19
Robert Sloanb6d070c2017-07-24 08:40:01 -070020#include <utility>
21
David Benjaminc895d6b2016-08-11 13:26:41 -040022#include <openssl/bytestring.h>
23#include <openssl/err.h>
24#include <openssl/hkdf.h>
25#include <openssl/mem.h>
26#include <openssl/stack.h>
27#include <openssl/x509.h>
David Benjaminc895d6b2016-08-11 13:26:41 -040028
Steven Valdez909b19f2016-11-21 15:35:44 -050029#include "../crypto/internal.h"
David Benjaminc895d6b2016-08-11 13:26:41 -040030#include "internal.h"
31
32
Robert Sloanb6d070c2017-07-24 08:40:01 -070033namespace bssl {
34
Robert Sloana27a6a42017-09-05 08:39:28 -070035// kMaxKeyUpdates is the number of consecutive KeyUpdates that will be
36// processed. Without this limit an attacker could force unbounded processing
37// without being able to return application data.
David Benjaminf0c4a6c2016-08-11 13:26:41 -040038static const uint8_t kMaxKeyUpdates = 32;
David Benjaminc895d6b2016-08-11 13:26:41 -040039
Robert Sloan921ef2c2017-10-17 09:02:20 -070040bool tls13_get_cert_verify_signature_input(
41 SSL_HANDSHAKE *hs, Array<uint8_t> *out,
Steven Valdez909b19f2016-11-21 15:35:44 -050042 enum ssl_cert_verify_context_t cert_verify_context) {
Robert Sloanb6d070c2017-07-24 08:40:01 -070043 ScopedCBB cbb;
44 if (!CBB_init(cbb.get(), 64 + 33 + 1 + 2 * EVP_MAX_MD_SIZE)) {
45 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -070046 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -040047 }
48
49 for (size_t i = 0; i < 64; i++) {
Robert Sloanb6d070c2017-07-24 08:40:01 -070050 if (!CBB_add_u8(cbb.get(), 0x20)) {
51 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -070052 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -040053 }
54 }
55
Robert Sloan921ef2c2017-10-17 09:02:20 -070056 Span<const char> context;
Steven Valdez909b19f2016-11-21 15:35:44 -050057 if (cert_verify_context == ssl_cert_verify_server) {
David Benjaminc895d6b2016-08-11 13:26:41 -040058 static const char kContext[] = "TLS 1.3, server CertificateVerify";
Robert Sloan921ef2c2017-10-17 09:02:20 -070059 context = kContext;
Steven Valdez909b19f2016-11-21 15:35:44 -050060 } else if (cert_verify_context == ssl_cert_verify_client) {
David Benjaminc895d6b2016-08-11 13:26:41 -040061 static const char kContext[] = "TLS 1.3, client CertificateVerify";
Robert Sloan921ef2c2017-10-17 09:02:20 -070062 context = kContext;
Steven Valdez909b19f2016-11-21 15:35:44 -050063 } else if (cert_verify_context == ssl_cert_verify_channel_id) {
64 static const char kContext[] = "TLS 1.3, Channel ID";
Robert Sloan921ef2c2017-10-17 09:02:20 -070065 context = kContext;
Steven Valdez909b19f2016-11-21 15:35:44 -050066 } else {
Robert Sloanb6d070c2017-07-24 08:40:01 -070067 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -070068 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -040069 }
70
Robert Sloan921ef2c2017-10-17 09:02:20 -070071 // Note |context| includes the NUL byte separator.
72 if (!CBB_add_bytes(cbb.get(),
73 reinterpret_cast<const uint8_t *>(context.data()),
74 context.size())) {
Robert Sloanb6d070c2017-07-24 08:40:01 -070075 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -070076 return false;
Steven Valdez909b19f2016-11-21 15:35:44 -050077 }
78
79 uint8_t context_hash[EVP_MAX_MD_SIZE];
80 size_t context_hash_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -070081 if (!hs->transcript.GetHash(context_hash, &context_hash_len) ||
82 !CBB_add_bytes(cbb.get(), context_hash, context_hash_len) ||
Robert Sloan921ef2c2017-10-17 09:02:20 -070083 !CBBFinishArray(cbb.get(), out)) {
Robert Sloanb6d070c2017-07-24 08:40:01 -070084 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -070085 return false;
David Benjaminc895d6b2016-08-11 13:26:41 -040086 }
87
Robert Sloan921ef2c2017-10-17 09:02:20 -070088 return true;
David Benjaminc895d6b2016-08-11 13:26:41 -040089}
90
Robert Sloan84377092017-08-14 09:33:19 -070091int tls13_process_certificate(SSL_HANDSHAKE *hs, const SSLMessage &msg,
92 int allow_anonymous) {
Steven Valdeze7531f02016-12-14 13:29:57 -050093 SSL *const ssl = hs->ssl;
Robert Sloan84377092017-08-14 09:33:19 -070094 CBS body = msg.body, context, certificate_list;
95 if (!CBS_get_u8_length_prefixed(&body, &context) ||
Robert Sloanfe7cd212017-08-07 09:03:39 -070096 CBS_len(&context) != 0 ||
Robert Sloan84377092017-08-14 09:33:19 -070097 !CBS_get_u24_length_prefixed(&body, &certificate_list) ||
98 CBS_len(&body) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -070099 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400100 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
101 return 0;
102 }
103
Robert Sloanb6d070c2017-07-24 08:40:01 -0700104 UniquePtr<STACK_OF(CRYPTO_BUFFER)> certs(sk_CRYPTO_BUFFER_new_null());
105 if (!certs) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700106 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez909b19f2016-11-21 15:35:44 -0500107 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700108 return 0;
David Benjaminc895d6b2016-08-11 13:26:41 -0400109 }
110
Robert Sloanb6d070c2017-07-24 08:40:01 -0700111 const bool retain_sha256 =
112 ssl->server && ssl->retain_only_sha256_of_client_certs;
113 UniquePtr<EVP_PKEY> pkey;
Steven Valdez909b19f2016-11-21 15:35:44 -0500114 while (CBS_len(&certificate_list) > 0) {
115 CBS certificate, extensions;
116 if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
Steven Valdeze7531f02016-12-14 13:29:57 -0500117 !CBS_get_u16_length_prefixed(&certificate_list, &extensions) ||
118 CBS_len(&certificate) == 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700119 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdez909b19f2016-11-21 15:35:44 -0500120 OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700121 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500122 }
123
Robert Sloanb6d070c2017-07-24 08:40:01 -0700124 if (sk_CRYPTO_BUFFER_num(certs.get()) == 0) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500125 pkey = ssl_cert_parse_pubkey(&certificate);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700126 if (!pkey) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700127 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Steven Valdeze7531f02016-12-14 13:29:57 -0500128 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700129 return 0;
Steven Valdeze7531f02016-12-14 13:29:57 -0500130 }
Robert Sloana27a6a42017-09-05 08:39:28 -0700131 // TLS 1.3 always uses certificate keys for signing thus the correct
132 // keyUsage is enforced.
Steven Valdeze7531f02016-12-14 13:29:57 -0500133 if (!ssl_cert_check_digital_signature_key_usage(&certificate)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700134 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700135 return 0;
Steven Valdeze7531f02016-12-14 13:29:57 -0500136 }
137
138 if (retain_sha256) {
Robert Sloana27a6a42017-09-05 08:39:28 -0700139 // Retain the hash of the leaf certificate if requested.
Steven Valdeze7531f02016-12-14 13:29:57 -0500140 SHA256(CBS_data(&certificate), CBS_len(&certificate),
Robert Sloana94fe052017-02-21 08:49:28 -0800141 hs->new_session->peer_sha256);
Steven Valdeze7531f02016-12-14 13:29:57 -0500142 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500143 }
144
Robert Sloanfe7cd212017-08-07 09:03:39 -0700145 UniquePtr<CRYPTO_BUFFER> buf(
146 CRYPTO_BUFFER_new_from_CBS(&certificate, ssl->ctx->pool));
147 if (!buf ||
148 !PushToStack(certs.get(), std::move(buf))) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700149 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Steven Valdez909b19f2016-11-21 15:35:44 -0500150 OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700151 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500152 }
153
Robert Sloana27a6a42017-09-05 08:39:28 -0700154 // Parse out the extensions.
155 bool have_status_request = false, have_sct = false;
Steven Valdez909b19f2016-11-21 15:35:44 -0500156 CBS status_request, sct;
157 const SSL_EXTENSION_TYPE ext_types[] = {
158 {TLSEXT_TYPE_status_request, &have_status_request, &status_request},
159 {TLSEXT_TYPE_certificate_timestamp, &have_sct, &sct},
160 };
161
Robert Sloan5d625782017-02-13 09:55:39 -0800162 uint8_t alert = SSL_AD_DECODE_ERROR;
Steven Valdez909b19f2016-11-21 15:35:44 -0500163 if (!ssl_parse_extensions(&extensions, &alert, ext_types,
Robert Sloan69939df2017-01-09 10:53:07 -0800164 OPENSSL_ARRAY_SIZE(ext_types),
165 0 /* reject unknown */)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700166 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700167 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500168 }
169
Robert Sloana27a6a42017-09-05 08:39:28 -0700170 // All Certificate extensions are parsed, but only the leaf extensions are
171 // stored.
Steven Valdez909b19f2016-11-21 15:35:44 -0500172 if (have_status_request) {
173 if (ssl->server || !ssl->ocsp_stapling_enabled) {
174 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700175 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700176 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500177 }
178
179 uint8_t status_type;
180 CBS ocsp_response;
181 if (!CBS_get_u8(&status_request, &status_type) ||
182 status_type != TLSEXT_STATUSTYPE_ocsp ||
183 !CBS_get_u24_length_prefixed(&status_request, &ocsp_response) ||
184 CBS_len(&ocsp_response) == 0 ||
185 CBS_len(&status_request) != 0) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700186 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700187 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500188 }
189
Robert Sloan8f860b12017-08-28 07:37:06 -0700190 if (sk_CRYPTO_BUFFER_num(certs.get()) == 1) {
191 CRYPTO_BUFFER_free(hs->new_session->ocsp_response);
192 hs->new_session->ocsp_response =
193 CRYPTO_BUFFER_new_from_CBS(&ocsp_response, ssl->ctx->pool);
194 if (hs->new_session->ocsp_response == nullptr) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700195 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan8f860b12017-08-28 07:37:06 -0700196 return 0;
197 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500198 }
199 }
200
201 if (have_sct) {
202 if (ssl->server || !ssl->signed_cert_timestamps_enabled) {
203 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700204 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700205 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500206 }
207
208 if (!ssl_is_sct_list_valid(&sct)) {
209 OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700210 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700211 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500212 }
213
Robert Sloan8f860b12017-08-28 07:37:06 -0700214 if (sk_CRYPTO_BUFFER_num(certs.get()) == 1) {
215 CRYPTO_BUFFER_free(hs->new_session->signed_cert_timestamp_list);
216 hs->new_session->signed_cert_timestamp_list =
217 CRYPTO_BUFFER_new_from_CBS(&sct, ssl->ctx->pool);
218 if (hs->new_session->signed_cert_timestamp_list == nullptr) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700219 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloan8f860b12017-08-28 07:37:06 -0700220 return 0;
221 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500222 }
223 }
224 }
225
Robert Sloana27a6a42017-09-05 08:39:28 -0700226 // Store a null certificate list rather than an empty one if the peer didn't
227 // send certificates.
Robert Sloanfe7cd212017-08-07 09:03:39 -0700228 if (sk_CRYPTO_BUFFER_num(certs.get()) == 0) {
229 certs.reset();
David Benjaminc895d6b2016-08-11 13:26:41 -0400230 }
231
Robert Sloanb6d070c2017-07-24 08:40:01 -0700232 hs->peer_pubkey = std::move(pkey);
Steven Valdeze7531f02016-12-14 13:29:57 -0500233
Robert Sloana94fe052017-02-21 08:49:28 -0800234 sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700235 hs->new_session->certs = certs.release();
Steven Valdeze7531f02016-12-14 13:29:57 -0500236
Robert Sloanb6d070c2017-07-24 08:40:01 -0700237 if (!ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500238 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700239 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700240 return 0;
Steven Valdeze7531f02016-12-14 13:29:57 -0500241 }
242
Robert Sloana94fe052017-02-21 08:49:28 -0800243 if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400244 if (!allow_anonymous) {
245 OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700246 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_CERTIFICATE_REQUIRED);
Robert Sloanb6d070c2017-07-24 08:40:01 -0700247 return 0;
David Benjaminc895d6b2016-08-11 13:26:41 -0400248 }
249
Robert Sloana27a6a42017-09-05 08:39:28 -0700250 // OpenSSL returns X509_V_OK when no certificates are requested. This is
251 // classed by them as a bug, but it's assumed by at least NGINX.
Robert Sloana94fe052017-02-21 08:49:28 -0800252 hs->new_session->verify_result = X509_V_OK;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400253
Robert Sloana27a6a42017-09-05 08:39:28 -0700254 // No certificate, so nothing more to do.
Robert Sloanb6d070c2017-07-24 08:40:01 -0700255 return 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400256 }
257
Robert Sloana94fe052017-02-21 08:49:28 -0800258 hs->new_session->peer_sha256_valid = retain_sha256;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700259 return 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400260}
261
Robert Sloan84377092017-08-14 09:33:19 -0700262int tls13_process_certificate_verify(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
Steven Valdeze7531f02016-12-14 13:29:57 -0500263 SSL *const ssl = hs->ssl;
Steven Valdeze7531f02016-12-14 13:29:57 -0500264 if (hs->peer_pubkey == NULL) {
Robert Sloana12bf462017-07-17 07:08:26 -0700265 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
266 return 0;
David Benjaminc895d6b2016-08-11 13:26:41 -0400267 }
268
Robert Sloan84377092017-08-14 09:33:19 -0700269 CBS body = msg.body, signature;
David Benjaminc895d6b2016-08-11 13:26:41 -0400270 uint16_t signature_algorithm;
Robert Sloan84377092017-08-14 09:33:19 -0700271 if (!CBS_get_u16(&body, &signature_algorithm) ||
272 !CBS_get_u16_length_prefixed(&body, &signature) ||
273 CBS_len(&body) != 0) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400274 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700275 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700276 return 0;
David Benjaminc895d6b2016-08-11 13:26:41 -0400277 }
278
Robert Sloane56da3e2017-06-26 08:26:42 -0700279 uint8_t alert = SSL_AD_DECODE_ERROR;
280 if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700281 ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
Robert Sloana12bf462017-07-17 07:08:26 -0700282 return 0;
David Benjaminc895d6b2016-08-11 13:26:41 -0400283 }
Robert Sloana94fe052017-02-21 08:49:28 -0800284 hs->new_session->peer_signature_algorithm = signature_algorithm;
David Benjaminc895d6b2016-08-11 13:26:41 -0400285
Robert Sloan921ef2c2017-10-17 09:02:20 -0700286 Array<uint8_t> input;
Steven Valdez909b19f2016-11-21 15:35:44 -0500287 if (!tls13_get_cert_verify_signature_input(
Robert Sloan921ef2c2017-10-17 09:02:20 -0700288 hs, &input,
Steven Valdez909b19f2016-11-21 15:35:44 -0500289 ssl->server ? ssl_cert_verify_client : ssl_cert_verify_server)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700290 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700291 return 0;
David Benjaminc895d6b2016-08-11 13:26:41 -0400292 }
293
Robert Sloan921ef2c2017-10-17 09:02:20 -0700294 bool sig_ok = ssl_public_key_verify(ssl, signature, signature_algorithm,
295 hs->peer_pubkey.get(), input);
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400296#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
Robert Sloan921ef2c2017-10-17 09:02:20 -0700297 sig_ok = true;
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400298 ERR_clear_error();
299#endif
David Benjaminc895d6b2016-08-11 13:26:41 -0400300 if (!sig_ok) {
301 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700302 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700303 return 0;
David Benjaminc895d6b2016-08-11 13:26:41 -0400304 }
305
Robert Sloana12bf462017-07-17 07:08:26 -0700306 return 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400307}
308
Robert Sloan84377092017-08-14 09:33:19 -0700309int tls13_process_finished(SSL_HANDSHAKE *hs, const SSLMessage &msg,
310 int use_saved_value) {
David Benjamin1b249672016-12-06 18:25:50 -0500311 SSL *const ssl = hs->ssl;
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700312 uint8_t verify_data_buf[EVP_MAX_MD_SIZE];
313 const uint8_t *verify_data;
David Benjaminc895d6b2016-08-11 13:26:41 -0400314 size_t verify_data_len;
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700315 if (use_saved_value) {
316 assert(ssl->server);
317 verify_data = hs->expected_client_finished;
318 verify_data_len = hs->hash_len;
319 } else {
320 if (!tls13_finished_mac(hs, verify_data_buf, &verify_data_len,
321 !ssl->server)) {
322 return 0;
323 }
324 verify_data = verify_data_buf;
David Benjaminc895d6b2016-08-11 13:26:41 -0400325 }
326
Robert Sloan84377092017-08-14 09:33:19 -0700327 int finished_ok = CBS_mem_equal(&msg.body, verify_data, verify_data_len);
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400328#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
329 finished_ok = 1;
330#endif
331 if (!finished_ok) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700332 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400333 OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
334 return 0;
335 }
336
337 return 1;
338}
339
Robert Sloan4d1ac502017-02-06 08:36:14 -0800340int tls13_add_certificate(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500341 SSL *const ssl = hs->ssl;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700342 ScopedCBB cbb;
Robert Sloana12bf462017-07-17 07:08:26 -0700343 CBB body, certificate_list;
344 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CERTIFICATE) ||
Robert Sloana27a6a42017-09-05 08:39:28 -0700345 // The request context is always empty in the handshake.
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400346 !CBB_add_u8(&body, 0) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500347 !CBB_add_u24_length_prefixed(&body, &certificate_list)) {
348 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700349 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500350 }
351
352 if (!ssl_has_certificate(ssl)) {
Robert Sloana12bf462017-07-17 07:08:26 -0700353 return ssl_add_message_cbb(ssl, cbb.get());
Steven Valdez909b19f2016-11-21 15:35:44 -0500354 }
355
356 CERT *cert = ssl->cert;
Robert Sloan4d1ac502017-02-06 08:36:14 -0800357 CRYPTO_BUFFER *leaf_buf = sk_CRYPTO_BUFFER_value(cert->chain, 0);
Steven Valdez909b19f2016-11-21 15:35:44 -0500358 CBB leaf, extensions;
359 if (!CBB_add_u24_length_prefixed(&certificate_list, &leaf) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -0800360 !CBB_add_bytes(&leaf, CRYPTO_BUFFER_data(leaf_buf),
361 CRYPTO_BUFFER_len(leaf_buf)) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500362 !CBB_add_u16_length_prefixed(&certificate_list, &extensions)) {
363 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700364 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500365 }
366
Robert Sloana94fe052017-02-21 08:49:28 -0800367 if (hs->scts_requested && ssl->cert->signed_cert_timestamp_list != NULL) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500368 CBB contents;
369 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_certificate_timestamp) ||
370 !CBB_add_u16_length_prefixed(&extensions, &contents) ||
Robert Sloana94fe052017-02-21 08:49:28 -0800371 !CBB_add_bytes(
372 &contents,
373 CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
374 CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500375 !CBB_flush(&extensions)) {
376 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700377 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500378 }
379 }
380
David Benjamin1b249672016-12-06 18:25:50 -0500381 if (hs->ocsp_stapling_requested &&
Robert Sloana94fe052017-02-21 08:49:28 -0800382 ssl->cert->ocsp_response != NULL) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500383 CBB contents, ocsp_response;
384 if (!CBB_add_u16(&extensions, TLSEXT_TYPE_status_request) ||
385 !CBB_add_u16_length_prefixed(&extensions, &contents) ||
386 !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
387 !CBB_add_u24_length_prefixed(&contents, &ocsp_response) ||
Robert Sloana94fe052017-02-21 08:49:28 -0800388 !CBB_add_bytes(&ocsp_response,
389 CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
390 CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500391 !CBB_flush(&extensions)) {
392 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700393 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500394 }
395 }
396
Robert Sloan4d1ac502017-02-06 08:36:14 -0800397 for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain); i++) {
398 CRYPTO_BUFFER *cert_buf = sk_CRYPTO_BUFFER_value(cert->chain, i);
Steven Valdez909b19f2016-11-21 15:35:44 -0500399 CBB child;
400 if (!CBB_add_u24_length_prefixed(&certificate_list, &child) ||
Robert Sloan4d1ac502017-02-06 08:36:14 -0800401 !CBB_add_bytes(&child, CRYPTO_BUFFER_data(cert_buf),
402 CRYPTO_BUFFER_len(cert_buf)) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500403 !CBB_add_u16(&certificate_list, 0 /* no extensions */)) {
404 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700405 return 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500406 }
407 }
408
Robert Sloana12bf462017-07-17 07:08:26 -0700409 return ssl_add_message_cbb(ssl, cbb.get());
David Benjaminc895d6b2016-08-11 13:26:41 -0400410}
411
Robert Sloane56da3e2017-06-26 08:26:42 -0700412enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500413 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -0400414 uint16_t signature_algorithm;
David Benjamin1b249672016-12-06 18:25:50 -0500415 if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
Robert Sloana12bf462017-07-17 07:08:26 -0700416 return ssl_private_key_failure;
David Benjaminc895d6b2016-08-11 13:26:41 -0400417 }
Robert Sloana12bf462017-07-17 07:08:26 -0700418
Robert Sloanb6d070c2017-07-24 08:40:01 -0700419 ScopedCBB cbb;
Robert Sloana12bf462017-07-17 07:08:26 -0700420 CBB body;
421 if (!ssl->method->init_message(ssl, cbb.get(), &body,
David Benjaminc895d6b2016-08-11 13:26:41 -0400422 SSL3_MT_CERTIFICATE_VERIFY) ||
423 !CBB_add_u16(&body, signature_algorithm)) {
424 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700425 return ssl_private_key_failure;
David Benjaminc895d6b2016-08-11 13:26:41 -0400426 }
427
Robert Sloana27a6a42017-09-05 08:39:28 -0700428 // Sign the digest.
David Benjaminc895d6b2016-08-11 13:26:41 -0400429 CBB child;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700430 const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get());
David Benjaminc895d6b2016-08-11 13:26:41 -0400431 uint8_t *sig;
432 size_t sig_len;
433 if (!CBB_add_u16_length_prefixed(&body, &child) ||
434 !CBB_reserve(&child, &sig, max_sig_len)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700435 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700436 return ssl_private_key_failure;
David Benjaminc895d6b2016-08-11 13:26:41 -0400437 }
438
Robert Sloan921ef2c2017-10-17 09:02:20 -0700439 Array<uint8_t> msg;
Robert Sloane56da3e2017-06-26 08:26:42 -0700440 if (!tls13_get_cert_verify_signature_input(
Robert Sloan921ef2c2017-10-17 09:02:20 -0700441 hs, &msg,
Robert Sloane56da3e2017-06-26 08:26:42 -0700442 ssl->server ? ssl_cert_verify_server : ssl_cert_verify_client)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700443 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
Robert Sloana12bf462017-07-17 07:08:26 -0700444 return ssl_private_key_failure;
David Benjaminc895d6b2016-08-11 13:26:41 -0400445 }
446
Robert Sloane56da3e2017-06-26 08:26:42 -0700447 enum ssl_private_key_result_t sign_result = ssl_private_key_sign(
Robert Sloan921ef2c2017-10-17 09:02:20 -0700448 hs, sig, &sig_len, max_sig_len, signature_algorithm, msg);
David Benjaminc895d6b2016-08-11 13:26:41 -0400449 if (sign_result != ssl_private_key_success) {
Robert Sloana12bf462017-07-17 07:08:26 -0700450 return sign_result;
David Benjaminc895d6b2016-08-11 13:26:41 -0400451 }
452
453 if (!CBB_did_write(&child, sig_len) ||
Robert Sloana12bf462017-07-17 07:08:26 -0700454 !ssl_add_message_cbb(ssl, cbb.get())) {
455 return ssl_private_key_failure;
David Benjaminc895d6b2016-08-11 13:26:41 -0400456 }
457
Robert Sloana12bf462017-07-17 07:08:26 -0700458 return ssl_private_key_success;
David Benjaminc895d6b2016-08-11 13:26:41 -0400459}
460
Robert Sloan4d1ac502017-02-06 08:36:14 -0800461int tls13_add_finished(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500462 SSL *const ssl = hs->ssl;
David Benjaminc895d6b2016-08-11 13:26:41 -0400463 size_t verify_data_len;
464 uint8_t verify_data[EVP_MAX_MD_SIZE];
465
David Benjamin1b249672016-12-06 18:25:50 -0500466 if (!tls13_finished_mac(hs, verify_data, &verify_data_len, ssl->server)) {
Robert Sloan921ef2c2017-10-17 09:02:20 -0700467 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400468 OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
469 return 0;
470 }
471
Robert Sloanb6d070c2017-07-24 08:40:01 -0700472 ScopedCBB cbb;
473 CBB body;
474 if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_FINISHED) ||
David Benjaminc895d6b2016-08-11 13:26:41 -0400475 !CBB_add_bytes(&body, verify_data, verify_data_len) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700476 !ssl_add_message_cbb(ssl, cbb.get())) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400477 return 0;
478 }
479
480 return 1;
481}
482
Robert Sloan84377092017-08-14 09:33:19 -0700483static int tls13_receive_key_update(SSL *ssl, const SSLMessage &msg) {
484 CBS body = msg.body;
David Benjamin95add822016-10-19 01:09:12 -0400485 uint8_t key_update_request;
Robert Sloan84377092017-08-14 09:33:19 -0700486 if (!CBS_get_u8(&body, &key_update_request) ||
487 CBS_len(&body) != 0 ||
David Benjamin95add822016-10-19 01:09:12 -0400488 (key_update_request != SSL_KEY_UPDATE_NOT_REQUESTED &&
489 key_update_request != SSL_KEY_UPDATE_REQUESTED)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400490 OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700491 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
David Benjaminc895d6b2016-08-11 13:26:41 -0400492 return 0;
493 }
494
Robert Sloan572a4e22017-04-17 10:52:19 -0700495 if (!tls13_rotate_traffic_key(ssl, evp_aead_open)) {
496 return 0;
497 }
498
Robert Sloana27a6a42017-09-05 08:39:28 -0700499 // Acknowledge the KeyUpdate
Robert Sloan572a4e22017-04-17 10:52:19 -0700500 if (key_update_request == SSL_KEY_UPDATE_REQUESTED &&
501 !ssl->s3->key_update_pending) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700502 ScopedCBB cbb;
Robert Sloan84377092017-08-14 09:33:19 -0700503 CBB body_cbb;
504 if (!ssl->method->init_message(ssl, cbb.get(), &body_cbb,
505 SSL3_MT_KEY_UPDATE) ||
506 !CBB_add_u8(&body_cbb, SSL_KEY_UPDATE_NOT_REQUESTED) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -0700507 !ssl_add_message_cbb(ssl, cbb.get()) ||
Robert Sloan572a4e22017-04-17 10:52:19 -0700508 !tls13_rotate_traffic_key(ssl, evp_aead_seal)) {
Robert Sloan572a4e22017-04-17 10:52:19 -0700509 return 0;
510 }
511
Robert Sloana27a6a42017-09-05 08:39:28 -0700512 // Suppress KeyUpdate acknowledgments until this change is written to the
513 // wire. This prevents us from accumulating write obligations when read and
514 // write progress at different rates. See draft-ietf-tls-tls13-18, section
515 // 4.5.3.
516 ssl->s3->key_update_pending = true;
Robert Sloan572a4e22017-04-17 10:52:19 -0700517 }
518
519 return 1;
David Benjaminc895d6b2016-08-11 13:26:41 -0400520}
521
Robert Sloan84377092017-08-14 09:33:19 -0700522int tls13_post_handshake(SSL *ssl, const SSLMessage &msg) {
523 if (msg.type == SSL3_MT_KEY_UPDATE) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400524 ssl->s3->key_update_count++;
525 if (ssl->s3->key_update_count > kMaxKeyUpdates) {
526 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_KEY_UPDATES);
Robert Sloan921ef2c2017-10-17 09:02:20 -0700527 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400528 return 0;
529 }
530
Robert Sloan84377092017-08-14 09:33:19 -0700531 return tls13_receive_key_update(ssl, msg);
David Benjaminc895d6b2016-08-11 13:26:41 -0400532 }
533
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400534 ssl->s3->key_update_count = 0;
535
Robert Sloan84377092017-08-14 09:33:19 -0700536 if (msg.type == SSL3_MT_NEW_SESSION_TICKET && !ssl->server) {
537 return tls13_process_new_session_ticket(ssl, msg);
David Benjaminc895d6b2016-08-11 13:26:41 -0400538 }
539
Robert Sloan921ef2c2017-10-17 09:02:20 -0700540 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjaminc895d6b2016-08-11 13:26:41 -0400541 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
542 return 0;
543}
Robert Sloanb6d070c2017-07-24 08:40:01 -0700544
545} // namespace bssl