Botan  1.10.9
alg_id.cpp
Go to the documentation of this file.
1 /*
2 * Algorithm Identifier
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
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 MemoryRegion<byte>& param)
20  {
21  oid = alg_id;
22  parameters = param;
23  }
24 
25 /*
26 * Create an AlgorithmIdentifier
27 */
28 AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id,
29  const MemoryRegion<byte>& param)
30  {
31  oid = OIDS::lookup(alg_id);
32  parameters = param;
33  }
34 
35 /*
36 * Create an AlgorithmIdentifier
37 */
39  Encoding_Option option)
40  {
41  const byte DER_NULL[] = { 0x05, 0x00 };
42 
43  oid = alg_id;
44 
45  if(option == USE_NULL_PARAM)
46  {
47  parameters += std::pair<const byte*, size_t>(
48  DER_NULL, sizeof(DER_NULL));
49  }
50  }
51 
52 /*
53 * Create an AlgorithmIdentifier
54 */
55 AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id,
56  Encoding_Option option)
57  {
58  const byte DER_NULL[] = { 0x05, 0x00 };
59 
60  oid = OIDS::lookup(alg_id);
61 
62  if(option == USE_NULL_PARAM)
63  {
64  parameters += std::pair<const byte*, size_t>(
65  DER_NULL, sizeof(DER_NULL));
66  }
67  }
68 
69 /*
70 * Compare two AlgorithmIdentifiers
71 */
73  {
74  if(a1.oid != a2.oid)
75  return false;
76  if(a1.parameters != a2.parameters)
77  return false;
78  return true;
79  }
80 
81 /*
82 * Compare two AlgorithmIdentifiers
83 */
85  {
86  return !(a1 == a2);
87  }
88 
89 /*
90 * DER encode an AlgorithmIdentifier
91 */
93  {
94  codec.start_cons(SEQUENCE)
95  .encode(oid)
97  .end_cons();
98  }
99 
100 /*
101 * Decode a BER encoded AlgorithmIdentifier
102 */
104  {
105  codec.start_cons(SEQUENCE)
106  .decode(oid)
108  .end_cons();
109  }
110 
111 }
bool operator!=(const OctetString &s1, const OctetString &s2)
Definition: symkey.cpp:106
DER_Encoder & raw_bytes(const byte val[], size_t len)
Definition: der_enc.cpp:188
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:338
bool operator==(const OctetString &s1, const OctetString &s2)
Definition: symkey.cpp:98
SecureVector< byte > parameters
Definition: alg_id.h:36
DER_Encoder & end_cons()
Definition: der_enc.cpp:145
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
Definition: ber_dec.cpp:232
unsigned char byte
Definition: types.h:22
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209
BER_Decoder & end_cons()
Definition: ber_dec.cpp:246
void decode_from(class BER_Decoder &)
Definition: alg_id.cpp:103
std::string lookup(const OID &oid)
Definition: oids.cpp:31
BER_Decoder & raw_bytes(MemoryRegion< byte > &)
Definition: ber_dec.cpp:170
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:135
void encode_into(class DER_Encoder &) const
Definition: alg_id.cpp:92