2020-07-09 19:10:39 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
2020-07-10 15:28:05 +02:00
|
|
|
#include <string.h>
|
2020-07-09 19:10:39 +02:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
#include <openssl/x509.h>
|
|
|
|
#include <openssl/serializer.h>
|
|
|
|
#include <openssl/deserializer.h>
|
|
|
|
|
2020-07-20 10:47:59 +02:00
|
|
|
#include "internal/cryptlib.h" /* ossl_assert */
|
|
|
|
|
2020-07-09 19:10:39 +02:00
|
|
|
#include "testutil.h"
|
|
|
|
|
2020-07-20 10:47:59 +02:00
|
|
|
/*
|
|
|
|
* TODO(3.0) Modify PEM_write_bio_PrivateKey_traditional() to handle
|
|
|
|
* provider side EVP_PKEYs (which don't necessarily have an ameth)
|
|
|
|
*
|
|
|
|
* In the mean time, we use separate "downgraded" EVP_PKEYs to test
|
|
|
|
* serializing/deserializing with "traditional" keys.
|
|
|
|
*/
|
|
|
|
|
2020-07-09 19:10:39 +02:00
|
|
|
static EVP_PKEY *key_RSA = NULL;
|
2020-07-20 10:47:59 +02:00
|
|
|
static EVP_PKEY *legacy_key_RSA = NULL;
|
2020-07-20 16:14:40 +02:00
|
|
|
static EVP_PKEY *key_RSA_PSS = NULL;
|
|
|
|
static EVP_PKEY *legacy_key_RSA_PSS = NULL;
|
2020-07-09 19:10:39 +02:00
|
|
|
|
2020-07-20 10:47:59 +02:00
|
|
|
static EVP_PKEY *make_RSA(const char *rsa_type, int make_legacy)
|
2020-07-09 19:10:39 +02:00
|
|
|
{
|
|
|
|
EVP_PKEY *pkey = NULL;
|
|
|
|
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, rsa_type, NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No real need to check the errors other than for the cascade
|
|
|
|
* effect. |pkey| will imply remain NULL if something goes wrong.
|
|
|
|
*/
|
|
|
|
(void)(ctx != NULL
|
|
|
|
&& EVP_PKEY_keygen_init(ctx) > 0
|
|
|
|
&& EVP_PKEY_keygen(ctx, &pkey) > 0);
|
|
|
|
EVP_PKEY_CTX_free(ctx);
|
2020-07-20 10:47:59 +02:00
|
|
|
if (make_legacy && EVP_PKEY_get0(pkey) == NULL) {
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
pkey = NULL;
|
|
|
|
}
|
2020-07-09 19:10:39 +02:00
|
|
|
|
|
|
|
return pkey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Main test driver */
|
|
|
|
|
2020-07-27 18:39:58 +02:00
|
|
|
/*
|
|
|
|
* TODO(3.0) For better error output, changed the callbacks to take __FILE__
|
|
|
|
* and __LINE__ as first two arguments, and have them use the lower case
|
|
|
|
* functions, such as test_strn_eq(), rather than the uppercase macros
|
|
|
|
* (TEST_strn2_eq(), for example).
|
|
|
|
*/
|
|
|
|
|
2020-07-09 19:10:39 +02:00
|
|
|
typedef int (serializer)(void **serialized, long *serialized_len,
|
2020-07-27 18:39:58 +02:00
|
|
|
void *object, const char *pass, const char *pcipher,
|
2020-07-10 15:28:05 +02:00
|
|
|
const char *ser_propq);
|
2020-07-09 19:10:39 +02:00
|
|
|
typedef int (deserializer)(void **object,
|
2020-07-10 15:28:05 +02:00
|
|
|
void *serialized, long serialized_len,
|
2020-07-27 18:39:58 +02:00
|
|
|
const char *pass);
|
2020-07-20 16:14:40 +02:00
|
|
|
typedef int (checker)(const char *type, const void *data, size_t data_len);
|
2020-07-09 19:10:39 +02:00
|
|
|
typedef void (dumper)(const char *label, const void *data, size_t data_len);
|
|
|
|
|
2020-07-20 16:14:40 +02:00
|
|
|
static int test_serialize_deserialize(const char *type, EVP_PKEY *pkey,
|
2020-07-10 15:28:05 +02:00
|
|
|
const char *pass, const char *pcipher,
|
2020-07-09 19:10:39 +02:00
|
|
|
serializer *serialize_cb,
|
|
|
|
deserializer *deserialize_cb,
|
|
|
|
checker *check_cb, dumper *dump_cb,
|
2020-07-20 10:47:59 +02:00
|
|
|
const char *ser_propq, int make_legacy)
|
2020-07-09 19:10:39 +02:00
|
|
|
{
|
|
|
|
void *serialized = NULL;
|
|
|
|
long serialized_len = 0;
|
|
|
|
EVP_PKEY *pkey2 = NULL;
|
|
|
|
void *serialized2 = NULL;
|
|
|
|
long serialized2_len = 0;
|
|
|
|
int ok = 0;
|
|
|
|
|
2020-07-10 15:28:05 +02:00
|
|
|
if (!serialize_cb(&serialized, &serialized_len, pkey,
|
|
|
|
pass, pcipher, ser_propq)
|
2020-07-20 16:14:40 +02:00
|
|
|
|| !check_cb(type, serialized, serialized_len)
|
2020-07-10 15:28:05 +02:00
|
|
|
|| !deserialize_cb((void **)&pkey2, serialized, serialized_len,
|
2020-07-27 18:39:58 +02:00
|
|
|
pass)
|
2020-07-09 19:10:39 +02:00
|
|
|
|| !TEST_int_eq(EVP_PKEY_eq(pkey, pkey2), 1))
|
|
|
|
goto end;
|
|
|
|
|
2020-07-20 10:47:59 +02:00
|
|
|
/*
|
|
|
|
* TODO(3.0) Remove this when PEM_write_bio_PrivateKey_traditional()
|
|
|
|
* handles provider side keys.
|
|
|
|
*/
|
|
|
|
if (make_legacy
|
|
|
|
&& !TEST_ptr(EVP_PKEY_get0(pkey2)))
|
|
|
|
goto end;
|
|
|
|
|
2020-07-09 19:10:39 +02:00
|
|
|
/*
|
2020-07-10 15:28:05 +02:00
|
|
|
* Double check the serialization, but only for unprotected keys,
|
|
|
|
* as protected keys have a random component, which makes the output
|
|
|
|
* differ.
|
2020-07-09 19:10:39 +02:00
|
|
|
*/
|
2020-07-10 15:28:05 +02:00
|
|
|
if ((pass == NULL && pcipher == NULL)
|
|
|
|
&& (!serialize_cb(&serialized2, &serialized2_len, pkey2,
|
|
|
|
pass, pcipher, ser_propq)
|
|
|
|
|| !TEST_mem_eq(serialized, serialized_len,
|
|
|
|
serialized2, serialized2_len)))
|
2020-07-09 19:10:39 +02:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
ok = 1;
|
|
|
|
end:
|
|
|
|
if (!ok)
|
|
|
|
dump_cb("serialized result", serialized, serialized_len);
|
|
|
|
|
|
|
|
OPENSSL_free(serialized);
|
|
|
|
OPENSSL_free(serialized2);
|
|
|
|
EVP_PKEY_free(pkey2);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Serializing and desserializing methods */
|
|
|
|
|
2020-07-20 10:47:59 +02:00
|
|
|
static int serialize_EVP_PKEY_prov(void **serialized, long *serialized_len,
|
|
|
|
void *object,
|
|
|
|
const char *pass, const char *pcipher,
|
|
|
|
const char *ser_propq)
|
2020-07-09 19:10:39 +02:00
|
|
|
{
|
|
|
|
EVP_PKEY *pkey = object;
|
|
|
|
OSSL_SERIALIZER_CTX *sctx = NULL;
|
|
|
|
BIO *mem_ser = NULL;
|
|
|
|
BUF_MEM *mem_buf = NULL;
|
2020-07-10 15:28:05 +02:00
|
|
|
const unsigned char *upass = (const unsigned char *)pass;
|
2020-07-09 19:10:39 +02:00
|
|
|
int ok = 0;
|
|
|
|
|
|
|
|
if (!TEST_ptr(sctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, ser_propq))
|
2020-07-10 15:28:05 +02:00
|
|
|
|| (pass != NULL
|
2020-07-20 10:47:59 +02:00
|
|
|
&& !TEST_true(OSSL_SERIALIZER_CTX_set_passphrase(sctx, upass,
|
|
|
|
strlen(pass))))
|
2020-07-10 15:28:05 +02:00
|
|
|
|| (pcipher != NULL
|
2020-07-20 10:47:59 +02:00
|
|
|
&& !TEST_true(OSSL_SERIALIZER_CTX_set_cipher(sctx, pcipher, NULL)))
|
2020-07-09 19:10:39 +02:00
|
|
|
|| !TEST_ptr(mem_ser = BIO_new(BIO_s_mem()))
|
|
|
|
|| !TEST_true(OSSL_SERIALIZER_to_bio(sctx, mem_ser))
|
|
|
|
|| !TEST_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0)
|
|
|
|
|| !TEST_ptr(*serialized = mem_buf->data)
|
|
|
|
|| !TEST_long_gt(*serialized_len = mem_buf->length, 0))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
/* Detach the serialized output */
|
|
|
|
mem_buf->data = NULL;
|
|
|
|
mem_buf->length = 0;
|
|
|
|
ok = 1;
|
|
|
|
end:
|
|
|
|
BIO_free(mem_ser);
|
|
|
|
OSSL_SERIALIZER_CTX_free(sctx);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2020-07-20 10:47:59 +02:00
|
|
|
static int deserialize_EVP_PKEY_prov(void **object,
|
|
|
|
void *serialized, long serialized_len,
|
2020-07-27 18:39:58 +02:00
|
|
|
const char *pass)
|
2020-07-09 19:10:39 +02:00
|
|
|
{
|
|
|
|
EVP_PKEY *pkey = NULL;
|
|
|
|
OSSL_DESERIALIZER_CTX *dctx = NULL;
|
|
|
|
BIO *mem_deser = NULL;
|
2020-07-10 15:28:05 +02:00
|
|
|
const unsigned char *upass = (const unsigned char *)pass;
|
2020-07-09 19:10:39 +02:00
|
|
|
int ok = 0;
|
|
|
|
|
|
|
|
if (!TEST_ptr(dctx = OSSL_DESERIALIZER_CTX_new_by_EVP_PKEY(&pkey, NULL,
|
|
|
|
NULL, NULL))
|
2020-07-10 15:28:05 +02:00
|
|
|
|| (pass != NULL
|
|
|
|
&& !OSSL_DESERIALIZER_CTX_set_passphrase(dctx, upass,
|
|
|
|
strlen(pass)))
|
2020-07-09 19:10:39 +02:00
|
|
|
|| !TEST_ptr(mem_deser = BIO_new_mem_buf(serialized, serialized_len))
|
|
|
|
|| !TEST_true(OSSL_DESERIALIZER_from_bio(dctx, mem_deser)))
|
|
|
|
goto end;
|
|
|
|
ok = 1;
|
|
|
|
*object = pkey;
|
|
|
|
end:
|
|
|
|
BIO_free(mem_deser);
|
|
|
|
OSSL_DESERIALIZER_CTX_free(dctx);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2020-07-20 10:47:59 +02:00
|
|
|
static int serialize_EVP_PKEY_legacy_PEM(void **serialized,
|
|
|
|
long *serialized_len,
|
|
|
|
void *object,
|
|
|
|
const char *pass, const char *pcipher,
|
|
|
|
ossl_unused const char *ser_propq)
|
|
|
|
{
|
|
|
|
EVP_PKEY *pkey = object;
|
|
|
|
EVP_CIPHER *cipher = NULL;
|
|
|
|
BIO *mem_ser = NULL;
|
|
|
|
BUF_MEM *mem_buf = NULL;
|
|
|
|
const unsigned char *upass = (const unsigned char *)pass;
|
|
|
|
size_t passlen = 0;
|
|
|
|
int ok = 0;
|
|
|
|
|
|
|
|
if (pcipher != NULL && pass != NULL) {
|
|
|
|
passlen = strlen(pass);
|
|
|
|
if (!TEST_ptr(cipher = EVP_CIPHER_fetch(NULL, pcipher, NULL)))
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (!TEST_ptr(mem_ser = BIO_new(BIO_s_mem()))
|
|
|
|
|| !TEST_true(PEM_write_bio_PrivateKey_traditional(mem_ser, pkey,
|
|
|
|
cipher,
|
|
|
|
upass, passlen,
|
|
|
|
NULL, NULL))
|
|
|
|
|| !TEST_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0)
|
|
|
|
|| !TEST_ptr(*serialized = mem_buf->data)
|
|
|
|
|| !TEST_long_gt(*serialized_len = mem_buf->length, 0))
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
/* Detach the serialized output */
|
|
|
|
mem_buf->data = NULL;
|
|
|
|
mem_buf->length = 0;
|
|
|
|
ok = 1;
|
|
|
|
end:
|
|
|
|
BIO_free(mem_ser);
|
|
|
|
EVP_CIPHER_free(cipher);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2020-07-09 19:10:39 +02:00
|
|
|
/* Test cases and their dumpers / checkers */
|
|
|
|
|
|
|
|
static void dump_der(const char *label, const void *data, size_t data_len)
|
|
|
|
{
|
|
|
|
test_output_memory(label, data, data_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_pem(const char *label, const void *data, size_t data_len)
|
|
|
|
{
|
|
|
|
test_output_string(label, data, data_len - 1);
|
|
|
|
}
|
|
|
|
|
2020-07-20 16:14:40 +02:00
|
|
|
static int check_unprotected_PKCS8_DER(const char *type,
|
2020-07-10 15:28:05 +02:00
|
|
|
const void *data, size_t data_len)
|
2020-07-09 19:10:39 +02:00
|
|
|
{
|
|
|
|
const unsigned char *datap = data;
|
|
|
|
PKCS8_PRIV_KEY_INFO *p8inf =
|
|
|
|
d2i_PKCS8_PRIV_KEY_INFO(NULL, &datap, data_len);
|
|
|
|
int ok = 0;
|
|
|
|
|
|
|
|
if (TEST_ptr(p8inf)) {
|
|
|
|
EVP_PKEY *pkey = EVP_PKCS82PKEY(p8inf);
|
|
|
|
|
2020-07-20 16:14:40 +02:00
|
|
|
ok = (TEST_ptr(pkey) && TEST_true(EVP_PKEY_is_a(pkey, type)));
|
2020-07-09 19:10:39 +02:00
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
}
|
|
|
|
PKCS8_PRIV_KEY_INFO_free(p8inf);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2020-07-10 15:28:05 +02:00
|
|
|
static int test_unprotected_RSA_via_DER(void)
|
2020-07-09 19:10:39 +02:00
|
|
|
{
|
2020-07-20 16:14:40 +02:00
|
|
|
return test_serialize_deserialize("RSA", key_RSA, NULL, NULL,
|
2020-07-20 10:47:59 +02:00
|
|
|
serialize_EVP_PKEY_prov,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
2020-07-10 15:28:05 +02:00
|
|
|
check_unprotected_PKCS8_DER, dump_der,
|
2020-07-20 10:47:59 +02:00
|
|
|
OSSL_SERIALIZER_PrivateKey_TO_DER_PQ,
|
|
|
|
0);
|
2020-07-09 19:10:39 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 16:14:40 +02:00
|
|
|
static int test_unprotected_RSA_PSS_via_DER(void)
|
|
|
|
{
|
|
|
|
return test_serialize_deserialize("RSA-PSS", key_RSA_PSS, NULL, NULL,
|
|
|
|
serialize_EVP_PKEY_prov,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
|
|
|
check_unprotected_PKCS8_DER, dump_der,
|
|
|
|
OSSL_SERIALIZER_PrivateKey_TO_DER_PQ,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_unprotected_PKCS8_PEM(const char *type,
|
2020-07-10 15:28:05 +02:00
|
|
|
const void *data, size_t data_len)
|
2020-07-09 19:10:39 +02:00
|
|
|
{
|
|
|
|
static const char pem_header[] = "-----BEGIN " PEM_STRING_PKCS8INF "-----";
|
|
|
|
|
|
|
|
return TEST_strn_eq(data, pem_header, sizeof(pem_header) - 1);
|
|
|
|
}
|
|
|
|
|
2020-07-10 15:28:05 +02:00
|
|
|
static int test_unprotected_RSA_via_PEM(void)
|
2020-07-09 19:10:39 +02:00
|
|
|
{
|
2020-07-20 16:14:40 +02:00
|
|
|
return test_serialize_deserialize("RSA", key_RSA, NULL, NULL,
|
|
|
|
serialize_EVP_PKEY_prov,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
|
|
|
check_unprotected_PKCS8_PEM, dump_pem,
|
|
|
|
OSSL_SERIALIZER_PrivateKey_TO_PEM_PQ,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_unprotected_RSA_PSS_via_PEM(void)
|
|
|
|
{
|
|
|
|
return test_serialize_deserialize("RSA-PSS", key_RSA_PSS, NULL, NULL,
|
2020-07-20 10:47:59 +02:00
|
|
|
serialize_EVP_PKEY_prov,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
2020-07-10 15:28:05 +02:00
|
|
|
check_unprotected_PKCS8_PEM, dump_pem,
|
2020-07-20 10:47:59 +02:00
|
|
|
OSSL_SERIALIZER_PrivateKey_TO_PEM_PQ,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
2020-07-20 16:14:40 +02:00
|
|
|
static int check_unprotected_legacy_PEM(const char *type,
|
2020-07-20 10:47:59 +02:00
|
|
|
const void *data, size_t data_len)
|
|
|
|
{
|
2020-07-20 16:14:40 +02:00
|
|
|
static char pem_header[80];
|
2020-07-20 10:47:59 +02:00
|
|
|
|
2020-07-20 16:14:40 +02:00
|
|
|
return
|
|
|
|
TEST_int_gt(BIO_snprintf(pem_header, sizeof(pem_header),
|
|
|
|
"-----BEGIN %s PRIVATE KEY-----", type), 0)
|
|
|
|
&& TEST_strn_eq(data, pem_header, strlen(pem_header));
|
2020-07-20 10:47:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int test_unprotected_RSA_via_legacy_PEM(void)
|
|
|
|
{
|
2020-07-20 16:14:40 +02:00
|
|
|
return test_serialize_deserialize("RSA", legacy_key_RSA, NULL, NULL,
|
|
|
|
serialize_EVP_PKEY_legacy_PEM,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
|
|
|
check_unprotected_legacy_PEM, dump_pem,
|
|
|
|
NULL, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_unprotected_RSA_PSS_via_legacy_PEM(void)
|
|
|
|
{
|
|
|
|
return test_serialize_deserialize("RSA-PSS", legacy_key_RSA_PSS, NULL, NULL,
|
2020-07-20 10:47:59 +02:00
|
|
|
serialize_EVP_PKEY_legacy_PEM,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
|
|
|
check_unprotected_legacy_PEM, dump_pem,
|
|
|
|
NULL, 1);
|
2020-07-10 15:28:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *pass_cipher = "AES-256-CBC";
|
|
|
|
static const char *pass = "the holy handgrenade of antioch";
|
|
|
|
|
2020-07-20 16:14:40 +02:00
|
|
|
static int check_protected_PKCS8_DER(const char *type,
|
2020-07-10 15:28:05 +02:00
|
|
|
const void *data, size_t data_len)
|
|
|
|
{
|
|
|
|
const unsigned char *datap = data;
|
|
|
|
X509_SIG *p8 = d2i_X509_SIG(NULL, &datap, data_len);
|
|
|
|
int ok = TEST_ptr(p8);
|
|
|
|
|
|
|
|
X509_SIG_free(p8);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_protected_RSA_via_DER(void)
|
|
|
|
{
|
2020-07-20 16:14:40 +02:00
|
|
|
return test_serialize_deserialize("RSA", key_RSA, pass, pass_cipher,
|
|
|
|
serialize_EVP_PKEY_prov,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
|
|
|
check_protected_PKCS8_DER, dump_der,
|
|
|
|
OSSL_SERIALIZER_PrivateKey_TO_DER_PQ,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_protected_RSA_PSS_via_DER(void)
|
|
|
|
{
|
|
|
|
return test_serialize_deserialize("RSA", key_RSA, pass, pass_cipher,
|
2020-07-20 10:47:59 +02:00
|
|
|
serialize_EVP_PKEY_prov,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
2020-07-10 15:28:05 +02:00
|
|
|
check_protected_PKCS8_DER, dump_der,
|
2020-07-20 10:47:59 +02:00
|
|
|
OSSL_SERIALIZER_PrivateKey_TO_DER_PQ,
|
|
|
|
0);
|
2020-07-10 15:28:05 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 16:14:40 +02:00
|
|
|
static int check_protected_PKCS8_PEM(const char *type,
|
2020-07-10 15:28:05 +02:00
|
|
|
const void *data, size_t data_len)
|
|
|
|
{
|
|
|
|
static const char pem_header[] = "-----BEGIN " PEM_STRING_PKCS8 "-----";
|
|
|
|
|
|
|
|
return TEST_strn_eq(data, pem_header, sizeof(pem_header) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_protected_RSA_via_PEM(void)
|
|
|
|
{
|
2020-07-20 16:14:40 +02:00
|
|
|
return test_serialize_deserialize("RSA", key_RSA, pass, pass_cipher,
|
|
|
|
serialize_EVP_PKEY_prov,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
|
|
|
check_protected_PKCS8_PEM, dump_pem,
|
|
|
|
OSSL_SERIALIZER_PrivateKey_TO_PEM_PQ,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_protected_RSA_PSS_via_PEM(void)
|
|
|
|
{
|
|
|
|
return test_serialize_deserialize("RSA-PSS", key_RSA_PSS, pass, pass_cipher,
|
2020-07-20 10:47:59 +02:00
|
|
|
serialize_EVP_PKEY_prov,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
2020-07-10 15:28:05 +02:00
|
|
|
check_protected_PKCS8_PEM, dump_pem,
|
2020-07-20 10:47:59 +02:00
|
|
|
OSSL_SERIALIZER_PrivateKey_TO_PEM_PQ,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
2020-07-20 16:14:40 +02:00
|
|
|
static int check_protected_legacy_PEM(const char *type,
|
2020-07-20 10:47:59 +02:00
|
|
|
const void *data, size_t data_len)
|
|
|
|
{
|
2020-07-20 16:14:40 +02:00
|
|
|
static char pem_header[80];
|
2020-07-20 10:47:59 +02:00
|
|
|
|
|
|
|
return
|
2020-07-20 16:14:40 +02:00
|
|
|
TEST_int_gt(BIO_snprintf(pem_header, sizeof(pem_header),
|
|
|
|
"-----BEGIN %s PRIVATE KEY-----", type), 0)
|
|
|
|
&& TEST_strn_eq(data, pem_header, strlen(pem_header))
|
2020-07-20 10:47:59 +02:00
|
|
|
&& TEST_ptr(strstr(data, "\nDEK-Info: "));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_protected_RSA_via_legacy_PEM(void)
|
|
|
|
{
|
2020-07-20 16:14:40 +02:00
|
|
|
return test_serialize_deserialize("RSA", legacy_key_RSA, pass, pass_cipher,
|
|
|
|
serialize_EVP_PKEY_legacy_PEM,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
|
|
|
check_protected_legacy_PEM, dump_pem,
|
|
|
|
NULL, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_protected_RSA_PSS_via_legacy_PEM(void)
|
|
|
|
{
|
|
|
|
return test_serialize_deserialize("RSA-PSS", legacy_key_RSA_PSS,
|
|
|
|
pass, pass_cipher,
|
2020-07-20 10:47:59 +02:00
|
|
|
serialize_EVP_PKEY_legacy_PEM,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
|
|
|
check_protected_legacy_PEM, dump_pem,
|
|
|
|
NULL, 1);
|
2020-07-09 19:10:39 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 18:40:02 +02:00
|
|
|
static int check_public_DER(int type, const void *data, size_t data_len)
|
|
|
|
{
|
|
|
|
const unsigned char *datap = data;
|
|
|
|
EVP_PKEY *pkey = d2i_PUBKEY(NULL, &datap, data_len);
|
|
|
|
int ok = (TEST_ptr(pkey) && TEST_true(EVP_PKEY_is_a(pkey, "RSA")));
|
|
|
|
|
|
|
|
EVP_PKEY_free(pkey);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_public_RSA_via_DER(void)
|
|
|
|
{
|
|
|
|
return test_serialize_deserialize("RSA", NULL, NULL,
|
|
|
|
serialize_EVP_PKEY_prov,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
|
|
|
check_public_DER, dump_der,
|
|
|
|
OSSL_SERIALIZER_PUBKEY_TO_DER_PQ,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_public_RSA_PSS_via_DER(void)
|
|
|
|
{
|
|
|
|
return test_serialize_deserialize("RSA-PSS", NULL, NULL,
|
|
|
|
serialize_EVP_PKEY_prov,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
|
|
|
check_public_DER, dump_der,
|
|
|
|
OSSL_SERIALIZER_PUBKEY_TO_DER_PQ,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_public_PEM(int type, const void *data, size_t data_len)
|
|
|
|
{
|
|
|
|
static const char pem_header[] = "-----BEGIN " PEM_STRING_PUBLIC "-----";
|
|
|
|
|
|
|
|
return
|
|
|
|
TEST_strn_eq(data, pem_header, sizeof(pem_header) - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_public_RSA_via_PEM(void)
|
|
|
|
{
|
|
|
|
return test_serialize_deserialize("RSA", NULL, NULL,
|
|
|
|
serialize_EVP_PKEY_prov,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
|
|
|
check_public_PEM, dump_pem,
|
|
|
|
OSSL_SERIALIZER_PUBKEY_TO_PEM_PQ,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_public_RSA_PSS_via_PEM(void)
|
|
|
|
{
|
|
|
|
return test_serialize_deserialize("RSA-PSS", NULL, NULL,
|
|
|
|
serialize_EVP_PKEY_prov,
|
|
|
|
deserialize_EVP_PKEY_prov,
|
|
|
|
check_public_PEM, dump_pem,
|
|
|
|
OSSL_SERIALIZER_PUBKEY_TO_PEM_PQ,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
2020-07-09 19:10:39 +02:00
|
|
|
int setup_tests(void)
|
|
|
|
{
|
2020-07-20 10:47:59 +02:00
|
|
|
TEST_info("Generating keys...");
|
|
|
|
if (!TEST_ptr(key_RSA = make_RSA("RSA", 0))
|
2020-07-20 16:14:40 +02:00
|
|
|
|| !TEST_ptr(legacy_key_RSA = make_RSA("RSA", 1))
|
|
|
|
|| !TEST_ptr(key_RSA_PSS = make_RSA("RSA-PSS", 0))
|
|
|
|
|| !TEST_ptr(legacy_key_RSA_PSS = make_RSA("RSA-PSS", 1))) {
|
2020-07-20 10:47:59 +02:00
|
|
|
EVP_PKEY_free(key_RSA);
|
|
|
|
EVP_PKEY_free(legacy_key_RSA);
|
2020-07-20 16:14:40 +02:00
|
|
|
EVP_PKEY_free(key_RSA_PSS);
|
|
|
|
EVP_PKEY_free(legacy_key_RSA_PSS);
|
2020-07-09 19:10:39 +02:00
|
|
|
return 0;
|
2020-07-20 10:47:59 +02:00
|
|
|
}
|
2020-07-09 19:10:39 +02:00
|
|
|
TEST_info("Generating key... done");
|
|
|
|
|
2020-07-10 15:28:05 +02:00
|
|
|
ADD_TEST(test_unprotected_RSA_via_DER);
|
|
|
|
ADD_TEST(test_unprotected_RSA_via_PEM);
|
2020-07-20 10:47:59 +02:00
|
|
|
ADD_TEST(test_unprotected_RSA_via_legacy_PEM);
|
2020-07-10 15:28:05 +02:00
|
|
|
ADD_TEST(test_protected_RSA_via_DER);
|
|
|
|
ADD_TEST(test_protected_RSA_via_PEM);
|
2020-07-20 10:47:59 +02:00
|
|
|
ADD_TEST(test_protected_RSA_via_legacy_PEM);
|
2020-07-27 18:40:02 +02:00
|
|
|
ADD_TEST(test_public_RSA_via_DER);
|
|
|
|
ADD_TEST(test_public_RSA_via_PEM);
|
2020-07-20 16:14:40 +02:00
|
|
|
ADD_TEST(test_unprotected_RSA_PSS_via_DER);
|
|
|
|
ADD_TEST(test_unprotected_RSA_PSS_via_PEM);
|
|
|
|
ADD_TEST(test_unprotected_RSA_PSS_via_legacy_PEM);
|
|
|
|
ADD_TEST(test_protected_RSA_PSS_via_DER);
|
|
|
|
ADD_TEST(test_protected_RSA_PSS_via_PEM);
|
|
|
|
ADD_TEST(test_protected_RSA_PSS_via_legacy_PEM);
|
2020-07-27 18:40:02 +02:00
|
|
|
ADD_TEST(test_public_RSA_PSS_via_DER);
|
|
|
|
ADD_TEST(test_public_RSA_PSS_via_PEM);
|
2020-07-09 19:10:39 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|