Botan 1.10.17

cert_req.cpp
Go to the documentation of this file.
1 /*
2 * Certificate Request Message
3 * (C) 2004-2006 Jack Lloyd
4 *
5 * Released under the terms of the Botan license
6 */
7
8 #include <botan/internal/tls_messages.h>
9 #include <botan/internal/tls_reader.h>
10 #include <botan/der_enc.h>
11 #include <botan/ber_dec.h>
12 #include <botan/loadstor.h>
13 #include <botan/secqueue.h>
14
15 namespace Botan {
16
17 /**
18 * Create a new Certificate Request message
19 */
20 Certificate_Req::Certificate_Req(Record_Writer& writer,
21 HandshakeHash& hash,
22 const std::vector<X509_Certificate>& certs)
23 {
24 for(size_t i = 0; i != certs.size(); ++i)
25 names.push_back(certs[i].subject_dn());
26
27 // FIXME: should be able to choose what to ask for
28 types.push_back(RSA_CERT);
29 types.push_back(DSS_CERT);
30
31 send(writer, hash);
32 }
33
34 /**
35 * Serialize a Certificate Request message
36 */
37 SecureVector<byte> Certificate_Req::serialize() const
38 {
39 SecureVector<byte> buf;
40
41 append_tls_length_value(buf, types, 1);
42
43 DER_Encoder encoder;
44 for(size_t i = 0; i != names.size(); ++i)
45 encoder.encode(names[i]);
46
47 append_tls_length_value(buf, encoder.get_contents(), 2);
48
49 return buf;
50 }
51
52 /**
53 * Deserialize a Certificate Request message
54 */
55 void Certificate_Req::deserialize(const MemoryRegion<byte>& buf)
56 {
57 if(buf.size() < 4)
58 throw Decoding_Error("Certificate_Req: Bad certificate request");
59
60 size_t types_size = buf[0];
61
62 if(buf.size() < types_size + 3)
63 throw Decoding_Error("Certificate_Req: Bad certificate request");
64
65 for(size_t i = 0; i != types_size; ++i)
66 types.push_back(static_cast<Certificate_Type>(buf[i+1]));
67
68 size_t names_size = make_u16bit(buf[types_size+2], buf[types_size+3]);
69
70 if(buf.size() != names_size + types_size + 3)
71 throw Decoding_Error("Certificate_Req: Bad certificate request");
72
73 BER_Decoder decoder(&buf[types_size + 3], names_size);
74
75 while(decoder.more_items())
76 {
77 X509_DN name;
78 decoder.decode(name);
79 names.push_back(name);
80 }
81 }
82
83 /**
84 * Create a new Certificate message
85 */
86 Certificate::Certificate(Record_Writer& writer,
87 const std::vector<X509_Certificate>& cert_list,
88 HandshakeHash& hash)
89 {
90 certs = cert_list;
91 send(writer, hash);
92 }
93
94 /**
95 * Serialize a Certificate message
96 */
97 SecureVector<byte> Certificate::serialize() const
98 {
99 SecureVector<byte> buf(3);
100
101 for(size_t i = 0; i != certs.size(); ++i)
102 {
103 SecureVector<byte> raw_cert = certs[i].BER_encode();
104 const size_t cert_size = raw_cert.size();
105 for(size_t i = 0; i != 3; ++i)
106 buf.push_back(get_byte<u32bit>(i+1, cert_size));
107 buf += raw_cert;
108 }
109
110 const size_t buf_size = buf.size() - 3;
111 for(size_t i = 0; i != 3; ++i)
112 buf[i] = get_byte<u32bit>(i+1, buf_size);
113
114 return buf;
115 }
116
117 /**
118 * Deserialize a Certificate message
119 */
120 void Certificate::deserialize(const MemoryRegion<byte>& buf)
121 {
122 if(buf.size() < 3)
123 throw Decoding_Error("Certificate: Message malformed");
124
125 const size_t total_size = make_u32bit(0, buf[0], buf[1], buf[2]);
126
127 SecureQueue queue;
128 queue.write(&buf[3], buf.size() - 3);
129
130 if(queue.size() != total_size)
131 throw Decoding_Error("Certificate: Message malformed");
132
133 while(queue.size())
134 {
135 if(queue.size() < 3)
136 throw Decoding_Error("Certificate: Message malformed");
137
138 byte len[3];
139 queue.read(len, 3);
140
141 const size_t cert_size = make_u32bit(0, len[0], len[1], len[2]);
142 const size_t original_size = queue.size();
143
144 X509_Certificate cert(queue);
145 if(queue.size() + cert_size != original_size)
146 throw Decoding_Error("Certificate: Message malformed");
147 certs.push_back(cert);
148 }
149 }
150
151 }
Botan::append_tls_length_value
void append_tls_length_value(MemoryRegion< byte > &buf, const T *vals, size_t vals_size, size_t tag_size)
Definition: tls_reader.h:145
Botan::DER_Encoder::get_contents
SecureVector< byte > get_contents()
Definition: der_enc.cpp:122
Botan::HandshakeMessage::send
void send(Record_Writer &, HandshakeHash &) const
Definition: hello.cpp:16
Botan::Certificate::Certificate
Certificate(Record_Writer &, const std::vector< X509_Certificate > &, HandshakeHash &)
Definition: cert_req.cpp:86
Botan::RSA_CERT
Definition: tls_magic.h:93
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209
Botan::DER_Encoder
Definition: der_enc.h:22
Botan
Definition: algo_base.h:14
Botan::Record_Writer
Definition: tls_record.h:39
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::make_u16bit
u16bit make_u16bit(byte i0, byte i1)
Definition: loadstor.h:47
Botan::HandshakeHash
Definition: tls_handshake_hash.h:20
Botan::SecureVector< byte >
Botan::make_u32bit
u32bit make_u32bit(byte i0, byte i1, byte i2, byte i3)
Definition: loadstor.h:60
Botan::DSS_CERT
Definition: tls_magic.h:94
Botan::Certificate_Req::Certificate_Req
Certificate_Req(Record_Writer &, HandshakeHash &, const std::vector< X509_Certificate > &)
Definition: cert_req.cpp:20