Coverage Report

Created: 2020-05-23 13:54

/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
54
   {}
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
54
   {}
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
5.89k
   {
40
5.89k
   const uint8_t DER_NULL[] = { 0x05, 0x00 };
41
5.89k
42
5.89k
   if(option == USE_NULL_PARAM)
43
5.89k
      parameters.assign(DER_NULL, DER_NULL + 2);
44
5.89k
   }
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
54
   {
54
54
   const uint8_t DER_NULL[] = { 0x05, 0x00 };
55
54
56
54
   if(option == USE_NULL_PARAM)
57
54
      parameters.assign(DER_NULL, DER_NULL + 2);
58
54
   }
59
60
/*
61
* Compare two AlgorithmIdentifiers
62
*/
63
namespace {
64
65
bool param_null_or_empty(const std::vector<uint8_t>& p)
66
38.4k
   {
67
38.4k
   if(p.size() == 2 && (p[0] == 0x05) && (p[1] == 0x00))
68
28.8k
      return true;
69
9.64k
   return p.empty();
70
9.64k
   }
71
72
}
73
74
bool operator==(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2)
75
19.4k
   {
76
19.4k
   if(a1.get_oid() != a2.get_oid())
77
149
      return false;
78
19.3k
79
19.3k
   if(param_null_or_empty(a1.get_parameters()) &&
80
19.3k
      param_null_or_empty(a2.get_parameters()))
81
19.1k
      return true;
82
219
83
219
   return (a1.get_parameters() == a2.get_parameters());
84
219
   }
85
86
/*
87
* Compare two AlgorithmIdentifiers
88
*/
89
bool operator!=(const AlgorithmIdentifier& a1, const AlgorithmIdentifier& a2)
90
19.4k
   {
91
19.4k
   return !(a1 == a2);
92
19.4k
   }
93
94
/*
95
* DER encode an AlgorithmIdentifier
96
*/
97
void AlgorithmIdentifier::encode_into(DER_Encoder& codec) const
98
10.4k
   {
99
10.4k
   codec.start_cons(SEQUENCE)
100
10.4k
      .encode(get_oid())
101
10.4k
      .raw_bytes(get_parameters())
102
10.4k
   .end_cons();
103
10.4k
   }
104
105
/*
106
* Decode a BER encoded AlgorithmIdentifier
107
*/
108
void AlgorithmIdentifier::decode_from(BER_Decoder& codec)
109
66.4k
   {
110
66.4k
   codec.start_cons(SEQUENCE)
111
66.4k
      .decode(oid)
112
66.4k
      .raw_bytes(parameters)
113
66.4k
   .end_cons();
114
66.4k
   }
115
116
}