Botan 3.14.1
Crypto and TLS for C&
Botan::PKCS12 Class Referencefinal

#include <pkcs12.h>

Public Member Functions

void add_certificate (X509_Certificate cert)
void add_key (std::shared_ptr< Private_Key > key)
 Add a private key. PKCS#12 supports multiple keys per file.
std::vector< X509_Certificate > ca_certificates () const
const std::vector< X509_Certificate > & certificates () const
void clear_friendly_name ()
 Clear the friendly-name attribute.
void clear_local_key_id ()
 Clear the localKeyId attribute.
std::optional< X509_Certificate > end_entity_certificate () const
std::vector< uint8_t > export_to (const PKCS12_Export_Options &options, RandomNumberGenerator &rng) const
const std::optional< std::string > & friendly_name () const
const std::optional< std::vector< uint8_t > > & local_key_id () const
std::optional< bool > mac_protected () const
 PKCS12 ()=default
 Construct an empty bundle.
 PKCS12 (std::span< const uint8_t > data, std::string_view password)
const std::vector< std::shared_ptr< Private_Key > > & private_keys () const
void set_friendly_name (std::string name)
 Set (or replace) the friendly-name attribute.
void set_local_key_id (std::vector< uint8_t > id)
 Set (or replace) the localKeyId attribute.
const std::vector< OID > & unknown_bag_types () const

Detailed Description

PKCS#12/PFX bundle: parsed contents, mutable container, and exporter.

PKCS#12 is a file format for storing cryptographic objects (private keys and X.509 certificates) together, typically protected by a password.

The class can be used both to inspect an existing PFX and to build a new one. Construction from bytes parses an existing file; the default constructor produces an empty bundle that the caller populates with mutators (add_key, add_certificate, ...) before calling export_to to serialize.

// Parse
Botan::PKCS12 p12(pfx_bytes, "password");
if(!p12.private_keys().empty()) {
const auto& key = p12.private_keys().front();
// ...
}
if(auto ee = p12.end_entity_certificate()) {
// ...
}
// Build
out.set_friendly_name("My Bundle");
out.add_key(my_key);
out.add_certificate(my_cert);
for(const auto& ca : ca_chain) {
out.add_certificate(ca);
}
const auto blob = out.export_to(
static PKCS12_Export_Options modern(std::string_view password, std::optional< std::string > friendly_name={})
Definition pkcs12.cpp:401
std::vector< uint8_t > export_to(const PKCS12_Export_Options &options, RandomNumberGenerator &rng) const
Definition pkcs12.cpp:685
void add_certificate(X509_Certificate cert)
Definition pkcs12.cpp:665
void set_friendly_name(std::string name)
Set (or replace) the friendly-name attribute.
Definition pkcs12.cpp:669
void add_key(std::shared_ptr< Private_Key > key)
Add a private key. PKCS#12 supports multiple keys per file.
Definition pkcs12.cpp:658

Definition at line 146 of file pkcs12.h.

Constructor & Destructor Documentation

◆ PKCS12() [1/2]

Botan::PKCS12::PKCS12 ( )
default

Construct an empty bundle.

References PKCS12().

Referenced by PKCS12().

◆ PKCS12() [2/2]

Botan::PKCS12::PKCS12 ( std::span< const uint8_t > data,
std::string_view password )

Parse a PKCS#12/PFX file.

Parameters
datathe PFX file contents
passwordthe password to decrypt the file
Exceptions
Decoding_Errorif parsing fails
Invalid_Authentication_Tagif MAC verification fails

Definition at line 449 of file pkcs12.cpp.

449 {
450 std::vector<ParsedCert> cert_entries;
451 std::vector<ParsedKey> key_entries;
452
453 BER_Decoder pfx(data);
454 BER_Decoder pfx_seq = pfx.start_sequence();
455
456 size_t version = 0;
457 pfx_seq.decode(version);
458 if(version != 3) {
459 throw Decoding_Error(fmt("Unsupported PKCS#12 version: {}", version));
460 }
461
462 OID auth_safe_type;
463 std::vector<uint8_t> auth_safe_content;
464
465 BER_Decoder auth_safe_info = pfx_seq.start_sequence();
466 auth_safe_info.decode(auth_safe_type);
467
468 const OID pkcs7_data_oid = OID::from_string("PKCS7.Data");
469 if(auth_safe_type != pkcs7_data_oid) {
470 throw Decoding_Error("PKCS#12 authSafe must be of type Data");
471 }
472
473 BER_Decoder auth_safe_content_wrapper = auth_safe_info.start_context_specific(0);
474 auth_safe_content_wrapper.decode(auth_safe_content, ASN1_Type::OctetString);
475 auth_safe_content_wrapper.verify_end();
476 auth_safe_info.verify_end();
477
478 // Tracks whether MAC verification succeeded with OpenSSL's non-conforming
479 // empty-password encoding; if so, the same convention is used for any
480 // subsequent EncryptedData / PKCS8ShroudedKeyBag decryption.
481 bool openssl_empty_pwd_compat = false;
482
483 const bool has_mac_data = pfx_seq.more_items();
484 m_mac_protected = has_mac_data;
485
486 if(has_mac_data) {
487 BER_Decoder mac_data = pfx_seq.start_sequence();
488
489 BER_Decoder digest_info = mac_data.start_sequence();
490 AlgorithmIdentifier digest_algo;
491 std::vector<uint8_t> mac_value;
492 digest_info.decode(digest_algo);
493 digest_info.decode(mac_value, ASN1_Type::OctetString);
494 digest_info.verify_end();
495
496 std::vector<uint8_t> mac_salt;
497 size_t iterations = 1;
498 mac_data.decode(mac_salt, ASN1_Type::OctetString);
499 if(mac_data.more_items()) {
500 mac_data.decode(iterations);
501 }
502 mac_data.verify_end();
503 if(iterations == 0 || iterations > PKCS12_MAX_ITERATIONS) {
504 throw Decoding_Error(fmt("PKCS#12 MAC has invalid iteration count: {}", iterations));
505 }
506
507 const std::string hash_name = resolve_mac_hash(digest_algo.oid());
508 // Try RFC 7292 password encoding first. If MAC verification fails and
509 // the password is empty, retry with OpenSSL's non-conforming empty
510 // encoding (some OpenSSL releases pass an empty byte string to the KDF
511 // instead of the RFC {0x00,0x00} form when the password is empty).
512 // Propagate the chosen convention to any subsequent PBE decryption.
513 try {
514 verify_mac(auth_safe_content, mac_value, mac_salt, iterations, hash_name, password, false);
515 } catch(const Invalid_Authentication_Tag&) {
516 if(!password.empty()) {
517 throw;
518 }
519 verify_mac(auth_safe_content, mac_value, mac_salt, iterations, hash_name, password, true);
520 openssl_empty_pwd_compat = true;
521 }
522 }
523
524 parse_authenticated_safe(
525 auth_safe_content, password, cert_entries, key_entries, m_unknown_bag_types, openssl_empty_pwd_compat);
526
527 // Move all parsed keys into storage.
528 m_private_keys.reserve(key_entries.size());
529 for(auto& ke : key_entries) {
530 m_private_keys.push_back(std::move(ke.key));
531 }
532
533 // Capture bundle-level attributes from the first key (if any), or from
534 // the end-entity certificate (if found below).
535 if(!key_entries.empty()) {
536 if(!key_entries.front().friendly_name.empty()) {
537 m_friendly_name = key_entries.front().friendly_name;
538 }
539 if(!key_entries.front().local_key_id.empty()) {
540 m_local_key_id = key_entries.front().local_key_id;
541 }
542 }
543
544 // Reorder certificates so the end-entity (cert matching the first key)
545 // comes first; rest follow in original order. Match prefers localKeyId,
546 // falls back to subjectPublicKeyInfo comparison.
547 std::optional<size_t> end_entity_idx;
548 if(!cert_entries.empty() && !m_private_keys.empty()) {
549 const auto& first_key = m_private_keys.front();
550 const auto& first_key_id = key_entries.empty() ? std::vector<uint8_t>{} : key_entries.front().local_key_id;
551
552 if(!first_key_id.empty()) {
553 for(size_t i = 0; i < cert_entries.size(); ++i) {
554 if(cert_entries[i].local_key_id == first_key_id) {
555 end_entity_idx = i;
556 break;
557 }
558 }
559 }
560 if(!end_entity_idx) {
561 const auto key_spki = first_key->subject_public_key();
562 for(size_t i = 0; i < cert_entries.size(); ++i) {
563 try {
564 if(cert_entries[i].cert.subject_public_key_info() == key_spki) {
565 end_entity_idx = i;
566 break;
567 }
568 } catch(const Decoding_Error&) {
569 // Certificate with unsupported key algorithm - skip
570 }
571 }
572 }
573 }
574
575 m_certificates.reserve(cert_entries.size());
576 if(end_entity_idx) {
577 m_certificates.push_back(std::move(cert_entries[*end_entity_idx].cert));
578 if(!m_friendly_name && !cert_entries[*end_entity_idx].friendly_name.empty()) {
579 m_friendly_name = cert_entries[*end_entity_idx].friendly_name;
580 }
581 if(!m_local_key_id && !cert_entries[*end_entity_idx].local_key_id.empty()) {
582 m_local_key_id = cert_entries[*end_entity_idx].local_key_id;
583 }
584 for(size_t i = 0; i < cert_entries.size(); ++i) {
585 if(i != *end_entity_idx) {
586 // Still surface any friendly name found on non-end-entity certs
587 // when the bundle doesn't have one yet (some producers attach the
588 // attribute to the CA bag instead of the end-entity bag).
589 if(!m_friendly_name && !cert_entries[i].friendly_name.empty()) {
590 m_friendly_name = cert_entries[i].friendly_name;
591 }
592 m_certificates.push_back(std::move(cert_entries[i].cert));
593 }
594 }
595 } else {
596 for(auto& ce : cert_entries) {
597 if(!m_friendly_name && !ce.friendly_name.empty()) {
598 m_friendly_name = ce.friendly_name;
599 }
600 m_certificates.push_back(std::move(ce.cert));
601 }
602 }
603
604 pfx_seq.verify_end();
605 pfx_seq.end_cons();
606 pfx.verify_end("PKCS#12: trailing data after PFX");
607}
static OID from_string(std::string_view str)
Definition asn1_oid.cpp:80
const std::optional< std::vector< uint8_t > > & local_key_id() const
Definition pkcs12.h:200
const std::optional< std::string > & friendly_name() const
Definition pkcs12.h:194
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr size_t PKCS12_MAX_ITERATIONS
Definition pkcs12_pbe.h:25

References Botan::BER_Decoder::decode(), Botan::BER_Decoder::end_cons(), Botan::fmt(), friendly_name(), Botan::OID::from_string(), local_key_id(), Botan::BER_Decoder::more_items(), Botan::OctetString, Botan::AlgorithmIdentifier::oid(), Botan::PKCS12_MAX_ITERATIONS, Botan::BER_Decoder::start_context_specific(), Botan::BER_Decoder::start_sequence(), and Botan::BER_Decoder::verify_end().

Member Function Documentation

◆ add_certificate()

void Botan::PKCS12::add_certificate ( X509_Certificate cert)

Add a certificate. End-entity vs CA is determined at export time by matching against stored keys.

Definition at line 665 of file pkcs12.cpp.

665 {
666 m_certificates.push_back(std::move(cert));
667}

◆ add_key()

void Botan::PKCS12::add_key ( std::shared_ptr< Private_Key > key)

Add a private key. PKCS#12 supports multiple keys per file.

Definition at line 658 of file pkcs12.cpp.

658 {
659 if(!key) {
660 throw Invalid_Argument("PKCS12::add_key: key must not be null");
661 }
662 m_private_keys.push_back(std::move(key));
663}

◆ ca_certificates()

std::vector< X509_Certificate > Botan::PKCS12::ca_certificates ( ) const

Convenience helper: every certificate except the one returned by end_entity_certificate. Returned in storage order.

Definition at line 609 of file pkcs12.cpp.

609 {
610 if(m_certificates.size() < 2) {
611 return {};
612 }
613 const auto ee = end_entity_certificate();
614 std::vector<X509_Certificate> result;
615 result.reserve(m_certificates.size() - 1);
616 if(ee) {
617 // Skip the first certificate matching the end-entity (only one, in case
618 // the bundle contains multiple certs signed for the same key, e.g. an
619 // old leaf still kept alongside a renewed one).
620 const auto ee_spki = ee->subject_public_key_info();
621 bool skipped = false;
622 for(const auto& c : m_certificates) {
623 if(!skipped && c.subject_public_key_info() == ee_spki) {
624 skipped = true;
625 continue;
626 }
627 result.push_back(c);
628 }
629 } else {
630 // No end-entity (e.g. key-less bundle): treat the first stored cert as
631 // the "primary" and surface the rest as CA / chain certs. This matches
632 // the storage order used by parsing.
633 for(size_t i = 1; i < m_certificates.size(); ++i) {
634 result.push_back(m_certificates[i]);
635 }
636 }
637 return result;
638}
std::optional< X509_Certificate > end_entity_certificate() const
Definition pkcs12.cpp:640

References end_entity_certificate().

◆ certificates()

const std::vector< X509_Certificate > & Botan::PKCS12::certificates ( ) const
inline

Certificates stored in the bundle, in the order they appear in the PFX or in insertion order. The end-entity certificate (if any) is not separated from CA/intermediate certificates at storage level; use end_entity_certificate to obtain it.

Definition at line 175 of file pkcs12.h.

175{ return m_certificates; }

◆ clear_friendly_name()

void Botan::PKCS12::clear_friendly_name ( )

Clear the friendly-name attribute.

Definition at line 673 of file pkcs12.cpp.

673 {
674 m_friendly_name.reset();
675}

◆ clear_local_key_id()

void Botan::PKCS12::clear_local_key_id ( )

Clear the localKeyId attribute.

Definition at line 681 of file pkcs12.cpp.

681 {
682 m_local_key_id.reset();
683}

◆ end_entity_certificate()

std::optional< X509_Certificate > Botan::PKCS12::end_entity_certificate ( ) const
Returns
the first certificate whose subjectPublicKeyInfo matches one of the stored private keys, or nullopt if none match (e.g. a certificate-only or key-only bundle).

Definition at line 640 of file pkcs12.cpp.

640 {
641 if(m_certificates.empty() || m_private_keys.empty()) {
642 return std::nullopt;
643 }
644 const auto& first_key = m_private_keys.front();
645 const auto key_spki = first_key->subject_public_key();
646 for(const auto& c : m_certificates) {
647 try {
648 if(c.subject_public_key_info() == key_spki) {
649 return c;
650 }
651 } catch(const Decoding_Error&) {
652 // Skip certificates with unsupported algorithms
653 }
654 }
655 return std::nullopt;
656}

Referenced by ca_certificates().

◆ export_to()

std::vector< uint8_t > Botan::PKCS12::export_to ( const PKCS12_Export_Options & options,
RandomNumberGenerator & rng ) const

Serialize the bundle as a PKCS#12/PFX file.

Parameters
optionsexport options (password, algorithms, ...).
Random Number GeneratorsRNG used to generate salts, IVs and (if requested) the localKeyId when none is set explicitly.
Exceptions
Invalid_Argumentif options is internally inconsistent (e.g. an unsupported algorithm is requested).
Invalid_Argumentif a stored private key does not match any stored certificate (this implementation requires the end-entity cert to be present when a key is exported).

Definition at line 685 of file pkcs12.cpp.

685 {
686 if(m_private_keys.empty() && m_certificates.empty()) {
687 throw Invalid_Argument("PKCS#12::export_to requires at least a key or certificate");
688 }
689
690 validate_options(options);
691
692 // Determine end-entity certificate(s). With a single key we pair it
693 // against a cert matching its SPKI; that pair gets the
694 // friendly_name/localKeyId from options or the bundle.
695 std::optional<size_t> end_entity_idx;
696 if(!m_private_keys.empty() && !m_certificates.empty()) {
697 const auto& first_key = m_private_keys.front();
698 const auto key_spki = first_key->subject_public_key();
699 for(size_t i = 0; i < m_certificates.size(); ++i) {
700 try {
701 if(m_certificates[i].subject_public_key_info() == key_spki) {
702 end_entity_idx = i;
703 break;
704 }
705 } catch(const Decoding_Error&) {
706 // skip
707 }
708 }
709 if(!end_entity_idx) {
710 throw Invalid_Argument("PKCS#12::export_to: private key does not match any certificate");
711 }
712 }
713
714 const OID cert_bag_oid = OID::from_string("PKCS12.CertBag");
715 const OID shrouded_key_oid = OID::from_string("PKCS12.PKCS8ShroudedKeyBag");
716 const OID x509_cert_oid = OID::from_string("PKCS9.X509Certificate");
717 const OID friendly_name_oid = OID::from_string("PKCS9.FriendlyName");
718 const OID local_key_id_oid = OID::from_string("PKCS9.LocalKeyId");
719 const OID pkcs7_data_oid = OID::from_string("PKCS7.Data");
720 const OID pkcs7_enc_data_oid = OID::from_string("PKCS7.EncryptedData");
721
722 // Pick the friendly-name and local-key-id used by attribute encoding.
723 // Options take precedence over the bundle-level fields.
724 const std::optional<std::string>& friendly_name =
725 options.friendly_name().has_value() ? options.friendly_name() : m_friendly_name;
726
727 std::vector<uint8_t> local_key_id;
728 if(m_local_key_id) {
729 local_key_id = *m_local_key_id;
730 } else if(end_entity_idx) {
731 local_key_id = m_certificates[*end_entity_idx].subject_public_key_bitstring_sha1();
732 } else if(!m_private_keys.empty()) {
733 // Key-only bundle: derive from SHA-1 of the public key bits (matching the
734 // convention used by X509_Certificate::subject_public_key_bitstring_sha1).
735 auto sha1 = HashFunction::create_or_throw("SHA-1");
736 const auto pub_bits = m_private_keys.front()->public_key_bits();
737 sha1->update(pub_bits);
738 local_key_id = unlock(sha1->final());
739 }
740
741 auto write_attributes = [&](DER_Encoder& enc) {
742 const bool has_fn = friendly_name.has_value() && !friendly_name->empty();
743 const bool has_id = !local_key_id.empty();
744 if(!has_fn && !has_id) {
745 return;
746 }
747 enc.start_set();
748 if(has_fn) {
749 enc.start_sequence();
750 enc.encode(friendly_name_oid);
751 enc.start_set();
752 encode_bmpstring(enc, *friendly_name);
753 enc.end_cons();
754 enc.end_cons();
755 }
756 if(has_id) {
757 enc.start_sequence();
758 enc.encode(local_key_id_oid);
759 enc.start_set();
761 enc.end_cons();
762 enc.end_cons();
763 }
764 enc.end_cons();
765 };
766
767 // CertBags
768 std::vector<uint8_t> cert_safe_contents;
769 if(!m_certificates.empty()) {
770 DER_Encoder cert_bags(cert_safe_contents);
771 cert_bags.start_sequence();
772
773 const auto add_cert_bag = [&](const X509_Certificate& c, bool add_attrs) {
774 cert_bags.start_sequence();
775 cert_bags.encode(cert_bag_oid);
776
777 cert_bags.start_context_specific(0);
778 cert_bags.start_sequence();
779 cert_bags.encode(x509_cert_oid);
780 cert_bags.start_context_specific(0);
781 cert_bags.encode(c.BER_encode(), ASN1_Type::OctetString);
782 cert_bags.end_cons();
783 cert_bags.end_cons();
784 cert_bags.end_cons();
785
786 if(add_attrs) {
787 write_attributes(cert_bags);
788 }
789
790 cert_bags.end_cons();
791 };
792
793 // End-entity first (so the file is read in the typical order), then
794 // the rest in their stored order.
795 if(end_entity_idx) {
796 add_cert_bag(m_certificates[*end_entity_idx], true);
797 for(size_t i = 0; i < m_certificates.size(); ++i) {
798 if(i != *end_entity_idx) {
799 add_cert_bag(m_certificates[i], false);
800 }
801 }
802 } else {
803 for(const auto& c : m_certificates) {
804 add_cert_bag(c, false);
805 }
806 }
807
808 cert_bags.end_cons();
809 }
810
811 // Key SafeBag(s)
812 std::vector<uint8_t> key_safe_contents;
813 if(!m_private_keys.empty()) {
814 DER_Encoder key_bags(key_safe_contents);
815 key_bags.start_sequence();
816
817 for(size_t i = 0; i < m_private_keys.size(); ++i) {
818 const Private_Key& key = *m_private_keys[i];
819
820 key_bags.start_sequence();
821 key_bags.encode(shrouded_key_oid);
822
824 auto [enc_algo, enc_key] =
825 pkcs12_pbe_encrypt(pkcs8_key, options.password(), options.key_encryption_algo(), options.iterations(), rng);
826
827 key_bags.start_context_specific(0);
828 key_bags.start_sequence();
829 key_bags.encode(enc_algo);
830 key_bags.encode(enc_key, ASN1_Type::OctetString);
831 key_bags.end_cons();
832 key_bags.end_cons();
833
834 // Only the first key carries the bundle-level attributes (preserves
835 // the historical single-key behavior).
836 if(i == 0) {
837 write_attributes(key_bags);
838 }
839
840 key_bags.end_cons();
841 }
842 key_bags.end_cons();
843 }
844
845 // AuthenticatedSafe
846 std::vector<uint8_t> auth_safe_content;
847 DER_Encoder auth_safe(auth_safe_content);
848 auth_safe.start_sequence();
849
850 if(!cert_safe_contents.empty()) {
851 if(!options.cert_encryption_algo().empty()) {
852 auto [enc_algo, enc_data] = pkcs12_pbe_encrypt(
853 cert_safe_contents, options.password(), options.cert_encryption_algo(), options.iterations(), rng);
854
855 auth_safe.start_sequence();
856 auth_safe.encode(pkcs7_enc_data_oid);
857 auth_safe.start_context_specific(0);
858 auth_safe.start_sequence();
859 auth_safe.encode(size_t(0));
860 auth_safe.start_sequence();
861 auth_safe.encode(pkcs7_data_oid);
862 auth_safe.encode(enc_algo);
863 auth_safe.add_object(ASN1_Type(0), ASN1_Class::ContextSpecific, enc_data);
864 auth_safe.end_cons();
865 auth_safe.end_cons();
866 auth_safe.end_cons();
867 auth_safe.end_cons();
868 } else {
869 auth_safe.start_sequence();
870 auth_safe.encode(pkcs7_data_oid);
871 auth_safe.start_context_specific(0);
872 auth_safe.encode(cert_safe_contents, ASN1_Type::OctetString);
873 auth_safe.end_cons();
874 auth_safe.end_cons();
875 }
876 }
877
878 if(!key_safe_contents.empty()) {
879 auth_safe.start_sequence();
880 auth_safe.encode(pkcs7_data_oid);
881 auth_safe.start_context_specific(0);
882 auth_safe.encode(key_safe_contents, ASN1_Type::OctetString);
883 auth_safe.end_cons();
884 auth_safe.end_cons();
885 }
886
887 auth_safe.end_cons();
888
889 // PFX
890 std::vector<uint8_t> pfx_data;
891 DER_Encoder pfx(pfx_data);
892 pfx.start_sequence();
893 pfx.encode(size_t(3));
894
895 pfx.start_sequence();
896 pfx.encode(pkcs7_data_oid);
897 pfx.start_context_specific(0);
898 pfx.encode(auth_safe_content, ASN1_Type::OctetString);
899 pfx.end_cons();
900 pfx.end_cons();
901
902 if(options.include_mac()) {
903 const std::string& mac_hash = options.mac_digest();
904
905 auto hmac = MessageAuthenticationCode::create_or_throw(fmt("HMAC({})", mac_hash));
906
907 std::vector<uint8_t> mac_salt(hmac->output_length());
908 rng.randomize(mac_salt.data(), mac_salt.size());
909 const size_t mac_key_len = hmac->output_length();
910 secure_vector<uint8_t> mac_key(mac_key_len);
911 const PKCS12_KDF kdf(HashFunction::create_or_throw(mac_hash), 3, options.iterations());
912 kdf.derive_key(mac_key.data(),
913 mac_key_len,
914 options.password().data(),
915 options.password().size(),
916 mac_salt.data(),
917 mac_salt.size());
918
919 hmac->set_key(mac_key);
920 hmac->update(auth_safe_content);
921 const secure_vector<uint8_t> mac_value = hmac->final();
922
923 pfx.start_sequence();
924 pfx.start_sequence();
925 const auto param_encoding =
926 (mac_hash == "SHA-1") ? AlgorithmIdentifier::USE_NULL_PARAM : AlgorithmIdentifier::USE_EMPTY_PARAM;
927 pfx.encode(AlgorithmIdentifier(OID::from_string(mac_hash), param_encoding));
928 pfx.encode(mac_value, ASN1_Type::OctetString);
929 pfx.end_cons();
930 pfx.encode(mac_salt, ASN1_Type::OctetString);
931 if(options.iterations() != 1) {
932 pfx.encode(options.iterations());
933 }
934 pfx.end_cons();
935 }
936
937 pfx.end_cons();
938
939 return pfx_data;
940}
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:311
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition mac.cpp:142
std::vector< uint8_t > BER_encode(const Private_Key &key, RandomNumberGenerator &rng, std::string_view pass, std::chrono::milliseconds msec, std::string_view pbe_algo)
Definition pkcs8.cpp:168
ASN1_Type
Definition asn1_obj.h:47
std::vector< T > unlock(const secure_vector< T > &in)
Definition secmem.h:155
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
std::pair< AlgorithmIdentifier, std::vector< uint8_t > > pkcs12_pbe_encrypt(std::span< const uint8_t > plaintext, std::string_view password, std::string_view algo, size_t iterations, RandomNumberGenerator &rng)

References Botan::DER_Encoder::add_object(), Botan::ASN1_Object::BER_encode(), Botan::PKCS8::BER_encode(), Botan::PKCS12_Export_Options::cert_encryption_algo(), Botan::ContextSpecific, Botan::HashFunction::create_or_throw(), Botan::MessageAuthenticationCode::create_or_throw(), Botan::PKCS12_KDF::derive_key(), Botan::DER_Encoder::encode(), Botan::DER_Encoder::end_cons(), Botan::fmt(), friendly_name(), Botan::PKCS12_Export_Options::friendly_name(), Botan::OID::from_string(), Botan::PKCS12_Export_Options::include_mac(), Botan::PKCS12_Export_Options::iterations(), Botan::PKCS12_Export_Options::key_encryption_algo(), local_key_id(), Botan::PKCS12_Export_Options::mac_digest(), Botan::OctetString, Botan::PKCS12_Export_Options::password(), Botan::pkcs12_pbe_encrypt(), Botan::RandomNumberGenerator::randomize(), Botan::DER_Encoder::start_context_specific(), Botan::DER_Encoder::start_sequence(), Botan::unlock(), Botan::AlgorithmIdentifier::USE_EMPTY_PARAM, and Botan::AlgorithmIdentifier::USE_NULL_PARAM.

◆ friendly_name()

const std::optional< std::string > & Botan::PKCS12::friendly_name ( ) const
inline

Friendly-name attribute attached to the private key / end-entity certificate bag, if present.

Definition at line 194 of file pkcs12.h.

194{ return m_friendly_name; }

Referenced by export_to(), and PKCS12().

◆ local_key_id()

const std::optional< std::vector< uint8_t > > & Botan::PKCS12::local_key_id ( ) const
inline

localKeyId attribute attached to the private key / end-entity certificate bag, if present.

Definition at line 200 of file pkcs12.h.

200{ return m_local_key_id; }

Referenced by export_to(), and PKCS12().

◆ mac_protected()

std::optional< bool > Botan::PKCS12::mac_protected ( ) const
inline

Whether the parsed PFX was integrity protected by a MAC.

Returns
true if the file carried MacData (which was verified during parsing), false if it carried none, and nullopt for a bundle constructed in-memory rather than parsed from a file.

Definition at line 217 of file pkcs12.h.

217{ return m_mac_protected; }

◆ private_keys()

const std::vector< std::shared_ptr< Private_Key > > & Botan::PKCS12::private_keys ( ) const
inline

Private keys stored in the bundle, in the order they appear in the PFX (for a parsed file) or in insertion order (for a built one). PKCS#12 allows multiple keys per file; parsing currently surfaces all KeyBag / PKCS8ShroudedKeyBag entries.

Definition at line 167 of file pkcs12.h.

167{ return m_private_keys; }

◆ set_friendly_name()

void Botan::PKCS12::set_friendly_name ( std::string name)

Set (or replace) the friendly-name attribute.

Definition at line 669 of file pkcs12.cpp.

669 {
670 m_friendly_name = std::move(name);
671}
const char * name
Definition ffi_tls.h:59

References name.

◆ set_local_key_id()

void Botan::PKCS12::set_local_key_id ( std::vector< uint8_t > id)

Set (or replace) the localKeyId attribute.

Definition at line 677 of file pkcs12.cpp.

677 {
678 m_local_key_id = std::move(id);
679}

◆ unknown_bag_types()

const std::vector< OID > & Botan::PKCS12::unknown_bag_types ( ) const
inline

OIDs of bag types encountered during parsing but not handled by this implementation (e.g. SecretBag). Empty for normal files and for bundles constructed in-memory.

Definition at line 207 of file pkcs12.h.

207{ return m_unknown_bag_types; }

The documentation for this class was generated from the following files: