23 : m_Root(root), m_DecoderType(std::move(decoderType))
28 template <
class Asn1RecordType>
29 Asn1RecordType* castSubRecordAs(
int index,
const std::string& fieldName)
const
33 return m_Root->
getSubRecords().at(index)->template castAs<Asn1RecordType>();
35 catch (
const std::exception&)
37 throw std::runtime_error(
"Invalid " + m_DecoderType +
" data: " + fieldName);
43 std::string m_DecoderType;
86 static constexpr
int versionOffset = 0;
87 static constexpr
int modulusOffset = 1;
88 static constexpr
int publicExponentOffset = 2;
89 static constexpr
int privateExponentOffset = 3;
90 static constexpr
int prime1Offset = 4;
91 static constexpr
int prime2Offset = 5;
92 static constexpr
int exponent1Offset = 6;
93 static constexpr
int exponent2Offset = 7;
94 static constexpr
int coefficientOffset = 8;
120 static constexpr
int versionOffset = 0;
121 static constexpr
int privateKeyOffset = 1;
123 int m_ParametersOffset = -1;
124 int m_PublicKeyOffset = -1;
136 return m_Root->encode();
148 : m_DerData(std::move(derData)), m_Root(
Asn1Record::decode(m_DerData.get(), derDataLen))
151 CryptographicKey(uint8_t* derData,
size_t derDataLen,
bool ownDerData)
156 m_DerData.reset(derData);
160 ~CryptographicKey() =
default;
162 Asn1SequenceRecord* getRoot()
const
166 return m_Root->castAs<Asn1SequenceRecord>();
168 catch (
const std::bad_cast&)
170 throw std::runtime_error(
"Invalid " + std::string(CryptoKey::keyType) +
" data");
174 template <
class Asn1RecordType>
175 Asn1RecordType* castSubRecordAs(
int index,
const std::string& fieldName)
const
179 return getRoot()->
getSubRecords().at(index)->template castAs<Asn1RecordType>();
181 catch (
const std::exception&)
183 throw std::runtime_error(
"Invalid " + std::string(CryptoKey::keyType) +
" data: " + fieldName);
188 std::unique_ptr<uint8_t[]> m_DerData;
189 std::unique_ptr<Asn1Record> m_Root;
242 constexpr
operator Value()
const
248 explicit operator bool()
const =
delete;
262 RSAPrivateKey(std::unique_ptr<uint8_t[]> derData,
size_t derDataLen)
263 : CryptographicKey(std::move(derData), derDataLen), RSAPrivateKeyDataView(getRoot(), keyType)
266 RSAPrivateKey(uint8_t* derData,
size_t derDataLen,
bool ownDerData)
267 : CryptographicKey(derData, derDataLen, ownDerData), RSAPrivateKeyDataView(getRoot(), keyType)
271 static constexpr
const char* pemLabel =
"RSA PRIVATE KEY";
272 static constexpr
const char* keyType =
"RSA private key";
274 using CryptographicKey::CryptographicKey;
287 ECPrivateKey(std::unique_ptr<uint8_t[]> derData,
size_t derDataLen)
288 : CryptographicKey(std::move(derData), derDataLen), ECPrivateKeyDataView(getRoot(), keyType)
291 ECPrivateKey(uint8_t* derData,
size_t derDataLen,
bool ownDerData)
292 : CryptographicKey(derData, derDataLen, ownDerData), ECPrivateKeyDataView(getRoot(), keyType)
296 static constexpr
const char* pemLabel =
"EC PRIVATE KEY";
297 static constexpr
const char* keyType =
"EC private key";
299 using CryptographicKey::CryptographicKey;
325 template <
class PrivateKeyDataType> PrivateKeyDataType*
castAs()
327 auto privateKeyData =
dynamic_cast<PrivateKeyDataType*
>(
this);
328 if (privateKeyData ==
nullptr)
330 throw std::runtime_error(
"Trying to PKCS#8 private key data to the wrong type");
332 return privateKeyData;
343 std::vector<uint8_t> m_DerData;
344 std::unique_ptr<Asn1Record> m_Root;
395 template <
typename PrivateKeyDataType> std::unique_ptr<PrivateKeyDataType>
getPrivateKeyAs()
const
398 if (privateKey ==
nullptr)
403 if (
auto* specificPrivateKey =
dynamic_cast<PrivateKeyDataType*
>(privateKey.get()))
405 privateKey.release();
406 return std::unique_ptr<PrivateKeyDataType>(specificPrivateKey);
413 static constexpr
const char* pemLabel =
"PRIVATE KEY";
414 static constexpr
const char* keyType =
"PKCS#8 private key";
415 static constexpr
int versionOffset = 0;
416 static constexpr
int privateKeyAlgorithmOffset = 1;
417 static constexpr
int privateKeyOffset = 2;
419 using CryptographicKey::CryptographicKey;
438 static constexpr
const char* pemLabel =
"RSA PUBLIC KEY";
439 static constexpr
const char* keyType =
"RSA public key";
440 static constexpr
int modulusOffset = 0;
441 static constexpr
int publicExponentOffset = 1;
443 using CryptographicKey::CryptographicKey;
463 static constexpr
const char* pemLabel =
"PUBLIC KEY";
464 static constexpr
const char* keyType =
"public key";
465 static constexpr
int algorithmOffset = 0;
466 static constexpr
int subjectPublicKeyOffset = 1;
468 using CryptographicKey::CryptographicKey;
PointerVector< Asn1Record > & getSubRecords()
Definition: Asn1Codec.h:312
Definition: Asn1Codec.h:699
Definition: Asn1Codec.h:131
static std::unique_ptr< Asn1Record > decode(const uint8_t *data, size_t dataLen, bool lazy=true)
Definition: Asn1Codec.h:355
Definition: CryptoKeyDecoder.h:198
static CryptographicKeyAlgorithm fromOidValue(const Asn1ObjectIdentifier &value)
Creates a CryptographicKeyAlgorithm object from an OID value.
std::string toString() const
constexpr CryptographicKeyAlgorithm(Value value)
Constructs a CryptographicKeyAlgorithm object from a Value enum.
Definition: CryptoKeyDecoder.h:226
std::string getOidValue() const
Value
Define enum types and the corresponding int values.
Definition: CryptoKeyDecoder.h:202
@ X448
Diffie-Hellman using Curve448 (Goldilocks curve)
Definition: CryptoKeyDecoder.h:216
@ ED25519
EdDSA using Curve25519 (Ed25519)
Definition: CryptoKeyDecoder.h:210
@ ECDSA
Elliptic Curve Digital Signature Algorithm.
Definition: CryptoKeyDecoder.h:208
@ DiffieHellman
Diffie-Hellman key exchange algorithm.
Definition: CryptoKeyDecoder.h:214
@ ED448
EdDSA using Curve448 (Ed448)
Definition: CryptoKeyDecoder.h:212
@ RSA
RSA encryption/signature algorithm.
Definition: CryptoKeyDecoder.h:204
@ Unknown
Unknown or unsupported algorithm.
Definition: CryptoKeyDecoder.h:218
@ DSA
Digital Signature Algorithm.
Definition: CryptoKeyDecoder.h:206
Represents an EC private key in SEC1 format This class provides methods to decode and access the comp...
Definition: CryptoKeyDecoder.h:285
Contains EC private key data extracted from PKCS#8 format This class provides access to the component...
Definition: CryptoKeyDecoder.h:362
Contains Ed25519 private key data extracted from PKCS#8 format This class provides access to the comp...
Definition: CryptoKeyDecoder.h:372
std::string getPrivateKey() const
Base class for private key data in PKCS#8 format This class serves as a base for different types of p...
Definition: CryptoKeyDecoder.h:316
virtual ~PrivateKeyData()=default
Virtual destructor.
PrivateKeyDataType * castAs()
Casts the private key data to a specific type.
Definition: CryptoKeyDecoder.h:325
Contains RSA private key data extracted from PKCS#8 format This class provides access to the componen...
Definition: CryptoKeyDecoder.h:352
Represents a private key in PKCS#8 format This class provides methods to decode and access the compon...
Definition: CryptoKeyDecoder.h:309
uint8_t getVersion() const
std::unique_ptr< PrivateKeyData > getPrivateKey() const
Gets the private key data.
std::unique_ptr< PrivateKeyDataType > getPrivateKeyAs() const
Gets the private key data cast to a requested type.
Definition: CryptoKeyDecoder.h:395
CryptographicKeyAlgorithm getPrivateKeyAlgorithm() const
static std::string encode(const std::vector< uint8_t > &data, const std::string &label)
Represents an RSA private key in PKCS#1 format This class provides methods to decode and access the c...
Definition: CryptoKeyDecoder.h:260
Represents an RSA public key in PKCS#1 format This class provides methods to decode and access the co...
Definition: CryptoKeyDecoder.h:429
std::string getModulus() const
uint64_t getPublicExponent() const
Represents a Subject Public Key Info (SPKI) structure This class provides methods to decode and acces...
Definition: CryptoKeyDecoder.h:454
CryptographicKeyAlgorithm getAlgorithm() const
std::string getSubjectPublicKey() const
A template helper class for reading and decoding cryptographic data in different formats (DER/PEM)
Definition: CryptoDataReader.h:23
Definition: CryptoKeyDecoder.h:130
std::string toPEM() const
Definition: CryptoKeyDecoder.h:141
std::vector< uint8_t > toDER() const
Definition: CryptoKeyDecoder.h:134
Definition: CryptoKeyDecoder.h:100
std::unique_ptr< Asn1ObjectIdentifier > getParameters() const
std::string getPublicKey() const
uint8_t getVersion() const
std::string getPrivateKey() const
Definition: CryptoKeyDecoder.h:20
Definition: CryptoKeyDecoder.h:49
std::string getCoefficient() const
std::string getModulus() const
std::string getPrime2() const
uint64_t getPublicExponent() const
uint8_t getVersion() const
std::string getPrime1() const
std::string getExponent1() const
std::string getExponent2() const
std::string getPrivateExponent() const
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19