Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/openssl/ssl/ssl_rsa_legacy.c
48150 views
1
/*
2
* Copyright 1995-2025 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
/* We need to use the deprecated RSA low level calls */
11
#define OPENSSL_SUPPRESS_DEPRECATED
12
13
#include <openssl/err.h>
14
#include <openssl/rsa.h>
15
#include <openssl/ssl.h>
16
17
int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
18
{
19
EVP_PKEY *pkey;
20
int ret;
21
22
if (rsa == NULL) {
23
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
24
return 0;
25
}
26
if ((pkey = EVP_PKEY_new()) == NULL) {
27
ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
28
return 0;
29
}
30
31
if (!RSA_up_ref(rsa)) {
32
EVP_PKEY_free(pkey);
33
return 0;
34
}
35
36
if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
37
RSA_free(rsa);
38
EVP_PKEY_free(pkey);
39
return 0;
40
}
41
42
ret = SSL_use_PrivateKey(ssl, pkey);
43
EVP_PKEY_free(pkey);
44
return ret;
45
}
46
47
int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
48
{
49
int j, ret = 0;
50
BIO *in = NULL;
51
RSA *rsa = NULL;
52
53
if (file == NULL) {
54
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
55
goto end;
56
}
57
58
in = BIO_new(BIO_s_file());
59
if (in == NULL) {
60
ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
61
goto end;
62
}
63
64
if (BIO_read_filename(in, file) <= 0) {
65
ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
66
goto end;
67
}
68
if (type == SSL_FILETYPE_ASN1) {
69
j = ERR_R_ASN1_LIB;
70
rsa = d2i_RSAPrivateKey_bio(in, NULL);
71
} else if (type == SSL_FILETYPE_PEM) {
72
j = ERR_R_PEM_LIB;
73
rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
74
SSL_get_default_passwd_cb(ssl),
75
SSL_get_default_passwd_cb_userdata(ssl));
76
} else {
77
ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
78
goto end;
79
}
80
if (rsa == NULL) {
81
ERR_raise(ERR_LIB_SSL, j);
82
goto end;
83
}
84
ret = SSL_use_RSAPrivateKey(ssl, rsa);
85
RSA_free(rsa);
86
end:
87
BIO_free(in);
88
return ret;
89
}
90
91
int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
92
{
93
int ret;
94
const unsigned char *p;
95
RSA *rsa;
96
97
p = d;
98
if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
99
ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
100
return 0;
101
}
102
103
ret = SSL_use_RSAPrivateKey(ssl, rsa);
104
RSA_free(rsa);
105
return ret;
106
}
107
108
int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
109
{
110
int ret;
111
EVP_PKEY *pkey;
112
113
if (rsa == NULL) {
114
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
115
return 0;
116
}
117
if ((pkey = EVP_PKEY_new()) == NULL) {
118
ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
119
return 0;
120
}
121
122
if (!RSA_up_ref(rsa)) {
123
EVP_PKEY_free(pkey);
124
return 0;
125
}
126
127
if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
128
RSA_free(rsa);
129
EVP_PKEY_free(pkey);
130
return 0;
131
}
132
133
ret = SSL_CTX_use_PrivateKey(ctx, pkey);
134
EVP_PKEY_free(pkey);
135
return ret;
136
}
137
138
int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
139
{
140
int j, ret = 0;
141
BIO *in = NULL;
142
RSA *rsa = NULL;
143
144
if (file == NULL) {
145
ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
146
goto end;
147
}
148
149
in = BIO_new(BIO_s_file());
150
if (in == NULL) {
151
ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
152
goto end;
153
}
154
155
if (BIO_read_filename(in, file) <= 0) {
156
ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
157
goto end;
158
}
159
if (type == SSL_FILETYPE_ASN1) {
160
j = ERR_R_ASN1_LIB;
161
rsa = d2i_RSAPrivateKey_bio(in, NULL);
162
} else if (type == SSL_FILETYPE_PEM) {
163
j = ERR_R_PEM_LIB;
164
rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
165
SSL_CTX_get_default_passwd_cb(ctx),
166
SSL_CTX_get_default_passwd_cb_userdata(ctx));
167
} else {
168
ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
169
goto end;
170
}
171
if (rsa == NULL) {
172
ERR_raise(ERR_LIB_SSL, j);
173
goto end;
174
}
175
ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
176
RSA_free(rsa);
177
end:
178
BIO_free(in);
179
return ret;
180
}
181
182
int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
183
long len)
184
{
185
int ret;
186
const unsigned char *p;
187
RSA *rsa;
188
189
p = d;
190
if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
191
ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
192
return 0;
193
}
194
195
ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
196
RSA_free(rsa);
197
return ret;
198
}
199
200