Botan 3.14.1
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#include <botan/internal/tls_reader.h>
20
21namespace Botan::TLS {
22
24
25/**
26* Create a new Certificate message
27*/
28Certificate_12::Certificate_12(Handshake_IO& io, Handshake_Hash& hash, std::vector<X509_Certificate> cert_list) :
29 m_certs(std::move(cert_list)) {
30 hash.update(io.send(*this));
31}
32
33/**
34* Deserialize a Certificate message
35*/
36Certificate_12::Certificate_12(std::span<const uint8_t> buf, const Policy& policy) {
37 if(buf.size() < 3) {
38 throw Decoding_Error("Certificate: Message malformed");
39 }
40
41 const size_t total_size = make_uint32(0, buf[0], buf[1], buf[2]);
42
43 if(total_size != buf.size() - 3) {
44 throw Decoding_Error("Certificate: Message malformed");
45 }
46
47 const size_t max_size = policy.maximum_certificate_chain_size();
48 if(max_size > 0 && total_size > max_size) {
49 throw Decoding_Error("Certificate chain exceeds policy specified maximum size");
50 }
51
52 const uint8_t* certs = buf.data() + 3;
53
54 while(const size_t remaining_bytes = buf.data() + buf.size() - certs) {
55 if(remaining_bytes < 3) {
56 throw Decoding_Error("Certificate: Message malformed");
57 }
58
59 const size_t cert_size = make_uint32(0, certs[0], certs[1], certs[2]);
60
61 if(remaining_bytes < (3 + cert_size)) {
62 throw Decoding_Error("Certificate: Message malformed");
63 }
64
65 DataSource_Memory cert_buf(&certs[3], cert_size);
66 try {
67 m_certs.push_back(X509_Certificate(cert_buf));
68 } catch(Exception& e) {
69 // bad_certificate would make more sense but BoGo expects decoding_error
70 throw TLS_Exception(Alert::DecodeError, e.what());
71 }
72
73 certs += cert_size + 3;
74 }
75
76 /*
77 * TLS 1.0 through 1.2 all seem to require that the certificate be
78 * precisely a v3 certificate. In fact the strict wording would seem
79 * to require that every certificate in the chain be v3. But often
80 * the intermediates are outside of the control of the server.
81 * But, require that the leaf certificate be v3
82 */
83 if(!m_certs.empty() && m_certs[0].x509_version() != 3) {
84 throw TLS_Exception(Alert::BadCertificate, "The leaf certificate must be v3");
85 }
86}
87
88/**
89* Serialize a Certificate message
90*/
91std::vector<uint8_t> Certificate_12::serialize() const {
92 // RFC 5246 7.4.2
93 // opaque ASN.1Cert<1..2^24-1>;
94 // [...]
95 // ASN.1Cert certificate_list<0..2^24-1>;
96 std::vector<uint8_t> certs;
97 for(const auto& cert : m_certs) {
98 append_tls_length_value(certs, cert.BER_encode(), 3);
99 }
100
101 std::vector<uint8_t> buf;
102 append_tls_length_value(buf, certs, 3);
103 return buf;
104}
105
106size_t Certificate_12::count() const {
107 return m_certs.size();
108}
109
110} // namespace Botan::TLS
const char * what() const noexcept override
Definition exceptn.h:94
std::vector< uint8_t > serialize() const override
Certificate_12(Handshake_IO &io, Handshake_Hash &hash, std::vector< X509_Certificate > certs)
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
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)
Definition tls_reader.h:182
constexpr uint32_t make_uint32(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3)
Definition loadstor.h:104