Botan 1.10.17
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
17namespace Botan {
18
19namespace X509 {
20
30
31/*
32* PEM encode a X.509 public key
33*/
34std::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 {
47 MemoryVector<byte> key_bits;
48
49 if(ASN1::maybe_BER(source) && !PEM_Code::matches(source))
50 {
51 BER_Decoder(source)
52 .start_cons(SEQUENCE)
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)
65 .start_cons(SEQUENCE)
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*/
86Public_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}
BER_Decoder(DataSource &)
Definition ber_dec.cpp:264
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition der_enc.cpp:135
SecureVector< byte > get_contents()
Definition der_enc.cpp:122
DER_Encoder & end_cons()
Definition der_enc.cpp:145
DER_Encoder & encode(bool b)
Definition der_enc.cpp:209
bool empty() const
Definition secmem.h:35
virtual MemoryVector< byte > x509_subject_public_key() const =0
virtual AlgorithmIdentifier algorithm_identifier() const =0
virtual std::string algo_name() const =0
bool maybe_BER(DataSource &source)
Definition asn1_int.cpp:55
std::string encode(const byte der[], size_t length, const std::string &label, size_t width)
Definition pem.cpp:19
SecureVector< byte > decode_check_label(DataSource &source, const std::string &label_want)
Definition pem.cpp:42
bool matches(DataSource &source, const std::string &extra, size_t search_range)
Definition pem.cpp:116
std::string PEM_encode(const Public_Key &key)
Definition x509_key.cpp:34
MemoryVector< byte > BER_encode(const Public_Key &key)
Definition x509_key.cpp:21
Public_Key * copy_key(const Public_Key &key)
Definition x509_key.cpp:104
Public_Key * load_key(DataSource &source)
Definition x509_key.cpp:43
Key_Constraints find_constraints(const Public_Key &pub_key, Key_Constraints limits)
Definition x509_key.cpp:113
Public_Key * make_public_key(const AlgorithmIdentifier &alg_id, const MemoryRegion< byte > &key_bits)
Definition pk_algs.cpp:49
@ BIT_STRING
Definition asn1_int.h:30
@ SEQUENCE
Definition asn1_int.h:35
Key_Constraints
@ DATA_ENCIPHERMENT
@ DIGITAL_SIGNATURE
@ KEY_AGREEMENT
@ KEY_ENCIPHERMENT
@ NON_REPUDIATION
Decoding_Error(const std::string &name)
Definition exceptn.h:140