| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include "libcrypto-compat.h" |
| 11 |
#if OPENSSL_VERSION_NUMBER < 0x10100000L |
| 12 |
#include <string.h> |
| 13 |
#include <openssl/engine.h> |
| 14 |
|
| 15 |
static void *OPENSSL_zalloc(size_t num) |
| 16 |
{ |
| 17 |
void *ret = OPENSSL_malloc(num); |
| 18 |
|
| 19 |
if (ret != NULL) |
| 20 |
memset(ret, 0, num); |
| 21 |
return ret; |
| 22 |
} |
| 23 |
|
| 24 |
int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) |
| 25 |
{ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
if ((r->n == NULL && n == NULL) |
| 31 |
|| (r->e == NULL && e == NULL)) |
| 32 |
return 0; |
| 33 |
|
| 34 |
if (n != NULL) { |
| 35 |
BN_free(r->n); |
| 36 |
r->n = n; |
| 37 |
} |
| 38 |
if (e != NULL) { |
| 39 |
BN_free(r->e); |
| 40 |
r->e = e; |
| 41 |
} |
| 42 |
if (d != NULL) { |
| 43 |
BN_free(r->d); |
| 44 |
r->d = d; |
| 45 |
} |
| 46 |
|
| 47 |
return 1; |
| 48 |
} |
| 49 |
|
| 50 |
int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) |
| 51 |
{ |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
if ((r->p == NULL && p == NULL) |
| 56 |
|| (r->q == NULL && q == NULL)) |
| 57 |
return 0; |
| 58 |
|
| 59 |
if (p != NULL) { |
| 60 |
BN_free(r->p); |
| 61 |
r->p = p; |
| 62 |
} |
| 63 |
if (q != NULL) { |
| 64 |
BN_free(r->q); |
| 65 |
r->q = q; |
| 66 |
} |
| 67 |
|
| 68 |
return 1; |
| 69 |
} |
| 70 |
|
| 71 |
int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) |
| 72 |
{ |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
if ((r->dmp1 == NULL && dmp1 == NULL) |
| 77 |
|| (r->dmq1 == NULL && dmq1 == NULL) |
| 78 |
|| (r->iqmp == NULL && iqmp == NULL)) |
| 79 |
return 0; |
| 80 |
|
| 81 |
if (dmp1 != NULL) { |
| 82 |
BN_free(r->dmp1); |
| 83 |
r->dmp1 = dmp1; |
| 84 |
} |
| 85 |
if (dmq1 != NULL) { |
| 86 |
BN_free(r->dmq1); |
| 87 |
r->dmq1 = dmq1; |
| 88 |
} |
| 89 |
if (iqmp != NULL) { |
| 90 |
BN_free(r->iqmp); |
| 91 |
r->iqmp = iqmp; |
| 92 |
} |
| 93 |
|
| 94 |
return 1; |
| 95 |
} |
| 96 |
|
| 97 |
void RSA_get0_key(const RSA *r, |
| 98 |
const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) |
| 99 |
{ |
| 100 |
if (n != NULL) |
| 101 |
*n = r->n; |
| 102 |
if (e != NULL) |
| 103 |
*e = r->e; |
| 104 |
if (d != NULL) |
| 105 |
*d = r->d; |
| 106 |
} |
| 107 |
|
| 108 |
void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) |
| 109 |
{ |
| 110 |
if (p != NULL) |
| 111 |
*p = r->p; |
| 112 |
if (q != NULL) |
| 113 |
*q = r->q; |
| 114 |
} |
| 115 |
|
| 116 |
void RSA_get0_crt_params(const RSA *r, |
| 117 |
const BIGNUM **dmp1, const BIGNUM **dmq1, |
| 118 |
const BIGNUM **iqmp) |
| 119 |
{ |
| 120 |
if (dmp1 != NULL) |
| 121 |
*dmp1 = r->dmp1; |
| 122 |
if (dmq1 != NULL) |
| 123 |
*dmq1 = r->dmq1; |
| 124 |
if (iqmp != NULL) |
| 125 |
*iqmp = r->iqmp; |
| 126 |
} |
| 127 |
|
| 128 |
void DSA_get0_pqg(const DSA *d, |
| 129 |
const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) |
| 130 |
{ |
| 131 |
if (p != NULL) |
| 132 |
*p = d->p; |
| 133 |
if (q != NULL) |
| 134 |
*q = d->q; |
| 135 |
if (g != NULL) |
| 136 |
*g = d->g; |
| 137 |
} |
| 138 |
|
| 139 |
int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) |
| 140 |
{ |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if ((d->p == NULL && p == NULL) |
| 145 |
|| (d->q == NULL && q == NULL) |
| 146 |
|| (d->g == NULL && g == NULL)) |
| 147 |
return 0; |
| 148 |
|
| 149 |
if (p != NULL) { |
| 150 |
BN_free(d->p); |
| 151 |
d->p = p; |
| 152 |
} |
| 153 |
if (q != NULL) { |
| 154 |
BN_free(d->q); |
| 155 |
d->q = q; |
| 156 |
} |
| 157 |
if (g != NULL) { |
| 158 |
BN_free(d->g); |
| 159 |
d->g = g; |
| 160 |
} |
| 161 |
|
| 162 |
return 1; |
| 163 |
} |
| 164 |
|
| 165 |
void DSA_get0_key(const DSA *d, |
| 166 |
const BIGNUM **pub_key, const BIGNUM **priv_key) |
| 167 |
{ |
| 168 |
if (pub_key != NULL) |
| 169 |
*pub_key = d->pub_key; |
| 170 |
if (priv_key != NULL) |
| 171 |
*priv_key = d->priv_key; |
| 172 |
} |
| 173 |
|
| 174 |
int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key) |
| 175 |
{ |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
if (d->pub_key == NULL && pub_key == NULL) |
| 181 |
return 0; |
| 182 |
|
| 183 |
if (pub_key != NULL) { |
| 184 |
BN_free(d->pub_key); |
| 185 |
d->pub_key = pub_key; |
| 186 |
} |
| 187 |
if (priv_key != NULL) { |
| 188 |
BN_free(d->priv_key); |
| 189 |
d->priv_key = priv_key; |
| 190 |
} |
| 191 |
|
| 192 |
return 1; |
| 193 |
} |
| 194 |
|
| 195 |
void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) |
| 196 |
{ |
| 197 |
if (pr != NULL) |
| 198 |
*pr = sig->r; |
| 199 |
if (ps != NULL) |
| 200 |
*ps = sig->s; |
| 201 |
} |
| 202 |
|
| 203 |
int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s) |
| 204 |
{ |
| 205 |
if (r == NULL || s == NULL) |
| 206 |
return 0; |
| 207 |
BN_clear_free(sig->r); |
| 208 |
BN_clear_free(sig->s); |
| 209 |
sig->r = r; |
| 210 |
sig->s = s; |
| 211 |
return 1; |
| 212 |
} |
| 213 |
|
| 214 |
void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) |
| 215 |
{ |
| 216 |
if (pr != NULL) |
| 217 |
*pr = sig->r; |
| 218 |
if (ps != NULL) |
| 219 |
*ps = sig->s; |
| 220 |
} |
| 221 |
|
| 222 |
int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) |
| 223 |
{ |
| 224 |
if (r == NULL || s == NULL) |
| 225 |
return 0; |
| 226 |
BN_clear_free(sig->r); |
| 227 |
BN_clear_free(sig->s); |
| 228 |
sig->r = r; |
| 229 |
sig->s = s; |
| 230 |
return 1; |
| 231 |
} |
| 232 |
|
| 233 |
EVP_MD_CTX *EVP_MD_CTX_new(void) |
| 234 |
{ |
| 235 |
return OPENSSL_zalloc(sizeof(EVP_MD_CTX)); |
| 236 |
} |
| 237 |
|
| 238 |
static void OPENSSL_clear_free(void *str, size_t num) |
| 239 |
{ |
| 240 |
if (str == NULL) |
| 241 |
return; |
| 242 |
if (num) |
| 243 |
OPENSSL_cleanse(str, num); |
| 244 |
OPENSSL_free(str); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
int EVP_MD_CTX_reset(EVP_MD_CTX *ctx) |
| 249 |
{ |
| 250 |
if (ctx == NULL) |
| 251 |
return 1; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
if (ctx->digest && ctx->digest->cleanup |
| 258 |
&& !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED)) |
| 259 |
ctx->digest->cleanup(ctx); |
| 260 |
if (ctx->digest && ctx->digest->ctx_size && ctx->md_data |
| 261 |
&& !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) { |
| 262 |
OPENSSL_clear_free(ctx->md_data, ctx->digest->ctx_size); |
| 263 |
} |
| 264 |
EVP_PKEY_CTX_free(ctx->pctx); |
| 265 |
#ifndef OPENSSL_NO_ENGINE |
| 266 |
ENGINE_finish(ctx->engine); |
| 267 |
#endif |
| 268 |
OPENSSL_cleanse(ctx, sizeof(*ctx)); |
| 269 |
|
| 270 |
return 1; |
| 271 |
} |
| 272 |
|
| 273 |
void EVP_MD_CTX_free(EVP_MD_CTX *ctx) |
| 274 |
{ |
| 275 |
EVP_MD_CTX_reset(ctx); |
| 276 |
OPENSSL_free(ctx); |
| 277 |
} |
| 278 |
|
| 279 |
HMAC_CTX *HMAC_CTX_new(void) |
| 280 |
{ |
| 281 |
HMAC_CTX *ctx = OPENSSL_zalloc(sizeof(HMAC_CTX)); |
| 282 |
|
| 283 |
if (ctx != NULL) { |
| 284 |
if (!HMAC_CTX_reset(ctx)) { |
| 285 |
HMAC_CTX_free(ctx); |
| 286 |
return NULL; |
| 287 |
} |
| 288 |
} |
| 289 |
return ctx; |
| 290 |
} |
| 291 |
|
| 292 |
static void hmac_ctx_cleanup(HMAC_CTX *ctx) |
| 293 |
{ |
| 294 |
EVP_MD_CTX_reset(&ctx->i_ctx); |
| 295 |
EVP_MD_CTX_reset(&ctx->o_ctx); |
| 296 |
EVP_MD_CTX_reset(&ctx->md_ctx); |
| 297 |
ctx->md = NULL; |
| 298 |
ctx->key_length = 0; |
| 299 |
OPENSSL_cleanse(ctx->key, sizeof(ctx->key)); |
| 300 |
} |
| 301 |
|
| 302 |
void HMAC_CTX_free(HMAC_CTX *ctx) |
| 303 |
{ |
| 304 |
if (ctx != NULL) { |
| 305 |
hmac_ctx_cleanup(ctx); |
| 306 |
EVP_MD_CTX_free(&ctx->i_ctx); |
| 307 |
EVP_MD_CTX_free(&ctx->o_ctx); |
| 308 |
EVP_MD_CTX_free(&ctx->md_ctx); |
| 309 |
OPENSSL_free(ctx); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
int HMAC_CTX_reset(HMAC_CTX *ctx) |
| 314 |
{ |
| 315 |
HMAC_CTX_init(ctx); |
| 316 |
return 1; |
| 317 |
} |
| 318 |
|
| 319 |
EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void) |
| 320 |
{ |
| 321 |
return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX)); |
| 322 |
} |
| 323 |
|
| 324 |
void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx) |
| 325 |
{ |
| 326 |
|
| 327 |
EVP_CIPHER_CTX_init(ctx); |
| 328 |
OPENSSL_free(ctx); |
| 329 |
} |
| 330 |
#endif |