291namespace Cert_Extension {
298 throw Invalid_State(
"Basic_Constraints::get_path_limit: Not a CA");
306std::vector<uint8_t> Basic_Constraints::encode_inner()
const {
307 std::vector<uint8_t> output;
310 .
encode_if(m_is_ca,
DER_Encoder().encode(m_is_ca).encode_optional(m_path_limit, NO_CERT_PATH_LIMIT))
318void Basic_Constraints::decode_inner(
const std::vector<uint8_t>& in) {
325 if(m_is_ca ==
false) {
333std::vector<uint8_t> Key_Usage::encode_inner()
const {
334 if(m_constraints.
empty()) {
335 throw Encoding_Error(
"Cannot encode empty PKIX key constraints");
338 const size_t constraint_bits = m_constraints.
value();
339 const size_t unused_bits =
ctz(
static_cast<uint32_t
>(constraint_bits));
341 std::vector<uint8_t> der;
343 der.push_back(2 + ((unused_bits < 8) ? 1 : 0));
344 der.push_back(unused_bits % 8);
345 der.push_back((constraint_bits >> 8) & 0xFF);
346 if(constraint_bits & 0xFF) {
347 der.push_back(constraint_bits & 0xFF);
356void Key_Usage::decode_inner(
const std::vector<uint8_t>& in) {
359 BER_Object obj = ber.get_next_object();
363 if(obj.length() == 2 || obj.length() == 3) {
366 const uint8_t* bits = obj.bits();
369 throw BER_Decoding_Error(
"Invalid unused bits in usage constraint");
372 const uint8_t mask =
static_cast<uint8_t
>(0xFF << bits[0]);
374 if(obj.length() == 2) {
376 }
else if(obj.length() == 3) {
380 m_constraints = Key_Constraints(usage);
382 m_constraints = Key_Constraints(0);
389std::vector<uint8_t> Subject_Key_ID::encode_inner()
const {
390 std::vector<uint8_t> output;
398void Subject_Key_ID::decode_inner(
const std::vector<uint8_t>& in) {
408 m_key_id.resize(hash->output_length());
410 hash->update(pub_key);
411 hash->final(m_key_id.data());
414 const size_t max_skid_len = (192 / 8);
415 if(m_key_id.size() > max_skid_len) {
416 m_key_id.resize(max_skid_len);
423std::vector<uint8_t> Authority_Key_ID::encode_inner()
const {
424 std::vector<uint8_t> output;
435void Authority_Key_ID::decode_inner(
const std::vector<uint8_t>& in) {
442std::vector<uint8_t> Subject_Alternative_Name::encode_inner()
const {
443 std::vector<uint8_t> output;
444 DER_Encoder(output).encode(m_alt_name);
451std::vector<uint8_t> Issuer_Alternative_Name::encode_inner()
const {
452 std::vector<uint8_t> output;
453 DER_Encoder(output).encode(m_alt_name);
460void Subject_Alternative_Name::decode_inner(
const std::vector<uint8_t>& in) {
461 BER_Decoder(in).
decode(m_alt_name);
467void Issuer_Alternative_Name::decode_inner(
const std::vector<uint8_t>& in) {
468 BER_Decoder(in).
decode(m_alt_name);
474std::vector<uint8_t> Extended_Key_Usage::encode_inner()
const {
475 std::vector<uint8_t> output;
483void Extended_Key_Usage::decode_inner(
const std::vector<uint8_t>& in) {
490std::vector<uint8_t> Name_Constraints::encode_inner()
const {
491 throw Not_Implemented(
"Name_Constraints encoding");
497void Name_Constraints::decode_inner(
const std::vector<uint8_t>& in) {
499 BER_Decoder inner = ber.start_sequence();
501 std::vector<GeneralSubtree> permitted;
503 if(permitted.empty()) {
504 throw Decoding_Error(
"Empty NameConstraint permitted list");
508 std::vector<GeneralSubtree> excluded;
510 if(excluded.empty()) {
511 throw Decoding_Error(
"Empty NameConstraint excluded list");
517 if(permitted.empty() && excluded.empty()) {
518 throw Decoding_Error(
"Empty NameConstraint extension");
521 m_name_constraints = NameConstraints(std::move(permitted), std::move(excluded));
526 const std::vector<X509_Certificate>& cert_path,
527 std::vector<std::set<Certificate_Status_Code>>& cert_status,
529 if(!m_name_constraints.
permitted().empty() || !m_name_constraints.
excluded().empty()) {
534 const bool issuer_name_constraint_critical = subject.
is_critical(
"X509v3.NameConstraints");
537 for(
size_t j = 0; j < pos; ++j) {
538 const auto& cert = cert_path.at(j);
540 if(!m_name_constraints.
is_permitted(cert, issuer_name_constraint_critical)) {
545 if(m_name_constraints.
is_excluded(cert, issuer_name_constraint_critical)) {
560 Policy_Information() =
default;
562 explicit Policy_Information(
const OID& oid) : m_oid(oid) {}
564 const OID& oid()
const {
return m_oid; }
566 void encode_into(DER_Encoder& codec)
const override { codec.start_sequence().encode(m_oid).end_cons(); }
568 void decode_from(BER_Decoder& codec)
override {
569 codec.start_sequence().decode(m_oid).discard_remaining().end_cons();
581std::vector<uint8_t> Certificate_Policies::encode_inner()
const {
582 std::vector<Policy_Information> policies;
584 policies.reserve(m_oids.size());
585 for(
const auto& oid : m_oids) {
586 policies.push_back(Policy_Information(oid));
589 std::vector<uint8_t> output;
590 DER_Encoder(output).start_sequence().encode_list(policies).end_cons();
597void Certificate_Policies::decode_inner(
const std::vector<uint8_t>& in) {
598 std::vector<Policy_Information> policies;
600 BER_Decoder(in).decode_list(policies);
602 for(
const auto& policy : policies) {
603 m_oids.push_back(policy.oid());
609 const std::vector<X509_Certificate>& ,
610 std::vector<std::set<Certificate_Status_Code>>& cert_status,
612 std::set<OID> oid_set(m_oids.begin(), m_oids.end());
613 if(oid_set.size() != m_oids.size()) {
618std::vector<uint8_t> Authority_Information_Access::encode_inner()
const {
619 std::vector<uint8_t> output;
622 der.start_sequence();
624 if(!m_ocsp_responder.empty()) {
633 for(
const auto& ca_isser : m_ca_issuers) {
645void Authority_Information_Access::decode_inner(
const std::vector<uint8_t>& in) {
646 BER_Decoder ber = BER_Decoder(in).start_sequence();
648 while(ber.more_items()) {
651 BER_Decoder info = ber.start_sequence();
656 BER_Object
name = info.get_next_object();
663 BER_Object
name = info.get_next_object();
689 return std::make_unique<CRL_Number>(m_crl_number);
695std::vector<uint8_t> CRL_Number::encode_inner()
const {
696 std::vector<uint8_t> output;
704void CRL_Number::decode_inner(
const std::vector<uint8_t>& in) {
712std::vector<uint8_t> CRL_ReasonCode::encode_inner()
const {
713 std::vector<uint8_t> output;
721void CRL_ReasonCode::decode_inner(
const std::vector<uint8_t>& in) {
722 size_t reason_code = 0;
724 m_reason =
static_cast<CRL_Code>(reason_code);
727std::vector<uint8_t> CRL_Distribution_Points::encode_inner()
const {
728 std::vector<uint8_t> output;
729 DER_Encoder(output).start_sequence().encode_list(m_distribution_points).end_cons();
733void CRL_Distribution_Points::decode_inner(
const std::vector<uint8_t>& buf) {
734 BER_Decoder(buf).decode_list(m_distribution_points).verify_end();
736 std::stringstream ss;
738 for(
const auto& distribution_point : m_distribution_points) {
739 auto contents = distribution_point.point().contents();
741 for(
const auto& pair : contents) {
742 ss << pair.first <<
": " << pair.second <<
" ";
746 m_crl_distribution_urls.push_back(ss.str());
750 const auto uris = m_point.
uris();
753 throw Not_Implemented(
"Empty CRL_Distribution_Point encoding not implemented");
756 for(
const auto& uri : uris) {
779std::vector<uint8_t> CRL_Issuing_Distribution_Point::encode_inner()
const {
783void CRL_Issuing_Distribution_Point::decode_inner(
const std::vector<uint8_t>& buf) {
788 throw Not_Implemented(
"TNAuthList extension entry serialization is not supported");
794 const uint32_t type_tag =
static_cast<Type>(obj.
type_tag());
796 if(type_tag == ServiceProviderCode) {
797 m_type = ServiceProviderCode;
800 m_data = std::move(spc_string);
801 }
else if(type_tag == TelephoneNumberRange) {
802 m_type = TelephoneNumberRange;
804 auto& range_items = std::get<RangeContainer>(m_data);
810 if(!is_valid_telephone_number(entry.
start)) {
815 if(entry.
count < 2) {
819 range_items.emplace_back(std::move(entry));
823 if(range_items.empty()) {
826 }
else if(type_tag == TelephoneNumber) {
827 m_type = TelephoneNumber;
830 if(!is_valid_telephone_number(one_string)) {
833 m_data = std::move(one_string);
839std::vector<uint8_t> TNAuthList::encode_inner()
const {
840 throw Not_Implemented(
"TNAuthList extension serialization is not supported");
843void TNAuthList::decode_inner(
const std::vector<uint8_t>& in) {
845 if(m_tn_entries.empty()) {
850void OCSP_NoCheck::decode_inner(
const std::vector<uint8_t>& buf) {
854std::vector<uint8_t> Unknown_Extension::encode_inner()
const {
858void Unknown_Extension::decode_inner(
const std::vector<uint8_t>& bytes) {