compat.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* vim: set tabstop=8 shiftwidth=4 softtabstop=4 expandtab smarttab colorcolumn=80: */
  2. /*
  3. * Copyright 2016 Red Hat, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include "compat.h"
  18. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  19. const unsigned char *
  20. EVP_PKEY_get0_hmac(EVP_PKEY *pkey, size_t *len)
  21. {
  22. ASN1_OCTET_STRING *os = NULL;
  23. if (EVP_PKEY_base_id(pkey) != EVP_PKEY_HMAC)
  24. return NULL;
  25. os = EVP_PKEY_get0(pkey);
  26. *len = os->length;
  27. return os->data;
  28. }
  29. void
  30. RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
  31. {
  32. if (n)
  33. *n = r->n;
  34. if (e)
  35. *e = r->e;
  36. if (d)
  37. *d = r->d;
  38. }
  39. void
  40. RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
  41. {
  42. if (p)
  43. *p = r->p;
  44. if (q)
  45. *q = r->q;
  46. }
  47. void
  48. RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
  49. const BIGNUM **iqmp)
  50. {
  51. if (dmp1)
  52. *dmp1 = r->dmp1;
  53. if (dmq1)
  54. *dmq1 = r->dmq1;
  55. if (iqmp)
  56. *iqmp = r->iqmp;
  57. }
  58. RSA *
  59. EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
  60. {
  61. if (pkey->type != EVP_PKEY_RSA)
  62. return NULL;
  63. return pkey->pkey.rsa;
  64. }
  65. EC_KEY *
  66. EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
  67. {
  68. if (pkey->type != EVP_PKEY_EC)
  69. return NULL;
  70. return pkey->pkey.ec;
  71. }
  72. int
  73. RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
  74. {
  75. if (!r->n && !n)
  76. return 0;
  77. if (!r->e && !e)
  78. return 0;
  79. if (n) {
  80. BN_free(r->n);
  81. r->n = n;
  82. }
  83. if (e) {
  84. BN_free(r->e);
  85. r->e = e;
  86. }
  87. if (d) {
  88. BN_free(r->d);
  89. r->d = d;
  90. }
  91. return 1;
  92. }
  93. int
  94. RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
  95. {
  96. if (!r->p && !p)
  97. return 0;
  98. if (!r->q && !q)
  99. return 0;
  100. if (p) {
  101. BN_free(r->p);
  102. r->p = p;
  103. }
  104. if (q) {
  105. BN_free(r->q);
  106. r->q = q;
  107. }
  108. return 1;
  109. }
  110. int
  111. RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
  112. {
  113. if (!r->dmp1 && !dmp1)
  114. return 0;
  115. if (!r->dmq1 && !dmq1)
  116. return 0;
  117. if (!r->iqmp && !iqmp)
  118. return 0;
  119. if (dmp1) {
  120. BN_free(r->dmp1);
  121. r->dmp1 = dmp1;
  122. }
  123. if (dmq1) {
  124. BN_free(r->dmq1);
  125. r->dmq1 = dmq1;
  126. }
  127. if (iqmp) {
  128. BN_free(r->iqmp);
  129. r->iqmp = iqmp;
  130. }
  131. return 1;
  132. }
  133. EVP_MD_CTX *
  134. EVP_MD_CTX_new(void)
  135. {
  136. EVP_MD_CTX *cfg = OPENSSL_malloc(sizeof(EVP_MD_CTX));
  137. if (!cfg)
  138. return NULL;
  139. EVP_MD_CTX_init(cfg);
  140. return cfg;
  141. }
  142. void
  143. EVP_MD_CTX_free(EVP_MD_CTX *cfg)
  144. {
  145. if (!cfg)
  146. return;
  147. EVP_MD_CTX_cleanup(cfg);
  148. OPENSSL_free(cfg);
  149. }
  150. void
  151. ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
  152. {
  153. if (pr)
  154. *pr = sig->r;
  155. if (ps)
  156. *ps = sig->s;
  157. }
  158. int
  159. ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
  160. {
  161. if (!r || !s)
  162. return 0;
  163. BN_clear_free(sig->r);
  164. BN_clear_free(sig->s);
  165. sig->r = r;
  166. sig->s = s;
  167. return 1;
  168. }
  169. HMAC_CTX *
  170. HMAC_CTX_new(void)
  171. {
  172. HMAC_CTX *cfg = OPENSSL_malloc(sizeof(HMAC_CTX));
  173. if (!cfg)
  174. return NULL;
  175. HMAC_CTX_init(cfg);
  176. return cfg;
  177. }
  178. const EVP_MD *
  179. HMAC_CTX_get_md(const HMAC_CTX *ctx)
  180. {
  181. return ctx->md;
  182. }
  183. void
  184. HMAC_CTX_free(HMAC_CTX *cfg)
  185. {
  186. if (!cfg)
  187. return;
  188. HMAC_CTX_cleanup(cfg);
  189. OPENSSL_free(cfg);
  190. }
  191. #endif