encoder_ec_param.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright 2020 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/pem.h>
  11. #include <openssl/ec.h>
  12. #include <openssl/types.h>
  13. #include <openssl/params.h>
  14. #include "prov/bio.h"
  15. #include "prov/implementations.h"
  16. #include "prov/providercommonerr.h"
  17. #include "prov/provider_ctx.h"
  18. #include "encoder_local.h"
  19. static OSSL_FUNC_encoder_newctx_fn ec_param_newctx;
  20. static OSSL_FUNC_encoder_freectx_fn ec_param_freectx;
  21. static OSSL_FUNC_encoder_encode_data_fn ec_param_der_data;
  22. static OSSL_FUNC_encoder_encode_object_fn ec_param_der;
  23. static OSSL_FUNC_encoder_encode_data_fn ec_param_pem_data;
  24. static OSSL_FUNC_encoder_encode_object_fn ec_param_pem;
  25. static OSSL_FUNC_encoder_encode_data_fn ec_param_print_data;
  26. static OSSL_FUNC_encoder_encode_object_fn ec_param_print;
  27. /* There is no specific implementation context, so use the provider context */
  28. static void *ec_param_newctx(void *provctx)
  29. {
  30. return provctx;
  31. }
  32. static void ec_param_freectx(void *vctx)
  33. {
  34. }
  35. /* Public key : DER */
  36. static int ec_param_der_data(void *vctx, const OSSL_PARAM params[],
  37. OSSL_CORE_BIO *out,
  38. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  39. {
  40. OSSL_FUNC_keymgmt_new_fn *ec_new;
  41. OSSL_FUNC_keymgmt_free_fn *ec_free;
  42. OSSL_FUNC_keymgmt_import_fn *ec_import;
  43. int ok = 0;
  44. ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
  45. if (ec_import != NULL) {
  46. EC_KEY *eckey;
  47. /* vctx == provctx */
  48. if ((eckey = ec_new(vctx)) != NULL
  49. && ec_import(eckey, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
  50. && ec_param_der(vctx, eckey, out, cb, cbarg))
  51. ok = 1;
  52. ec_free(eckey);
  53. }
  54. return ok;
  55. }
  56. static int ec_param_der(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
  57. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  58. {
  59. BIO *out = bio_new_from_core_bio(vctx, cout);
  60. int ret;
  61. if (out == NULL)
  62. return 0;
  63. ret = i2d_ECPKParameters_bio(out, EC_KEY_get0_group(eckey));
  64. BIO_free(out);
  65. return ret;
  66. }
  67. /* Public key : PEM */
  68. static int ec_param_pem_data(void *vctx, const OSSL_PARAM params[],
  69. OSSL_CORE_BIO *out,
  70. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  71. {
  72. OSSL_FUNC_keymgmt_new_fn *ec_new;
  73. OSSL_FUNC_keymgmt_free_fn *ec_free;
  74. OSSL_FUNC_keymgmt_import_fn *ec_import;
  75. int ok = 0;
  76. ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
  77. if (ec_import != NULL) {
  78. EC_KEY *eckey;
  79. /* vctx == provctx */
  80. if ((eckey = ec_new(vctx)) != NULL
  81. && ec_import(eckey, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
  82. && ec_param_pem(vctx, eckey, out, cb, cbarg))
  83. ok = 1;
  84. ec_free(eckey);
  85. }
  86. return ok;
  87. }
  88. static int ec_param_pem(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
  89. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  90. {
  91. BIO *out = bio_new_from_core_bio(vctx, cout);
  92. int ret;
  93. if (out == NULL)
  94. return 0;
  95. ret = PEM_write_bio_ECPKParameters(out, EC_KEY_get0_group(eckey));
  96. BIO_free(out);
  97. return ret;
  98. }
  99. static int ec_param_print_data(void *vctx, const OSSL_PARAM params[],
  100. OSSL_CORE_BIO *out,
  101. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  102. {
  103. OSSL_FUNC_keymgmt_new_fn *ec_new;
  104. OSSL_FUNC_keymgmt_free_fn *ec_free;
  105. OSSL_FUNC_keymgmt_import_fn *ec_import;
  106. int ok = 0;
  107. ec_get_new_free_import(&ec_new, &ec_free, &ec_import);
  108. if (ec_import != NULL) {
  109. EC_KEY *eckey;
  110. /* vctx == provctx */
  111. if ((eckey = ec_new(vctx)) != NULL
  112. && ec_import(eckey, OSSL_KEYMGMT_SELECT_ALL_PARAMETERS, params)
  113. && ec_param_print(vctx, eckey, out, cb, cbarg))
  114. ok = 1;
  115. ec_free(eckey);
  116. }
  117. return ok;
  118. }
  119. static int ec_param_print(void *vctx, void *eckey, OSSL_CORE_BIO *cout,
  120. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  121. {
  122. BIO *out = bio_new_from_core_bio(vctx, cout);
  123. int ret;
  124. if (out == NULL)
  125. return 0;
  126. ret = ossl_prov_print_eckey(out, eckey, ec_print_params);
  127. BIO_free(out);
  128. return ret;
  129. }
  130. const OSSL_DISPATCH ec_param_der_encoder_functions[] = {
  131. { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))ec_param_newctx },
  132. { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))ec_param_freectx },
  133. { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))ec_param_der_data },
  134. { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))ec_param_der },
  135. { 0, NULL }
  136. };
  137. const OSSL_DISPATCH ec_param_pem_encoder_functions[] = {
  138. { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))ec_param_newctx },
  139. { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))ec_param_freectx },
  140. { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))ec_param_pem_data },
  141. { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))ec_param_pem },
  142. { 0, NULL }
  143. };
  144. const OSSL_DISPATCH ec_param_text_encoder_functions[] = {
  145. { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))ec_param_newctx },
  146. { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))ec_param_freectx },
  147. { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))ec_param_print },
  148. { OSSL_FUNC_ENCODER_ENCODE_DATA,
  149. (void (*)(void))ec_param_print_data },
  150. { 0, NULL }
  151. };