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