2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* We need to use the deprecated RSA low level calls */
11 #define OPENSSL_SUPPRESS_DEPRECATED
13 #include <openssl/err.h>
14 #include <openssl/rsa.h>
15 #include <openssl/ssl.h>
17 int SSL_use_RSAPrivateKey(SSL
*ssl
, RSA
*rsa
)
23 ERR_raise(ERR_LIB_SSL
, ERR_R_PASSED_NULL_PARAMETER
);
26 if ((pkey
= EVP_PKEY_new()) == NULL
) {
27 ERR_raise(ERR_LIB_SSL
, ERR_R_EVP_LIB
);
32 if (EVP_PKEY_assign_RSA(pkey
, rsa
) <= 0) {
38 ret
= SSL_use_PrivateKey(ssl
, pkey
);
43 int SSL_use_RSAPrivateKey_file(SSL
*ssl
, const char *file
, int type
)
49 in
= BIO_new(BIO_s_file());
51 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
55 if (BIO_read_filename(in
, file
) <= 0) {
56 ERR_raise(ERR_LIB_SSL
, ERR_R_SYS_LIB
);
59 if (type
== SSL_FILETYPE_ASN1
) {
61 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
62 } else if (type
== SSL_FILETYPE_PEM
) {
64 rsa
= PEM_read_bio_RSAPrivateKey(in
, NULL
,
65 SSL_get_default_passwd_cb(ssl
),
66 SSL_get_default_passwd_cb_userdata(ssl
));
68 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_SSL_FILETYPE
);
72 ERR_raise(ERR_LIB_SSL
, j
);
75 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
82 int SSL_use_RSAPrivateKey_ASN1(SSL
*ssl
, const unsigned char *d
, long len
)
85 const unsigned char *p
;
89 if ((rsa
= d2i_RSAPrivateKey(NULL
, &p
, (long)len
)) == NULL
) {
90 ERR_raise(ERR_LIB_SSL
, ERR_R_ASN1_LIB
);
94 ret
= SSL_use_RSAPrivateKey(ssl
, rsa
);
99 int SSL_CTX_use_RSAPrivateKey(SSL_CTX
*ctx
, RSA
*rsa
)
105 ERR_raise(ERR_LIB_SSL
, ERR_R_PASSED_NULL_PARAMETER
);
108 if ((pkey
= EVP_PKEY_new()) == NULL
) {
109 ERR_raise(ERR_LIB_SSL
, ERR_R_EVP_LIB
);
114 if (EVP_PKEY_assign_RSA(pkey
, rsa
) <= 0) {
120 ret
= SSL_CTX_use_PrivateKey(ctx
, pkey
);
125 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
131 in
= BIO_new(BIO_s_file());
133 ERR_raise(ERR_LIB_SSL
, ERR_R_BUF_LIB
);
137 if (BIO_read_filename(in
, file
) <= 0) {
138 ERR_raise(ERR_LIB_SSL
, ERR_R_SYS_LIB
);
141 if (type
== SSL_FILETYPE_ASN1
) {
143 rsa
= d2i_RSAPrivateKey_bio(in
, NULL
);
144 } else if (type
== SSL_FILETYPE_PEM
) {
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
));
150 ERR_raise(ERR_LIB_SSL
, SSL_R_BAD_SSL_FILETYPE
);
154 ERR_raise(ERR_LIB_SSL
, j
);
157 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);
164 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX
*ctx
, const unsigned char *d
,
168 const unsigned char *p
;
172 if ((rsa
= d2i_RSAPrivateKey(NULL
, &p
, (long)len
)) == NULL
) {
173 ERR_raise(ERR_LIB_SSL
, ERR_R_ASN1_LIB
);
177 ret
= SSL_CTX_use_RSAPrivateKey(ctx
, rsa
);