• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright 2005 Nokia. All rights reserved.
4  *
5  * Licensed under the OpenSSL license (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10 
11 #include <openssl/ssl.h>
12 
13 #include <string_view>
14 
15 #include <openssl/buf.h>
16 #include <openssl/digest.h>
17 #include <openssl/err.h>
18 
19 #include "internal.h"
20 
21 
22 BSSL_NAMESPACE_BEGIN
23 
SSLTranscript(bool is_dtls)24 SSLTranscript::SSLTranscript(bool is_dtls) : is_dtls_(is_dtls) {}
25 
~SSLTranscript()26 SSLTranscript::~SSLTranscript() {}
27 
Init()28 bool SSLTranscript::Init() {
29   buffer_.reset(BUF_MEM_new());
30   if (!buffer_) {
31     return false;
32   }
33 
34   hash_.Reset();
35   return true;
36 }
37 
InitHash(uint16_t version,const SSL_CIPHER * cipher)38 bool SSLTranscript::InitHash(uint16_t version, const SSL_CIPHER *cipher) {
39   version_ = version;
40   const EVP_MD *md = ssl_get_handshake_digest(version, cipher);
41   if (Digest() == md) {
42     // No need to re-hash the buffer.
43     return true;
44   }
45   if (!HashBuffer(hash_.get(), md)) {
46     return false;
47   }
48   if (is_dtls_ && version_ >= TLS1_3_VERSION) {
49     // In DTLS 1.3, prior to the call to InitHash, the message (if present) in
50     // the buffer has the DTLS 1.2 header. After the call to InitHash, the TLS
51     // 1.3 header is written by SSLTranscript::Update. If the buffer isn't freed
52     // here, it would have a mix of different header formats and using it would
53     // yield wrong results. However, there's no need for the buffer once the
54     // version and the digest for the cipher suite are known, so the buffer is
55     // freed here to avoid potential misuse of the SSLTranscript object.
56     FreeBuffer();
57   }
58   return true;
59 }
60 
HashBuffer(EVP_MD_CTX * ctx,const EVP_MD * digest) const61 bool SSLTranscript::HashBuffer(EVP_MD_CTX *ctx, const EVP_MD *digest) const {
62   if (!EVP_DigestInit_ex(ctx, digest, nullptr)) {
63     return false;
64   }
65   if (!is_dtls_ || version_ < TLS1_3_VERSION) {
66     return EVP_DigestUpdate(ctx, buffer_->data, buffer_->length);
67   }
68 
69   // If the version is DTLS 1.3 and we still have a buffer, then there should be
70   // at most a single DTLSHandshake message in the buffer, for the ClientHello.
71   // On the server side, the version (DTLS 1.3) and cipher suite are chosen in
72   // response to the first ClientHello, and InitHash is called before that
73   // ClientHello is added to the SSLTranscript, so the buffer is empty if this
74   // SSLTranscript is on the server.
75   if (buffer_->length == 0) {
76     return true;
77   }
78 
79   // On the client side, we can receive either a ServerHello or
80   // HelloRetryRequest in response to the ClientHello. Regardless of which
81   // message we receive, the client code calls InitHash before updating the
82   // transcript with that message, so the ClientHello is the only message in the
83   // buffer. In DTLS 1.3, we need to skip the message_seq, fragment_offset, and
84   // fragment_length fields from the DTLSHandshake message in the buffer. The
85   // structure of a DTLSHandshake message is as follows (RFC 9147, section 5.2):
86   //
87   //   struct {
88   //       HandshakeType msg_type;    /* handshake type */
89   //       uint24 length;             /* bytes in message */
90   //       uint16 message_seq;        /* DTLS-required field */
91   //       uint24 fragment_offset;    /* DTLS-required field */
92   //       uint24 fragment_length;    /* DTLS-required field */
93   //       select (msg_type) {
94   //         /* omitted for brevity */
95   //       } body;
96   //   } DTLSHandshake;
97   CBS buf, header;
98   CBS_init(&buf, reinterpret_cast<uint8_t *>(buffer_->data), buffer_->length);
99   if (!CBS_get_bytes(&buf, &header, 4) ||                             //
100       !CBS_skip(&buf, 8) ||                                           //
101       !EVP_DigestUpdate(ctx, CBS_data(&header), CBS_len(&header)) ||  //
102       !EVP_DigestUpdate(ctx, CBS_data(&buf), CBS_len(&buf))) {
103     return false;
104   }
105   return true;
106 }
107 
FreeBuffer()108 void SSLTranscript::FreeBuffer() { buffer_.reset(); }
109 
DigestLen() const110 size_t SSLTranscript::DigestLen() const { return EVP_MD_size(Digest()); }
111 
Digest() const112 const EVP_MD *SSLTranscript::Digest() const {
113   return EVP_MD_CTX_get0_md(hash_.get());
114 }
115 
UpdateForHelloRetryRequest()116 bool SSLTranscript::UpdateForHelloRetryRequest() {
117   if (buffer_) {
118     buffer_->length = 0;
119   }
120 
121   uint8_t old_hash[EVP_MAX_MD_SIZE];
122   size_t hash_len;
123   if (!GetHash(old_hash, &hash_len)) {
124     return false;
125   }
126   const uint8_t header[4] = {SSL3_MT_MESSAGE_HASH, 0, 0,
127                              static_cast<uint8_t>(hash_len)};
128   if (!EVP_DigestInit_ex(hash_.get(), Digest(), nullptr) ||
129       !AddToBufferOrHash(header) ||
130       !AddToBufferOrHash(Span(old_hash, hash_len))) {
131     return false;
132   }
133   return true;
134 }
135 
CopyToHashContext(EVP_MD_CTX * ctx,const EVP_MD * digest) const136 bool SSLTranscript::CopyToHashContext(EVP_MD_CTX *ctx,
137                                       const EVP_MD *digest) const {
138   const EVP_MD *transcript_digest = Digest();
139   if (transcript_digest != nullptr &&
140       EVP_MD_type(transcript_digest) == EVP_MD_type(digest)) {
141     return EVP_MD_CTX_copy_ex(ctx, hash_.get());
142   }
143 
144   if (buffer_) {
145     return HashBuffer(ctx, digest);
146   }
147 
148   OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
149   return false;
150 }
151 
Update(Span<const uint8_t> in)152 bool SSLTranscript::Update(Span<const uint8_t> in) {
153   if (!is_dtls_ || version_ < TLS1_3_VERSION) {
154     return AddToBufferOrHash(in);
155   }
156   if (in.size() < DTLS1_HM_HEADER_LENGTH) {
157     return false;
158   }
159   // The message passed into Update is the whole Handshake or DTLSHandshake
160   // message, including the msg_type and length. In DTLS, the DTLSHandshake
161   // message also has message_seq, fragment_offset, and fragment_length
162   // fields. In DTLS 1.3, those fields are omitted so that the same
163   // transcript format as TLS 1.3 is used. This means we write the 1-byte
164   // msg_type, 3-byte length, then skip 2+3+3 bytes for the DTLS-specific
165   // fields that get omitted.
166   if (!AddToBufferOrHash(in.subspan(0, 4)) ||
167       !AddToBufferOrHash(in.subspan(12))) {
168     return false;
169   }
170   return true;
171 }
172 
AddToBufferOrHash(Span<const uint8_t> in)173 bool SSLTranscript::AddToBufferOrHash(Span<const uint8_t> in) {
174   // Depending on the state of the handshake, either the handshake buffer may be
175   // active, the rolling hash, or both.
176   if (buffer_ &&  //
177       !BUF_MEM_append(buffer_.get(), in.data(), in.size())) {
178     return false;
179   }
180 
181   if (EVP_MD_CTX_md(hash_.get()) != NULL) {
182     EVP_DigestUpdate(hash_.get(), in.data(), in.size());
183   }
184 
185   return true;
186 }
187 
GetHash(uint8_t * out,size_t * out_len) const188 bool SSLTranscript::GetHash(uint8_t *out, size_t *out_len) const {
189   ScopedEVP_MD_CTX ctx;
190   unsigned len;
191   if (!EVP_MD_CTX_copy_ex(ctx.get(), hash_.get()) ||
192       !EVP_DigestFinal_ex(ctx.get(), out, &len)) {
193     return false;
194   }
195   *out_len = len;
196   return true;
197 }
198 
GetFinishedMAC(uint8_t * out,size_t * out_len,const SSL_SESSION * session,bool from_server) const199 bool SSLTranscript::GetFinishedMAC(uint8_t *out, size_t *out_len,
200                                    const SSL_SESSION *session,
201                                    bool from_server) const {
202   uint8_t digest[EVP_MAX_MD_SIZE];
203   size_t digest_len;
204   if (!GetHash(digest, &digest_len)) {
205     return false;
206   }
207 
208   std::string_view label = from_server ? "server finished" : "client finished";
209   static const size_t kFinishedLen = 12;
210   if (!tls1_prf(Digest(), Span(out, kFinishedLen), session->secret, label,
211                 Span(digest, digest_len), {})) {
212     return false;
213   }
214 
215   *out_len = kFinishedLen;
216   return true;
217 }
218 
219 BSSL_NAMESPACE_END
220