/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/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 | 2 | {} |
23 | | |
24 | | /* |
25 | | * Create an AlgorithmIdentifier |
26 | | */ |
27 | | AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id, |
28 | | const std::vector<uint8_t>& param) : |
29 | | AlgorithmIdentifier(OID::from_string(alg_id), param) |
30 | 2 | {} |
31 | | |
32 | | /* |
33 | | * Create an AlgorithmIdentifier |
34 | | */ |
35 | | AlgorithmIdentifier::AlgorithmIdentifier(const OID& alg_id, |
36 | | Encoding_Option option) : |
37 | | oid(alg_id), |
38 | | parameters() |
39 | 6.30k | { |
40 | 6.30k | const uint8_t DER_NULL[] = { 0x05, 0x00 }; |
41 | 6.30k | |
42 | 6.30k | if(option == USE_NULL_PARAM) |
43 | 6.30k | parameters.assign(DER_NULL, DER_NULL + 2); |
44 | 6.30k | } |
45 | | |
46 | | /* |
47 | | * Create an AlgorithmIdentifier |
48 | | */ |
49 | | AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id, |
50 | | Encoding_Option option) : |
51 | | oid(OID::from_string(alg_id)), |
52 | | parameters() |
53 | 2 | { |
54 | 2 | const uint8_t DER_NULL[] = { 0x05, 0x00 }; |
55 | 2 | |
56 | 2 | if(option == USE_NULL_PARAM) |
57 | 2 | parameters.assign(DER_NULL, DER_NULL + 2); |
58 | 2 | } |
59 | | |
60 | | /* |
61 | | * Compare two AlgorithmIdentifiers |
62 | | */ |
63 | | namespace { |
64 | | |
65 | | bool param_null_or_empty(const std::vector<uint8_t>& p) |
66 | 42.7k | { |
67 | 42.7k | if(p.size() == 2 && (p[0] == 0x05) && (p[1] == 0x00)) |
68 | 31.3k | return true; |
69 | 11.3k | return p.empty(); |
70 | 11.3k | } |
71 | | |
72 | | } |
73 | | |
74 | | bool operator==(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2) |
75 | 21.5k | { |
76 | 21.5k | if(a1.get_oid() != a2.get_oid()) |
77 | 145 | return false; |
78 | 21.4k | |
79 | 21.4k | if(param_null_or_empty(a1.get_parameters()) && |
80 | 21.4k | param_null_or_empty(a2.get_parameters())) |
81 | 21.2k | return true; |
82 | 174 | |
83 | 174 | return (a1.get_parameters() == a2.get_parameters()); |
84 | 174 | } |
85 | | |
86 | | /* |
87 | | * Compare two AlgorithmIdentifiers |
88 | | */ |
89 | | bool operator!=(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2) |
90 | 21.5k | { |
91 | 21.5k | return !(a1 == a2); |
92 | 21.5k | } |
93 | | |
94 | | /* |
95 | | * DER encode an AlgorithmIdentifier |
96 | | */ |
97 | | void AlgorithmIdentifier::encode_into(DER_Encoder& codec) const |
98 | 13.1k | { |
99 | 13.1k | codec.start_cons(SEQUENCE) |
100 | 13.1k | .encode(get_oid()) |
101 | 13.1k | .raw_bytes(get_parameters()) |
102 | 13.1k | .end_cons(); |
103 | 13.1k | } |
104 | | |
105 | | /* |
106 | | * Decode a BER encoded AlgorithmIdentifier |
107 | | */ |
108 | | void AlgorithmIdentifier::decode_from(BER_Decoder& codec) |
109 | 75.1k | { |
110 | 75.1k | codec.start_cons(SEQUENCE) |
111 | 75.1k | .decode(oid) |
112 | 75.1k | .raw_bytes(parameters) |
113 | 75.1k | .end_cons(); |
114 | 75.1k | } |
115 | | |
116 | | } |