Coverage Report

Created: 2021-11-25 09:31

/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
#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
106
   {}
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
106
   {}
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
5.92k
   {
40
5.92k
   const uint8_t DER_NULL[] = { 0x05, 0x00 };
41
42
5.92k
   if(option == USE_NULL_PARAM)
43
5.92k
      m_parameters.assign(DER_NULL, DER_NULL + 2);
44
5.92k
   }
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
106
   {
54
106
   const uint8_t DER_NULL[] = { 0x05, 0x00 };
55
56
106
   if(option == USE_NULL_PARAM)
57
106
      m_parameters.assign(DER_NULL, DER_NULL + 2);
58
106
   }
59
60
bool AlgorithmIdentifier::parameters_are_null() const
61
44.2k
   {
62
44.2k
   return (m_parameters.size() == 2 && (m_parameters[0] == 0x05) && (m_parameters[1] == 0x00));
63
44.2k
   }
64
65
bool operator==(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2)
66
22.1k
   {
67
22.1k
   if(a1.get_oid() != a2.get_oid())
68
42
      return false;
69
70
   /*
71
   * Treat NULL and empty as equivalent
72
   */
73
22.0k
   if(a1.parameters_are_null_or_empty() &&
74
22.0k
      a2.parameters_are_null_or_empty())
75
21.5k
      {
76
21.5k
      return true;
77
21.5k
      }
78
79
477
   return (a1.parameters() == a2.parameters());
80
22.0k
   }
81
82
bool operator!=(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2)
83
22.1k
   {
84
22.1k
   return !(a1 == a2);
85
22.1k
   }
86
87
/*
88
* DER encode an AlgorithmIdentifier
89
*/
90
void AlgorithmIdentifier::encode_into(DER_Encoder& codec) const
91
12.7k
   {
92
12.7k
   codec.start_sequence()
93
12.7k
      .encode(oid())
94
12.7k
      .raw_bytes(parameters())
95
12.7k
   .end_cons();
96
12.7k
   }
97
98
/*
99
* Decode a BER encoded AlgorithmIdentifier
100
*/
101
void AlgorithmIdentifier::decode_from(BER_Decoder& codec)
102
81.7k
   {
103
81.7k
   codec.start_sequence()
104
81.7k
      .decode(m_oid)
105
81.7k
      .raw_bytes(m_parameters)
106
81.7k
   .end_cons();
107
81.7k
   }
108
109
}