Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

alg_id.cpp
Go to the documentation of this file.
1 /*
2 * Algorithm Identifier
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #include <botan/alg_id.h>
9 #include <botan/der_enc.h>
10 #include <botan/ber_dec.h>
11 #include <botan/oids.h>
12
13 namespace Botan {
14
15 /*
16 * Create an AlgorithmIdentifier
17 */
18 AlgorithmIdentifier::AlgorithmIdentifier(const OID& alg_id,
19 const std::vector<uint8_t>& param) :
20 oid(alg_id),
21 parameters(param)
22 {}
23
24 /*
25 * Create an AlgorithmIdentifier
26 */
27 AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id,
28 const std::vector<uint8_t>& param) :
29 AlgorithmIdentifier(OID::from_string(alg_id), param)
30 {}
31
32 /*
33 * Create an AlgorithmIdentifier
34 */
35 AlgorithmIdentifier::AlgorithmIdentifier(const OID& alg_id,
36 Encoding_Option option) :
37 oid(alg_id),
38 parameters()
39 {
40 const uint8_t DER_NULL[] = { 0x05, 0x00 };
41
42 if(option == USE_NULL_PARAM)
43 parameters.assign(DER_NULL, DER_NULL + 2);
44 }
45
46 /*
47 * Create an AlgorithmIdentifier
48 */
49 AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id,
50 Encoding_Option option) :
51 oid(OID::from_string(alg_id)),
52 parameters()
53 {
54 const uint8_t DER_NULL[] = { 0x05, 0x00 };
55
56 if(option == USE_NULL_PARAM)
57 parameters.assign(DER_NULL, DER_NULL + 2);
58 }
59
60 /*
61 * Compare two AlgorithmIdentifiers
62 */
63 namespace {
64
65 bool param_null_or_empty(const std::vector<uint8_t>& p)
66 {
67 if(p.size() == 2 && (p[0] == 0x05) && (p[1] == 0x00))
68 return true;
69 return p.empty();
70 }
71
72 }
73
74 bool operator==(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2)
75 {
76 if(a1.get_oid() != a2.get_oid())
77 return false;
78
79 if(param_null_or_empty(a1.get_parameters()) &&
80 param_null_or_empty(a2.get_parameters()))
81 return true;
82
83 return (a1.get_parameters() == a2.get_parameters());
84 }
85
86 /*
87 * Compare two AlgorithmIdentifiers
88 */
89 bool operator!=(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2)
90 {
91 return !(a1 == a2);
92 }
93
94 /*
95 * DER encode an AlgorithmIdentifier
96 */
97 void AlgorithmIdentifier::encode_into(DER_Encoder& codec) const
98 {
99 codec.start_cons(SEQUENCE)
100 .encode(get_oid())
101 .raw_bytes(get_parameters())
102 .end_cons();
103 }
104
105 /*
106 * Decode a BER encoded AlgorithmIdentifier
107 */
108 void AlgorithmIdentifier::decode_from(BER_Decoder& codec)
109 {
110 codec.start_cons(SEQUENCE)
111 .decode(oid)
112 .raw_bytes(parameters)
113 .end_cons();
114 }
115
116 }
Botan::operator!=
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:89
Botan::AlgorithmIdentifier::parameters
std::vector< uint8_t > parameters
Definition: alg_id.h:46
Botan::AlgorithmIdentifier::decode_from
void decode_from(class BER_Decoder &) override
Definition: alg_id.cpp:108
Botan::BER_Decoder::raw_bytes
BER_Decoder & raw_bytes(std::vector< uint8_t, Alloc > &out)
Definition: ber_dec.h:156
Botan::DER_Encoder::end_cons
DER_Encoder & end_cons()
Definition: der_enc.cpp:191
Botan::AlgorithmIdentifier::AlgorithmIdentifier
AlgorithmIdentifier()=default
Botan::AlgorithmIdentifier::Encoding_Option
Encoding_Option
Definition: alg_id.h:24
Botan::BER_Decoder::decode
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:170
Botan::DER_Encoder::raw_bytes
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition: der_enc.cpp:228
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:285
Botan::SEQUENCE
Definition: asn1_obj.h:40
Botan::DER_Encoder
Definition: der_enc.h:23
Botan::BER_Decoder::end_cons
BER_Decoder & end_cons()
Definition: ber_dec.cpp:300
Botan::BER_Decoder::start_cons
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:290
Botan
Definition: alg_id.cpp:13
Botan::AlgorithmIdentifier::get_oid
const OID & get_oid() const
Definition: alg_id.h:37
Botan::AlgorithmIdentifier
Definition: alg_id.h:21
Botan::DER_Encoder::start_cons
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:181
Botan::AlgorithmIdentifier::encode_into
void encode_into(class DER_Encoder &) const override
Definition: alg_id.cpp:97
Botan::operator==
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:74
Botan::AlgorithmIdentifier::USE_NULL_PARAM
Definition: alg_id.h:24
Botan::OID
Definition: asn1_oid.h:20
Botan::BER_Decoder
Definition: ber_dec.h:21
Botan::AlgorithmIdentifier::get_parameters
const std::vector< uint8_t > & get_parameters() const
Definition: alg_id.h:38