Coverage Report

Created: 2024-11-21 06:51

/src/botan/src/lib/asn1/alg_id.cpp
Line
Count
Source (jump to first uncovered line)
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), m_parameters() {
31
0
   const 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
AlgorithmIdentifier::AlgorithmIdentifier(std::string_view oid, Encoding_Option option) :
42
0
      m_oid(OID::from_string(oid)), m_parameters() {
43
0
   const uint8_t DER_NULL[] = {0x05, 0x00};
44
45
0
   if(option == USE_NULL_PARAM) {
46
0
      m_parameters.assign(DER_NULL, DER_NULL + 2);
47
0
   }
48
0
}
49
50
0
bool AlgorithmIdentifier::parameters_are_null() const {
51
0
   return (m_parameters.size() == 2 && (m_parameters[0] == 0x05) && (m_parameters[1] == 0x00));
52
0
}
53
54
0
bool operator==(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2) {
55
0
   if(a1.oid() != a2.oid()) {
56
0
      return false;
57
0
   }
58
59
   /*
60
   * Treat NULL and empty as equivalent
61
   */
62
0
   if(a1.parameters_are_null_or_empty() && a2.parameters_are_null_or_empty()) {
63
0
      return true;
64
0
   }
65
66
0
   return (a1.parameters() == a2.parameters());
67
0
}
68
69
0
bool operator!=(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2) {
70
0
   return !(a1 == a2);
71
0
}
72
73
/*
74
* DER encode an AlgorithmIdentifier
75
*/
76
0
void AlgorithmIdentifier::encode_into(DER_Encoder& codec) const {
77
0
   codec.start_sequence().encode(oid()).raw_bytes(parameters()).end_cons();
78
0
}
79
80
/*
81
* Decode a BER encoded AlgorithmIdentifier
82
*/
83
0
void AlgorithmIdentifier::decode_from(BER_Decoder& codec) {
84
0
   codec.start_sequence().decode(m_oid).raw_bytes(m_parameters).end_cons();
85
0
}
86
87
}  // namespace Botan