crypto_compat.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include <assert.h>
  2. #include <stddef.h>
  3. #include <openssl/bn.h>
  4. #include <openssl/crypto.h>
  5. #include <openssl/err.h>
  6. #include <openssl/evp.h>
  7. #include <openssl/opensslv.h>
  8. #include <openssl/rsa.h>
  9. #include "warnp.h"
  10. #include "crypto_compat.h"
  11. #ifndef OPENSSL_VERSION_NUMBER
  12. #error "OPENSSL_VERSION_NUMBER must be defined"
  13. #endif
  14. /* LibreSSL compatibility. */
  15. #ifdef LIBRESSL_VERSION_NUMBER
  16. /* LibreSSL claims to be OpenSSL 2.0; ignore that. */
  17. #undef OPENSSL_VERSION_NUMBER
  18. #if LIBRESSL_VERSION_NUMBER >= 0x2070000fL
  19. /* Compatibility for LibreSSL 2.7.0+: pretend to be OpenSSL 1.1.0. */
  20. #define OPENSSL_VERSION_NUMBER 0x1010000fL
  21. #else
  22. /* Compatibility for LibreSSL before 2.7.0: pretend to be OpenSSL 1.0.1g. */
  23. #define OPENSSL_VERSION_NUMBER 0x1000107fL
  24. #endif
  25. #endif /* LIBRESSL_VERSION_NUMBER */
  26. /**
  27. * crypto_compat_RSA_valid_size(key):
  28. * Return nonzero if ${key} has a valid size, and zero for an invalid size.
  29. */
  30. int
  31. crypto_compat_RSA_valid_size(const RSA * const key)
  32. {
  33. /* Sanity checks. */
  34. assert(key != NULL);
  35. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  36. assert(key->n != NULL);
  37. return ((RSA_size(key) == 256) && (BN_num_bits(key->n) == 2048));
  38. #else
  39. return ((RSA_size(key) == 256) && (RSA_bits(key) == 2048));
  40. #endif
  41. }
  42. /**
  43. * crypto_compat_RSA_import(key, n, e, d, p, q, dmp1, dmq1, iqmp):
  44. * Import the given BIGNUMs into the RSA ${key}. If this function fails,
  45. * free any any BIGNUMs which have not been imported into the ${key}, but do
  46. * not free the ${key} itself.
  47. */
  48. int
  49. crypto_compat_RSA_import(RSA * key, BIGNUM * n, BIGNUM * e, BIGNUM * d,
  50. BIGNUM * p, BIGNUM * q, BIGNUM * dmp1, BIGNUM * dmq1, BIGNUM * iqmp)
  51. {
  52. /* Sanity checks. */
  53. assert(key != NULL);
  54. assert((n != NULL) && (e != NULL));
  55. /* All the private-key-related variables are NULL, or they're not. */
  56. if (d == NULL) {
  57. assert((p == NULL) && (q == NULL) && (dmp1 == NULL)
  58. && (dmq1 == NULL) && (iqmp == NULL));
  59. } else {
  60. assert((p != NULL) && (q != NULL) && (dmp1 != NULL)
  61. && (dmq1 != NULL) && (iqmp != NULL));
  62. }
  63. /* Put values into RSA key. */
  64. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  65. key->n = n;
  66. key->e = e;
  67. if (d != NULL) {
  68. /* Private key. */
  69. key->d = d;
  70. key->p = p;
  71. key->q = q;
  72. key->dmp1 = dmp1;
  73. key->dmq1 = dmq1;
  74. key->iqmp = iqmp;
  75. }
  76. #else
  77. /* Do we have a public key, or private key? */
  78. if (d == NULL) {
  79. /* We could use d here, but using NULL makes it more clear. */
  80. if (RSA_set0_key(key, n, e, NULL) != 1)
  81. goto err3;
  82. } else {
  83. /* Private key. */
  84. if (RSA_set0_key(key, n, e, d) != 1)
  85. goto err3;
  86. if (RSA_set0_factors(key, p, q) != 1)
  87. goto err2;
  88. if (RSA_set0_crt_params(key, dmp1, dmq1, iqmp) != 1)
  89. goto err1;
  90. }
  91. #endif
  92. /* Success! */
  93. return (0);
  94. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  95. #else
  96. err3:
  97. BN_free(n);
  98. BN_free(e);
  99. BN_clear_free(d);
  100. err2:
  101. BN_clear_free(p);
  102. BN_clear_free(q);
  103. err1:
  104. BN_clear_free(dmp1);
  105. BN_clear_free(dmq1);
  106. BN_clear_free(iqmp);
  107. /* Failure! */
  108. return (-1);
  109. #endif
  110. }
  111. /**
  112. * crypto_compat_RSA_export(key, n, e, d, p, q, dmp1, dmq1, iqmp):
  113. * Export values from the given RSA ${key} into the BIGNUMs. ${n} and ${e}
  114. * must be non-NULL; the other values may be NULL if desired, and will
  115. * therefore not be exported.
  116. */
  117. int
  118. crypto_compat_RSA_export(RSA * key, const BIGNUM ** n, const BIGNUM ** e,
  119. const BIGNUM ** d, const BIGNUM ** p, const BIGNUM ** q,
  120. const BIGNUM ** dmp1, const BIGNUM ** dmq1, const BIGNUM ** iqmp)
  121. {
  122. /* Sanity checks. */
  123. assert(key != NULL);
  124. assert((n != NULL) && (e != NULL));
  125. /* All the private-key-related variables are NULL, or they're not. */
  126. if (d == NULL) {
  127. assert((p == NULL) && (q == NULL) && (dmp1 == NULL)
  128. && (dmq1 == NULL) && (iqmp == NULL));
  129. } else {
  130. assert((p != NULL) && (q != NULL) && (dmp1 != NULL)
  131. && (dmq1 != NULL) && (iqmp != NULL));
  132. }
  133. /* Get values from RSA key. */
  134. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  135. *n = key->n;
  136. *e = key->e;
  137. if (d != NULL) {
  138. /* Private key. */
  139. *d = key->d;
  140. *p = key->p;
  141. *q = key->q;
  142. *dmp1 = key->dmp1;
  143. *dmq1 = key->dmq1;
  144. *iqmp = key->iqmp;
  145. }
  146. #else
  147. /* Do we have a public key, or private key? */
  148. if (d == NULL) {
  149. /* We could use d here, but using NULL makes it more clear. */
  150. RSA_get0_key(key, n, e, NULL);
  151. } else {
  152. /* Private key. */
  153. RSA_get0_key(key, n, e, d);
  154. RSA_get0_factors(key, p, q);
  155. RSA_get0_crt_params(key, dmp1, dmq1, iqmp);
  156. }
  157. #endif
  158. /* Success! */
  159. return (0);
  160. }
  161. /**
  162. * crypto_compat_RSA_generate_key(void):
  163. * Generate a key pair.
  164. */
  165. RSA *
  166. crypto_compat_RSA_generate_key(void)
  167. {
  168. RSA * key;
  169. #if OPENSSL_VERSION_NUMBER < 0x00908000L
  170. /* Generate key. */
  171. if ((key = RSA_generate_key(2048, 65537, NULL, NULL)) == NULL) {
  172. warn0("%s", ERR_error_string(ERR_get_error(), NULL));
  173. goto err0;
  174. }
  175. /* Success! */
  176. return (key);
  177. #else
  178. BIGNUM * e;
  179. /* Set up parameter. */
  180. if ((e = BN_new()) == NULL) {
  181. warn0("%s", ERR_error_string(ERR_get_error(), NULL));
  182. goto err0;
  183. }
  184. BN_set_word(e, 65537);
  185. /* Generate key. */
  186. if ((key = RSA_new()) == NULL) {
  187. warn0("%s", ERR_error_string(ERR_get_error(), NULL));
  188. goto err1;
  189. }
  190. if (RSA_generate_key_ex(key, 2048, e, NULL) != 1) {
  191. warn0("%s", ERR_error_string(ERR_get_error(), NULL));
  192. goto err2;
  193. }
  194. /* Clean up. */
  195. BN_free(e);
  196. /* Success! */
  197. return (key);
  198. err2:
  199. RSA_free(key);
  200. err1:
  201. BN_free(e);
  202. #endif
  203. err0:
  204. /* Failure! */
  205. return (NULL);
  206. }
  207. /**
  208. * crypto_compat_free(void):
  209. * Free the shared memory allocated by OpenSSL.
  210. */
  211. void
  212. crypto_compat_free(void)
  213. {
  214. #if !defined(LIBRESSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER > 0x1010000fL
  215. /* OpenSSL 1.1.0 and higher: do nothing; the library uses atexit(). */
  216. #elif defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER > 0x3070100fL
  217. /* LibreSSL 3.7.1 and higher. */
  218. OPENSSL_cleanup();
  219. #elif defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER > 0x3060000fL
  220. /* LibreSSL 3.6.0 to 3.7.0. */
  221. OPENSSL_cleanup();
  222. CRYPTO_cleanup_all_ex_data();
  223. #else
  224. /* Earlier versions of OpenSSL and LibreSSL. */
  225. /* Free OpenSSL error queue. */
  226. #if OPENSSL_VERSION_NUMBER < 0x10000000L
  227. ERR_remove_state(0);
  228. #elif OPENSSL_VERSION_NUMBER < 0x10100000L
  229. ERR_remove_thread_state(NULL);
  230. #endif
  231. /* Free OpenSSL error strings. */
  232. ERR_free_strings();
  233. #if LIBRESSL_VERSION_NUMBER >= 0x2070000fL
  234. /* Additional cleaning needed for LibreSSL 2.7.0 to 3.5.x. */
  235. EVP_cleanup();
  236. #endif
  237. /* A more general OpenSSL cleanup function. */
  238. CRYPTO_cleanup_all_ex_data();
  239. #endif
  240. }