Coverage Report

Created: 2021-02-21 07:20

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