Botan  2.4.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) :
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  oid(OIDS::lookup(alg_id)),
30  parameters(param)
31  {}
32 
33 /*
34 * Create an AlgorithmIdentifier
35 */
37  Encoding_Option option) :
38  oid(alg_id),
39  parameters()
40  {
41  const uint8_t DER_NULL[] = { 0x05, 0x00 };
42 
43  if(option == USE_NULL_PARAM)
44  parameters.assign(DER_NULL, DER_NULL + 2);
45  }
46 
47 /*
48 * Create an AlgorithmIdentifier
49 */
50 AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id,
51  Encoding_Option option) :
52  oid(OIDS::lookup(alg_id)),
53  parameters()
54  {
55  const uint8_t DER_NULL[] = { 0x05, 0x00 };
56 
57  if(option == USE_NULL_PARAM)
58  parameters.assign(DER_NULL, DER_NULL + 2);
59  }
60 
61 /*
62 * Compare two AlgorithmIdentifiers
63 */
64 namespace {
65 
66 bool param_null_or_empty(const std::vector<uint8_t>& p)
67  {
68  if(p.size() == 2 && (p[0] == 0x05) && (p[1] == 0x00))
69  return true;
70  return p.empty();
71  }
72 
73 }
74 
76  {
77  if(a1.get_oid() != a2.get_oid())
78  return false;
79 
80  if(param_null_or_empty(a1.get_parameters()) &&
81  param_null_or_empty(a2.get_parameters()))
82  return true;
83 
84  return (a1.get_parameters() == a2.get_parameters());
85  }
86 
87 /*
88 * Compare two AlgorithmIdentifiers
89 */
91  {
92  return !(a1 == a2);
93  }
94 
95 /*
96 * DER encode an AlgorithmIdentifier
97 */
99  {
100  codec.start_cons(SEQUENCE)
101  .encode(get_oid())
103  .end_cons();
104  }
105 
106 /*
107 * Decode a BER encoded AlgorithmIdentifier
108 */
110  {
111  codec.start_cons(SEQUENCE)
112  .decode(oid)
114  .end_cons();
115  }
116 
117 }
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:90
std::vector< uint8_t > parameters
Definition: alg_id.h:45
void decode_from(class BER_Decoder &) override
Definition: alg_id.cpp:109
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:355
BER_Decoder & raw_bytes(std::vector< uint8_t, Alloc > &out)
Definition: ber_dec.h:73
DER_Encoder & end_cons()
Definition: der_enc.cpp:146
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition: der_enc.cpp:181
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202
BER_Decoder & end_cons()
Definition: ber_dec.cpp:265
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: ber_dec.cpp:251
Definition: alg_id.cpp:13
const std::vector< uint8_t > & get_parameters() const
Definition: alg_id.h:38
const OID & get_oid() const
Definition: alg_id.h:37
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:136
void encode_into(class DER_Encoder &) const override
Definition: alg_id.cpp:98
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:75
std::string lookup(const OID &oid)
Definition: oids.cpp:18