Coverage Report

Created: 2020-11-21 08:34

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