Path: blob/main/contrib/bearssl/src/ssl/ssl_keyexport.c
39488 views
/*1* Copyright (c) 2017 Thomas Pornin <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining4* a copy of this software and associated documentation files (the5* "Software"), to deal in the Software without restriction, including6* without limitation the rights to use, copy, modify, merge, publish,7* distribute, sublicense, and/or sell copies of the Software, and to8* permit persons to whom the Software is furnished to do so, subject to9* the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*/2324#include "inner.h"2526/*27* Supported cipher suites that use SHA-384 for the PRF when selected28* for TLS 1.2. All other cipher suites are deemed to use SHA-256.29*/30static const uint16_t suites_sha384[] = {31BR_TLS_RSA_WITH_AES_256_GCM_SHA384,32BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,33BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,34BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,35BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,36BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,37BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,38BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,39BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA38440};4142/* see bearssl_ssl.h */43int44br_ssl_key_export(br_ssl_engine_context *cc,45void *dst, size_t len, const char *label,46const void *context, size_t context_len)47{48br_tls_prf_seed_chunk chunks[4];49br_tls_prf_impl iprf;50size_t num_chunks, u;51unsigned char tmp[2];52int prf_id;5354if (cc->application_data != 1) {55return 0;56}57chunks[0].data = cc->client_random;58chunks[0].len = sizeof cc->client_random;59chunks[1].data = cc->server_random;60chunks[1].len = sizeof cc->server_random;61if (context != NULL) {62br_enc16be(tmp, (unsigned)context_len);63chunks[2].data = tmp;64chunks[2].len = 2;65chunks[3].data = context;66chunks[3].len = context_len;67num_chunks = 4;68} else {69num_chunks = 2;70}71prf_id = BR_SSLPRF_SHA256;72for (u = 0; u < (sizeof suites_sha384) / sizeof(uint16_t); u ++) {73if (suites_sha384[u] == cc->session.cipher_suite) {74prf_id = BR_SSLPRF_SHA384;75}76}77iprf = br_ssl_engine_get_PRF(cc, prf_id);78iprf(dst, len,79cc->session.master_secret, sizeof cc->session.master_secret,80label, num_chunks, chunks);81return 1;82}838485