Coverage Report

Created: 2020-09-16 07:52

/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/alg_id.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& alg_id,
19
                                         const std::vector<uint8_t>& param) :
20
   oid(alg_id),
21
   parameters(param)
22
74
   {}
23
24
/*
25
* Create an AlgorithmIdentifier
26
*/
27
AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id,
28
                                         const std::vector<uint8_t>& param) :
29
   AlgorithmIdentifier(OID::from_string(alg_id), param)
30
74
   {}
31
32
/*
33
* Create an AlgorithmIdentifier
34
*/
35
AlgorithmIdentifier::AlgorithmIdentifier(const OID& alg_id,
36
                                         Encoding_Option option) :
37
   oid(alg_id),
38
   parameters()
39
6.43k
   {
40
6.43k
   const uint8_t DER_NULL[] = { 0x05, 0x00 };
41
6.43k
42
6.43k
   if(option == USE_NULL_PARAM)
43
6.43k
      parameters.assign(DER_NULL, DER_NULL + 2);
44
6.43k
   }
45
46
/*
47
* Create an AlgorithmIdentifier
48
*/
49
AlgorithmIdentifier::AlgorithmIdentifier(const std::string& alg_id,
50
                                         Encoding_Option option) :
51
   oid(OID::from_string(alg_id)),
52
   parameters()
53
74
   {
54
74
   const uint8_t DER_NULL[] = { 0x05, 0x00 };
55
74
56
74
   if(option == USE_NULL_PARAM)
57
74
      parameters.assign(DER_NULL, DER_NULL + 2);
58
74
   }
59
60
bool AlgorithmIdentifier::parameters_are_null() const
61
38.2k
   {
62
38.2k
   return (parameters.size() == 2 && (parameters[0] == 0x05) && (parameters[1] == 0x00));
63
38.2k
   }
64
65
bool operator==(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2)
66
21.7k
   {
67
21.7k
   if(a1.get_oid() != a2.get_oid())
68
147
      return false;
69
21.6k
70
   /*
71
   * Treat NULL and empty as equivalent
72
   */
73
21.6k
   if(a1.parameters_are_null_or_empty() &&
74
21.4k
      a2.parameters_are_null_or_empty())
75
21.3k
      {
76
21.3k
      return true;
77
21.3k
      }
78
267
79
267
   return (a1.get_parameters() == a2.get_parameters());
80
267
   }
81
82
bool operator!=(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2)
83
21.7k
   {
84
21.7k
   return !(a1 == a2);
85
21.7k
   }
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_cons(SEQUENCE)
93
11.8k
      .encode(get_oid())
94
11.8k
      .raw_bytes(get_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
74.8k
   {
103
74.8k
   codec.start_cons(SEQUENCE)
104
74.8k
      .decode(oid)
105
74.8k
      .raw_bytes(parameters)
106
74.8k
   .end_cons();
107
74.8k
   }
108
109
}