Botan  1.10.9
x509_key.cpp
Go to the documentation of this file.
1 /*
2 * X.509 Public Key
3 * (C) 1999-2010 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/x509_key.h>
9 #include <botan/filters.h>
10 #include <botan/asn1_obj.h>
11 #include <botan/der_enc.h>
12 #include <botan/ber_dec.h>
13 #include <botan/pem.h>
14 #include <botan/internal/pk_algs.h>
15 #include <memory>
16 
17 namespace Botan {
18 
19 namespace X509 {
20 
22  {
23  return DER_Encoder()
27  .end_cons()
28  .get_contents();
29  }
30 
31 /*
32 * PEM encode a X.509 public key
33 */
34 std::string PEM_encode(const Public_Key& key)
35  {
37  "PUBLIC KEY");
38  }
39 
40 /*
41 * Extract a public key and return it
42 */
44  {
45  try {
46  AlgorithmIdentifier alg_id;
47  MemoryVector<byte> key_bits;
48 
49  if(ASN1::maybe_BER(source) && !PEM_Code::matches(source))
50  {
51  BER_Decoder(source)
53  .decode(alg_id)
54  .decode(key_bits, BIT_STRING)
55  .verify_end()
56  .end_cons();
57  }
58  else
59  {
61  PEM_Code::decode_check_label(source, "PUBLIC KEY")
62  );
63 
64  BER_Decoder(ber)
66  .decode(alg_id)
67  .decode(key_bits, BIT_STRING)
68  .verify_end()
69  .end_cons();
70  }
71 
72  if(key_bits.empty())
73  throw Decoding_Error("X.509 public key decoding failed");
74 
75  return make_public_key(alg_id, key_bits);
76  }
77  catch(Decoding_Error)
78  {
79  throw Decoding_Error("X.509 public key decoding failed");
80  }
81  }
82 
83 /*
84 * Extract a public key and return it
85 */
86 Public_Key* load_key(const std::string& fsname)
87  {
88  DataSource_Stream source(fsname, true);
89  return X509::load_key(source);
90  }
91 
92 /*
93 * Extract a public key and return it
94 */
96  {
97  DataSource_Memory source(mem);
98  return X509::load_key(source);
99  }
100 
101 /*
102 * Make a copy of this public key
103 */
105  {
106  DataSource_Memory source(PEM_encode(key));
107  return X509::load_key(source);
108  }
109 
110 /*
111 * Find the allowable key constraints
112 */
114  Key_Constraints limits)
115  {
116  const std::string name = pub_key.algo_name();
117 
118  size_t constraints = 0;
119 
120  if(name == "DH" || name == "ECDH")
121  constraints |= KEY_AGREEMENT;
122 
123  if(name == "RSA" || name == "ElGamal")
124  constraints |= KEY_ENCIPHERMENT | DATA_ENCIPHERMENT;
125 
126  if(name == "RSA" || name == "RW" || name == "NR" ||
127  name == "DSA" || name == "ECDSA")
128  constraints |= DIGITAL_SIGNATURE | NON_REPUDIATION;
129 
130  if(limits)
131  constraints &= limits;
132 
133  return Key_Constraints(constraints);
134  }
135 
136 }
137 
138 }
virtual AlgorithmIdentifier algorithm_identifier() const =0
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:338
Public_Key * load_key(DataSource &source)
Definition: x509_key.cpp:43
MemoryVector< byte > BER_encode(const Public_Key &key)
Definition: x509_key.cpp:21
virtual std::string algo_name() const =0
bool maybe_BER(DataSource &source)
Definition: asn1_int.cpp:55
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
Definition: ber_dec.cpp:232
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209
BER_Decoder & end_cons()
Definition: ber_dec.cpp:246
Key_Constraints find_constraints(const Public_Key &pub_key, Key_Constraints limits)
Definition: x509_key.cpp:113
virtual MemoryVector< byte > x509_subject_public_key() const =0
bool empty() const
Definition: secmem.h:35
void encode(const Public_Key &key, Pipe &pipe, X509_Encoding encoding=PEM)
Definition: x509_key.h:87
std::string PEM_encode(const Public_Key &key)
Definition: x509_key.cpp:34
bool matches(DataSource &source, const std::string &extra, size_t search_range)
Definition: pem.cpp:116
Public_Key * copy_key(const Public_Key &key)
Definition: x509_key.cpp:104
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition: pem.cpp:19
BER_Decoder & verify_end()
Definition: ber_dec.cpp:160
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:135
Key_Constraints
Definition: pubkey_enums.h:18
SecureVector< byte > decode_check_label(DataSource &source, const std::string &label_want)
Definition: pem.cpp:42
Public_Key * make_public_key(const AlgorithmIdentifier &alg_id, const MemoryRegion< byte > &key_bits)
Definition: pk_algs.cpp:49