encoder_rsa_pub.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright 2019-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. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/core_dispatch.h>
  15. #include <openssl/pem.h>
  16. #include <openssl/rsa.h>
  17. #include <openssl/types.h>
  18. #include <openssl/params.h>
  19. #include "prov/bio.h"
  20. #include "prov/implementations.h"
  21. #include "prov/providercommonerr.h"
  22. #include "prov/provider_ctx.h"
  23. #include "encoder_local.h"
  24. static OSSL_FUNC_encoder_newctx_fn rsa_pub_newctx;
  25. static OSSL_FUNC_encoder_freectx_fn rsa_pub_freectx;
  26. static OSSL_FUNC_encoder_encode_data_fn rsa_pub_der_data;
  27. static OSSL_FUNC_encoder_encode_object_fn rsa_pub_der;
  28. static OSSL_FUNC_encoder_encode_data_fn rsa_pub_pem_data;
  29. static OSSL_FUNC_encoder_encode_object_fn rsa_pub_pem;
  30. static OSSL_FUNC_encoder_encode_data_fn rsa_pub_print_data;
  31. static OSSL_FUNC_encoder_encode_object_fn rsa_pub_print;
  32. /* Public key : context */
  33. /*
  34. * There's no specific implementation context, so we use the provider context
  35. */
  36. static void *rsa_pub_newctx(void *provctx)
  37. {
  38. return provctx;
  39. }
  40. static void rsa_pub_freectx(void *ctx)
  41. {
  42. }
  43. /* Public key : DER */
  44. static int rsa_pub_der_data(void *ctx, const OSSL_PARAM params[],
  45. OSSL_CORE_BIO *out,
  46. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  47. {
  48. OSSL_FUNC_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
  49. OSSL_FUNC_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
  50. OSSL_FUNC_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
  51. int ok = 0;
  52. if (rsa_import != NULL) {
  53. RSA *rsa;
  54. /* ctx == provctx */
  55. if ((rsa = rsa_new(ctx)) != NULL
  56. && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
  57. && rsa_pub_der(ctx, rsa, out, cb, cbarg))
  58. ok = 1;
  59. rsa_free(rsa);
  60. }
  61. return ok;
  62. }
  63. static int rsa_pub_der(void *ctx, void *rsa, OSSL_CORE_BIO *cout,
  64. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  65. {
  66. BIO *out = bio_new_from_core_bio(ctx, cout);
  67. int ret;
  68. if (out == NULL)
  69. return 0;
  70. ret = ossl_prov_write_pub_der_from_obj(out, rsa,
  71. ossl_prov_rsa_type_to_evp(rsa),
  72. ossl_prov_prepare_rsa_params,
  73. (i2d_of_void *)i2d_RSAPublicKey);
  74. BIO_free(out);
  75. return ret;
  76. }
  77. /* Public key : PEM */
  78. static int rsa_pub_pem_data(void *ctx, const OSSL_PARAM params[],
  79. OSSL_CORE_BIO *out,
  80. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  81. {
  82. OSSL_FUNC_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
  83. OSSL_FUNC_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
  84. OSSL_FUNC_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
  85. int ok = 0;
  86. if (rsa_import != NULL) {
  87. RSA *rsa;
  88. /* ctx == provctx */
  89. if ((rsa = rsa_new(ctx)) != NULL
  90. && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
  91. && rsa_pub_pem(ctx, rsa, out, cb, cbarg))
  92. ok = 1;
  93. rsa_free(rsa);
  94. }
  95. return ok;
  96. }
  97. static int rsa_pub_pem(void *ctx, void *rsa, OSSL_CORE_BIO *cout,
  98. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  99. {
  100. BIO *out = bio_new_from_core_bio(ctx, cout);
  101. int ret;
  102. if (out == NULL)
  103. return 0;
  104. ret = ossl_prov_write_pub_pem_from_obj(out, rsa,
  105. ossl_prov_rsa_type_to_evp(rsa),
  106. ossl_prov_prepare_rsa_params,
  107. (i2d_of_void *)i2d_RSAPublicKey);
  108. BIO_free(out);
  109. return ret;
  110. }
  111. static int rsa_pub_print_data(void *ctx, const OSSL_PARAM params[],
  112. OSSL_CORE_BIO *out,
  113. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  114. {
  115. OSSL_FUNC_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
  116. OSSL_FUNC_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
  117. OSSL_FUNC_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
  118. int ok = 0;
  119. if (rsa_import != NULL) {
  120. RSA *rsa;
  121. /* ctx == provctx */
  122. if ((rsa = rsa_new(ctx)) != NULL
  123. && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
  124. && rsa_pub_print(ctx, rsa, out, cb, cbarg))
  125. ok = 1;
  126. rsa_free(rsa);
  127. }
  128. return ok;
  129. }
  130. static int rsa_pub_print(void *ctx, void *rsa, OSSL_CORE_BIO *cout,
  131. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  132. {
  133. BIO *out = bio_new_from_core_bio(ctx, cout);
  134. int ret;
  135. if (out == NULL)
  136. return 0;
  137. ret = ossl_prov_print_rsa(out, rsa, 0);
  138. BIO_free(out);
  139. return ret;
  140. }
  141. const OSSL_DISPATCH rsa_pub_der_encoder_functions[] = {
  142. { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))rsa_pub_newctx },
  143. { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))rsa_pub_freectx },
  144. { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))rsa_pub_der_data },
  145. { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))rsa_pub_der },
  146. { 0, NULL }
  147. };
  148. const OSSL_DISPATCH rsa_pub_pem_encoder_functions[] = {
  149. { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))rsa_pub_newctx },
  150. { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))rsa_pub_freectx },
  151. { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))rsa_pub_pem_data },
  152. { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))rsa_pub_pem },
  153. { 0, NULL }
  154. };
  155. const OSSL_DISPATCH rsa_pub_text_encoder_functions[] = {
  156. { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))rsa_pub_newctx },
  157. { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))rsa_pub_freectx },
  158. { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))rsa_pub_print },
  159. { OSSL_FUNC_ENCODER_ENCODE_DATA,
  160. (void (*)(void))rsa_pub_print_data },
  161. { 0, NULL }
  162. };