Path: blob/main/crypto/openssl/ssl/ssl_rsa_legacy.c
48150 views
/*1* Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License"). You may not use4* this file except in compliance with the License. You can obtain a copy5* in the file LICENSE in the source distribution or at6* https://www.openssl.org/source/license.html7*/89/* We need to use the deprecated RSA low level calls */10#define OPENSSL_SUPPRESS_DEPRECATED1112#include <openssl/err.h>13#include <openssl/rsa.h>14#include <openssl/ssl.h>1516int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)17{18EVP_PKEY *pkey;19int ret;2021if (rsa == NULL) {22ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);23return 0;24}25if ((pkey = EVP_PKEY_new()) == NULL) {26ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);27return 0;28}2930if (!RSA_up_ref(rsa)) {31EVP_PKEY_free(pkey);32return 0;33}3435if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {36RSA_free(rsa);37EVP_PKEY_free(pkey);38return 0;39}4041ret = SSL_use_PrivateKey(ssl, pkey);42EVP_PKEY_free(pkey);43return ret;44}4546int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)47{48int j, ret = 0;49BIO *in = NULL;50RSA *rsa = NULL;5152if (file == NULL) {53ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);54goto end;55}5657in = BIO_new(BIO_s_file());58if (in == NULL) {59ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);60goto end;61}6263if (BIO_read_filename(in, file) <= 0) {64ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);65goto end;66}67if (type == SSL_FILETYPE_ASN1) {68j = ERR_R_ASN1_LIB;69rsa = d2i_RSAPrivateKey_bio(in, NULL);70} else if (type == SSL_FILETYPE_PEM) {71j = ERR_R_PEM_LIB;72rsa = PEM_read_bio_RSAPrivateKey(in, NULL,73SSL_get_default_passwd_cb(ssl),74SSL_get_default_passwd_cb_userdata(ssl));75} else {76ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);77goto end;78}79if (rsa == NULL) {80ERR_raise(ERR_LIB_SSL, j);81goto end;82}83ret = SSL_use_RSAPrivateKey(ssl, rsa);84RSA_free(rsa);85end:86BIO_free(in);87return ret;88}8990int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)91{92int ret;93const unsigned char *p;94RSA *rsa;9596p = d;97if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {98ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);99return 0;100}101102ret = SSL_use_RSAPrivateKey(ssl, rsa);103RSA_free(rsa);104return ret;105}106107int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)108{109int ret;110EVP_PKEY *pkey;111112if (rsa == NULL) {113ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);114return 0;115}116if ((pkey = EVP_PKEY_new()) == NULL) {117ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);118return 0;119}120121if (!RSA_up_ref(rsa)) {122EVP_PKEY_free(pkey);123return 0;124}125126if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {127RSA_free(rsa);128EVP_PKEY_free(pkey);129return 0;130}131132ret = SSL_CTX_use_PrivateKey(ctx, pkey);133EVP_PKEY_free(pkey);134return ret;135}136137int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)138{139int j, ret = 0;140BIO *in = NULL;141RSA *rsa = NULL;142143if (file == NULL) {144ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);145goto end;146}147148in = BIO_new(BIO_s_file());149if (in == NULL) {150ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);151goto end;152}153154if (BIO_read_filename(in, file) <= 0) {155ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);156goto end;157}158if (type == SSL_FILETYPE_ASN1) {159j = ERR_R_ASN1_LIB;160rsa = d2i_RSAPrivateKey_bio(in, NULL);161} else if (type == SSL_FILETYPE_PEM) {162j = ERR_R_PEM_LIB;163rsa = PEM_read_bio_RSAPrivateKey(in, NULL,164SSL_CTX_get_default_passwd_cb(ctx),165SSL_CTX_get_default_passwd_cb_userdata(ctx));166} else {167ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);168goto end;169}170if (rsa == NULL) {171ERR_raise(ERR_LIB_SSL, j);172goto end;173}174ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);175RSA_free(rsa);176end:177BIO_free(in);178return ret;179}180181int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,182long len)183{184int ret;185const unsigned char *p;186RSA *rsa;187188p = d;189if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {190ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);191return 0;192}193194ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);195RSA_free(rsa);196return ret;197}198199200