/src/botan/src/lib/asn1/alg_id.cpp
Line | Count | Source |
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/asn1_obj.h> |
9 | | |
10 | | #include <botan/ber_dec.h> |
11 | | #include <botan/der_enc.h> |
12 | | |
13 | | namespace Botan { |
14 | | |
15 | | /* |
16 | | * Create an AlgorithmIdentifier |
17 | | */ |
18 | | AlgorithmIdentifier::AlgorithmIdentifier(const OID& oid, const std::vector<uint8_t>& param) : |
19 | 0 | m_oid(oid), m_parameters(param) {} |
20 | | |
21 | | /* |
22 | | * Create an AlgorithmIdentifier |
23 | | */ |
24 | | AlgorithmIdentifier::AlgorithmIdentifier(std::string_view oid, const std::vector<uint8_t>& param) : |
25 | 0 | AlgorithmIdentifier(OID::from_string(oid), param) {} |
26 | | |
27 | | /* |
28 | | * Create an AlgorithmIdentifier |
29 | | */ |
30 | 0 | AlgorithmIdentifier::AlgorithmIdentifier(const OID& oid, Encoding_Option option) : m_oid(oid) { |
31 | 0 | constexpr uint8_t DER_NULL[] = {0x05, 0x00}; |
32 | |
|
33 | 0 | if(option == USE_NULL_PARAM) { |
34 | 0 | m_parameters.assign(DER_NULL, DER_NULL + 2); |
35 | 0 | } |
36 | 0 | } |
37 | | |
38 | | /* |
39 | | * Create an AlgorithmIdentifier |
40 | | */ |
41 | 0 | AlgorithmIdentifier::AlgorithmIdentifier(std::string_view oid, Encoding_Option option) : m_oid(OID::from_string(oid)) { |
42 | 0 | constexpr uint8_t DER_NULL[2] = {0x05, 0x00}; |
43 | |
|
44 | 0 | if(option == USE_NULL_PARAM) { |
45 | 0 | m_parameters.assign(DER_NULL, DER_NULL + 2); |
46 | 0 | } |
47 | 0 | } |
48 | | |
49 | 0 | bool AlgorithmIdentifier::parameters_are_null() const { |
50 | 0 | return (m_parameters.size() == 2 && (m_parameters[0] == 0x05) && (m_parameters[1] == 0x00)); |
51 | 0 | } |
52 | | |
53 | 0 | bool operator==(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2) { |
54 | 0 | if(a1.oid() != a2.oid()) { |
55 | 0 | return false; |
56 | 0 | } |
57 | | |
58 | | /* |
59 | | * Treat NULL and empty as equivalent |
60 | | */ |
61 | 0 | if(a1.parameters_are_null_or_empty() && a2.parameters_are_null_or_empty()) { |
62 | 0 | return true; |
63 | 0 | } |
64 | | |
65 | 0 | return (a1.parameters() == a2.parameters()); |
66 | 0 | } |
67 | | |
68 | 0 | bool operator!=(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2) { |
69 | 0 | return !(a1 == a2); |
70 | 0 | } |
71 | | |
72 | | /* |
73 | | * DER encode an AlgorithmIdentifier |
74 | | */ |
75 | 0 | void AlgorithmIdentifier::encode_into(DER_Encoder& codec) const { |
76 | 0 | codec.start_sequence().encode(oid()).raw_bytes(parameters()).end_cons(); |
77 | 0 | } |
78 | | |
79 | | /* |
80 | | * Decode a BER encoded AlgorithmIdentifier |
81 | | */ |
82 | 0 | void AlgorithmIdentifier::decode_from(BER_Decoder& codec) { |
83 | 0 | codec.start_sequence().decode(m_oid).raw_bytes(m_parameters).end_cons(); |
84 | 0 | } |
85 | | |
86 | | } // namespace Botan |