2
0

ssl_rsa_legacy.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. /* We need to use the deprecated RSA low level calls */
  10. #define OPENSSL_SUPPRESS_DEPRECATED
  11. #include <openssl/err.h>
  12. #include <openssl/rsa.h>
  13. #include <openssl/ssl.h>
  14. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa)
  15. {
  16. EVP_PKEY *pkey;
  17. int ret;
  18. if (rsa == NULL) {
  19. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  20. return 0;
  21. }
  22. if ((pkey = EVP_PKEY_new()) == NULL) {
  23. ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
  24. return 0;
  25. }
  26. RSA_up_ref(rsa);
  27. if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
  28. RSA_free(rsa);
  29. EVP_PKEY_free(pkey);
  30. return 0;
  31. }
  32. ret = SSL_use_PrivateKey(ssl, pkey);
  33. EVP_PKEY_free(pkey);
  34. return ret;
  35. }
  36. int SSL_use_RSAPrivateKey_file(SSL *ssl, const char *file, int type)
  37. {
  38. int j, ret = 0;
  39. BIO *in;
  40. RSA *rsa = NULL;
  41. in = BIO_new(BIO_s_file());
  42. if (in == NULL) {
  43. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  44. goto end;
  45. }
  46. if (BIO_read_filename(in, file) <= 0) {
  47. ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
  48. goto end;
  49. }
  50. if (type == SSL_FILETYPE_ASN1) {
  51. j = ERR_R_ASN1_LIB;
  52. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  53. } else if (type == SSL_FILETYPE_PEM) {
  54. j = ERR_R_PEM_LIB;
  55. rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
  56. SSL_get_default_passwd_cb(ssl),
  57. SSL_get_default_passwd_cb_userdata(ssl));
  58. } else {
  59. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
  60. goto end;
  61. }
  62. if (rsa == NULL) {
  63. ERR_raise(ERR_LIB_SSL, j);
  64. goto end;
  65. }
  66. ret = SSL_use_RSAPrivateKey(ssl, rsa);
  67. RSA_free(rsa);
  68. end:
  69. BIO_free(in);
  70. return ret;
  71. }
  72. int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const unsigned char *d, long len)
  73. {
  74. int ret;
  75. const unsigned char *p;
  76. RSA *rsa;
  77. p = d;
  78. if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
  79. ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
  80. return 0;
  81. }
  82. ret = SSL_use_RSAPrivateKey(ssl, rsa);
  83. RSA_free(rsa);
  84. return ret;
  85. }
  86. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa)
  87. {
  88. int ret;
  89. EVP_PKEY *pkey;
  90. if (rsa == NULL) {
  91. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  92. return 0;
  93. }
  94. if ((pkey = EVP_PKEY_new()) == NULL) {
  95. ERR_raise(ERR_LIB_SSL, ERR_R_EVP_LIB);
  96. return 0;
  97. }
  98. RSA_up_ref(rsa);
  99. if (EVP_PKEY_assign_RSA(pkey, rsa) <= 0) {
  100. RSA_free(rsa);
  101. EVP_PKEY_free(pkey);
  102. return 0;
  103. }
  104. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  105. EVP_PKEY_free(pkey);
  106. return ret;
  107. }
  108. int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX *ctx, const char *file, int type)
  109. {
  110. int j, ret = 0;
  111. BIO *in;
  112. RSA *rsa = NULL;
  113. in = BIO_new(BIO_s_file());
  114. if (in == NULL) {
  115. ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
  116. goto end;
  117. }
  118. if (BIO_read_filename(in, file) <= 0) {
  119. ERR_raise(ERR_LIB_SSL, ERR_R_SYS_LIB);
  120. goto end;
  121. }
  122. if (type == SSL_FILETYPE_ASN1) {
  123. j = ERR_R_ASN1_LIB;
  124. rsa = d2i_RSAPrivateKey_bio(in, NULL);
  125. } else if (type == SSL_FILETYPE_PEM) {
  126. j = ERR_R_PEM_LIB;
  127. rsa = PEM_read_bio_RSAPrivateKey(in, NULL,
  128. SSL_CTX_get_default_passwd_cb(ctx),
  129. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  130. } else {
  131. ERR_raise(ERR_LIB_SSL, SSL_R_BAD_SSL_FILETYPE);
  132. goto end;
  133. }
  134. if (rsa == NULL) {
  135. ERR_raise(ERR_LIB_SSL, j);
  136. goto end;
  137. }
  138. ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  139. RSA_free(rsa);
  140. end:
  141. BIO_free(in);
  142. return ret;
  143. }
  144. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const unsigned char *d,
  145. long len)
  146. {
  147. int ret;
  148. const unsigned char *p;
  149. RSA *rsa;
  150. p = d;
  151. if ((rsa = d2i_RSAPrivateKey(NULL, &p, (long)len)) == NULL) {
  152. ERR_raise(ERR_LIB_SSL, ERR_R_ASN1_LIB);
  153. return 0;
  154. }
  155. ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  156. RSA_free(rsa);
  157. return ret;
  158. }