Botan 3.12.0
Crypto and TLS for C&
msg_certificate_12.cpp
Go to the documentation of this file.
1/*
2* Certificate Message
3* (C) 2004-2006,2012,2020 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/tls_messages_12.h>
9
10#include <botan/data_src.h>
11#include <botan/tls_alert.h>
12#include <botan/tls_exceptn.h>
13#include <botan/tls_extensions.h>
14#include <botan/tls_policy.h>
15#include <botan/x509cert.h>
16#include <botan/internal/loadstor.h>
17#include <botan/internal/tls_handshake_hash.h>
18#include <botan/internal/tls_handshake_io.h>
19
20namespace Botan::TLS {
21
23
24/**
25* Create a new Certificate message
26*/
27Certificate_12::Certificate_12(Handshake_IO& io, Handshake_Hash& hash, const std::vector<X509_Certificate>& cert_list) :
28 m_certs(cert_list) {
29 hash.update(io.send(*this));
30}
31
32/**
33* Deserialize a Certificate message
34*/
35Certificate_12::Certificate_12(const std::vector<uint8_t>& buf, const Policy& policy) {
36 if(buf.size() < 3) {
37 throw Decoding_Error("Certificate: Message malformed");
38 }
39
40 const size_t total_size = make_uint32(0, buf[0], buf[1], buf[2]);
41
42 if(total_size != buf.size() - 3) {
43 throw Decoding_Error("Certificate: Message malformed");
44 }
45
46 const size_t max_size = policy.maximum_certificate_chain_size();
47 if(max_size > 0 && total_size > max_size) {
48 throw Decoding_Error("Certificate chain exceeds policy specified maximum size");
49 }
50
51 const uint8_t* certs = buf.data() + 3;
52
53 while(const size_t remaining_bytes = buf.data() + buf.size() - certs) {
54 if(remaining_bytes < 3) {
55 throw Decoding_Error("Certificate: Message malformed");
56 }
57
58 const size_t cert_size = make_uint32(0, certs[0], certs[1], certs[2]);
59
60 if(remaining_bytes < (3 + cert_size)) {
61 throw Decoding_Error("Certificate: Message malformed");
62 }
63
64 DataSource_Memory cert_buf(&certs[3], cert_size);
65 try {
66 m_certs.push_back(X509_Certificate(cert_buf));
67 } catch(Exception& e) {
68 // bad_certificate would make more sense but BoGo expects decoding_error
69 throw TLS_Exception(Alert::DecodeError, e.what());
70 }
71
72 certs += cert_size + 3;
73 }
74
75 /*
76 * TLS 1.0 through 1.2 all seem to require that the certificate be
77 * precisely a v3 certificate. In fact the strict wording would seem
78 * to require that every certificate in the chain be v3. But often
79 * the intermediates are outside of the control of the server.
80 * But, require that the leaf certificate be v3
81 */
82 if(!m_certs.empty() && m_certs[0].x509_version() != 3) {
83 throw TLS_Exception(Alert::BadCertificate, "The leaf certificate must be v3");
84 }
85}
86
87/**
88* Serialize a Certificate message
89*/
90std::vector<uint8_t> Certificate_12::serialize() const {
91 std::vector<uint8_t> buf(3);
92
93 for(const auto& cert : m_certs) {
94 const auto raw_cert = cert.BER_encode();
95 const size_t cert_size = raw_cert.size();
96 for(size_t j = 0; j != 3; ++j) {
97 buf.push_back(get_byte_var(j + 1, static_cast<uint32_t>(cert_size)));
98 }
99 buf += raw_cert;
100 }
101
102 const size_t buf_size = buf.size() - 3;
103 for(size_t i = 0; i != 3; ++i) {
104 buf[i] = get_byte_var(i + 1, static_cast<uint32_t>(buf_size));
105 }
106
107 return buf;
108}
109
110size_t Certificate_12::count() const {
111 return m_certs.size();
112}
113
114} // namespace Botan::TLS
const char * what() const noexcept override
Definition exceptn.h:94
Certificate_12(Handshake_IO &io, Handshake_Hash &hash, const std::vector< X509_Certificate > &certs)
std::vector< uint8_t > serialize() const override
void update(const uint8_t in[], size_t length)
virtual std::vector< uint8_t > send(const Handshake_Message &msg)=0
virtual size_t maximum_certificate_chain_size() const
constexpr uint32_t make_uint32(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3)
Definition loadstor.h:104
constexpr uint8_t get_byte_var(size_t byte_num, T input)
Definition loadstor.h:69