1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved. 3e1051a39Sopenharmony_ci * 4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License"). You may not use 5e1051a39Sopenharmony_ci * this file except in compliance with the License. You can obtain a copy 6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at 7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html 8e1051a39Sopenharmony_ci */ 9e1051a39Sopenharmony_ci 10e1051a39Sopenharmony_ci#include <openssl/core_dispatch.h> 11e1051a39Sopenharmony_ci#include <openssl/types.h> 12e1051a39Sopenharmony_ci#include <openssl/safestack.h> 13e1051a39Sopenharmony_ci#include <openssl/encoder.h> 14e1051a39Sopenharmony_ci#include <openssl/decoder.h> 15e1051a39Sopenharmony_ci#include "internal/cryptlib.h" 16e1051a39Sopenharmony_ci#include "internal/passphrase.h" 17e1051a39Sopenharmony_ci#include "internal/property.h" 18e1051a39Sopenharmony_ci#include "internal/refcount.h" 19e1051a39Sopenharmony_ci 20e1051a39Sopenharmony_cistruct ossl_endecode_base_st { 21e1051a39Sopenharmony_ci OSSL_PROVIDER *prov; 22e1051a39Sopenharmony_ci int id; 23e1051a39Sopenharmony_ci char *name; 24e1051a39Sopenharmony_ci const OSSL_ALGORITHM *algodef; 25e1051a39Sopenharmony_ci OSSL_PROPERTY_LIST *parsed_propdef; 26e1051a39Sopenharmony_ci 27e1051a39Sopenharmony_ci CRYPTO_REF_COUNT refcnt; 28e1051a39Sopenharmony_ci CRYPTO_RWLOCK *lock; 29e1051a39Sopenharmony_ci}; 30e1051a39Sopenharmony_ci 31e1051a39Sopenharmony_cistruct ossl_encoder_st { 32e1051a39Sopenharmony_ci struct ossl_endecode_base_st base; 33e1051a39Sopenharmony_ci OSSL_FUNC_encoder_newctx_fn *newctx; 34e1051a39Sopenharmony_ci OSSL_FUNC_encoder_freectx_fn *freectx; 35e1051a39Sopenharmony_ci OSSL_FUNC_encoder_get_params_fn *get_params; 36e1051a39Sopenharmony_ci OSSL_FUNC_encoder_gettable_params_fn *gettable_params; 37e1051a39Sopenharmony_ci OSSL_FUNC_encoder_set_ctx_params_fn *set_ctx_params; 38e1051a39Sopenharmony_ci OSSL_FUNC_encoder_settable_ctx_params_fn *settable_ctx_params; 39e1051a39Sopenharmony_ci OSSL_FUNC_encoder_does_selection_fn *does_selection; 40e1051a39Sopenharmony_ci OSSL_FUNC_encoder_encode_fn *encode; 41e1051a39Sopenharmony_ci OSSL_FUNC_encoder_import_object_fn *import_object; 42e1051a39Sopenharmony_ci OSSL_FUNC_encoder_free_object_fn *free_object; 43e1051a39Sopenharmony_ci}; 44e1051a39Sopenharmony_ci 45e1051a39Sopenharmony_cistruct ossl_decoder_st { 46e1051a39Sopenharmony_ci struct ossl_endecode_base_st base; 47e1051a39Sopenharmony_ci OSSL_FUNC_decoder_newctx_fn *newctx; 48e1051a39Sopenharmony_ci OSSL_FUNC_decoder_freectx_fn *freectx; 49e1051a39Sopenharmony_ci OSSL_FUNC_decoder_get_params_fn *get_params; 50e1051a39Sopenharmony_ci OSSL_FUNC_decoder_gettable_params_fn *gettable_params; 51e1051a39Sopenharmony_ci OSSL_FUNC_decoder_set_ctx_params_fn *set_ctx_params; 52e1051a39Sopenharmony_ci OSSL_FUNC_decoder_settable_ctx_params_fn *settable_ctx_params; 53e1051a39Sopenharmony_ci OSSL_FUNC_decoder_does_selection_fn *does_selection; 54e1051a39Sopenharmony_ci OSSL_FUNC_decoder_decode_fn *decode; 55e1051a39Sopenharmony_ci OSSL_FUNC_decoder_export_object_fn *export_object; 56e1051a39Sopenharmony_ci}; 57e1051a39Sopenharmony_ci 58e1051a39Sopenharmony_cistruct ossl_encoder_instance_st { 59e1051a39Sopenharmony_ci OSSL_ENCODER *encoder; /* Never NULL */ 60e1051a39Sopenharmony_ci void *encoderctx; /* Never NULL */ 61e1051a39Sopenharmony_ci const char *output_type; /* Never NULL */ 62e1051a39Sopenharmony_ci const char *output_structure; /* May be NULL */ 63e1051a39Sopenharmony_ci}; 64e1051a39Sopenharmony_ci 65e1051a39Sopenharmony_ciDEFINE_STACK_OF(OSSL_ENCODER_INSTANCE) 66e1051a39Sopenharmony_ci 67e1051a39Sopenharmony_civoid ossl_encoder_instance_free(OSSL_ENCODER_INSTANCE *encoder_inst); 68e1051a39Sopenharmony_ci 69e1051a39Sopenharmony_cistruct ossl_encoder_ctx_st { 70e1051a39Sopenharmony_ci /* 71e1051a39Sopenharmony_ci * Select what parts of an object will be encoded. This selection is 72e1051a39Sopenharmony_ci * bit encoded, and the bits correspond to selection bits available with 73e1051a39Sopenharmony_ci * the provider side operation. For example, when encoding an EVP_PKEY, 74e1051a39Sopenharmony_ci * the OSSL_KEYMGMT_SELECT_ macros are used for this. 75e1051a39Sopenharmony_ci */ 76e1051a39Sopenharmony_ci int selection; 77e1051a39Sopenharmony_ci /* 78e1051a39Sopenharmony_ci * The desired output type. The encoder implementation must have a 79e1051a39Sopenharmony_ci * gettable "output-type" parameter that this will match against. 80e1051a39Sopenharmony_ci */ 81e1051a39Sopenharmony_ci const char *output_type; 82e1051a39Sopenharmony_ci /* 83e1051a39Sopenharmony_ci * The desired output structure, if that's relevant for the type of 84e1051a39Sopenharmony_ci * object being encoded. It may be used for selection of the starting 85e1051a39Sopenharmony_ci * encoder implementations in a chain. 86e1051a39Sopenharmony_ci */ 87e1051a39Sopenharmony_ci const char *output_structure; 88e1051a39Sopenharmony_ci 89e1051a39Sopenharmony_ci /* 90e1051a39Sopenharmony_ci * Decoders that are components of any current decoding path. 91e1051a39Sopenharmony_ci */ 92e1051a39Sopenharmony_ci STACK_OF(OSSL_ENCODER_INSTANCE) *encoder_insts; 93e1051a39Sopenharmony_ci 94e1051a39Sopenharmony_ci /* 95e1051a39Sopenharmony_ci * The constructor and destructor of an object to pass to the first 96e1051a39Sopenharmony_ci * encoder in a chain. 97e1051a39Sopenharmony_ci */ 98e1051a39Sopenharmony_ci OSSL_ENCODER_CONSTRUCT *construct; 99e1051a39Sopenharmony_ci OSSL_ENCODER_CLEANUP *cleanup; 100e1051a39Sopenharmony_ci void *construct_data; 101e1051a39Sopenharmony_ci 102e1051a39Sopenharmony_ci /* For any function that needs a passphrase reader */ 103e1051a39Sopenharmony_ci struct ossl_passphrase_data_st pwdata; 104e1051a39Sopenharmony_ci}; 105e1051a39Sopenharmony_ci 106e1051a39Sopenharmony_cistruct ossl_decoder_instance_st { 107e1051a39Sopenharmony_ci OSSL_DECODER *decoder; /* Never NULL */ 108e1051a39Sopenharmony_ci void *decoderctx; /* Never NULL */ 109e1051a39Sopenharmony_ci const char *input_type; /* Never NULL */ 110e1051a39Sopenharmony_ci const char *input_structure; /* May be NULL */ 111e1051a39Sopenharmony_ci 112e1051a39Sopenharmony_ci unsigned int flag_input_structure_was_set : 1; 113e1051a39Sopenharmony_ci}; 114e1051a39Sopenharmony_ci 115e1051a39Sopenharmony_ciDEFINE_STACK_OF(OSSL_DECODER_INSTANCE) 116e1051a39Sopenharmony_ci 117e1051a39Sopenharmony_cistruct ossl_decoder_ctx_st { 118e1051a39Sopenharmony_ci /* 119e1051a39Sopenharmony_ci * The caller may know the input type of the data they pass. If not, 120e1051a39Sopenharmony_ci * this will remain NULL and the decoding functionality will start 121e1051a39Sopenharmony_ci * with trying to decode with any desencoder in |decoder_insts|, 122e1051a39Sopenharmony_ci * regardless of their respective input type. 123e1051a39Sopenharmony_ci */ 124e1051a39Sopenharmony_ci const char *start_input_type; 125e1051a39Sopenharmony_ci /* 126e1051a39Sopenharmony_ci * The desired input structure, if that's relevant for the type of 127e1051a39Sopenharmony_ci * object being encoded. It may be used for selection of the ending 128e1051a39Sopenharmony_ci * decoder implementations in a chain, i.e. those chosen using the 129e1051a39Sopenharmony_ci * expected output data type. 130e1051a39Sopenharmony_ci */ 131e1051a39Sopenharmony_ci const char *input_structure; 132e1051a39Sopenharmony_ci /* 133e1051a39Sopenharmony_ci * Select what parts of an object are expected. This may affect what 134e1051a39Sopenharmony_ci * decoder implementations are selected, because there are structures 135e1051a39Sopenharmony_ci * that look different depending on this selection; for example, EVP_PKEY 136e1051a39Sopenharmony_ci * objects often have different encoding structures for private keys, 137e1051a39Sopenharmony_ci * public keys and key parameters. 138e1051a39Sopenharmony_ci * This selection is bit encoded, and the bits correspond to selection 139e1051a39Sopenharmony_ci * bits available with the provider side operation. For example, when 140e1051a39Sopenharmony_ci * encoding an EVP_PKEY, the OSSL_KEYMGMT_SELECT_ macros are used for 141e1051a39Sopenharmony_ci * this. 142e1051a39Sopenharmony_ci */ 143e1051a39Sopenharmony_ci int selection; 144e1051a39Sopenharmony_ci 145e1051a39Sopenharmony_ci /* 146e1051a39Sopenharmony_ci * Decoders that are components of any current decoding path. 147e1051a39Sopenharmony_ci */ 148e1051a39Sopenharmony_ci STACK_OF(OSSL_DECODER_INSTANCE) *decoder_insts; 149e1051a39Sopenharmony_ci 150e1051a39Sopenharmony_ci /* 151e1051a39Sopenharmony_ci * The constructors of a decoding, and its caller argument. 152e1051a39Sopenharmony_ci */ 153e1051a39Sopenharmony_ci OSSL_DECODER_CONSTRUCT *construct; 154e1051a39Sopenharmony_ci OSSL_DECODER_CLEANUP *cleanup; 155e1051a39Sopenharmony_ci void *construct_data; 156e1051a39Sopenharmony_ci 157e1051a39Sopenharmony_ci /* For any function that needs a passphrase reader */ 158e1051a39Sopenharmony_ci struct ossl_passphrase_data_st pwdata; 159e1051a39Sopenharmony_ci}; 160e1051a39Sopenharmony_ci 161e1051a39Sopenharmony_ciconst OSSL_PROPERTY_LIST * 162e1051a39Sopenharmony_ciossl_decoder_parsed_properties(const OSSL_DECODER *decoder); 163e1051a39Sopenharmony_ciconst OSSL_PROPERTY_LIST * 164e1051a39Sopenharmony_ciossl_encoder_parsed_properties(const OSSL_ENCODER *encoder); 165