blob: 4714b128597a27ac02cf8daf26343e8bd47e2b48 (
plain)
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
|
/*
* X.509 Public Key
* (C) 1999-2010 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#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/pem.h>
#include <botan/internal/pk_algs.h>
#include <memory>
namespace Botan {
namespace X509 {
MemoryVector<byte> BER_encode(const Public_Key& key)
{
return DER_Encoder()
.start_cons(SEQUENCE)
.encode(key.algorithm_identifier())
.encode(key.x509_subject_public_key(), BIT_STRING)
.end_cons()
.get_contents();
}
/*
* PEM encode a X.509 public key
*/
std::string PEM_encode(const Public_Key& key)
{
return PEM_Code::encode(X509::BER_encode(key),
"PUBLIC KEY");
}
/*
* Extract a public key and return it
*/
Public_Key* 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.empty())
throw Decoding_Error("X.509 public key decoding failed");
return make_public_key(alg_id, key_bits);
}
catch(Decoding_Error)
{
throw Decoding_Error("X.509 public key decoding failed");
}
}
/*
* Extract a public key and return it
*/
Public_Key* load_key(const std::string& fsname)
{
DataSource_Stream source(fsname, true);
return X509::load_key(source);
}
/*
* Extract a public key and return it
*/
Public_Key* load_key(const MemoryRegion<byte>& mem)
{
DataSource_Memory source(mem);
return X509::load_key(source);
}
/*
* Make a copy of this public key
*/
Public_Key* copy_key(const Public_Key& key)
{
DataSource_Memory source(PEM_encode(key));
return X509::load_key(source);
}
/*
* Find the allowable key constraints
*/
Key_Constraints find_constraints(const Public_Key& pub_key,
Key_Constraints limits)
{
const std::string name = pub_key.algo_name();
size_t constraints = 0;
if(name == "DH" || name == "ECDH")
constraints |= KEY_AGREEMENT;
if(name == "RSA" || name == "ElGamal")
constraints |= KEY_ENCIPHERMENT | DATA_ENCIPHERMENT;
if(name == "RSA" || name == "RW" || name == "NR" ||
name == "DSA" || name == "ECDSA")
constraints |= DIGITAL_SIGNATURE | NON_REPUDIATION;
if(limits)
constraints &= limits;
return Key_Constraints(constraints);
}
}
}
|