Botan  2.10.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 */
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  oid(OIDS::lookup(alg_id)),
30  parameters(param)
31  {}
32 
33 /*
34 * Create an AlgorithmIdentifier
35 */
36 AlgorithmIdentifier::AlgorithmIdentifier(const OID& alg_id,
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 
75 bool operator==(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2)
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 */
90 bool operator!=(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2)
91  {
92  return !(a1 == a2);
93  }
94 
95 /*
96 * DER encode an AlgorithmIdentifier
97 */
98 void AlgorithmIdentifier::encode_into(DER_Encoder& codec) const
99  {
100  codec.start_cons(SEQUENCE)
101  .encode(get_oid())
102  .raw_bytes(get_parameters())
103  .end_cons();
104  }
105 
106 /*
107 * Decode a BER encoded AlgorithmIdentifier
108 */
109 void AlgorithmIdentifier::decode_from(BER_Decoder& codec)
110  {
111  codec.start_cons(SEQUENCE)
112  .decode(oid)
113  .raw_bytes(parameters)
114  .end_cons();
115  }
116 
117 }
bool operator!=(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:90
BigInt const BigInt & p
Definition: numthry.h:150
Definition: alg_id.cpp:13
bool operator==(const AlgorithmIdentifier &a1, const AlgorithmIdentifier &a2)
Definition: alg_id.cpp:75
botan_rng_t const char * param
Definition: ffi.h:944
RandomNumberGenerator const std::string AlgorithmIdentifier & alg_id
Definition: x509_ca.h:230
std::string lookup(const OID &oid)
Definition: oids.cpp:113