/src/botan/src/lib/pubkey/x509_key.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * X.509 Public Key |
3 | | * (C) 1999-2010 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #include <botan/x509_key.h> |
9 | | #include <botan/data_src.h> |
10 | | #include <botan/ber_dec.h> |
11 | | #include <botan/pem.h> |
12 | | #include <botan/alg_id.h> |
13 | | #include <botan/pk_algs.h> |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | | namespace X509 { |
18 | | |
19 | | std::vector<uint8_t> BER_encode(const Public_Key& key) |
20 | 0 | { |
21 | 0 | // keeping it around for compat |
22 | 0 | return key.subject_public_key(); |
23 | 0 | } |
24 | | |
25 | | /* |
26 | | * PEM encode a X.509 public key |
27 | | */ |
28 | | std::string PEM_encode(const Public_Key& key) |
29 | 0 | { |
30 | 0 | return PEM_Code::encode(key.subject_public_key(), |
31 | 0 | "PUBLIC KEY"); |
32 | 0 | } |
33 | | |
34 | | /* |
35 | | * Extract a public key and return it |
36 | | */ |
37 | | Public_Key* load_key(DataSource& source) |
38 | 10.3k | { |
39 | 10.3k | try { |
40 | 10.3k | AlgorithmIdentifier alg_id; |
41 | 10.3k | std::vector<uint8_t> key_bits; |
42 | 10.3k | |
43 | 10.3k | if(ASN1::maybe_BER(source) && !PEM_Code::matches(source)) |
44 | 10.3k | { |
45 | 10.3k | BER_Decoder(source) |
46 | 10.3k | .start_cons(SEQUENCE) |
47 | 10.3k | .decode(alg_id) |
48 | 10.3k | .decode(key_bits, BIT_STRING) |
49 | 10.3k | .end_cons(); |
50 | 10.3k | } |
51 | 0 | else |
52 | 0 | { |
53 | 0 | DataSource_Memory ber( |
54 | 0 | PEM_Code::decode_check_label(source, "PUBLIC KEY") |
55 | 0 | ); |
56 | 0 |
|
57 | 0 | BER_Decoder(ber) |
58 | 0 | .start_cons(SEQUENCE) |
59 | 0 | .decode(alg_id) |
60 | 0 | .decode(key_bits, BIT_STRING) |
61 | 0 | .end_cons(); |
62 | 0 | } |
63 | 10.3k | |
64 | 10.3k | if(key_bits.empty()) |
65 | 5 | throw Decoding_Error("X.509 public key decoding"); |
66 | 10.3k | |
67 | 10.3k | return load_public_key(alg_id, key_bits).release(); |
68 | 10.3k | } |
69 | 1.41k | catch(Decoding_Error& e) |
70 | 1.41k | { |
71 | 1.41k | throw Decoding_Error("X.509 public key decoding", e); |
72 | 1.41k | } |
73 | 10.3k | } |
74 | | |
75 | | #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM) |
76 | | /* |
77 | | * Extract a public key and return it |
78 | | */ |
79 | | Public_Key* load_key(const std::string& fsname) |
80 | 0 | { |
81 | 0 | DataSource_Stream source(fsname, true); |
82 | 0 | return X509::load_key(source); |
83 | 0 | } |
84 | | #endif |
85 | | |
86 | | /* |
87 | | * Extract a public key and return it |
88 | | */ |
89 | | Public_Key* load_key(const std::vector<uint8_t>& mem) |
90 | 10.3k | { |
91 | 10.3k | DataSource_Memory source(mem); |
92 | 10.3k | return X509::load_key(source); |
93 | 10.3k | } |
94 | | |
95 | | /* |
96 | | * Make a copy of this public key |
97 | | */ |
98 | | Public_Key* copy_key(const Public_Key& key) |
99 | 0 | { |
100 | 0 | DataSource_Memory source(PEM_encode(key)); |
101 | 0 | return X509::load_key(source); |
102 | 0 | } |
103 | | |
104 | | } |
105 | | |
106 | | } |