Botan 3.13.0
Crypto and TLS for C&
pkcs12_pbe.h
Go to the documentation of this file.
1/*
2* PKCS#12 PBE (RFC 7292 Appendix B)
3* (C) 2026 Damiano Mazzella
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_PKCS12_PBE_H_
9#define BOTAN_PKCS12_PBE_H_
10
11#include <botan/asn1_obj.h>
12#include <botan/secmem.h>
13#include <span>
14#include <string_view>
15#include <utility>
16#include <vector>
17
18namespace Botan {
19
21
22/// Maximum allowed iteration count for PKCS#12 KDF/PBE/MAC operations
23///
24/// This matches the limit used by NSS, AWS-LC and BoringSSL for PKCS12 files
25inline constexpr size_t PKCS12_MAX_ITERATIONS = 100'000'000;
26
27/**
28* Decrypt data protected by PKCS#12 PBE (RFC 7292 Appendix B) or PBES2.
29* @param ciphertext the encrypted data
30* @param password the decryption password
31* @param pbe_algo the AlgorithmIdentifier from the EncryptedData structure
32* @param openssl_empty_pwd_compat if @c true and @p password is empty, the
33* password is fed to the PKCS#12 KDF as an empty byte string (matching
34* OpenSSL's non-conforming behavior) instead of the RFC 7292 form
35* (a two-byte {0x00,0x00} terminator). Has no effect on PBES2 or when
36* the password is non-empty.
37*/
38secure_vector<uint8_t> pkcs12_pbe_decrypt(std::span<const uint8_t> ciphertext,
39 std::string_view password,
40 const AlgorithmIdentifier& pbe_algo,
41 bool openssl_empty_pwd_compat = false);
42
43/**
44* Encrypt data using PKCS#12 PBE (RFC 7292 Appendix B) or PBES2.
45* @param plaintext the data to encrypt
46* @param password the encryption password
47* @param algo algorithm name: "PBES2-SHA256-AES256", "PBES2-SHA256-AES128",
48* "PBE-SHA1-3DES" (legacy), "PBE-SHA1-2DES" (legacy).
49* Higher-level APIs default to "PBES2-SHA256-AES256" via PKCS12_Options.
50* @param iterations PBKDF iteration count
51* @param rng a random number generator
52* @return the AlgorithmIdentifier and encrypted data
53*/
54std::pair<AlgorithmIdentifier, std::vector<uint8_t>> pkcs12_pbe_encrypt(std::span<const uint8_t> plaintext,
55 std::string_view password,
56 std::string_view algo,
57 size_t iterations,
59
60} // namespace Botan
61
62#endif
secure_vector< uint8_t > pkcs12_pbe_decrypt(std::span< const uint8_t > ciphertext, std::string_view password, const AlgorithmIdentifier &pbe_algo, bool openssl_empty_pwd_compat)
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)
constexpr size_t PKCS12_MAX_ITERATIONS
Definition pkcs12_pbe.h:25