| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
// Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 3 |
// Copyright 2005 Nokia. All rights reserved. |
| 4 |
// |
| 5 |
// The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 6 |
// Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 7 |
// support (see RFC 4279) to OpenSSL. |
| 8 |
// |
| 9 |
// SPDX-License-Identifier: Apache-2.0 |
| 10 |
|
| 11 |
#include <openssl/ssl.h> |
| 12 |
|
| 13 |
#include <openssl/buf.h> |
| 14 |
#include <openssl/digest.h> |
| 15 |
#include <openssl/err.h> |
| 16 |
|
| 17 |
#include "internal.h" |
| 18 |
|
| 19 |
|
| 20 |
BSSL_NAMESPACE_BEGIN |
| 21 |
|
| 22 |
SSLTranscript::SSLTranscript() {} |
| 23 |
|
| 24 |
SSLTranscript::~SSLTranscript() {} |
| 25 |
|
| 26 |
bool SSLTranscript::Init() { |
| 27 |
buffer_.reset(BUF_MEM_new()); |
| 28 |
if (!buffer_) { |
| 29 |
return false; |
| 30 |
} |
| 31 |
|
| 32 |
hash_.Reset(); |
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
bool SSLTranscript::InitHash(uint16_t version, const SSL_CIPHER *cipher) { |
| 37 |
const EVP_MD *md = ssl_get_handshake_digest(version, cipher); |
| 38 |
if (Digest() == md) { |
| 39 |
// No need to re-hash the buffer. |
| 40 |
return true; |
| 41 |
} |
| 42 |
return EVP_DigestInit_ex(hash_.get(), md, nullptr) && |
| 43 |
EVP_DigestUpdate(hash_.get(), buffer_->data, buffer_->length); |
| 44 |
} |
| 45 |
|
| 46 |
void SSLTranscript::FreeBuffer() { |
| 47 |
buffer_.reset(); |
| 48 |
} |
| 49 |
|
| 50 |
size_t SSLTranscript::DigestLen() const { |
| 51 |
return EVP_MD_size(Digest()); |
| 52 |
} |
| 53 |
|
| 54 |
const EVP_MD *SSLTranscript::Digest() const { |
| 55 |
return EVP_MD_CTX_md(hash_.get()); |
| 56 |
} |
| 57 |
|
| 58 |
bool SSLTranscript::UpdateForHelloRetryRequest() { |
| 59 |
if (buffer_) { |
| 60 |
buffer_->length = 0; |
| 61 |
} |
| 62 |
|
| 63 |
uint8_t old_hash[EVP_MAX_MD_SIZE]; |
| 64 |
size_t hash_len; |
| 65 |
if (!GetHash(old_hash, &hash_len)) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
const uint8_t header[4] = {SSL3_MT_MESSAGE_HASH, 0, 0, |
| 69 |
static_cast<uint8_t>(hash_len)}; |
| 70 |
if (!EVP_DigestInit_ex(hash_.get(), Digest(), nullptr) || |
| 71 |
!Update(header) || |
| 72 |
!Update(MakeConstSpan(old_hash, hash_len))) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
bool SSLTranscript::CopyToHashContext(EVP_MD_CTX *ctx, |
| 79 |
const EVP_MD *digest) const { |
| 80 |
const EVP_MD *transcript_digest = Digest(); |
| 81 |
if (transcript_digest != nullptr && |
| 82 |
EVP_MD_type(transcript_digest) == EVP_MD_type(digest)) { |
| 83 |
return EVP_MD_CTX_copy_ex(ctx, hash_.get()); |
| 84 |
} |
| 85 |
|
| 86 |
if (buffer_) { |
| 87 |
return EVP_DigestInit_ex(ctx, digest, nullptr) && |
| 88 |
EVP_DigestUpdate(ctx, buffer_->data, buffer_->length); |
| 89 |
} |
| 90 |
|
| 91 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
bool SSLTranscript::Update(Span<const uint8_t> in) { |
| 96 |
// Depending on the state of the handshake, either the handshake buffer may be |
| 97 |
// active, the rolling hash, or both. |
| 98 |
if (buffer_ && |
| 99 |
!BUF_MEM_append(buffer_.get(), in.data(), in.size())) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
if (EVP_MD_CTX_md(hash_.get()) != NULL) { |
| 104 |
EVP_DigestUpdate(hash_.get(), in.data(), in.size()); |
| 105 |
} |
| 106 |
|
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
bool SSLTranscript::GetHash(uint8_t *out, size_t *out_len) const { |
| 111 |
ScopedEVP_MD_CTX ctx; |
| 112 |
unsigned len; |
| 113 |
if (!EVP_MD_CTX_copy_ex(ctx.get(), hash_.get()) || |
| 114 |
!EVP_DigestFinal_ex(ctx.get(), out, &len)) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
*out_len = len; |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
bool SSLTranscript::GetFinishedMAC(uint8_t *out, size_t *out_len, |
| 122 |
const SSL_SESSION *session, |
| 123 |
bool from_server) const { |
| 124 |
static const char kClientLabel[] = "client finished"; |
| 125 |
static const char kServerLabel[] = "server finished"; |
| 126 |
auto label = from_server |
| 127 |
? MakeConstSpan(kServerLabel, sizeof(kServerLabel) - 1) |
| 128 |
: MakeConstSpan(kClientLabel, sizeof(kClientLabel) - 1); |
| 129 |
|
| 130 |
uint8_t digest[EVP_MAX_MD_SIZE]; |
| 131 |
size_t digest_len; |
| 132 |
if (!GetHash(digest, &digest_len)) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
static const size_t kFinishedLen = 12; |
| 137 |
if (!tls1_prf(Digest(), MakeSpan(out, kFinishedLen), |
| 138 |
MakeConstSpan(session->secret, session->secret_length), label, |
| 139 |
MakeConstSpan(digest, digest_len), {})) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
*out_len = kFinishedLen; |
| 144 |
return true; |
| 145 |
} |
| 146 |
|
| 147 |
BSSL_NAMESPACE_END |