123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- /*
- * Copyright 2019-2020 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
- */
- /*
- * RSA low level APIs are deprecated for public use, but still ok for
- * internal use.
- */
- #include "internal/deprecated.h"
- #include <openssl/core_dispatch.h>
- #include <openssl/pem.h>
- #include <openssl/rsa.h>
- #include <openssl/types.h>
- #include <openssl/params.h>
- #include "prov/bio.h"
- #include "prov/implementations.h"
- #include "prov/providercommonerr.h"
- #include "prov/provider_ctx.h"
- #include "encoder_local.h"
- static OSSL_FUNC_encoder_newctx_fn rsa_pub_newctx;
- static OSSL_FUNC_encoder_freectx_fn rsa_pub_freectx;
- static OSSL_FUNC_encoder_encode_data_fn rsa_pub_der_data;
- static OSSL_FUNC_encoder_encode_object_fn rsa_pub_der;
- static OSSL_FUNC_encoder_encode_data_fn rsa_pub_pem_data;
- static OSSL_FUNC_encoder_encode_object_fn rsa_pub_pem;
- static OSSL_FUNC_encoder_encode_data_fn rsa_pub_print_data;
- static OSSL_FUNC_encoder_encode_object_fn rsa_pub_print;
- /* Public key : context */
- /*
- * There's no specific implementation context, so we use the provider context
- */
- static void *rsa_pub_newctx(void *provctx)
- {
- return provctx;
- }
- static void rsa_pub_freectx(void *ctx)
- {
- }
- /* Public key : DER */
- static int rsa_pub_der_data(void *ctx, const OSSL_PARAM params[],
- OSSL_CORE_BIO *out,
- OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
- {
- OSSL_FUNC_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
- OSSL_FUNC_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
- OSSL_FUNC_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
- int ok = 0;
- if (rsa_import != NULL) {
- RSA *rsa;
- /* ctx == provctx */
- if ((rsa = rsa_new(ctx)) != NULL
- && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
- && rsa_pub_der(ctx, rsa, out, cb, cbarg))
- ok = 1;
- rsa_free(rsa);
- }
- return ok;
- }
- static int rsa_pub_der(void *ctx, void *rsa, OSSL_CORE_BIO *cout,
- OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
- {
- BIO *out = bio_new_from_core_bio(ctx, cout);
- int ret;
- if (out == NULL)
- return 0;
- ret = ossl_prov_write_pub_der_from_obj(out, rsa,
- ossl_prov_rsa_type_to_evp(rsa),
- ossl_prov_prepare_rsa_params,
- (i2d_of_void *)i2d_RSAPublicKey);
- BIO_free(out);
- return ret;
- }
- /* Public key : PEM */
- static int rsa_pub_pem_data(void *ctx, const OSSL_PARAM params[],
- OSSL_CORE_BIO *out,
- OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
- {
- OSSL_FUNC_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
- OSSL_FUNC_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
- OSSL_FUNC_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
- int ok = 0;
- if (rsa_import != NULL) {
- RSA *rsa;
- /* ctx == provctx */
- if ((rsa = rsa_new(ctx)) != NULL
- && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
- && rsa_pub_pem(ctx, rsa, out, cb, cbarg))
- ok = 1;
- rsa_free(rsa);
- }
- return ok;
- }
- static int rsa_pub_pem(void *ctx, void *rsa, OSSL_CORE_BIO *cout,
- OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
- {
- BIO *out = bio_new_from_core_bio(ctx, cout);
- int ret;
- if (out == NULL)
- return 0;
- ret = ossl_prov_write_pub_pem_from_obj(out, rsa,
- ossl_prov_rsa_type_to_evp(rsa),
- ossl_prov_prepare_rsa_params,
- (i2d_of_void *)i2d_RSAPublicKey);
- BIO_free(out);
- return ret;
- }
- static int rsa_pub_print_data(void *ctx, const OSSL_PARAM params[],
- OSSL_CORE_BIO *out,
- OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
- {
- OSSL_FUNC_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
- OSSL_FUNC_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
- OSSL_FUNC_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
- int ok = 0;
- if (rsa_import != NULL) {
- RSA *rsa;
- /* ctx == provctx */
- if ((rsa = rsa_new(ctx)) != NULL
- && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
- && rsa_pub_print(ctx, rsa, out, cb, cbarg))
- ok = 1;
- rsa_free(rsa);
- }
- return ok;
- }
- static int rsa_pub_print(void *ctx, void *rsa, OSSL_CORE_BIO *cout,
- OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
- {
- BIO *out = bio_new_from_core_bio(ctx, cout);
- int ret;
- if (out == NULL)
- return 0;
- ret = ossl_prov_print_rsa(out, rsa, 0);
- BIO_free(out);
- return ret;
- }
- const OSSL_DISPATCH rsa_pub_der_encoder_functions[] = {
- { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))rsa_pub_newctx },
- { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))rsa_pub_freectx },
- { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))rsa_pub_der_data },
- { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))rsa_pub_der },
- { 0, NULL }
- };
- const OSSL_DISPATCH rsa_pub_pem_encoder_functions[] = {
- { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))rsa_pub_newctx },
- { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))rsa_pub_freectx },
- { OSSL_FUNC_ENCODER_ENCODE_DATA, (void (*)(void))rsa_pub_pem_data },
- { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))rsa_pub_pem },
- { 0, NULL }
- };
- const OSSL_DISPATCH rsa_pub_text_encoder_functions[] = {
- { OSSL_FUNC_ENCODER_NEWCTX, (void (*)(void))rsa_pub_newctx },
- { OSSL_FUNC_ENCODER_FREECTX, (void (*)(void))rsa_pub_freectx },
- { OSSL_FUNC_ENCODER_ENCODE_OBJECT, (void (*)(void))rsa_pub_print },
- { OSSL_FUNC_ENCODER_ENCODE_DATA,
- (void (*)(void))rsa_pub_print_data },
- { 0, NULL }
- };
|