1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*************************************************
* PKCS #10 Source File *
* (C) 1999-2006 The Botan Project *
*************************************************/
#include <botan/pkcs10.h>
#include <botan/der_enc.h>
#include <botan/ber_dec.h>
#include <botan/parsing.h>
#include <botan/x509stor.h>
#include <botan/x509_ext.h>
#include <botan/oids.h>
#include <botan/pem.h>
namespace Botan {
/*************************************************
* PKCS10_Request Constructor *
*************************************************/
PKCS10_Request::PKCS10_Request(DataSource& in) :
X509_Object(in, "CERTIFICATE REQUEST/NEW CERTIFICATE REQUEST")
{
do_decode();
}
/*************************************************
* PKCS10_Request Constructor *
*************************************************/
PKCS10_Request::PKCS10_Request(const std::string& in) :
X509_Object(in, "CERTIFICATE REQUEST/NEW CERTIFICATE REQUEST")
{
do_decode();
}
/*************************************************
* Deocde the CertificateRequestInfo *
*************************************************/
void PKCS10_Request::force_decode()
{
BER_Decoder cert_req_info(tbs_bits);
u32bit version;
cert_req_info.decode(version);
if(version != 0)
throw Decoding_Error("Unknown version code in PKCS #10 request: " +
to_string(version));
X509_DN dn_subject;
cert_req_info.decode(dn_subject);
info.add(dn_subject.contents());
BER_Object public_key = cert_req_info.get_next_object();
if(public_key.type_tag != SEQUENCE || public_key.class_tag != CONSTRUCTED)
throw BER_Bad_Tag("PKCS10_Request: Unexpected tag for public key",
public_key.type_tag, public_key.class_tag);
info.add("X509.Certificate.public_key",
PEM_Code::encode(
ASN1::put_in_sequence(public_key.value),
"PUBLIC KEY"
)
);
BER_Object attr_bits = cert_req_info.get_next_object();
if(attr_bits.type_tag == 0 &&
attr_bits.class_tag == ASN1_Tag(CONSTRUCTED | CONTEXT_SPECIFIC))
{
BER_Decoder attributes(attr_bits.value);
while(attributes.more_items())
{
Attribute attr;
attributes.decode(attr);
handle_attribute(attr);
}
attributes.verify_end();
}
else if(attr_bits.type_tag != NO_OBJECT)
throw BER_Bad_Tag("PKCS10_Request: Unexpected tag for attributes",
attr_bits.type_tag, attr_bits.class_tag);
cert_req_info.verify_end();
X509_Code sig_check = X509_Store::check_sig(*this, subject_public_key());
if(sig_check != VERIFIED)
throw Decoding_Error("PKCS #10 request: Bad signature detected");
}
/*************************************************
* Handle attributes in a PKCS #10 request *
*************************************************/
void PKCS10_Request::handle_attribute(const Attribute& attr)
{
BER_Decoder value(attr.parameters);
if(attr.oid == OIDS::lookup("PKCS9.EmailAddress"))
{
ASN1_String email;
value.decode(email);
info.add("RFC822", email.value());
}
else if(attr.oid == OIDS::lookup("PKCS9.ChallengePassword"))
{
ASN1_String challenge_password;
value.decode(challenge_password);
info.add("PKCS9.ChallengePassword", challenge_password.value());
}
else if(attr.oid == OIDS::lookup("PKCS9.ExtensionRequest"))
{
BER_Decoder sequence = value.start_cons(SEQUENCE);
while(sequence.more_items())
{
Extension extn;
sequence.decode(extn);
handle_v3_extension(extn);
}
sequence.verify_end();
}
}
/*************************************************
* Decode a requested X.509v3 extension *
*************************************************/
void PKCS10_Request::handle_v3_extension(const Extension& extn)
{
BER_Decoder value(extn.value);
if(extn.oid == OIDS::lookup("X509v3.KeyUsage"))
{
Key_Constraints constraints;
BER::decode(value, constraints);
if(constraints != NO_CONSTRAINTS)
info.add("X509v3.KeyUsage", constraints);
}
else if(extn.oid == OIDS::lookup("X509v3.ExtendedKeyUsage"))
{
BER_Decoder key_usage = value.start_cons(SEQUENCE);
while(key_usage.more_items())
{
OID usage_oid;
key_usage.decode(usage_oid);
info.add("X509v3.ExtendedKeyUsage", usage_oid.as_string());
}
}
else if(extn.oid == OIDS::lookup("X509v3.BasicConstraints"))
{
u32bit max_path_len = 0;
bool is_ca = false;
value.start_cons(SEQUENCE)
.decode_optional(is_ca, BOOLEAN, UNIVERSAL, false)
.decode_optional(max_path_len, INTEGER, UNIVERSAL,
NO_CERT_PATH_LIMIT)
.verify_end()
.end_cons();
info.add("X509v3.BasicConstraints.is_ca", (is_ca ? 1 : 0));
info.add("X509v3.BasicConstraints.path_constraint", max_path_len);
}
else if(extn.oid == OIDS::lookup("X509v3.SubjectAlternativeName"))
{
AlternativeName alt_name;
value.decode(alt_name);
info.add(alt_name.contents());
}
else
return;
value.verify_end();
}
/*************************************************
* Return the challenge password (if any) *
*************************************************/
std::string PKCS10_Request::challenge_password() const
{
return info.get1("PKCS9.ChallengePassword");
}
/*************************************************
* Return the name of the requestor *
*************************************************/
X509_DN PKCS10_Request::subject_dn() const
{
return create_dn(info);
}
/*************************************************
* Return the public key of the requestor *
*************************************************/
MemoryVector<byte> PKCS10_Request::raw_public_key() const
{
DataSource_Memory source(info.get1("X509.Certificate.public_key"));
return PEM_Code::decode_check_label(source, "PUBLIC KEY");
}
/*************************************************
* Return the public key of the requestor *
*************************************************/
X509_PublicKey* PKCS10_Request::subject_public_key() const
{
DataSource_Memory source(info.get1("X509.Certificate.public_key"));
return X509::load_key(source);
}
/*************************************************
* Return the alternative names of the requestor *
*************************************************/
AlternativeName PKCS10_Request::subject_alt_name() const
{
return create_alt_name(info);
}
/*************************************************
* Return the key constraints (if any) *
*************************************************/
Key_Constraints PKCS10_Request::constraints() const
{
return Key_Constraints(info.get1_u32bit("X509v3.KeyUsage", NO_CONSTRAINTS));
}
/*************************************************
* Return the extendend key constraints (if any) *
*************************************************/
std::vector<OID> PKCS10_Request::ex_constraints() const
{
std::vector<std::string> oids = info.get("X509v3.ExtendedKeyUsage");
std::vector<OID> result;
for(u32bit j = 0; j != oids.size(); ++j)
result.push_back(OIDS::lookup(oids[j]));
return result;
}
/*************************************************
* Return is a CA certificate is requested *
*************************************************/
bool PKCS10_Request::is_CA() const
{
return info.get1_u32bit("X509v3.BasicConstraints.is_ca");
}
/*************************************************
* Return the desired path limit (if any) *
*************************************************/
u32bit PKCS10_Request::path_limit() const
{
return info.get1_u32bit("X509v3.BasicConstraints.path_constraint", 0);
}
}
|