1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
/*
* Algorithm Identifier
* (C) 1999-2007 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/asn1_obj.h>
#include <botan/der_enc.h>
#include <botan/ber_dec.h>
#include <botan/oids.h>
namespace Botan {
/*
* Create an AlgorithmIdentifier
*/
AlgorithmIdentifier::AlgorithmIdentifier(const OID& oid,
const std::vector<uint8_t>& param) :
m_oid(oid),
m_parameters(param)
{}
/*
* Create an AlgorithmIdentifier
*/
AlgorithmIdentifier::AlgorithmIdentifier(const std::string& oid,
const std::vector<uint8_t>& param) :
AlgorithmIdentifier(OID::from_string(oid), param)
{}
/*
* Create an AlgorithmIdentifier
*/
AlgorithmIdentifier::AlgorithmIdentifier(const OID& oid,
Encoding_Option option) :
m_oid(oid),
m_parameters()
{
const uint8_t DER_NULL[] = { 0x05, 0x00 };
if(option == USE_NULL_PARAM)
m_parameters.assign(DER_NULL, DER_NULL + 2);
}
/*
* Create an AlgorithmIdentifier
*/
AlgorithmIdentifier::AlgorithmIdentifier(const std::string& oid,
Encoding_Option option) :
m_oid(OID::from_string(oid)),
m_parameters()
{
const uint8_t DER_NULL[] = { 0x05, 0x00 };
if(option == USE_NULL_PARAM)
m_parameters.assign(DER_NULL, DER_NULL + 2);
}
bool AlgorithmIdentifier::parameters_are_null() const
{
return (m_parameters.size() == 2 && (m_parameters[0] == 0x05) && (m_parameters[1] == 0x00));
}
bool operator==(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2)
{
if(a1.get_oid() != a2.get_oid())
return false;
/*
* Treat NULL and empty as equivalent
*/
if(a1.parameters_are_null_or_empty() &&
a2.parameters_are_null_or_empty())
{
return true;
}
return (a1.parameters() == a2.parameters());
}
bool operator!=(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2)
{
return !(a1 == a2);
}
/*
* DER encode an AlgorithmIdentifier
*/
void AlgorithmIdentifier::encode_into(DER_Encoder& codec) const
{
codec.start_sequence()
.encode(oid())
.raw_bytes(parameters())
.end_cons();
}
/*
* Decode a BER encoded AlgorithmIdentifier
*/
void AlgorithmIdentifier::decode_from(BER_Decoder& codec)
{
codec.start_sequence()
.decode(m_oid)
.raw_bytes(m_parameters)
.end_cons();
}
}
|