Skip to content

Commit

Permalink
Add EVP_PKEY_RSA_PSS ameth.
Browse files Browse the repository at this point in the history
  • Loading branch information
bryce-shang committed Jan 22, 2021
1 parent 650bdda commit a97e356
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 1 deletion.
4 changes: 3 additions & 1 deletion crypto/evp/evp.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ static const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_find(int nid) {
switch (nid) {
case EVP_PKEY_RSA:
return &rsa_asn1_meth;
case EVP_PKEY_RSA_PSS:
return &rsa_pss_asn1_meth;
case EVP_PKEY_EC:
return &ec_asn1_meth;
case EVP_PKEY_DSA:
Expand Down Expand Up @@ -238,7 +240,7 @@ int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
}

RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) {
if (pkey->type != EVP_PKEY_RSA) {
if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
return NULL;
}
Expand Down
1 change: 1 addition & 0 deletions crypto/evp/evp_asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

static const EVP_PKEY_ASN1_METHOD *const kASN1Methods[] = {
&rsa_asn1_meth,
&rsa_pss_asn1_meth,
&ec_asn1_meth,
&dsa_asn1_meth,
&ed25519_asn1_meth,
Expand Down
112 changes: 112 additions & 0 deletions crypto/evp/evp_extra_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,112 @@ static const uint8_t kExampleRSAKeyPKCS8[] = {
0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
};

// kExampleRSAPSSKeyPKCS8 is encoded in a PKCS #8 PrivateKeyInfo.
static const uint8_t kExampleRSAPSSKeyPKCS8[] = {
0x30, 0x82, 0x04, 0xbc, 0x02, 0x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e, 0x84, 0xde, 0x2c, 0xc9, 0xb5, 0xa9, 0xf4, 0xd5, 0x22,
0x3b, 0x8f, 0x85, 0xb2, 0xe7, 0x4a, 0x07, 0x02, 0x83, 0xd4, 0x52, 0x02,
0x79, 0x14, 0x5a, 0xbc, 0x9b, 0x90, 0x04, 0x07, 0x84, 0xf2, 0xb5, 0x2d,
0xee, 0x4e, 0x26, 0xcd, 0x6d, 0xa7, 0x61, 0x59, 0xfa, 0x62, 0xbd, 0x47,
0x1a, 0x17, 0x82, 0x98, 0x22, 0x85, 0x5f, 0x0b, 0x0b, 0x29, 0x05, 0xfd,
0x56, 0x8a, 0xb2, 0x6e, 0xb1, 0x90, 0x82, 0x03, 0x15, 0xf2, 0x83, 0x5d,
0xb1, 0xc1, 0xfb, 0xa3, 0x07, 0xea, 0xd7, 0x6a, 0xba, 0x12, 0xa4, 0xbf,
0x8c, 0xf2, 0xc0, 0x72, 0x37, 0x7d, 0x25, 0xfa, 0xf3, 0x91, 0x7f, 0x22,
0x7c, 0x50, 0x31, 0xec,
};

// kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
// structure.
static const uint8_t kExampleECKeyDER[] = {
Expand Down Expand Up @@ -463,6 +569,8 @@ TEST(EVPExtraTest, d2i_AutoPrivateKey) {
EVP_PKEY_RSA);
TestValidPrivateKey(kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8),
EVP_PKEY_RSA);
TestValidPrivateKey(kExampleRSAPSSKeyPKCS8, sizeof(kExampleRSAPSSKeyPKCS8),
EVP_PKEY_RSA_PSS);
TestValidPrivateKey(kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC);
TestValidPrivateKey(kExampleECKeyPKCS8, sizeof(kExampleECKeyPKCS8),
EVP_PKEY_EC);
Expand Down Expand Up @@ -521,6 +629,8 @@ TEST(EVPExtraTest, d2i_PrivateKey) {
sizeof(kExampleDSAKeyDER)));
EXPECT_TRUE(ParsePrivateKey(EVP_PKEY_RSA, kExampleRSAKeyPKCS8,
sizeof(kExampleRSAKeyPKCS8)));
EXPECT_TRUE(ParsePrivateKey(EVP_PKEY_RSA_PSS, kExampleRSAPSSKeyPKCS8,
sizeof(kExampleRSAPSSKeyPKCS8)));
EXPECT_TRUE(
ParsePrivateKey(EVP_PKEY_EC, kExampleECKeyDER, sizeof(kExampleECKeyDER)));

Expand All @@ -538,6 +648,8 @@ TEST(EVPExtraTest, d2i_PrivateKey) {
// Test that an RSA key may not be imported as an EC key.
EXPECT_FALSE(ParsePrivateKey(EVP_PKEY_EC, kExampleRSAKeyPKCS8,
sizeof(kExampleRSAKeyPKCS8)));
EXPECT_FALSE(ParsePrivateKey(EVP_PKEY_EC, kExampleRSAPSSKeyPKCS8,
sizeof(kExampleRSAPSSKeyPKCS8)));
ERR_clear_error();
}

Expand Down
1 change: 1 addition & 0 deletions crypto/evp/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ typedef struct {
extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD ec_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth;
extern const EVP_PKEY_ASN1_METHOD x25519_asn1_meth;

Expand Down
53 changes: 53 additions & 0 deletions crypto/evp/p_rsa_asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ static int rsa_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
return 1;
}

static int rsa_pss_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
// TODO(shang): add pss paramters decode.
RSA *rsa = RSA_parse_public_key(key);
if (rsa == NULL || CBS_len(key) != 0) {
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
RSA_free(rsa);
return 0;
}

EVP_PKEY_assign(out, EVP_PKEY_RSA_PSS, rsa);
return 1;
}

static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
return BN_cmp(b->pkey.rsa->n, a->pkey.rsa->n) == 0 &&
BN_cmp(b->pkey.rsa->e, a->pkey.rsa->e) == 0;
Expand Down Expand Up @@ -152,6 +165,19 @@ static int rsa_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
return 1;
}

static int rsa_pss_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
// TODO(shang): add pss paramters decode.
RSA *rsa = RSA_parse_private_key(key);
if (rsa == NULL || CBS_len(key) != 0) {
OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
RSA_free(rsa);
return 0;
}

EVP_PKEY_assign(out, EVP_PKEY_RSA_PSS, rsa);
return 1;
}

static int rsa_opaque(const EVP_PKEY *pkey) {
return RSA_is_opaque(pkey->pkey.rsa);
}
Expand Down Expand Up @@ -192,3 +218,30 @@ const EVP_PKEY_ASN1_METHOD rsa_asn1_meth = {

int_rsa_free,
};

const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = {
EVP_PKEY_RSA_PSS,
// 1.2.840.113549.1.1.10
{0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0a}, 9,

rsa_pss_pub_decode,
NULL, /* pub_encode */,
rsa_pub_cmp,

rsa_pss_priv_decode,
NULL, /* priv_encode */,

NULL /* set_priv_raw */,
NULL /* set_pub_raw */,
NULL /* get_priv_raw */,
NULL /* get_pub_raw */,

rsa_opaque,

int_rsa_size,
rsa_bits,

0,0,0,

int_rsa_free,
};

0 comments on commit a97e356

Please sign in to comment.