Coverage Report

Created: 2020-02-14 15:38

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