Botan  2.1.0
Crypto and TLS for C++11
msg_certificate.cpp
Go to the documentation of this file.
1 /*
2 * Certificate Message
3 * (C) 2004-2006,2012 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/tls_messages.h>
9 #include <botan/tls_extensions.h>
10 #include <botan/internal/tls_reader.h>
11 #include <botan/internal/tls_handshake_io.h>
12 #include <botan/internal/tls_handshake_hash.h>
13 #include <botan/der_enc.h>
14 #include <botan/ber_dec.h>
15 #include <botan/loadstor.h>
16 
17 namespace Botan {
18 
19 namespace TLS {
20 
21 /**
22 * Create a new Certificate message
23 */
26  const std::vector<X509_Certificate>& cert_list) :
27  m_certs(cert_list)
28  {
29  hash.update(io.send(*this));
30  }
31 
32 /**
33 * Deserialize a Certificate message
34 */
35 Certificate::Certificate(const std::vector<uint8_t>& buf, const Policy& /*policy_currently_unused*/)
36  {
37  if(buf.size() < 3)
38  throw Decoding_Error("Certificate: Message malformed");
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  const uint8_t* certs = buf.data() + 3;
46 
47  while(size_t remaining_bytes = buf.data() + buf.size() - certs)
48  {
49  if(remaining_bytes < 3)
50  throw Decoding_Error("Certificate: Message malformed");
51 
52  const size_t cert_size = make_uint32(0, certs[0], certs[1], certs[2]);
53 
54  if(remaining_bytes < (3 + cert_size))
55  throw Decoding_Error("Certificate: Message malformed");
56 
57  DataSource_Memory cert_buf(&certs[3], cert_size);
58  m_certs.push_back(X509_Certificate(cert_buf));
59 
60  certs += cert_size + 3;
61  }
62  }
63 
64 /**
65 * Serialize a Certificate message
66 */
67 std::vector<uint8_t> Certificate::serialize() const
68  {
69  std::vector<uint8_t> buf(3);
70 
71  for(size_t i = 0; i != m_certs.size(); ++i)
72  {
73  std::vector<uint8_t> raw_cert = m_certs[i].BER_encode();
74  const size_t cert_size = raw_cert.size();
75  for(size_t j = 0; j != 3; ++j)
76  {
77  buf.push_back(get_byte(j+1, static_cast<uint32_t>(cert_size)));
78  }
79  buf += raw_cert;
80  }
81 
82  const size_t buf_size = buf.size() - 3;
83  for(size_t i = 0; i != 3; ++i)
84  buf[i] = get_byte(i+1, static_cast<uint32_t>(buf_size));
85 
86  return buf;
87  }
88 
89 }
90 
91 }
virtual std::vector< uint8_t > send(const Handshake_Message &msg)=0
Certificate(Handshake_IO &io, Handshake_Hash &hash, const std::vector< X509_Certificate > &certs)
void update(const uint8_t in[], size_t length)
Definition: alg_id.cpp:13
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:47
uint32_t make_uint32(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3)
Definition: loadstor.h:73
MechanismType hash