Skip to content

Commit 1b51a2b

Browse files
committed
Implement SignaturePrivateKey::parse_der
1 parent 16ffaf7 commit 1b51a2b

File tree

7 files changed

+64
-1
lines changed

7 files changed

+64
-1
lines changed

include/mls/crypto.h

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ struct SignaturePrivateKey
223223
{
224224
static SignaturePrivateKey generate(CipherSuite suite);
225225
static SignaturePrivateKey parse(CipherSuite suite, const bytes& data);
226+
static SignaturePrivateKey parse_der(CipherSuite suite, const bytes& data);
226227
static SignaturePrivateKey derive(CipherSuite suite, const bytes& secret);
227228

228229
SignaturePrivateKey() = default;

lib/hpke/include/hpke/signature.h

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ struct Signature
5050
virtual std::unique_ptr<PrivateKey> deserialize_private(
5151
const bytes& skm) const;
5252

53+
virtual std::unique_ptr<PrivateKey> deserialize_private_der(
54+
const bytes& der) const;
55+
5356
virtual bytes sign(const bytes& data, const PrivateKey& sk) const = 0;
5457
virtual bool verify(const bytes& data,
5558
const bytes& sig,

lib/hpke/src/group.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "openssl/ec.h"
99
#include "openssl/evp.h"
1010
#include "openssl/obj_mac.h"
11+
#include "openssl/pem.h"
1112
#if defined(WITH_OPENSSL3)
1213
#include "openssl/core_names.h"
1314
#include "openssl/param_build.h"
@@ -526,6 +527,22 @@ struct ECKeyGroup : public EVPGroup
526527
#endif
527528
}
528529

530+
std::unique_ptr<Group::PrivateKey> deserialize_private_der(
531+
const bytes& der) const override
532+
{
533+
BIO* mem = BIO_new_mem_buf(der.data(), static_cast<int>(der.size()));
534+
if (!mem) {
535+
throw openssl_error();
536+
}
537+
EVP_PKEY* pkey = d2i_PrivateKey_bio(mem, NULL);
538+
BIO_free(mem);
539+
if (!pkey) {
540+
throw openssl_error();
541+
}
542+
543+
return std::make_unique<EVPGroup::PrivateKey>(pkey);
544+
}
545+
529546
private:
530547
int curve_nid;
531548

@@ -651,6 +668,22 @@ struct RawKeyGroup : public EVPGroup
651668
return std::make_unique<EVPGroup::PrivateKey>(pkey);
652669
}
653670

671+
std::unique_ptr<Group::PrivateKey> deserialize_private_der(
672+
const bytes& der) const override
673+
{
674+
BIO* mem = BIO_new_mem_buf(der.data(), static_cast<int>(der.size()));
675+
if (!mem) {
676+
throw openssl_error();
677+
}
678+
EVP_PKEY* pkey = d2i_PrivateKey_bio(mem, NULL);
679+
BIO_free(mem);
680+
if (!pkey) {
681+
throw openssl_error();
682+
}
683+
684+
return std::make_unique<RawKeyGroup::PrivateKey>(pkey);
685+
}
686+
654687
private:
655688
const int evp_type;
656689

lib/hpke/src/group.h

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ struct Group
5555
virtual bytes serialize_private(const PrivateKey& sk) const = 0;
5656
virtual std::unique_ptr<PrivateKey> deserialize_private(
5757
const bytes& skm) const = 0;
58+
virtual std::unique_ptr<PrivateKey> deserialize_private_der(
59+
const bytes& der) const = 0;
5860

5961
virtual bytes dh(const PrivateKey& sk, const PublicKey& pk) const = 0;
6062

lib/hpke/src/signature.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "group.h"
88
#include "rsa.h"
99
#include <openssl/evp.h>
10+
#include <openssl/pem.h>
1011
#include <openssl/rsa.h>
1112

1213
namespace hpke {
@@ -89,6 +90,13 @@ struct GroupSignature : public Signature
8990
group.deserialize_private(skm).release());
9091
}
9192

93+
std::unique_ptr<Signature::PrivateKey> deserialize_private_der(
94+
const bytes& der) const override
95+
{
96+
return std::make_unique<PrivateKey>(
97+
group.deserialize_private_der(der).release());
98+
}
99+
92100
bytes sign(const bytes& data, const Signature::PrivateKey& sk) const override
93101
{
94102
const auto& rsk = dynamic_cast<const PrivateKey&>(sk);
@@ -188,6 +196,12 @@ Signature::deserialize_private(const bytes& /* unused */) const
188196
throw std::runtime_error("Not implemented");
189197
}
190198

199+
std::unique_ptr<Signature::PrivateKey>
200+
Signature::deserialize_private_der(const bytes&) const
201+
{
202+
throw std::runtime_error("Not implemented");
203+
}
204+
191205
std::unique_ptr<Signature::PrivateKey>
192206
Signature::generate_rsa(size_t bits)
193207
{

src/crypto.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,16 @@ SignaturePrivateKey::parse(CipherSuite suite, const bytes& data)
402402
return { data, pub_data };
403403
}
404404

405+
SignaturePrivateKey
406+
SignaturePrivateKey::parse_der(CipherSuite suite, const bytes& data)
407+
{
408+
auto priv = suite.sig().deserialize_private_der(data);
409+
auto pub = priv->public_key();
410+
auto pub_data = suite.sig().serialize(*pub);
411+
auto priv_data = suite.sig().serialize_private(*priv);
412+
return { priv_data, pub_data };
413+
}
414+
405415
SignaturePrivateKey
406416
SignaturePrivateKey::derive(CipherSuite suite, const bytes& secret)
407417
{

test/credential.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ TEST_CASE("X509 Credential EC certificates")
7474

7575
const std::vector<bytes> der_in{ cert };
7676

77-
auto key = SignaturePrivateKey::parse(
77+
auto key = SignaturePrivateKey::parse_der(
7878
mls::CipherSuite::ID::P256_AES128GCM_SHA256_P256, keydata);
7979

8080
auto cred = Credential::x509(der_in);

0 commit comments

Comments
 (0)