Coverage Report

Created: 2022-08-24 06:37

/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
#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& oid,
19
                                         const std::vector<uint8_t>& param) :
20
   m_oid(oid),
21
   m_parameters(param)
22
0
   {}
23
24
/*
25
* Create an AlgorithmIdentifier
26
*/
27
AlgorithmIdentifier::AlgorithmIdentifier(const std::string& oid,
28
                                         const std::vector<uint8_t>& param) :
29
   AlgorithmIdentifier(OID::from_string(oid), param)
30
0
   {}
31
32
/*
33
* Create an AlgorithmIdentifier
34
*/
35
AlgorithmIdentifier::AlgorithmIdentifier(const OID& oid,
36
                                         Encoding_Option option) :
37
   m_oid(oid),
38
   m_parameters()
39
0
   {
40
0
   const uint8_t DER_NULL[] = { 0x05, 0x00 };
41
42
0
   if(option == USE_NULL_PARAM)
43
0
      m_parameters.assign(DER_NULL, DER_NULL + 2);
44
0
   }
45
46
/*
47
* Create an AlgorithmIdentifier
48
*/
49
AlgorithmIdentifier::AlgorithmIdentifier(const std::string& oid,
50
                                         Encoding_Option option) :
51
   m_oid(OID::from_string(oid)),
52
   m_parameters()
53
0
   {
54
0
   const uint8_t DER_NULL[] = { 0x05, 0x00 };
55
56
0
   if(option == USE_NULL_PARAM)
57
0
      m_parameters.assign(DER_NULL, DER_NULL + 2);
58
0
   }
59
60
bool AlgorithmIdentifier::parameters_are_null() const
61
0
   {
62
0
   return (m_parameters.size() == 2 && (m_parameters[0] == 0x05) && (m_parameters[1] == 0x00));
63
0
   }
64
65
bool operator==(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2)
66
0
   {
67
0
   if(a1.get_oid() != a2.get_oid())
68
0
      return false;
69
70
   /*
71
   * Treat NULL and empty as equivalent
72
   */
73
0
   if(a1.parameters_are_null_or_empty() &&
74
0
      a2.parameters_are_null_or_empty())
75
0
      {
76
0
      return true;
77
0
      }
78
79
0
   return (a1.parameters() == a2.parameters());
80
0
   }
81
82
bool operator!=(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2)
83
0
   {
84
0
   return !(a1 == a2);
85
0
   }
86
87
/*
88
* DER encode an AlgorithmIdentifier
89
*/
90
void AlgorithmIdentifier::encode_into(DER_Encoder& codec) const
91
0
   {
92
0
   codec.start_sequence()
93
0
      .encode(oid())
94
0
      .raw_bytes(parameters())
95
0
   .end_cons();
96
0
   }
97
98
/*
99
* Decode a BER encoded AlgorithmIdentifier
100
*/
101
void AlgorithmIdentifier::decode_from(BER_Decoder& codec)
102
0
   {
103
0
   codec.start_sequence()
104
0
      .decode(m_oid)
105
0
      .raw_bytes(m_parameters)
106
0
   .end_cons();
107
0
   }
108
109
}