encoder_local.h 5.7 KB

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