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
254
255
|
/*************************************************
* X.509 Public Key Source File *
* (C) 1999-2006 The Botan Project *
*************************************************/
#include <botan/x509_key.h>
#include <botan/filters.h>
#include <botan/asn1_obj.h>
#include <botan/der_enc.h>
#include <botan/ber_dec.h>
#include <botan/pk_algs.h>
#include <botan/oids.h>
#include <botan/pem.h>
#include <memory>
namespace Botan {
/*************************************************
* Compute the key id *
*************************************************/
u64bit X509_PublicKey::key_id() const
{
Pipe pipe(new Hash_Filter("SHA-1", 8));
std::auto_ptr<X509_Encoder> encoder(x509_encoder());
if(!encoder.get())
throw Internal_Error("X509_PublicKey:key_id: No encoder found");
pipe.start_msg();
pipe.write(algo_name());
pipe.write(encoder->alg_id().parameters);
pipe.write(encoder->key_bits());
//pipe.write(DER_encode_pub());
//pipe.write(DER_encode_params());
pipe.end_msg();
SecureVector<byte> output = pipe.read_all();
if(output.size() != 8)
throw Internal_Error("X509_PublicKey::key_id: Incorrect output size");
u64bit id = 0;
for(u32bit j = 0; j != 8; ++j)
id = (id << 8) | output[j];
return id;
}
/*************************************************
* Return an encoder for this key *
*************************************************/
/*
X509_Encoder* X509_PublicKey::x509_encoder() const
{
class X509_Default_Encoder : public X509_Encoder
{
public:
AlgorithmIdentifier alg_id() const { return id; }
MemoryVector<byte> key_bits() const { return bits; }
X509_Default_Encoder(const X509_PublicKey* key)
{
id = AlgorithmIdentifier(key->get_oid(), key->DER_encode_params());
bits = key->DER_encode_pub();
}
private:
AlgorithmIdentifier id;
MemoryVector<byte> bits;
};
return new X509_Default_Encoder(this);
}
*/
/*
X509_Decoder* X509_PublicKey::x509_decoder()
{
class X509_Default_Decoder : public X509_Decoder
{
public:
void alg_id(const AlgorithmIdentifier& alg_id)
{
DataSource_Memory params(alg_id.parameters);
key->BER_decode_params(params);
}
void key_bits(const MemoryRegion<byte>& bits)
{
DataSource_Memory key_bits(bits);
key->BER_decode_pub(key_bits);
}
X509_Default_Decoder(X509_PublicKey* k) : key(k) {}
private:
X509_PublicKey* key;
};
return new X509_Default_Decoder(this);
}
*/
namespace X509 {
/*************************************************
* DER or PEM encode a X.509 public key *
*************************************************/
void encode(const X509_PublicKey& key, Pipe& pipe, X509_Encoding encoding)
{
std::auto_ptr<X509_Encoder> encoder(key.x509_encoder());
if(!encoder.get())
throw Encoding_Error("X509::encode: Key does not support encoding");
MemoryVector<byte> der =
DER_Encoder()
.start_cons(SEQUENCE)
.encode(encoder->alg_id())
.encode(encoder->key_bits(), BIT_STRING)
.end_cons()
.get_contents();
if(encoding == PEM)
pipe.write(PEM_Code::encode(der, "PUBLIC KEY"));
else
pipe.write(der);
}
/*************************************************
* PEM encode a X.509 public key *
*************************************************/
std::string PEM_encode(const X509_PublicKey& key)
{
Pipe pem;
pem.start_msg();
encode(key, pem, PEM);
pem.end_msg();
return pem.read_all_as_string();
}
/*************************************************
* Extract a public key and return it *
*************************************************/
X509_PublicKey* load_key(DataSource& source)
{
try {
AlgorithmIdentifier alg_id;
MemoryVector<byte> key_bits;
if(ASN1::maybe_BER(source) && !PEM_Code::matches(source))
{
BER_Decoder(source)
.start_cons(SEQUENCE)
.decode(alg_id)
.decode(key_bits, BIT_STRING)
.verify_end()
.end_cons();
}
else
{
DataSource_Memory ber(
PEM_Code::decode_check_label(source, "PUBLIC KEY")
);
BER_Decoder(ber)
.start_cons(SEQUENCE)
.decode(alg_id)
.decode(key_bits, BIT_STRING)
.verify_end()
.end_cons();
}
if(key_bits.is_empty())
throw Decoding_Error("X.509 public key decoding failed");
const std::string alg_name = OIDS::lookup(alg_id.oid);
if(alg_name == "")
throw Decoding_Error("Unknown algorithm OID: " +
alg_id.oid.as_string());
std::auto_ptr<X509_PublicKey> key_obj(get_public_key(alg_name));
if(!key_obj.get())
throw Decoding_Error("Unknown PK algorithm/OID: " + alg_name + ", " +
alg_id.oid.as_string());
std::auto_ptr<X509_Decoder> decoder(key_obj->x509_decoder());
if(!decoder.get())
throw Decoding_Error("Key does not support X.509 decoding");
decoder->alg_id(alg_id);
decoder->key_bits(key_bits);
return key_obj.release();
}
catch(Decoding_Error)
{
throw Decoding_Error("X.509 public key decoding failed");
}
}
/*************************************************
* Extract a public key and return it *
*************************************************/
X509_PublicKey* load_key(const std::string& fsname)
{
DataSource_Stream source(fsname, true);
return X509::load_key(source);
}
/*************************************************
* Extract a public key and return it *
*************************************************/
X509_PublicKey* load_key(const MemoryRegion<byte>& mem)
{
DataSource_Memory source(mem);
return X509::load_key(source);
}
/*************************************************
* Make a copy of this public key *
*************************************************/
X509_PublicKey* copy_key(const X509_PublicKey& key)
{
Pipe bits;
bits.start_msg();
X509::encode(key, bits, RAW_BER);
bits.end_msg();
DataSource_Memory source(bits.read_all());
return X509::load_key(source);
}
/*************************************************
* Find the allowable key constraints *
*************************************************/
Key_Constraints find_constraints(const X509_PublicKey& pub_key,
Key_Constraints limits)
{
const X509_PublicKey* key = &pub_key;
u32bit constraints = 0;
if(dynamic_cast<const PK_Encrypting_Key*>(key))
constraints |= KEY_ENCIPHERMENT;
if(dynamic_cast<const PK_Key_Agreement_Key*>(key))
constraints |= KEY_AGREEMENT;
if(dynamic_cast<const PK_Verifying_wo_MR_Key*>(key) ||
dynamic_cast<const PK_Verifying_with_MR_Key*>(key))
constraints |= DIGITAL_SIGNATURE | NON_REPUDIATION;
if(limits)
constraints &= limits;
return Key_Constraints(constraints);
}
}
}
|