1 /*
2 * Copyright 1995-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 /* 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
SSL_use_RSAPrivateKey(SSL * ssl,RSA * rsa)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 RSA_up_ref(rsa);
32 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
33 RSA_free(rsa);
34 EVP_PKEY_free(pkey);
35 return 0;
36 }
37
38 ret = SSL_use_PrivateKey(ssl, pkey);
39 EVP_PKEY_free(pkey);
40 return ret;
41 }
42
SSL_use_RSAPrivateKey_file(SSL * ssl,const char * file,int type)43 int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
44 {
45 int j, ret = 0;
46 BIO *in = NULL;
47 RSA *rsa = NULL;
48
49 if (file == NULL) {
50 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
51 goto end;
52 }
53
54 in = BIO_new(BIO_s_file());
55 if (in == NULL) {
56 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
57 goto end;
58 }
59
60 if (BIO_read_filename(in, file) <= 0) {
61 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
62 goto end;
63 }
64 if (type == SSL_FILETYPE_ASN1) {
65 j = ERR_R_ASN1_LIB;
66 rsa = d2i_RSAPrivateKey_bio(in, NULL);
67 } else if (type == SSL_FILETYPE_PEM) {
68 j = ERR_R_PEM_LIB;
69 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
70 SSL_get_default_passwd_cb(ssl),
71 SSL_get_default_passwd_cb_userdata(ssl));
72 } else {
73 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
74 goto end;
75 }
76 if (rsa == NULL) {
77 ERR_raise(ERR_LIB_SSL, j);
78 goto end;
79 }
80 ret = SSL_use_RSAPrivateKey(ssl, rsa);
81 RSA_free(rsa);
82 end:
83 BIO_free(in);
84 return ret;
85 }
86
SSL_use_RSAPrivateKey_ASN1(SSL * ssl,const unsigned char * d,long len)87 int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
88 {
89 int ret;
90 const unsigned char *p;
91 RSA *rsa;
92
93 p = d;
94 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
95 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
96 return 0;
97 }
98
99 ret = SSL_use_RSAPrivateKey(ssl, rsa);
100 RSA_free(rsa);
101 return ret;
102 }
103
SSL_CTX_use_RSAPrivateKey(SSL_CTX * ctx,RSA * rsa)104 int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
105 {
106 int ret;
107 EVP_PKEY *pkey;
108
109 if (rsa == NULL) {
110 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
111 return 0;
112 }
113 if ((pkey = EVP_PKEY_new()) == NULL) {
114 ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
115 return 0;
116 }
117
118 RSA_up_ref(rsa);
119 if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
120 RSA_free(rsa);
121 EVP_PKEY_free(pkey);
122 return 0;
123 }
124
125 ret = SSL_CTX_use_PrivateKey(ctx, pkey);
126 EVP_PKEY_free(pkey);
127 return ret;
128 }
129
SSL_CTX_use_RSAPrivateKey_file(SSL_CTX * ctx,const char * file,int type)130 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
131 {
132 int j, ret = 0;
133 BIO *in = NULL;
134 RSA *rsa = NULL;
135
136 if (file == NULL) {
137 ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
138 goto end;
139 }
140
141 in = BIO_new(BIO_s_file());
142 if (in == NULL) {
143 ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
144 goto end;
145 }
146
147 if (BIO_read_filename(in, file) <= 0) {
148 ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
149 goto end;
150 }
151 if (type == SSL_FILETYPE_ASN1) {
152 j = ERR_R_ASN1_LIB;
153 rsa = d2i_RSAPrivateKey_bio(in, NULL);
154 } else if (type == SSL_FILETYPE_PEM) {
155 j = ERR_R_PEM_LIB;
156 rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
157 SSL_CTX_get_default_passwd_cb(ctx),
158 SSL_CTX_get_default_passwd_cb_userdata(ctx));
159 } else {
160 ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
161 goto end;
162 }
163 if (rsa == NULL) {
164 ERR_raise(ERR_LIB_SSL, j);
165 goto end;
166 }
167 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
168 RSA_free(rsa);
169 end:
170 BIO_free(in);
171 return ret;
172 }
173
SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX * ctx,const unsigned char * d,long len)174 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
175 long len)
176 {
177 int ret;
178 const unsigned char *p;
179 RSA *rsa;
180
181 p = d;
182 if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
183 ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
184 return 0;
185 }
186
187 ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
188 RSA_free(rsa);
189 return ret;
190 }
191