• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.59
/src/lib/tls/msg_cert_req.cpp
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.h>
11

12
#include <botan/ber_dec.h>
13
#include <botan/der_enc.h>
14
#include <botan/tls_extensions.h>
15
#include <botan/internal/tls_handshake_hash.h>
16
#include <botan/internal/tls_handshake_io.h>
17
#include <botan/internal/tls_reader.h>
18

19
namespace Botan::TLS {
20

21
Handshake_Type Certificate_Request_12::type() const { return Handshake_Type::CertificateRequest; }
621✔
22

23
namespace {
24

25
std::string cert_type_code_to_name(uint8_t code) {
357✔
26
   switch(code) {
357✔
27
      case 1:
178✔
28
         return "RSA";
178✔
29
      case 64:
178✔
30
         return "ECDSA";
178✔
31
      default:
1✔
32
         return "";  // DH or something else
1✔
33
   }
34
}
35

36
uint8_t cert_type_name_to_code(std::string_view name) {
466✔
37
   if(name == "RSA")
466✔
38
      return 1;
233✔
39
   if(name == "ECDSA")
233✔
40
      return 64;
233✔
41

42
   throw Invalid_Argument(fmt("Unknown/unhandled TLS cert type {}", name));
×
43
}
44

45
}
46

47
/**
48
* Create a new Certificate Request message
49
*/
50
Certificate_Request_12::Certificate_Request_12(Handshake_IO& io,
233✔
51
                                               Handshake_Hash& hash,
52
                                               const Policy& policy,
53
                                               const std::vector<X509_DN>& ca_certs) :
233✔
54
      m_names(ca_certs), m_cert_key_types({"RSA", "ECDSA"}) {
932✔
55
   m_schemes = policy.acceptable_signature_schemes();
233✔
56
   hash.update(io.send(*this));
466✔
57
}
233✔
58

59
/**
60
* Deserialize a Certificate Request message
61
*/
62
Certificate_Request_12::Certificate_Request_12(const std::vector<uint8_t>& buf) {
178✔
63
   if(buf.size() < 4)
178✔
64
      throw Decoding_Error("Certificate_Req: Bad certificate request");
×
65

66
   TLS_Data_Reader reader("CertificateRequest", buf);
178✔
67

68
   const auto cert_type_codes = reader.get_range_vector<uint8_t>(1, 1, 255);
178✔
69

70
   for(const auto cert_type_code : cert_type_codes) {
535✔
71
      const std::string cert_type_name = cert_type_code_to_name(cert_type_code);
357✔
72

73
      if(cert_type_name.empty())  // something we don't know
357✔
74
         continue;
1✔
75

76
      m_cert_key_types.emplace_back(cert_type_name);
356✔
77
   }
357✔
78

79
   const std::vector<uint8_t> algs = reader.get_range_vector<uint8_t>(2, 2, 65534);
178✔
80

81
   if(algs.size() % 2 != 0)
176✔
82
      throw Decoding_Error("Bad length for signature IDs in certificate request");
×
83

84
   for(size_t i = 0; i != algs.size(); i += 2) {
1,456✔
85
      m_schemes.emplace_back(make_uint16(algs[i], algs[i + 1]));
1,282✔
86
   }
87

88
   const uint16_t purported_size = reader.get_uint16_t();
176✔
89

90
   if(reader.remaining_bytes() != purported_size)
176✔
91
      throw Decoding_Error("Inconsistent length in certificate request");
2✔
92

93
   while(reader.has_remaining()) {
336✔
94
      std::vector<uint8_t> name_bits = reader.get_range_vector<uint8_t>(2, 0, 65535);
162✔
95

96
      BER_Decoder decoder(name_bits.data(), name_bits.size());
162✔
97
      X509_DN name;
162✔
98
      decoder.decode(name);
99
      m_names.emplace_back(name);
162✔
100
   }
324✔
101
}
360✔
102

103
const std::vector<std::string>& Certificate_Request_12::acceptable_cert_types() const { return m_cert_key_types; }
174✔
104

105
const std::vector<X509_DN>& Certificate_Request_12::acceptable_CAs() const { return m_names; }
174✔
106

107
const std::vector<Signature_Scheme>& Certificate_Request_12::signature_schemes() const { return m_schemes; }
206✔
108

109
/**
110
* Serialize a Certificate Request message
111
*/
112
std::vector<uint8_t> Certificate_Request_12::serialize() const {
233✔
113
   std::vector<uint8_t> buf;
233✔
114

115
   std::vector<uint8_t> cert_types;
233✔
116

117
   cert_types.reserve(m_cert_key_types.size());
233✔
118
   for(const auto& cert_key_type : m_cert_key_types)
699✔
119
      cert_types.push_back(cert_type_name_to_code(cert_key_type));
466✔
120

121
   append_tls_length_value(buf, cert_types, 1);
233✔
122

123
   if(!m_schemes.empty())
233✔
124
      buf += Signature_Algorithms(m_schemes).serialize(Connection_Side::Server);
466✔
125

126
   std::vector<uint8_t> encoded_names;
233✔
127

128
   for(const auto& name : m_names) {
389✔
129
      DER_Encoder encoder;
156✔
130
      encoder.encode(name);
156✔
131

132
      append_tls_length_value(encoded_names, encoder.get_contents(), 2);
312✔
133
   }
156✔
134

135
   append_tls_length_value(buf, encoded_names, 2);
233✔
136

137
   return buf;
233✔
138
}
466✔
139
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc