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
|
/*************************************************
* PKCS #10/Self Signed Cert Creation Source File *
* (C) 1999-2006 The Botan Project *
*************************************************/
#include <botan/x509self.h>
#include <botan/x509_ext.h>
#include <botan/x509_ca.h>
#include <botan/der_enc.h>
#include <botan/config.h>
#include <botan/look_pk.h>
#include <botan/oids.h>
#include <botan/pipe.h>
#include <memory>
namespace Botan {
namespace {
/*************************************************
* Shared setup for self-signed items *
*************************************************/
MemoryVector<byte> shared_setup(const X509_Cert_Options& opts,
const PKCS8_PrivateKey& key)
{
const PKCS8_PrivateKey* key_pointer = &key;
if(!dynamic_cast<const PK_Signing_Key*>(key_pointer))
throw Invalid_Argument("Key type " + key.algo_name() + " cannot sign");
opts.sanity_check();
Pipe key_encoder;
key_encoder.start_msg();
X509::encode(key, key_encoder, RAW_BER);
key_encoder.end_msg();
return key_encoder.read_all();
}
/*************************************************
* Load information from the X509_Cert_Options *
*************************************************/
void load_info(const X509_Cert_Options& opts, X509_DN& subject_dn,
AlternativeName& subject_alt)
{
subject_dn.add_attribute("X520.CommonName", opts.common_name);
subject_dn.add_attribute("X520.Country", opts.country);
subject_dn.add_attribute("X520.State", opts.state);
subject_dn.add_attribute("X520.Locality", opts.locality);
subject_dn.add_attribute("X520.Organization", opts.organization);
subject_dn.add_attribute("X520.OrganizationalUnit", opts.org_unit);
subject_dn.add_attribute("X520.SerialNumber", opts.serial_number);
subject_alt = AlternativeName(opts.email, opts.uri, opts.dns);
subject_alt.add_othername(OIDS::lookup("PKIX.XMPPAddr"),
opts.xmpp, UTF8_STRING);
}
/*************************************************
* Choose a signing format for the key *
*************************************************/
PK_Signer* choose_sig_format(const PKCS8_PrivateKey& key,
AlgorithmIdentifier& sig_algo)
{
std::string padding;
Signature_Format format;
Config::choose_sig_format(key.algo_name(), padding, format);
sig_algo.oid = OIDS::lookup(key.algo_name() + "/" + padding);
sig_algo.parameters = key.DER_encode_params();
const PK_Signing_Key& sig_key = dynamic_cast<const PK_Signing_Key&>(key);
return get_pk_signer(sig_key, padding, format);
}
}
namespace X509 {
/*************************************************
* Create a new self-signed X.509 certificate *
*************************************************/
X509_Certificate create_self_signed_cert(const X509_Cert_Options& opts,
const PKCS8_PrivateKey& key)
{
AlgorithmIdentifier sig_algo;
X509_DN subject_dn;
AlternativeName subject_alt;
MemoryVector<byte> pub_key = shared_setup(opts, key);
std::auto_ptr<PK_Signer> signer(choose_sig_format(key, sig_algo));
load_info(opts, subject_dn, subject_alt);
Key_Constraints constraints;
if(opts.is_CA)
constraints = Key_Constraints(KEY_CERT_SIGN | CRL_SIGN);
else
constraints = find_constraints(key, opts.constraints);
Extensions extensions;
extensions.add(new Cert_Extension::Subject_Key_ID(pub_key));
extensions.add(new Cert_Extension::Key_Usage(constraints));
extensions.add(
new Cert_Extension::Extended_Key_Usage(opts.ex_constraints));
extensions.add(
new Cert_Extension::Subject_Alternative_Name(subject_alt));
extensions.add(
new Cert_Extension::Basic_Constraints(opts.is_CA, opts.path_limit));
return X509_CA::make_cert(signer.get(), sig_algo, pub_key,
opts.start, opts.end,
subject_dn, subject_dn,
extensions);
}
/*************************************************
* Create a PKCS #10 certificate request *
*************************************************/
PKCS10_Request create_cert_req(const X509_Cert_Options& opts,
const PKCS8_PrivateKey& key)
{
AlgorithmIdentifier sig_algo;
X509_DN subject_dn;
AlternativeName subject_alt;
MemoryVector<byte> pub_key = shared_setup(opts, key);
std::auto_ptr<PK_Signer> signer(choose_sig_format(key, sig_algo));
load_info(opts, subject_dn, subject_alt);
const u32bit PKCS10_VERSION = 0;
Extensions extensions;
extensions.add(
new Cert_Extension::Basic_Constraints(opts.is_CA, opts.path_limit));
extensions.add(
new Cert_Extension::Key_Usage(
opts.is_CA ? Key_Constraints(KEY_CERT_SIGN | CRL_SIGN) :
find_constraints(key, opts.constraints)
)
);
extensions.add(
new Cert_Extension::Extended_Key_Usage(opts.ex_constraints));
extensions.add(
new Cert_Extension::Subject_Alternative_Name(subject_alt));
DER_Encoder tbs_req;
tbs_req.start_cons(SEQUENCE)
.encode(PKCS10_VERSION)
.encode(subject_dn)
.raw_bytes(pub_key)
.start_explicit(0);
if(opts.challenge != "")
{
ASN1_String challenge(opts.challenge, DIRECTORY_STRING);
tbs_req.encode(
Attribute("PKCS9.ChallengePassword",
DER_Encoder().encode(challenge).get_contents()
)
);
}
tbs_req.encode(
Attribute("PKCS9.ExtensionRequest",
DER_Encoder()
.start_cons(SEQUENCE)
.encode(extensions)
.end_cons()
.get_contents()
)
)
.end_explicit()
.end_cons();
DataSource_Memory source(
X509_Object::make_signed(signer.get(), sig_algo,
tbs_req.get_contents())
);
return PKCS10_Request(source);
}
}
}
|