2020-07-10 01:09:40 +08:00
|
|
|
/*
|
2022-05-03 18:52:38 +08:00
|
|
|
* Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
|
2020-07-10 01:09:40 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RSA low level APIs are deprecated for public use, but still ok for
|
|
|
|
* internal use.
|
|
|
|
*/
|
|
|
|
#include "internal/deprecated.h"
|
|
|
|
|
2020-07-20 16:46:49 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2020-07-10 01:09:40 +08:00
|
|
|
#include <openssl/core_dispatch.h>
|
|
|
|
#include <openssl/core_names.h>
|
2021-02-13 22:24:15 +08:00
|
|
|
#include <openssl/core_object.h>
|
2020-07-10 01:09:40 +08:00
|
|
|
#include <openssl/crypto.h>
|
2020-07-20 16:46:49 +08:00
|
|
|
#include <openssl/err.h>
|
2020-07-10 01:09:40 +08:00
|
|
|
#include <openssl/params.h>
|
|
|
|
#include <openssl/pem.h>
|
2021-02-06 00:40:42 +08:00
|
|
|
#include <openssl/proverr.h>
|
2020-10-02 19:56:54 +08:00
|
|
|
#include "internal/nelem.h"
|
2020-07-10 01:09:40 +08:00
|
|
|
#include "prov/bio.h"
|
2020-07-26 16:39:00 +08:00
|
|
|
#include "prov/implementations.h"
|
2020-09-07 18:25:17 +08:00
|
|
|
#include "endecoder_local.h"
|
|
|
|
|
|
|
|
static int read_pem(PROV_CTX *provctx, OSSL_CORE_BIO *cin,
|
|
|
|
char **pem_name, char **pem_header,
|
|
|
|
unsigned char **data, long *len)
|
|
|
|
{
|
2021-02-25 07:08:54 +08:00
|
|
|
BIO *in = ossl_bio_new_from_core_bio(provctx, cin);
|
2021-11-29 17:09:36 +08:00
|
|
|
int ok;
|
|
|
|
|
|
|
|
if (in == NULL)
|
|
|
|
return 0;
|
|
|
|
ok = (PEM_read_bio(in, pem_name, pem_header, data, len) > 0);
|
2020-09-07 18:25:17 +08:00
|
|
|
|
|
|
|
BIO_free(in);
|
|
|
|
return ok;
|
|
|
|
}
|
2020-07-10 01:09:40 +08:00
|
|
|
|
2020-08-17 03:25:08 +08:00
|
|
|
static OSSL_FUNC_decoder_newctx_fn pem2der_newctx;
|
|
|
|
static OSSL_FUNC_decoder_freectx_fn pem2der_freectx;
|
|
|
|
static OSSL_FUNC_decoder_decode_fn pem2der_decode;
|
2020-07-10 01:09:40 +08:00
|
|
|
|
2020-07-20 16:46:49 +08:00
|
|
|
/*
|
2020-08-17 03:25:08 +08:00
|
|
|
* Context used for PEM to DER decoding.
|
2020-07-20 16:46:49 +08:00
|
|
|
*/
|
|
|
|
struct pem2der_ctx_st {
|
|
|
|
PROV_CTX *provctx;
|
|
|
|
};
|
|
|
|
|
2020-07-10 01:09:40 +08:00
|
|
|
static void *pem2der_newctx(void *provctx)
|
|
|
|
{
|
2020-07-20 16:46:49 +08:00
|
|
|
struct pem2der_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
|
|
|
|
|
|
|
|
if (ctx != NULL)
|
|
|
|
ctx->provctx = provctx;
|
|
|
|
return ctx;
|
2020-07-10 01:09:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pem2der_freectx(void *vctx)
|
|
|
|
{
|
2020-07-20 16:46:49 +08:00
|
|
|
struct pem2der_ctx_st *ctx = vctx;
|
|
|
|
|
|
|
|
OPENSSL_free(ctx);
|
2020-07-10 01:09:40 +08:00
|
|
|
}
|
|
|
|
|
2020-07-20 16:46:49 +08:00
|
|
|
/* pem_password_cb compatible function */
|
2020-07-28 00:39:58 +08:00
|
|
|
struct pem2der_pass_data_st {
|
|
|
|
OSSL_PASSPHRASE_CALLBACK *cb;
|
|
|
|
void *cbarg;
|
|
|
|
};
|
|
|
|
|
2020-07-20 16:46:49 +08:00
|
|
|
static int pem2der_pass_helper(char *buf, int num, int w, void *data)
|
|
|
|
{
|
2020-07-28 00:39:58 +08:00
|
|
|
struct pem2der_pass_data_st *pass_data = data;
|
2020-07-20 16:46:49 +08:00
|
|
|
size_t plen;
|
|
|
|
|
2020-07-28 00:39:58 +08:00
|
|
|
if (pass_data == NULL
|
|
|
|
|| pass_data->cb == NULL
|
|
|
|
|| !pass_data->cb(buf, num, &plen, NULL, pass_data->cbarg))
|
2020-07-20 16:46:49 +08:00
|
|
|
return -1;
|
2020-07-28 00:39:58 +08:00
|
|
|
return (int)plen;
|
2020-07-20 16:46:49 +08:00
|
|
|
}
|
|
|
|
|
PROV: Re-implement all the keypair decoders
The base functionality to implement the keypair decoders doesn't
change much, but this results in a more massive amount of
OSSL_DISPATCH and OSSL_ALGORITHM arrays, to support a fine grained
selection of implementation based on what parts of the keypair
structure (combinations of key parameters, public key and private key)
should be expected as input, the input type ("DER", "PEM", ...) and the
outermost input structure ("pkcs8", "SubjectPublicKeyInfo", key
type specific structures, ...).
We add support for the generic structure name "type-specific", to
allow selecting that without knowing the exact name of that structure.
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/13248)
2020-10-26 20:22:54 +08:00
|
|
|
/*
|
|
|
|
* The selection parameter in pem2der_decode() is not used by this function
|
|
|
|
* because it's not relevant just to decode PEM to DER.
|
|
|
|
*/
|
|
|
|
static int pem2der_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
|
2020-08-17 03:25:08 +08:00
|
|
|
OSSL_CALLBACK *data_cb, void *data_cbarg,
|
|
|
|
OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
|
2020-07-10 01:09:40 +08:00
|
|
|
{
|
2021-06-28 10:29:17 +08:00
|
|
|
/*
|
|
|
|
* PEM names we recognise. Other PEM names should be recognised by
|
|
|
|
* other decoder implementations.
|
|
|
|
*/
|
|
|
|
static struct pem_name_map_st {
|
|
|
|
const char *pem_name;
|
|
|
|
int object_type;
|
|
|
|
const char *data_type;
|
2021-02-13 22:24:15 +08:00
|
|
|
const char *data_structure;
|
2021-06-28 10:29:17 +08:00
|
|
|
} pem_name_map[] = {
|
|
|
|
/* PKCS#8 and SubjectPublicKeyInfo */
|
|
|
|
{ PEM_STRING_PKCS8, OSSL_OBJECT_PKEY, NULL, "EncryptedPrivateKeyInfo" },
|
|
|
|
{ PEM_STRING_PKCS8INF, OSSL_OBJECT_PKEY, NULL, "PrivateKeyInfo" },
|
|
|
|
{ PEM_STRING_PUBLIC, OSSL_OBJECT_PKEY, NULL, "SubjectPublicKeyInfo" },
|
|
|
|
|
|
|
|
/* Our set of type specific PEM types */
|
|
|
|
{ PEM_STRING_DHPARAMS, OSSL_OBJECT_PKEY, "DH", "type-specific" },
|
|
|
|
{ PEM_STRING_DHXPARAMS, OSSL_OBJECT_PKEY, "X9.42 DH", "type-specific" },
|
|
|
|
{ PEM_STRING_DSA, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
|
|
|
|
{ PEM_STRING_DSA_PUBLIC, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
|
|
|
|
{ PEM_STRING_DSAPARAMS, OSSL_OBJECT_PKEY, "DSA", "type-specific" },
|
|
|
|
{ PEM_STRING_ECPRIVATEKEY, OSSL_OBJECT_PKEY, "EC", "type-specific" },
|
|
|
|
{ PEM_STRING_ECPARAMETERS, OSSL_OBJECT_PKEY, "EC", "type-specific" },
|
2022-07-19 01:18:12 +08:00
|
|
|
{ PEM_STRING_SM2PARAMETERS, OSSL_OBJECT_PKEY, "SM2", "type-specific" },
|
2021-06-28 10:29:17 +08:00
|
|
|
{ PEM_STRING_RSA, OSSL_OBJECT_PKEY, "RSA", "type-specific" },
|
|
|
|
{ PEM_STRING_RSA_PUBLIC, OSSL_OBJECT_PKEY, "RSA", "type-specific" },
|
2020-10-02 19:56:54 +08:00
|
|
|
|
|
|
|
/*
|
2021-06-28 10:29:17 +08:00
|
|
|
* A few others that there is at least have an object type for, even
|
|
|
|
* though there is no provider interface to handle such objects, yet.
|
|
|
|
* However, this is beneficial for the OSSL_STORE result handler.
|
2020-10-02 19:56:54 +08:00
|
|
|
*/
|
2021-08-30 19:19:30 +08:00
|
|
|
{ PEM_STRING_X509, OSSL_OBJECT_CERT, NULL, "Certificate" },
|
|
|
|
{ PEM_STRING_X509_TRUSTED, OSSL_OBJECT_CERT, NULL, "Certificate" },
|
|
|
|
{ PEM_STRING_X509_OLD, OSSL_OBJECT_CERT, NULL, "Certificate" },
|
|
|
|
{ PEM_STRING_X509_CRL, OSSL_OBJECT_CRL, NULL, "CertificateList" }
|
2020-10-02 19:56:54 +08:00
|
|
|
};
|
2020-07-20 16:46:49 +08:00
|
|
|
struct pem2der_ctx_st *ctx = vctx;
|
2020-07-10 01:09:40 +08:00
|
|
|
char *pem_name = NULL, *pem_header = NULL;
|
2021-06-28 10:29:17 +08:00
|
|
|
size_t i;
|
2020-07-10 01:09:40 +08:00
|
|
|
unsigned char *der = NULL;
|
|
|
|
long der_len = 0;
|
|
|
|
int ok = 0;
|
2021-02-13 22:24:15 +08:00
|
|
|
int objtype = OSSL_OBJECT_UNKNOWN;
|
2020-07-10 01:09:40 +08:00
|
|
|
|
2021-04-12 18:20:20 +08:00
|
|
|
ok = read_pem(ctx->provctx, cin, &pem_name, &pem_header,
|
|
|
|
&der, &der_len) > 0;
|
|
|
|
/* We return "empty handed". This is not an error. */
|
|
|
|
if (!ok)
|
|
|
|
return 1;
|
2020-07-10 01:09:40 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 10 is the number of characters in "Proc-Type:", which
|
|
|
|
* PEM_get_EVP_CIPHER_INFO() requires to be present.
|
|
|
|
* If the PEM header has less characters than that, it's
|
|
|
|
* not worth spending cycles on it.
|
|
|
|
*/
|
2020-07-20 16:46:49 +08:00
|
|
|
if (strlen(pem_header) > 10) {
|
2020-07-10 01:09:40 +08:00
|
|
|
EVP_CIPHER_INFO cipher;
|
2020-07-28 00:39:58 +08:00
|
|
|
struct pem2der_pass_data_st pass_data;
|
2020-07-10 01:09:40 +08:00
|
|
|
|
2021-04-12 18:20:20 +08:00
|
|
|
ok = 0; /* Assume that we fail */
|
2020-07-28 00:39:58 +08:00
|
|
|
pass_data.cb = pw_cb;
|
|
|
|
pass_data.cbarg = pw_cbarg;
|
2020-07-20 16:46:49 +08:00
|
|
|
if (!PEM_get_EVP_CIPHER_INFO(pem_header, &cipher)
|
2020-07-28 00:39:58 +08:00
|
|
|
|| !PEM_do_header(&cipher, der, &der_len,
|
|
|
|
pem2der_pass_helper, &pass_data))
|
2020-07-10 01:09:40 +08:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2021-04-12 18:20:20 +08:00
|
|
|
/*
|
|
|
|
* Indicated that we successfully decoded something, or not at all.
|
|
|
|
* Ending up "empty handed" is not an error.
|
|
|
|
*/
|
|
|
|
ok = 1;
|
|
|
|
|
2021-06-28 10:29:17 +08:00
|
|
|
/* Have a look to see if we recognise anything */
|
|
|
|
for (i = 0; i < OSSL_NELEM(pem_name_map); i++)
|
|
|
|
if (strcmp(pem_name, pem_name_map[i].pem_name) == 0)
|
|
|
|
break;
|
2021-02-13 22:24:15 +08:00
|
|
|
|
2021-06-28 10:29:17 +08:00
|
|
|
if (i < OSSL_NELEM(pem_name_map)) {
|
2021-02-13 22:24:15 +08:00
|
|
|
OSSL_PARAM params[5], *p = params;
|
2021-06-28 10:29:17 +08:00
|
|
|
/* We expect these to be read only so casting away the const is ok */
|
|
|
|
char *data_type = (char *)pem_name_map[i].data_type;
|
|
|
|
char *data_structure = (char *)pem_name_map[i].data_structure;
|
2020-07-10 01:09:40 +08:00
|
|
|
|
2021-08-30 19:19:30 +08:00
|
|
|
objtype = pem_name_map[i].object_type;
|
2021-06-28 10:29:17 +08:00
|
|
|
if (data_type != NULL)
|
2020-10-02 19:56:54 +08:00
|
|
|
*p++ =
|
|
|
|
OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
|
2021-06-28 10:29:17 +08:00
|
|
|
data_type, 0);
|
2021-02-13 22:24:15 +08:00
|
|
|
|
|
|
|
/* We expect this to be read only so casting away the const is ok */
|
|
|
|
if (data_structure != NULL)
|
|
|
|
*p++ =
|
|
|
|
OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_STRUCTURE,
|
2021-06-28 10:29:17 +08:00
|
|
|
data_structure, 0);
|
2020-10-02 19:56:54 +08:00
|
|
|
*p++ =
|
CORE: Define provider-native abstract objects
This is placed as CORE because the core of libcrypto is the authority
for what is possible to do and what's required to make these abstract
objects work.
In essence, an abstract object is an OSSL_PARAM array with well
defined parameter keys and values:
- an object type, which is a number indicating what kind of
libcrypto structure the object in question can be used with. The
currently possible numbers are defined in <openssl/core_object.h>.
- an object data type, which is a string that indicates more closely
what the contents of the object are.
- the object data, an octet string. The exact encoding used depends
on the context in which it's used. For example, the decoder
sub-system accepts any encoding, as long as there is a decoder
implementation that takes that as input. If central code is to
handle the data directly, DER encoding is assumed. (*)
- an object reference, also an octet string. This octet string is
not the object contents, just a mere reference to a provider-native
object. (**)
- an object description, which is a human readable text string that
can be displayed if some software desires to do so.
The intent is that certain provider-native operations (called X
here) are able to return any sort of object that belong with other
operations, or an object that has no provider support otherwise.
(*) A future extension might be to be able to specify encoding.
(**) The possible mechanisms for dealing with object references are:
- An object loading function in the target operation. The exact
target operation is determined by the object type (for example,
OSSL_OBJECT_PKEY implies that the target operation is a KEYMGMT)
and the implementation to be fetched by its object data type (for
an OSSL_OBJECT_PKEY, that's the KEYMGMT keytype to be fetched).
This loading function is only useful for this if the implementations
that are involved (X and KEYMGMT, for example) are from the same
provider.
- An object exporter function in the operation X implementation.
That exporter function can be used to export the object data in
OSSL_PARAM form that can be imported by a target operation's
import function. This can be used when it's not possible to fetch
the target operation implementation from the same provider.
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/12512)
2020-07-22 21:34:25 +08:00
|
|
|
OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
|
2020-07-10 01:09:40 +08:00
|
|
|
der, der_len);
|
2021-02-13 22:24:15 +08:00
|
|
|
*p++ =
|
|
|
|
OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &objtype);
|
|
|
|
|
2020-10-02 19:56:54 +08:00
|
|
|
*p = OSSL_PARAM_construct_end();
|
2020-07-10 01:09:40 +08:00
|
|
|
|
|
|
|
ok = data_cb(params, data_cbarg);
|
|
|
|
}
|
|
|
|
|
2020-07-20 16:46:49 +08:00
|
|
|
end:
|
2020-07-10 01:09:40 +08:00
|
|
|
OPENSSL_free(pem_name);
|
|
|
|
OPENSSL_free(pem_header);
|
|
|
|
OPENSSL_free(der);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2020-09-28 10:28:29 +08:00
|
|
|
const OSSL_DISPATCH ossl_pem_to_der_decoder_functions[] = {
|
2020-08-17 03:25:08 +08:00
|
|
|
{ OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))pem2der_newctx },
|
|
|
|
{ OSSL_FUNC_DECODER_FREECTX, (void (*)(void))pem2der_freectx },
|
|
|
|
{ OSSL_FUNC_DECODER_DECODE, (void (*)(void))pem2der_decode },
|
2023-04-19 22:08:22 +08:00
|
|
|
OSSL_DISPATCH_END
|
2020-07-10 01:09:40 +08:00
|
|
|
};
|