diff options
author | Richard Levitte <levitte@openssl.org> | 2019-11-18 01:29:06 +0100 |
---|---|---|
committer | Richard Levitte <levitte@openssl.org> | 2019-11-29 20:54:48 +0100 |
commit | 0d003c52d3dcf4b076bb01a6767cdd5ace2d79f6 (patch) | |
tree | c04a81334735f506d3c94a3591e224683feb78ad /include | |
parent | 36fa4d8a0df9dc168047fadd0365966c7116b31d (diff) |
SERIALIZER: New API for serialization of objects through providers
Serialization is needed to be able to take a provider object (such as
the provider side key data) and output it in PEM form, DER form, text
form (for display), and possibly other future forms (XML? JSON? JWK?)
The idea is that a serializer should be able to handle objects it has
intimate knowledge of, as well as object data in OSSL_PARAM form. The
latter will allow libcrypto to serialize some object with a different
provider than the one holding the data, if exporting of that data is
allowed and there is a serializer that can handle it.
We will provide serializers for the types of objects we know about,
which should be useful together with any other provider that provides
implementations of the same type of object.
Serializers are selected by method name and a couple of additional
properties:
- format used to tell what format the output should be in.
Possibilities could include "format=text",
"format=pem", "format=der", "format=pem-pkcs1"
(traditional), "format=der-pkcs1" (traditional)
- type used to tell exactly what type of data should be
output, for example "type=public" (the public part of
a key), "type=private" (the private part of a key),
"type=domainparams" (domain parameters).
This also adds a passphrase callback function type,
OSSL_PASSPHRASE_CALLBACK, which is a bit like OSSL_CALLBACK, but it
takes a few extra arguments to place the result in.
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/10394)
Diffstat (limited to 'include')
-rw-r--r-- | include/crypto/serializer.h | 13 | ||||
-rw-r--r-- | include/internal/cryptlib.h | 3 | ||||
-rw-r--r-- | include/openssl/core.h | 10 | ||||
-rw-r--r-- | include/openssl/core_numbers.h | 25 | ||||
-rw-r--r-- | include/openssl/err.h | 2 | ||||
-rw-r--r-- | include/openssl/pem.h | 1 | ||||
-rw-r--r-- | include/openssl/serializer.h | 59 | ||||
-rw-r--r-- | include/openssl/types.h | 5 |
8 files changed, 115 insertions, 3 deletions
diff --git a/include/crypto/serializer.h b/include/crypto/serializer.h new file mode 100644 index 0000000000..c40788f78b --- /dev/null +++ b/include/crypto/serializer.h @@ -0,0 +1,13 @@ +/* + * Copyright 2019 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 + */ + +#include <openssl/types.h> + +OSSL_SERIALIZER *ossl_serializer_fetch_by_number(OPENSSL_CTX *libctx, int id, + const char *properties); diff --git a/include/internal/cryptlib.h b/include/internal/cryptlib.h index 7428453a35..8be3861d4f 100644 --- a/include/internal/cryptlib.h +++ b/include/internal/cryptlib.h @@ -154,7 +154,8 @@ typedef struct ossl_ex_data_global_st { # define OPENSSL_CTX_RAND_CRNGT_INDEX 7 # define OPENSSL_CTX_THREAD_EVENT_HANDLER_INDEX 8 # define OPENSSL_CTX_FIPS_PROV_INDEX 9 -# define OPENSSL_CTX_MAX_INDEXES 10 +# define OPENSSL_CTX_SERIALIZER_STORE_INDEX 10 +# define OPENSSL_CTX_MAX_INDEXES 11 typedef struct openssl_ctx_method { void *(*new_func)(OPENSSL_CTX *ctx); diff --git a/include/openssl/core.h b/include/openssl/core.h index bed580c811..5959a31880 100644 --- a/include/openssl/core.h +++ b/include/openssl/core.h @@ -202,6 +202,16 @@ extern OSSL_provider_init_fn OSSL_provider_init; */ typedef int (OSSL_CALLBACK)(const OSSL_PARAM params[], void *arg); +/* + * Passphrase callback function signature + * + * This is similar to the generic callback function above, but adds a + * result parameter. + */ +typedef int (OSSL_PASSPHRASE_CALLBACK)(char *pass, size_t pass_size, + size_t *pass_len, + const OSSL_PARAM params[], void *arg); + # ifdef __cplusplus } # endif diff --git a/include/openssl/core_numbers.h b/include/openssl/core_numbers.h index d07ef556d9..889362bc37 100644 --- a/include/openssl/core_numbers.h +++ b/include/openssl/core_numbers.h @@ -160,8 +160,10 @@ OSSL_CORE_MAKE_FUNC(const OSSL_ITEM *,provider_get_reason_strings, # define OSSL_OP_KEYEXCH 11 # define OSSL_OP_SIGNATURE 12 # define OSSL_OP_ASYM_CIPHER 13 +/* New section for non-EVP operations */ +# define OSSL_OP_SERIALIZER 20 /* Highest known operation number */ -# define OSSL_OP__HIGHEST 13 +# define OSSL_OP__HIGHEST 20 /* Digests */ @@ -534,6 +536,27 @@ OSSL_CORE_MAKE_FUNC(int, OP_asym_cipher_set_ctx_params, OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_asym_cipher_settable_ctx_params, (void)) +/* Serializers */ +# define OSSL_FUNC_SERIALIZER_NEWCTX 1 +# define OSSL_FUNC_SERIALIZER_FREECTX 2 +# define OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS 3 +# define OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS 4 +# define OSSL_FUNC_SERIALIZER_SERIALIZE_DATA 10 +# define OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT 11 +OSSL_CORE_MAKE_FUNC(void *, OP_serializer_newctx, (void *provctx)) +OSSL_CORE_MAKE_FUNC(void, OP_serializer_freectx, (void *ctx)) +OSSL_CORE_MAKE_FUNC(int, OP_serializer_set_ctx_params, + (void *ctx, const OSSL_PARAM params[])) +OSSL_CORE_MAKE_FUNC(const OSSL_PARAM *, OP_serializer_settable_ctx_params, + (void)) + +OSSL_CORE_MAKE_FUNC(int, OP_serializer_serialize_data, + (void *ctx, const OSSL_PARAM[], BIO *out, + OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)) +OSSL_CORE_MAKE_FUNC(int, OP_serializer_serialize_object, + (void *ctx, void *obj, BIO *out, + OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)) + # ifdef __cplusplus } # endif diff --git a/include/openssl/err.h b/include/openssl/err.h index 9244bb84b1..37f3cc1d86 100644 --- a/include/openssl/err.h +++ b/include/openssl/err.h @@ -112,6 +112,7 @@ struct err_state_st { # define ERR_LIB_CRMF 56 # define ERR_LIB_PROV 57 # define ERR_LIB_CMP 58 +# define ERR_LIB_OSSL_SERIALIZER 59 # define ERR_LIB_USER 128 @@ -231,6 +232,7 @@ struct err_state_st { # define ERR_R_INIT_FAIL (6|ERR_R_FATAL) # define ERR_R_PASSED_INVALID_ARGUMENT (7) # define ERR_R_OPERATION_FAIL (8|ERR_R_FATAL) +# define ERR_R_INVALID_PROVIDER_FUNCTIONS (9|ERR_R_FATAL) /* * 99 is the maximum possible ERR_R_... code, higher values are reserved for diff --git a/include/openssl/pem.h b/include/openssl/pem.h index ef79c1a46a..35d01544ba 100644 --- a/include/openssl/pem.h +++ b/include/openssl/pem.h @@ -261,7 +261,6 @@ extern "C" { # define DECLARE_PEM_rw_cb(name, type) \ DECLARE_PEM_read(name, type) \ DECLARE_PEM_write_cb(name, type) -typedef int pem_password_cb (char *buf, int size, int rwflag, void *userdata); int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher); int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *len, diff --git a/include/openssl/serializer.h b/include/openssl/serializer.h new file mode 100644 index 0000000000..79f8abecb5 --- /dev/null +++ b/include/openssl/serializer.h @@ -0,0 +1,59 @@ +/* + * Copyright 2019 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 + */ + +#ifndef OPENSSL_SERIALIZER_H +# define OPENSSL_SERIALIZER_H +# pragma once + +# include <openssl/opensslconf.h> + +# ifndef OPENSSL_NO_STDIO +# include <stdio.h> +# endif +# include <stdarg.h> +# include <stddef.h> +# include <openssl/types.h> +# include <openssl/core.h> + +# ifdef __cplusplus +extern "C" { +# endif + +OSSL_SERIALIZER *OSSL_SERIALIZER_fetch(OPENSSL_CTX *libctx, + const char *name, + const char *properties); +int OSSL_SERIALIZER_up_ref(OSSL_SERIALIZER *ser); +void OSSL_SERIALIZER_free(OSSL_SERIALIZER *ser); + +const OSSL_PROVIDER *OSSL_SERIALIZER_provider(const OSSL_SERIALIZER *ser); +const char *OSSL_SERIALIZER_properties(const OSSL_SERIALIZER *ser); +int OSSL_SERIALIZER_number(const OSSL_SERIALIZER *ser); +int OSSL_SERIALIZER_is_a(const OSSL_SERIALIZER *ser, + const char *name); + +void OSSL_SERIALIZER_do_all_provided(OPENSSL_CTX *libctx, + void (*fn)(OSSL_SERIALIZER *ser, + void *arg), + void *arg); +void OSSL_SERIALIZER_names_do_all(const OSSL_SERIALIZER *ser, + void (*fn)(const char *name, void *data), + void *data); + +const OSSL_PARAM *OSSL_SERIALIZER_settable_ctx_params(OSSL_SERIALIZER *ser); +OSSL_SERIALIZER_CTX *OSSL_SERIALIZER_CTX_new(OSSL_SERIALIZER *ser); +const OSSL_SERIALIZER * +OSSL_SERIALIZER_CTX_get_serializer(OSSL_SERIALIZER_CTX *ctx); +int OSSL_SERIALIZER_CTX_set_params(OSSL_SERIALIZER_CTX *ctx, + const OSSL_PARAM params[]); +void OSSL_SERIALIZER_CTX_free(OSSL_SERIALIZER_CTX *ctx); + +# ifdef __cplusplus +} +# endif +#endif diff --git a/include/openssl/types.h b/include/openssl/types.h index 151e3f1713..b8450ce709 100644 --- a/include/openssl/types.h +++ b/include/openssl/types.h @@ -203,6 +203,11 @@ typedef struct ossl_item_st OSSL_ITEM; typedef struct ossl_algorithm_st OSSL_ALGORITHM; typedef struct ossl_param_st OSSL_PARAM; +typedef int pem_password_cb (char *buf, int size, int rwflag, void *userdata); + +typedef struct ossl_serializer_st OSSL_SERIALIZER; +typedef struct ossl_serializer_ctx_st OSSL_SERIALIZER_CTX; + #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L && \ defined(INTMAX_MAX) && defined(UINTMAX_MAX) typedef intmax_t ossl_intmax_t; |