Botan 3.13.0
Crypto and TLS for C&
alg_id.cpp
Go to the documentation of this file.
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
10#include <botan/ber_dec.h>
11#include <botan/der_enc.h>
12#include <botan/internal/asn1_utils.h>
13
14namespace Botan {
15
16/*
17* Create an AlgorithmIdentifier
18*/
19AlgorithmIdentifier::AlgorithmIdentifier(const OID& oid, const std::vector<uint8_t>& param) :
20 m_oid(oid), m_parameters(param) {}
21
22/*
23* Create an AlgorithmIdentifier
24*/
25AlgorithmIdentifier::AlgorithmIdentifier(std::string_view oid, const std::vector<uint8_t>& param) :
26 AlgorithmIdentifier(OID::from_string(oid), param) {}
27
28/*
29* Create an AlgorithmIdentifier
30*/
32 constexpr uint8_t DER_NULL[] = {0x05, 0x00};
33
34 if(option == USE_NULL_PARAM) {
35 m_parameters.assign(DER_NULL, DER_NULL + 2);
36 }
37}
38
39/*
40* Create an AlgorithmIdentifier
41*/
42AlgorithmIdentifier::AlgorithmIdentifier(std::string_view oid, Encoding_Option option) : m_oid(OID::from_string(oid)) {
43 constexpr uint8_t DER_NULL[2] = {0x05, 0x00};
44
45 if(option == USE_NULL_PARAM) {
46 m_parameters.assign(DER_NULL, DER_NULL + 2);
47 }
48}
49
51 return (m_parameters.size() == 2 && (m_parameters[0] == 0x05) && (m_parameters[1] == 0x00));
52}
53
55 return (x.oid() == y.oid() && x.parameters() == y.parameters());
56}
57
59 return !(x == y);
60}
61
62/*
63* DER encode an AlgorithmIdentifier
64*/
68
69/*
70* Decode a BER encoded AlgorithmIdentifier
71*/
73 codec.start_sequence().decode(m_oid).raw_bytes(m_parameters).end_cons();
74
75 /*
76 * The parameters field is OPTIONAL ANY but in practice it is one of
77 * - empty
78 * - NULL
79 * - SEQUENCE
80 * - OBJECT IDENTIFIER (namedCurve)
81 * - OCTET STRING (CBC IV in PBES2)
82 *
83 * So require it be exactly one of these values. In particular this ensures that
84 * there is not any additional trailing data (eg after the SEQUENCE encoding)
85 * that might be otherwise skipped over by a reader.
86 */
87
88 const bool acceptable_parameters = [&]() {
90 return true;
91 }
92 if(ASN1::is_der_sequence_header(m_parameters)) {
93 return true;
94 }
96 return true;
97 }
99 return true;
100 }
101 return false;
102 }();
103
104 if(!acceptable_parameters) {
105 throw Decoding_Error("AlgorithmIdentifier parameters were not NULL, a SEQUENCE, an OID, or an OCTET STRING");
106 }
107}
108
109} // namespace Botan
void encode_into(DER_Encoder &to) const override
Definition alg_id.cpp:65
bool parameters_are_null_or_empty() const
Definition asn1_obj.h:720
const std::vector< uint8_t > & parameters() const
Definition asn1_obj.h:693
bool parameters_are_null() const
Definition alg_id.cpp:50
void decode_from(BER_Decoder &from) override
Definition alg_id.cpp:72
const OID & oid() const
Definition asn1_obj.h:688
BER_Decoder & decode(bool &out)
Definition ber_dec.h:358
BER_Decoder & raw_bytes(std::vector< uint8_t, Alloc > &out)
Definition ber_dec.h:338
BER_Decoder & end_cons()
Definition ber_dec.cpp:630
BER_Decoder start_sequence()
Definition ber_dec.h:275
DER_Encoder & start_sequence()
Definition der_enc.h:86
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition der_enc.cpp:237
DER_Encoder & end_cons()
Definition der_enc.cpp:208
DER_Encoder & encode(bool b)
Definition der_enc.cpp:313
bool is_single_der_object(std::span< const uint8_t > bytes, ASN1_Type expected_type, ASN1_Class expected_class)
Definition ber_dec.cpp:1100
bool is_der_sequence_header(std::span< const uint8_t > bytes)
Definition ber_dec.cpp:1128
bool operator!=(const AlgorithmIdentifier &x, const AlgorithmIdentifier &y)
Definition alg_id.cpp:58
bool operator==(const AlgorithmIdentifier &x, const AlgorithmIdentifier &y)
Definition alg_id.cpp:54