/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/asn1_obj.h> |
13 | | #include <botan/pk_algs.h> |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | | namespace X509 { |
18 | | |
19 | | /* |
20 | | * PEM encode a X.509 public key |
21 | | */ |
22 | | std::string PEM_encode(const Public_Key& key) |
23 | 0 | { |
24 | 0 | return PEM_Code::encode(key.subject_public_key(), |
25 | 0 | "PUBLIC KEY"); |
26 | 0 | } |
27 | | |
28 | | /* |
29 | | * Extract a public key and return it |
30 | | */ |
31 | | Public_Key* load_key(DataSource& source) |
32 | 11.0k | { |
33 | 11.0k | try { |
34 | 11.0k | AlgorithmIdentifier alg_id; |
35 | 11.0k | std::vector<uint8_t> key_bits; |
36 | | |
37 | 11.0k | if(ASN1::maybe_BER(source) && !PEM_Code::matches(source)) |
38 | 11.0k | { |
39 | 11.0k | BER_Decoder(source) |
40 | 11.0k | .start_sequence() |
41 | 11.0k | .decode(alg_id) |
42 | 11.0k | .decode(key_bits, ASN1_Type::BIT_STRING) |
43 | 11.0k | .end_cons(); |
44 | 11.0k | } |
45 | 0 | else |
46 | 0 | { |
47 | 0 | DataSource_Memory ber( |
48 | 0 | PEM_Code::decode_check_label(source, "PUBLIC KEY") |
49 | 0 | ); |
50 | |
|
51 | 0 | BER_Decoder(ber) |
52 | 0 | .start_sequence() |
53 | 0 | .decode(alg_id) |
54 | 0 | .decode(key_bits, ASN1_Type::BIT_STRING) |
55 | 0 | .end_cons(); |
56 | 0 | } |
57 | | |
58 | 11.0k | if(key_bits.empty()) |
59 | 8 | throw Decoding_Error("X.509 public key decoding"); |
60 | | |
61 | 11.0k | return load_public_key(alg_id, key_bits).release(); |
62 | 11.0k | } |
63 | 1.68k | catch(Decoding_Error& e) |
64 | 1.68k | { |
65 | 1.68k | throw Decoding_Error("X.509 public key decoding", e); |
66 | 1.68k | } |
67 | 11.0k | } |
68 | | |
69 | | } |
70 | | |
71 | | } |