/src/botan/src/lib/tls/msg_certificate.cpp
Line | Count | Source (jump to first uncovered line) |
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/loadstor.h> |
14 | | #include <botan/data_src.h> |
15 | | |
16 | | namespace Botan { |
17 | | |
18 | | namespace TLS { |
19 | | |
20 | | /** |
21 | | * Create a new Certificate message |
22 | | */ |
23 | | Certificate::Certificate(Handshake_IO& io, |
24 | | Handshake_Hash& hash, |
25 | | const std::vector<X509_Certificate>& cert_list) : |
26 | | m_certs(cert_list) |
27 | 138 | { |
28 | 138 | hash.update(io.send(*this)); |
29 | 138 | } |
30 | | |
31 | | /** |
32 | | * Deserialize a Certificate message |
33 | | */ |
34 | | Certificate::Certificate(const std::vector<uint8_t>& buf, const Policy& policy) |
35 | 2.75k | { |
36 | 2.75k | if(buf.size() < 3) |
37 | 2 | throw Decoding_Error("Certificate: Message malformed"); |
38 | 2.75k | |
39 | 2.75k | const size_t total_size = make_uint32(0, buf[0], buf[1], buf[2]); |
40 | 2.75k | |
41 | 2.75k | if(total_size != buf.size() - 3) |
42 | 41 | throw Decoding_Error("Certificate: Message malformed"); |
43 | 2.71k | |
44 | 2.71k | const size_t max_size = policy.maximum_certificate_chain_size(); |
45 | 2.71k | if(max_size > 0 && total_size > max_size) |
46 | 0 | throw Decoding_Error("Certificate chain exceeds policy specified maximum size"); |
47 | 2.71k | |
48 | 2.71k | const uint8_t* certs = buf.data() + 3; |
49 | 2.71k | |
50 | 6.96k | while(size_t remaining_bytes = buf.data() + buf.size() - certs) |
51 | 4.42k | { |
52 | 4.42k | if(remaining_bytes < 3) |
53 | 1 | throw Decoding_Error("Certificate: Message malformed"); |
54 | 4.42k | |
55 | 4.42k | const size_t cert_size = make_uint32(0, certs[0], certs[1], certs[2]); |
56 | 4.42k | |
57 | 4.42k | if(remaining_bytes < (3 + cert_size)) |
58 | 168 | throw Decoding_Error("Certificate: Message malformed"); |
59 | 4.25k | |
60 | 4.25k | DataSource_Memory cert_buf(&certs[3], cert_size); |
61 | 4.25k | m_certs.push_back(X509_Certificate(cert_buf)); |
62 | 4.25k | |
63 | 4.25k | certs += cert_size + 3; |
64 | 4.25k | } |
65 | 2.71k | } |
66 | | |
67 | | /** |
68 | | * Serialize a Certificate message |
69 | | */ |
70 | | std::vector<uint8_t> Certificate::serialize() const |
71 | 138 | { |
72 | 138 | std::vector<uint8_t> buf(3); |
73 | 138 | |
74 | 274 | for(size_t i = 0; i != m_certs.size(); ++i) |
75 | 136 | { |
76 | 136 | std::vector<uint8_t> raw_cert = m_certs[i].BER_encode(); |
77 | 136 | const size_t cert_size = raw_cert.size(); |
78 | 544 | for(size_t j = 0; j != 3; ++j) |
79 | 408 | { |
80 | 408 | buf.push_back(get_byte(j+1, static_cast<uint32_t>(cert_size))); |
81 | 408 | } |
82 | 136 | buf += raw_cert; |
83 | 136 | } |
84 | 138 | |
85 | 138 | const size_t buf_size = buf.size() - 3; |
86 | 552 | for(size_t i = 0; i != 3; ++i) |
87 | 414 | buf[i] = get_byte(i+1, static_cast<uint32_t>(buf_size)); |
88 | 138 | |
89 | 138 | return buf; |
90 | 138 | } |
91 | | |
92 | | } |
93 | | |
94 | | } |