Botan  2.1.0
Crypto and TLS for C++11
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 */
19  const std::vector<uint8_t>& param) : oid(alg_id), parameters(param)
20  {}
21 
22 /*
23 * Create an AlgorithmIdentifier
24 */
25 AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id,
26  const std::vector<uint8_t>& param) : oid(OIDS::lookup(alg_id)), parameters(param)
27  {}
28 
29 /*
30 * Create an AlgorithmIdentifier
31 */
33  Encoding_Option option) : oid(alg_id), parameters()
34  {
35  const uint8_t DER_NULL[] = { 0x05, 0x00 };
36 
37  if(option == USE_NULL_PARAM)
38  parameters += std::pair<const uint8_t*, size_t>(DER_NULL, sizeof(DER_NULL));
39  }
40 
41 /*
42 * Create an AlgorithmIdentifier
43 */
44 AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id,
45  Encoding_Option option) : oid(OIDS::lookup(alg_id)), parameters()
46  {
47  const uint8_t DER_NULL[] = { 0x05, 0x00 };
48 
49  if(option == USE_NULL_PARAM)
50  parameters += std::pair<const uint8_t*, size_t>(DER_NULL, sizeof(DER_NULL));
51  }
52 
53 /*
54 * Compare two AlgorithmIdentifiers
55 */
56 namespace {
57 
58 bool param_null_or_empty(const std::vector<uint8_t>& p)
59  {
60  if(p.size() == 2 && (p[0] == 0x05) && (p[1] == 0x00))
61  return true;
62  return p.empty();
63  }
64 
65 }
66 
68  {
69  if(a1.oid != a2.oid)
70  return false;
71 
72  if(param_null_or_empty(a1.parameters) &&
73  param_null_or_empty(a2.parameters))
74  return true;
75 
76  return (a1.parameters == a2.parameters);
77  }
78 
79 /*
80 * Compare two AlgorithmIdentifiers
81 */
83  {
84  return !(a1 == a2);
85  }
86 
87 /*
88 * DER encode an AlgorithmIdentifier
89 */
91  {
92  codec.start_cons(SEQUENCE)
93  .encode(oid)
95  .end_cons();
96  }
97 
98 /*
99 * Decode a BER encoded AlgorithmIdentifier
100 */
102  {
103  codec.start_cons(SEQUENCE)
104  .decode(oid)
106  .end_cons();
107  }
108 
109 }
std::vector< uint8_t > parameters
Definition: alg_id.h:39
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:82
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:67
void decode_from(class BER_Decoder &) override
Definition: alg_id.cpp:101
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
DER_Encoder & end_cons()
Definition: der_enc.cpp:147
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition: der_enc.cpp:195
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:216
BER_Decoder & end_cons()
Definition: ber_dec.cpp:272
std::string lookup(const OID &oid)
Definition: oids.cpp:18
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:258
Definition: alg_id.cpp:13
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:137
void encode_into(class DER_Encoder &) const override
Definition: alg_id.cpp:90
BER_Decoder & raw_bytes(secure_vector< uint8_t > &v)
Definition: ber_dec.cpp:178