Botan 3.11.0
Crypto and TLS for C&
msg_certificate_req_12.cpp
Go to the documentation of this file.
1/*
2* Certificate Request Message
3* (C) 2004-2006,2012 Jack Lloyd
4* 2021 Elektrobit Automotive GmbH
5* 2022 René Meusel, Hannes Rantzsch - neXenio GmbH
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#include <botan/tls_messages_12.h>
11
12#include <botan/ber_dec.h>
13#include <botan/der_enc.h>
14#include <botan/pkix_types.h>
15#include <botan/tls_extensions.h>
16#include <botan/tls_policy.h>
17#include <botan/internal/fmt.h>
18#include <botan/internal/tls_handshake_hash.h>
19#include <botan/internal/tls_handshake_io.h>
20#include <botan/internal/tls_reader.h>
21
22namespace Botan::TLS {
23
25
29
30namespace {
31
32std::string cert_type_code_to_name(uint8_t code) {
33 switch(code) {
34 case 1:
35 return "RSA";
36 case 64:
37 return "ECDSA";
38 default:
39 return ""; // DH or something else
40 }
41}
42
43uint8_t cert_type_name_to_code(std::string_view name) {
44 if(name == "RSA") {
45 return 1;
46 }
47 if(name == "ECDSA") {
48 return 64;
49 }
50
51 throw Invalid_Argument(fmt("Unknown/unhandled TLS cert type {}", name));
52}
53
54} // namespace
55
56/**
57* Create a new Certificate Request message
58*/
60 Handshake_Hash& hash,
61 const Policy& policy,
62 const std::vector<X509_DN>& ca_certs) :
63 m_names(ca_certs), m_cert_key_types({"RSA", "ECDSA"}) {
64 m_schemes = policy.acceptable_signature_schemes();
65 hash.update(io.send(*this));
66}
67
68/**
69* Deserialize a Certificate Request message
70*/
71Certificate_Request_12::Certificate_Request_12(const std::vector<uint8_t>& buf) {
72 if(buf.size() < 4) {
73 throw Decoding_Error("Certificate_Req: Bad certificate request");
74 }
75
76 TLS_Data_Reader reader("CertificateRequest", buf);
77
78 const auto cert_type_codes = reader.get_range_vector<uint8_t>(1, 1, 255);
79
80 for(const auto cert_type_code : cert_type_codes) {
81 const std::string cert_type_name = cert_type_code_to_name(cert_type_code);
82
83 if(cert_type_name.empty()) { // something we don't know
84 continue;
85 }
86
87 m_cert_key_types.emplace_back(cert_type_name);
88 }
89
90 const std::vector<uint8_t> algs = reader.get_range_vector<uint8_t>(2, 2, 65534);
91
92 if(algs.size() % 2 != 0) {
93 throw Decoding_Error("Bad length for signature IDs in certificate request");
94 }
95
96 for(size_t i = 0; i != algs.size(); i += 2) {
97 m_schemes.emplace_back(make_uint16(algs[i], algs[i + 1]));
98 }
99
100 const uint16_t purported_size = reader.get_uint16_t();
101
102 if(reader.remaining_bytes() != purported_size) {
103 throw Decoding_Error("Inconsistent length in certificate request");
104 }
105
106 while(reader.has_remaining()) {
107 std::vector<uint8_t> name_bits = reader.get_range_vector<uint8_t>(2, 0, 65535);
108
109 BER_Decoder decoder(name_bits.data(), name_bits.size());
110 X509_DN name;
111 decoder.decode(name);
112 m_names.emplace_back(name);
113 }
114}
115
116const std::vector<std::string>& Certificate_Request_12::acceptable_cert_types() const {
117 return m_cert_key_types;
118}
119
120const std::vector<X509_DN>& Certificate_Request_12::acceptable_CAs() const {
121 return m_names;
122}
123
124const std::vector<Signature_Scheme>& Certificate_Request_12::signature_schemes() const {
125 return m_schemes;
126}
127
128/**
129* Serialize a Certificate Request message
130*/
131std::vector<uint8_t> Certificate_Request_12::serialize() const {
132 std::vector<uint8_t> buf;
133
134 std::vector<uint8_t> cert_types;
135
136 cert_types.reserve(m_cert_key_types.size());
137 for(const auto& cert_key_type : m_cert_key_types) {
138 cert_types.push_back(cert_type_name_to_code(cert_key_type));
139 }
140
141 append_tls_length_value(buf, cert_types, 1);
142
143 if(!m_schemes.empty()) {
145 }
146
147 std::vector<uint8_t> encoded_names;
148
149 for(const auto& name : m_names) {
150 DER_Encoder encoder;
151 encoder.encode(name);
152
153 append_tls_length_value(encoded_names, encoder.get_contents(), 2);
154 }
155
156 append_tls_length_value(buf, encoded_names, 2);
157
158 return buf;
159}
160} // namespace Botan::TLS
secure_vector< uint8_t > get_contents()
Definition der_enc.cpp:134
DER_Encoder & encode(bool b)
Definition der_enc.cpp:252
const std::vector< std::string > & acceptable_cert_types() const
const std::vector< Signature_Scheme > & signature_schemes() const
std::vector< uint8_t > serialize() const override
const std::vector< X509_DN > & acceptable_CAs() const
Certificate_Request_12(Handshake_IO &io, Handshake_Hash &hash, const Policy &policy, const std::vector< X509_DN > &allowed_cas)
Handshake_Type type() const override
std::vector< uint8_t > serialize(Connection_Side whoami) const override
size_t remaining_bytes() const
Definition tls_reader.h:37
std::vector< T > get_range_vector(size_t len_bytes, size_t min_elems, size_t max_elems)
Definition tls_reader.h:117
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:177
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr uint16_t make_uint16(uint8_t i0, uint8_t i1)
Definition loadstor.h:92