target
int64
0
1
func
stringlengths
0
484k
idx
int64
1
378k
0
void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords) { BN_ULONG t; int i; bn_wcheck_size(a, nwords); bn_wcheck_size(b, nwords); assert(a != b); assert((condition & (condition - 1)) == 0); assert(sizeof(BN_ULONG) >= sizeof(int)); condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1; t = (a->top ^ b->top) & condition; a->top ^= t; b->top ^= t; t = (a->neg ^ b->neg) & condition; a->neg ^= t; b->neg ^= t; /* * cannot just arbitrarily swap flags. * The way a->d is allocated etc. * BN_FLG_MALLOCED, BN_FLG_STATIC_DATA, ... */ t = (a->flags ^ b->flags) & condition & BN_FLG_CONSTTIME; a->flags ^= t; b->flags ^= t; #define BN_CONSTTIME_SWAP(ind) \ do { \ t = (a->d[ind] ^ b->d[ind]) & condition; \ a->d[ind] ^= t; \ b->d[ind] ^= t; \ } while (0) switch (nwords) { default: for (i = 10; i < nwords; i++) BN_CONSTTIME_SWAP(i); /* Fallthrough */ case 10: BN_CONSTTIME_SWAP(9); /* Fallthrough */ case 9: BN_CONSTTIME_SWAP(8); /* Fallthrough */ case 8: BN_CONSTTIME_SWAP(7); /* Fallthrough */ case 7: BN_CONSTTIME_SWAP(6); /* Fallthrough */ case 6: BN_CONSTTIME_SWAP(5); /* Fallthrough */ case 5: BN_CONSTTIME_SWAP(4); /* Fallthrough */ case 4: BN_CONSTTIME_SWAP(3); /* Fallthrough */ case 3: BN_CONSTTIME_SWAP(2); /* Fallthrough */ case 2: BN_CONSTTIME_SWAP(1); /* Fallthrough */ case 1: BN_CONSTTIME_SWAP(0); } #undef BN_CONSTTIME_SWAP }
377,370
0
BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) { int i; BN_ULONG *A; const BN_ULONG *B; bn_check_top(b); if (a == b) return (a); if (bn_wexpand(a, b->top) == NULL) return (NULL); #if 1 A = a->d; B = b->d; for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { BN_ULONG a0, a1, a2, a3; a0 = B[0]; a1 = B[1]; a2 = B[2]; a3 = B[3]; A[0] = a0; A[1] = a1; A[2] = a2; A[3] = a3; } /* ultrix cc workaround, see comments in bn_expand_internal */ switch (b->top & 3) { case 3: A[2] = B[2]; /* fall thru */ case 2: A[1] = B[1]; /* fall thru */ case 1: A[0] = B[0]; /* fall thru */ case 0:; } #else memcpy(a->d, b->d, sizeof(b->d[0]) * b->top); #endif a->top = b->top; a->neg = b->neg; bn_check_top(a); return (a); }
377,371
0
void BN_clear(BIGNUM *a) { bn_check_top(a); if (a->d != NULL) OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax); a->top = 0; a->neg = 0; }
377,372
0
BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret) { unsigned int i, m; unsigned int n; BN_ULONG l; BIGNUM *bn = NULL; if (ret == NULL) ret = bn = BN_new(); if (ret == NULL) return (NULL); bn_check_top(ret); /* Skip leading zero's. */ for ( ; len > 0 && *s == 0; s++, len--) continue; n = len; if (n == 0) { ret->top = 0; return (ret); } i = ((n - 1) / BN_BYTES) + 1; m = ((n - 1) % (BN_BYTES)); if (bn_wexpand(ret, (int)i) == NULL) { BN_free(bn); return NULL; } ret->top = i; ret->neg = 0; l = 0; while (n--) { l = (l << 8L) | *(s++); if (m-- == 0) { ret->d[--i] = l; l = 0; m = BN_BYTES - 1; } } /* * need to call this due to clear byte at top if avoiding having the top * bit set (-ve number) */ bn_correct_top(ret); return (ret); }
377,373
0
int BN_set_word(BIGNUM *a, BN_ULONG w) { bn_check_top(a); if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL) return (0); a->neg = 0; a->d[0] = w; a->top = (w ? 1 : 0); bn_check_top(a); return (1); }
377,376
0
int BN_get_flags(const BIGNUM *b, int n) { return b->flags & n; }
377,378
0
static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) { BN_ULONG *A, *a = NULL; const BN_ULONG *B; int i; bn_check_top(b); if (words > (INT_MAX / (4 * BN_BITS2))) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG); return NULL; } if (BN_get_flags(b, BN_FLG_STATIC_DATA)) { BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); return (NULL); } if (BN_get_flags(b, BN_FLG_SECURE)) a = A = OPENSSL_secure_zalloc(words * sizeof(*a)); else a = A = OPENSSL_zalloc(words * sizeof(*a)); if (A == NULL) { BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE); return (NULL); } #if 1 B = b->d; /* Check if the previous number needs to be copied */ if (B != NULL) { for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) { /* * The fact that the loop is unrolled * 4-wise is a tribute to Intel. It's * the one that doesn't have enough * registers to accommodate more data. * I'd unroll it 8-wise otherwise:-) * * <appro@fy.chalmers.se> */ BN_ULONG a0, a1, a2, a3; a0 = B[0]; a1 = B[1]; a2 = B[2]; a3 = B[3]; A[0] = a0; A[1] = a1; A[2] = a2; A[3] = a3; } switch (b->top & 3) { case 3: A[2] = B[2]; /* fall thru */ case 2: A[1] = B[1]; /* fall thru */ case 1: A[0] = B[0]; /* fall thru */ case 0: /* Without the "case 0" some old optimizers got this wrong. */ ; } } #else memset(A, 0, sizeof(*A) * words); memcpy(A, b->d, sizeof(b->d[0]) * b->top); #endif return (a); }
377,379
0
static void bn_free_d(BIGNUM *a) { if (BN_get_flags(a, BN_FLG_SECURE)) OPENSSL_secure_free(a->d); else OPENSSL_free(a->d); }
377,381
0
void BN_free(BIGNUM *a) { if (a == NULL) return; bn_check_top(a); if (!BN_get_flags(a, BN_FLG_STATIC_DATA)) bn_free_d(a); if (a->flags & BN_FLG_MALLOCED) OPENSSL_free(a); else { #if OPENSSL_API_COMPAT < 0x00908000L a->flags |= BN_FLG_FREE; #endif a->d = NULL; } }
377,382
0
BIGNUM *BN_dup(const BIGNUM *a) { BIGNUM *t; if (a == NULL) return NULL; bn_check_top(a); t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new(); if (t == NULL) return NULL; if (!BN_copy(t, a)) { BN_free(t); return NULL; } bn_check_top(t); return t; }
377,383
0
static int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen) { int i; BN_ULONG l; bn_check_top(a); i = BN_num_bytes(a); if (tolen == -1) tolen = i; else if (tolen < i) return -1; /* Add leading zeroes if necessary */ if (tolen > i) { memset(to, 0, tolen - i); to += tolen - i; } while (i--) { l = a->d[i / BN_BYTES]; *(to++) = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff; } return tolen; }
377,384
0
int BN_set_bit(BIGNUM *a, int n) { int i, j, k; if (n < 0) return 0; i = n / BN_BITS2; j = n % BN_BITS2; if (a->top <= i) { if (bn_wexpand(a, i + 1) == NULL) return (0); for (k = a->top; k < i + 1; k++) a->d[k] = 0; a->top = i + 1; } a->d[i] |= (((BN_ULONG)1) << j); bn_check_top(a); return (1); }
377,385
0
void BN_zero_ex(BIGNUM *a) { a->top = 0; a->neg = 0; }
377,386
0
void BN_GENCB_free(BN_GENCB *cb) { if (cb == NULL) return; OPENSSL_free(cb); }
377,388
0
void BN_GENCB_set(BN_GENCB *gencb, int (*callback) (int, int, BN_GENCB *), void *cb_arg) { BN_GENCB *tmp_gencb = gencb; tmp_gencb->ver = 2; tmp_gencb->arg = cb_arg; tmp_gencb->cb.cb_2 = callback; }
377,389
0
void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback) (int, int, void *), void *cb_arg) { BN_GENCB *tmp_gencb = gencb; tmp_gencb->ver = 1; tmp_gencb->arg = cb_arg; tmp_gencb->cb.cb_1 = callback; }
377,390
0
void bn_correct_top(BIGNUM *a) { BN_ULONG *ftl; int tmp_top = a->top; if (tmp_top > 0) { for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) { ftl--; if (*ftl != 0) break; } a->top = tmp_top; } if (a->top == 0) a->neg = 0; bn_pollute(a); }
377,391
0
void *BN_GENCB_get_arg(BN_GENCB *cb) { return cb->arg; }
377,392
0
static int ec_mul_consttime(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar, const EC_POINT *point, BN_CTX *ctx) { int i, order_bits, group_top, kbit, pbit, Z_is_one, ret; ret = 0; EC_POINT *s = NULL; BIGNUM *k = NULL; BIGNUM *lambda = NULL; BN_CTX *new_ctx = NULL; if (ctx == NULL) if ((ctx = new_ctx = BN_CTX_secure_new()) == NULL) return 0; if ((group->order == NULL) || (group->field == NULL)) goto err; order_bits = BN_num_bits(group->order); s = EC_POINT_new(group); if (s == NULL) goto err; if (point == NULL) { if (group->generator == NULL) goto err; if (!EC_POINT_copy(s, group->generator)) goto err; } else { if (!EC_POINT_copy(s, point)) goto err; } EC_POINT_set_flags(s, BN_FLG_CONSTTIME); BN_CTX_start(ctx); lambda = BN_CTX_get(ctx); k = BN_CTX_get(ctx); if (k == NULL) goto err; /* * Group orders are often on a word boundary. * So when we pad the scalar, some timing diff might * pop if it needs to be expanded due to carries. * So expand ahead of time. */ group_top = bn_get_top(group->order); if ((bn_wexpand(k, group_top + 1) == NULL) || (bn_wexpand(lambda, group_top + 1) == NULL)) goto err; if (!BN_copy(k, scalar)) goto err; BN_set_flags(k, BN_FLG_CONSTTIME); if ((BN_num_bits(k) > order_bits) || (BN_is_negative(k))) { /* * this is an unusual input, and we don't guarantee * constant-timeness */ if(!BN_nnmod(k, k, group->order, ctx)) goto err; } if (!BN_add(lambda, k, group->order)) goto err; BN_set_flags(lambda, BN_FLG_CONSTTIME); if (!BN_add(k, lambda, group->order)) goto err; /* * lambda := scalar + order * k := scalar + 2*order */ kbit = BN_is_bit_set(lambda, order_bits); BN_consttime_swap(kbit, k, lambda, group_top + 1); group_top = bn_get_top(group->field); if ((bn_wexpand(s->X, group_top) == NULL) || (bn_wexpand(s->Y, group_top) == NULL) || (bn_wexpand(s->Z, group_top) == NULL) || (bn_wexpand(r->X, group_top) == NULL) || (bn_wexpand(r->Y, group_top) == NULL) || (bn_wexpand(r->Z, group_top) == NULL)) goto err; /* top bit is a 1, in a fixed pos */ if (!EC_POINT_copy(r, s)) goto err; EC_POINT_set_flags(r, BN_FLG_CONSTTIME); if (!EC_POINT_dbl(group, s, s, ctx)) goto err; pbit = 0; #define EC_POINT_CSWAP(c, a, b, w, t) do { \ BN_consttime_swap(c, (a)->X, (b)->X, w); \ BN_consttime_swap(c, (a)->Y, (b)->Y, w); \ BN_consttime_swap(c, (a)->Z, (b)->Z, w); \ t = ((a)->Z_is_one ^ (b)->Z_is_one) & (c); \ (a)->Z_is_one ^= (t); \ (b)->Z_is_one ^= (t); \ } while(0) for (i = order_bits - 1; i >= 0; i--) { kbit = BN_is_bit_set(k, i) ^ pbit; EC_POINT_CSWAP(kbit, r, s, group_top, Z_is_one); if (!EC_POINT_add(group, s, r, s, ctx)) goto err; if (!EC_POINT_dbl(group, r, r, ctx)) goto err; /* * pbit logic merges this cswap with that of the * next iteration */ pbit ^= kbit; } /* one final cswap to move the right value into r */ EC_POINT_CSWAP(pbit, r, s, group_top, Z_is_one); #undef EC_POINT_CSWAP ret = 1; err: EC_POINT_free(s); BN_CTX_end(ctx); BN_CTX_free(new_ctx); return ret; }
377,393
0
void BN_set_flags(BIGNUM *b, int n) { b->flags |= n; }
377,395
0
BIGNUM *BN_secure_new(void) { BIGNUM *ret = BN_new(); if (ret != NULL) ret->flags |= BN_FLG_SECURE; return (ret); }
377,398
0
int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w) { return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0)); }
377,399
0
BIGNUM *bn_wexpand(BIGNUM *a, int words) { return (words <= a->dmax) ? a : bn_expand2(a, words); }
377,400
0
int BN_is_one(const BIGNUM *a) { return BN_abs_is_word(a, 1) && !a->neg; }
377,401
0
void BN_clear_free(BIGNUM *a) { int i; if (a == NULL) return; bn_check_top(a); if (a->d != NULL) { OPENSSL_cleanse(a->d, a->dmax * sizeof(a->d[0])); if (!BN_get_flags(a, BN_FLG_STATIC_DATA)) bn_free_d(a); } i = BN_get_flags(a, BN_FLG_MALLOCED); OPENSSL_cleanse(a, sizeof(*a)); if (i) OPENSSL_free(a); }
377,402
0
int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont, BN_CTX *ctx) { return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx); }
377,403
0
int BN_is_odd(const BIGNUM *a) { return (a->top > 0) && (a->d[0] & 1); }
377,404
0
int BN_GF2m_mod(BIGNUM *r, const BIGNUM *a, const BIGNUM *p) { int ret = 0; int arr[6]; bn_check_top(a); bn_check_top(p); ret = BN_GF2m_poly2arr(p, arr, OSSL_NELEM(arr)); if (!ret || ret > (int)OSSL_NELEM(arr)) { BNerr(BN_F_BN_GF2M_MOD, BN_R_INVALID_LENGTH); return 0; } ret = BN_GF2m_mod_arr(r, a, arr); bn_check_top(r); return ret; }
377,405
0
int BN_GF2m_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx) { int ret = 0; const int max = BN_num_bits(p) + 1; int *arr = NULL; bn_check_top(a); bn_check_top(b); bn_check_top(p); if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL) goto err; ret = BN_GF2m_poly2arr(p, arr, max); if (!ret || ret > max) { BNerr(BN_F_BN_GF2M_MOD_MUL, BN_R_INVALID_LENGTH); goto err; } ret = BN_GF2m_mod_mul_arr(r, a, b, arr, ctx); bn_check_top(r); err: OPENSSL_free(arr); return ret; }
377,406
0
int BN_GF2m_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *p, BN_CTX *ctx) { int ret = 0; const int max = BN_num_bits(p) + 1; int *arr = NULL; bn_check_top(a); bn_check_top(b); bn_check_top(p); if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL) goto err; ret = BN_GF2m_poly2arr(p, arr, max); if (!ret || ret > max) { BNerr(BN_F_BN_GF2M_MOD_EXP, BN_R_INVALID_LENGTH); goto err; } ret = BN_GF2m_mod_exp_arr(r, a, b, arr, ctx); bn_check_top(r); err: OPENSSL_free(arr); return ret; }
377,407
0
int BN_GF2m_mod_solve_quad(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { int ret = 0; const int max = BN_num_bits(p) + 1; int *arr = NULL; bn_check_top(a); bn_check_top(p); if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL) goto err; ret = BN_GF2m_poly2arr(p, arr, max); if (!ret || ret > max) { BNerr(BN_F_BN_GF2M_MOD_SOLVE_QUAD, BN_R_INVALID_LENGTH); goto err; } ret = BN_GF2m_mod_solve_quad_arr(r, a, arr, ctx); bn_check_top(r); err: OPENSSL_free(arr); return ret; }
377,408
0
int BN_GF2m_mod_sqrt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx) { int ret = 0; const int max = BN_num_bits(p) + 1; int *arr = NULL; bn_check_top(a); bn_check_top(p); if ((arr = OPENSSL_malloc(sizeof(*arr) * max)) == NULL) goto err; ret = BN_GF2m_poly2arr(p, arr, max); if (!ret || ret > max) { BNerr(BN_F_BN_GF2M_MOD_SQRT, BN_R_INVALID_LENGTH); goto err; } ret = BN_GF2m_mod_sqrt_arr(r, a, arr, ctx); bn_check_top(r); err: OPENSSL_free(arr); return ret; }
377,410
0
int BN_GF2m_mod_arr(BIGNUM *r, const BIGNUM *a, const int p[]) { int j, k; int n, dN, d0, d1; BN_ULONG zz, *z; bn_check_top(a); if (!p[0]) { /* reduction mod 1 => return 0 */ BN_zero(r); return 1; } /* * Since the algorithm does reduction in the r value, if a != r, copy the * contents of a into r so we can do reduction in r. */ if (a != r) { if (!bn_wexpand(r, a->top)) return 0; for (j = 0; j < a->top; j++) { r->d[j] = a->d[j]; } r->top = a->top; } z = r->d; /* start reduction */ dN = p[0] / BN_BITS2; for (j = r->top - 1; j > dN;) { zz = z[j]; if (z[j] == 0) { j--; continue; } z[j] = 0; for (k = 1; p[k] != 0; k++) { /* reducing component t^p[k] */ n = p[0] - p[k]; d0 = n % BN_BITS2; d1 = BN_BITS2 - d0; n /= BN_BITS2; z[j - n] ^= (zz >> d0); if (d0) z[j - n - 1] ^= (zz << d1); } /* reducing component t^0 */ n = dN; d0 = p[0] % BN_BITS2; d1 = BN_BITS2 - d0; z[j - n] ^= (zz >> d0); if (d0) z[j - n - 1] ^= (zz << d1); } /* final round of reduction */ while (j == dN) { d0 = p[0] % BN_BITS2; zz = z[dN] >> d0; if (zz == 0) break; d1 = BN_BITS2 - d0; /* clear up the top d1 bits */ if (d0) z[dN] = (z[dN] << d1) >> d1; else z[dN] = 0; z[0] ^= zz; /* reduction t^0 component */ for (k = 1; p[k] != 0; k++) { BN_ULONG tmp_ulong; /* reducing component t^p[k] */ n = p[k] / BN_BITS2; d0 = p[k] % BN_BITS2; d1 = BN_BITS2 - d0; z[n] ^= (zz << d0); if (d0 && (tmp_ulong = zz >> d1)) z[n + 1] ^= tmp_ulong; } } bn_correct_top(r); return 1; }
377,411
0
static int check_chain_extensions(X509_STORE_CTX *ctx) { int i, ok = 0, must_be_ca, plen = 0; X509 *x; int (*cb) (int xok, X509_STORE_CTX *xctx); int proxy_path_length = 0; int purpose; int allow_proxy_certs; cb = ctx->verify_cb; /*- * must_be_ca can have 1 of 3 values: * -1: we accept both CA and non-CA certificates, to allow direct * use of self-signed certificates (which are marked as CA). * 0: we only accept non-CA certificates. This is currently not * used, but the possibility is present for future extensions. * 1: we only accept CA certificates. This is currently used for * all certificates in the chain except the leaf certificate. */ must_be_ca = -1; /* CRL path validation */ if (ctx->parent) { allow_proxy_certs = 0; purpose = X509_PURPOSE_CRL_SIGN; } else { allow_proxy_certs = ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS); /* * A hack to keep people who don't want to modify their software * happy */ if (getenv("OPENSSL_ALLOW_PROXY_CERTS")) allow_proxy_certs = 1; purpose = ctx->param->purpose; } /* Check all untrusted certificates */ for (i = 0; i < ctx->last_untrusted; i++) { int ret; x = sk_X509_value(ctx->chain, i); if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) && (x->ex_flags & EXFLAG_CRITICAL)) { ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) { ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } ret = X509_check_ca(x); switch (must_be_ca) { case -1: if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) && (ret != 1) && (ret != 0)) { ret = 0; ctx->error = X509_V_ERR_INVALID_CA; } else ret = 1; break; case 0: if (ret != 0) { ret = 0; ctx->error = X509_V_ERR_INVALID_NON_CA; } else ret = 1; break; default: if ((ret == 0) || ((ctx->param->flags & X509_V_FLAG_X509_STRICT) && (ret != 1))) { ret = 0; ctx->error = X509_V_ERR_INVALID_CA; } else ret = 1; break; } if (ret == 0) { ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } if (ctx->param->purpose > 0) { ret = X509_check_purpose(x, purpose, must_be_ca > 0); if ((ret == 0) || ((ctx->param->flags & X509_V_FLAG_X509_STRICT) && (ret != 1))) { ctx->error = X509_V_ERR_INVALID_PURPOSE; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } } /* Check pathlen if not self issued */ if ((i > 1) && !(x->ex_flags & EXFLAG_SI) && (x->ex_pathlen != -1) && (plen > (x->ex_pathlen + proxy_path_length + 1))) { ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } /* Increment path length if not self issued */ if (!(x->ex_flags & EXFLAG_SI)) plen++; /* * If this certificate is a proxy certificate, the next certificate * must be another proxy certificate or a EE certificate. If not, * the next certificate must be a CA certificate. */ if (x->ex_flags & EXFLAG_PROXY) { if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) { ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED; ctx->error_depth = i; ctx->current_cert = x; ok = cb(0, ctx); if (!ok) goto end; } proxy_path_length++; must_be_ca = 0; } else must_be_ca = 1; } ok = 1; end: return ok; }
377,413
0
void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx) { if (ctx->cleanup) ctx->cleanup(ctx); if (ctx->param != NULL) { if (ctx->parent == NULL) X509_VERIFY_PARAM_free(ctx->param); ctx->param = NULL; } X509_policy_tree_free(ctx->tree); ctx->tree = NULL; sk_X509_pop_free(ctx->chain, X509_free); ctx->chain = NULL; CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data)); memset(&ctx->ex_data, 0, sizeof(ctx->ex_data)); }
377,414
0
int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain) { EVP_PKEY *ktmp = NULL, *ktmp2; int i, j; if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey)) return 1; for (i = 0; i < sk_X509_num(chain); i++) { ktmp = X509_get_pubkey(sk_X509_value(chain, i)); if (ktmp == NULL) { X509err(X509_F_X509_GET_PUBKEY_PARAMETERS, X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY); return 0; } if (!EVP_PKEY_missing_parameters(ktmp)) break; EVP_PKEY_free(ktmp); ktmp = NULL; } if (ktmp == NULL) { X509err(X509_F_X509_GET_PUBKEY_PARAMETERS, X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN); return 0; } /* first, populate the other certs */ for (j = i - 1; j >= 0; j--) { ktmp2 = X509_get_pubkey(sk_X509_value(chain, j)); EVP_PKEY_copy_parameters(ktmp2, ktmp); EVP_PKEY_free(ktmp2); } if (pkey != NULL) EVP_PKEY_copy_parameters(pkey, ktmp); EVP_PKEY_free(ktmp); return 1; }
377,416
0
X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer, EVP_PKEY *skey, const EVP_MD *md, unsigned int flags) { X509_CRL *crl = NULL; int i; STACK_OF(X509_REVOKED) *revs = NULL; /* CRLs can't be delta already */ if (base->base_crl_number || newer->base_crl_number) { X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_ALREADY_DELTA); return NULL; } /* Base and new CRL must have a CRL number */ if (!base->crl_number || !newer->crl_number) { X509err(X509_F_X509_CRL_DIFF, X509_R_NO_CRL_NUMBER); return NULL; } /* Issuer names must match */ if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(newer))) { X509err(X509_F_X509_CRL_DIFF, X509_R_ISSUER_MISMATCH); return NULL; } /* AKID and IDP must match */ if (!crl_extension_match(base, newer, NID_authority_key_identifier)) { X509err(X509_F_X509_CRL_DIFF, X509_R_AKID_MISMATCH); return NULL; } if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) { X509err(X509_F_X509_CRL_DIFF, X509_R_IDP_MISMATCH); return NULL; } /* Newer CRL number must exceed full CRL number */ if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) { X509err(X509_F_X509_CRL_DIFF, X509_R_NEWER_CRL_NOT_NEWER); return NULL; } /* CRLs must verify */ if (skey && (X509_CRL_verify(base, skey) <= 0 || X509_CRL_verify(newer, skey) <= 0)) { X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_VERIFY_FAILURE); return NULL; } /* Create new CRL */ crl = X509_CRL_new(); if (!crl || !X509_CRL_set_version(crl, 1)) goto memerr; /* Set issuer name */ if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer))) goto memerr; if (!X509_CRL_set_lastUpdate(crl, X509_CRL_get_lastUpdate(newer))) goto memerr; if (!X509_CRL_set_nextUpdate(crl, X509_CRL_get_nextUpdate(newer))) goto memerr; /* Set base CRL number: must be critical */ if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0)) goto memerr; /* * Copy extensions across from newest CRL to delta: this will set CRL * number to correct value too. */ for (i = 0; i < X509_CRL_get_ext_count(newer); i++) { X509_EXTENSION *ext; ext = X509_CRL_get_ext(newer, i); if (!X509_CRL_add_ext(crl, ext, -1)) goto memerr; } /* Go through revoked entries, copying as needed */ revs = X509_CRL_get_REVOKED(newer); for (i = 0; i < sk_X509_REVOKED_num(revs); i++) { X509_REVOKED *rvn, *rvtmp; rvn = sk_X509_REVOKED_value(revs, i); /* * Add only if not also in base. TODO: need something cleverer here * for some more complex CRLs covering multiple CAs. */ if (!X509_CRL_get0_by_serial(base, &rvtmp, rvn->serialNumber)) { rvtmp = X509_REVOKED_dup(rvn); if (!rvtmp) goto memerr; if (!X509_CRL_add0_revoked(crl, rvtmp)) { X509_REVOKED_free(rvtmp); goto memerr; } } } /* TODO: optionally prune deleted entries */ if (skey && md && !X509_CRL_sign(crl, skey, md)) goto memerr; return crl; memerr: X509err(X509_F_X509_CRL_DIFF, ERR_R_MALLOC_FAILURE); X509_CRL_free(crl); return NULL; }
377,417
0
int x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int quiet) { time_t *ptime; int i; if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) ptime = &ctx->param->check_time; else ptime = NULL; i = X509_cmp_time(X509_get_notBefore(x), ptime); if (i == 0) { if (quiet) return 0; ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD; ctx->current_cert = x; if (!ctx->verify_cb(0, ctx)) return 0; } if (i > 0) { if (quiet) return 0; ctx->error = X509_V_ERR_CERT_NOT_YET_VALID; ctx->current_cert = x; if (!ctx->verify_cb(0, ctx)) return 0; } i = X509_cmp_time(X509_get_notAfter(x), ptime); if (i == 0) { if (quiet) return 0; ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD; ctx->current_cert = x; if (!ctx->verify_cb(0, ctx)) return 0; } if (i < 0) { if (quiet) return 0; ctx->error = X509_V_ERR_CERT_HAS_EXPIRED; ctx->current_cert = x; if (!ctx->verify_cb(0, ctx)) return 0; } return 1; }
377,420
0
static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl, X509 **pissuer, int *pscore, unsigned int *preasons, STACK_OF(X509_CRL) *crls) { int i, crl_score, best_score = *pscore; unsigned int reasons, best_reasons = 0; X509 *x = ctx->current_cert; X509_CRL *crl, *best_crl = NULL; X509 *crl_issuer = NULL, *best_crl_issuer = NULL; for (i = 0; i < sk_X509_CRL_num(crls); i++) { crl = sk_X509_CRL_value(crls, i); reasons = *preasons; crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x); if (crl_score > best_score) { best_crl = crl; best_crl_issuer = crl_issuer; best_score = crl_score; best_reasons = reasons; } } if (best_crl) { X509_CRL_free(*pcrl); *pcrl = best_crl; *pissuer = best_crl_issuer; *pscore = best_score; *preasons = best_reasons; CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509_CRL); X509_CRL_free(*pdcrl); *pdcrl = NULL; get_delta_sk(ctx, pdcrl, pscore, best_crl, crls); } if (best_score >= CRL_SCORE_VALID) return 1; return 0; }
377,421
0
static int internal_verify(X509_STORE_CTX *ctx) { int ok = 0, n; X509 *xs, *xi; EVP_PKEY *pkey = NULL; int (*cb) (int xok, X509_STORE_CTX *xctx); cb = ctx->verify_cb; n = sk_X509_num(ctx->chain); ctx->error_depth = n - 1; n--; xi = sk_X509_value(ctx->chain, n); if (ctx->check_issued(ctx, xi, xi)) xs = xi; else { if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) { xs = xi; goto check_cert; } if (n <= 0) { ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE; ctx->current_cert = xi; ok = cb(0, ctx); goto end; } else { n--; ctx->error_depth = n; xs = sk_X509_value(ctx->chain, n); } } /* ctx->error=0; not needed */ while (n >= 0) { ctx->error_depth = n; /* * Skip signature check for self signed certificates unless * explicitly asked for. It doesn't add any security and just wastes * time. */ if (!xs->valid && (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) { if ((pkey = X509_get_pubkey(xi)) == NULL) { ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY; ctx->current_cert = xi; ok = (*cb) (0, ctx); if (!ok) goto end; } else if (X509_verify(xs, pkey) <= 0) { ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE; ctx->current_cert = xs; ok = (*cb) (0, ctx); if (!ok) { EVP_PKEY_free(pkey); goto end; } } EVP_PKEY_free(pkey); pkey = NULL; } xs->valid = 1; check_cert: ok = x509_check_cert_time(ctx, xs, 0); if (!ok) goto end; /* The last error (if any) is still in the error value */ ctx->current_issuer = xi; ctx->current_cert = xs; ok = (*cb) (1, ctx); if (!ok) goto end; n--; if (n >= 0) { xi = xs; xs = sk_X509_value(ctx->chain, n); } } ok = 1; end: return ok; }
377,422
0
static int i2r_IPAddressOrRanges(BIO *out, const int indent, const IPAddressOrRanges *aors, const unsigned afi) { int i; for (i = 0; i < sk_IPAddressOrRange_num(aors); i++) { const IPAddressOrRange *aor = sk_IPAddressOrRange_value(aors, i); BIO_printf(out, "%*s", indent, ""); switch (aor->type) { case IPAddressOrRange_addressPrefix: if (!i2r_address(out, afi, 0x00, aor->u.addressPrefix)) return 0; BIO_printf(out, "/%d\n", addr_prefixlen(aor->u.addressPrefix)); continue; case IPAddressOrRange_addressRange: if (!i2r_address(out, afi, 0x00, aor->u.addressRange->min)) return 0; BIO_puts(out, "-"); if (!i2r_address(out, afi, 0xFF, aor->u.addressRange->max)) return 0; BIO_puts(out, "\n"); continue; } } return 1; }
377,423
0
int X509v3_addr_validate_resource_set(STACK_OF(X509) *chain, IPAddrBlocks *ext, int allow_inheritance) { if (ext == NULL) return 1; if (chain == NULL || sk_X509_num(chain) == 0) return 0; if (!allow_inheritance && X509v3_addr_inherits(ext)) return 0; return addr_validate_path_internal(NULL, chain, ext); }
377,424
0
static int make_addressRange(IPAddressOrRange **result, unsigned char *min, unsigned char *max, const int length) { IPAddressOrRange *aor; int i, prefixlen; if ((prefixlen = range_should_be_prefix(min, max, length)) >= 0) return make_addressPrefix(result, min, prefixlen); if ((aor = IPAddressOrRange_new()) == NULL) return 0; aor->type = IPAddressOrRange_addressRange; OPENSSL_assert(aor->u.addressRange == NULL); if ((aor->u.addressRange = IPAddressRange_new()) == NULL) goto err; if (aor->u.addressRange->min == NULL && (aor->u.addressRange->min = ASN1_BIT_STRING_new()) == NULL) goto err; if (aor->u.addressRange->max == NULL && (aor->u.addressRange->max = ASN1_BIT_STRING_new()) == NULL) goto err; for (i = length; i > 0 && min[i - 1] == 0x00; --i) ; if (!ASN1_BIT_STRING_set(aor->u.addressRange->min, min, i)) goto err; aor->u.addressRange->min->flags &= ~7; aor->u.addressRange->min->flags |= ASN1_STRING_FLAG_BITS_LEFT; if (i > 0) { unsigned char b = min[i - 1]; int j = 1; while ((b & (0xFFU >> j)) != 0) ++j; aor->u.addressRange->min->flags |= 8 - j; } for (i = length; i > 0 && max[i - 1] == 0xFF; --i) ; if (!ASN1_BIT_STRING_set(aor->u.addressRange->max, max, i)) goto err; aor->u.addressRange->max->flags &= ~7; aor->u.addressRange->max->flags |= ASN1_STRING_FLAG_BITS_LEFT; if (i > 0) { unsigned char b = max[i - 1]; int j = 1; while ((b & (0xFFU >> j)) != (0xFFU >> j)) ++j; aor->u.addressRange->max->flags |= 8 - j; } *result = aor; return 1; err: IPAddressOrRange_free(aor); return 0; }
377,425
0
static int addr_validate_path_internal(X509_STORE_CTX *ctx, STACK_OF(X509) *chain, IPAddrBlocks *ext) { IPAddrBlocks *child = NULL; int i, j, ret = 1; X509 *x; OPENSSL_assert(chain != NULL && sk_X509_num(chain) > 0); OPENSSL_assert(ctx != NULL || ext != NULL); OPENSSL_assert(ctx == NULL || ctx->verify_cb != NULL); /* * Figure out where to start. If we don't have an extension to * check, we're done. Otherwise, check canonical form and * set up for walking up the chain. */ if (ext != NULL) { i = -1; x = NULL; } else { i = 0; x = sk_X509_value(chain, i); OPENSSL_assert(x != NULL); if ((ext = x->rfc3779_addr) == NULL) goto done; } if (!X509v3_addr_is_canonical(ext)) validation_err(X509_V_ERR_INVALID_EXTENSION); (void)sk_IPAddressFamily_set_cmp_func(ext, IPAddressFamily_cmp); if ((child = sk_IPAddressFamily_dup(ext)) == NULL) { X509V3err(X509V3_F_ADDR_VALIDATE_PATH_INTERNAL, ERR_R_MALLOC_FAILURE); ctx->error = X509_V_ERR_OUT_OF_MEM; ret = 0; goto done; } /* * Now walk up the chain. No cert may list resources that its * parent doesn't list. */ for (i++; i < sk_X509_num(chain); i++) { x = sk_X509_value(chain, i); OPENSSL_assert(x != NULL); if (!X509v3_addr_is_canonical(x->rfc3779_addr)) validation_err(X509_V_ERR_INVALID_EXTENSION); if (x->rfc3779_addr == NULL) { for (j = 0; j < sk_IPAddressFamily_num(child); j++) { IPAddressFamily *fc = sk_IPAddressFamily_value(child, j); if (fc->ipAddressChoice->type != IPAddressChoice_inherit) { validation_err(X509_V_ERR_UNNESTED_RESOURCE); break; } } continue; } (void)sk_IPAddressFamily_set_cmp_func(x->rfc3779_addr, IPAddressFamily_cmp); for (j = 0; j < sk_IPAddressFamily_num(child); j++) { IPAddressFamily *fc = sk_IPAddressFamily_value(child, j); int k = sk_IPAddressFamily_find(x->rfc3779_addr, fc); IPAddressFamily *fp = sk_IPAddressFamily_value(x->rfc3779_addr, k); if (fp == NULL) { if (fc->ipAddressChoice->type == IPAddressChoice_addressesOrRanges) { validation_err(X509_V_ERR_UNNESTED_RESOURCE); break; } continue; } if (fp->ipAddressChoice->type == IPAddressChoice_addressesOrRanges) { if (fc->ipAddressChoice->type == IPAddressChoice_inherit || addr_contains(fp->ipAddressChoice->u.addressesOrRanges, fc->ipAddressChoice->u.addressesOrRanges, length_from_afi(X509v3_addr_get_afi(fc)))) sk_IPAddressFamily_set(child, j, fp); else validation_err(X509_V_ERR_UNNESTED_RESOURCE); } } } /* * Trust anchor can't inherit. */ OPENSSL_assert(x != NULL); if (x->rfc3779_addr != NULL) { for (j = 0; j < sk_IPAddressFamily_num(x->rfc3779_addr); j++) { IPAddressFamily *fp = sk_IPAddressFamily_value(x->rfc3779_addr, j); if (fp->ipAddressChoice->type == IPAddressChoice_inherit && sk_IPAddressFamily_find(child, fp) >= 0) validation_err(X509_V_ERR_UNNESTED_RESOURCE); } } done: sk_IPAddressFamily_free(child); return ret; }
377,426
0
int X509v3_addr_add_range(IPAddrBlocks *addr, const unsigned afi, const unsigned *safi, unsigned char *min, unsigned char *max) { IPAddressOrRanges *aors = make_prefix_or_range(addr, afi, safi); IPAddressOrRange *aor; int length = length_from_afi(afi); if (aors == NULL) return 0; if (!make_addressRange(&aor, min, max, length)) return 0; if (sk_IPAddressOrRange_push(aors, aor)) return 1; IPAddressOrRange_free(aor); return 0; }
377,427
0
static int IPAddressOrRanges_canonize(IPAddressOrRanges *aors, const unsigned afi) { int i, j, length = length_from_afi(afi); /* * Sort the IPAddressOrRanges sequence. */ sk_IPAddressOrRange_sort(aors); /* * Clean up representation issues, punt on duplicates or overlaps. */ for (i = 0; i < sk_IPAddressOrRange_num(aors) - 1; i++) { IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, i); IPAddressOrRange *b = sk_IPAddressOrRange_value(aors, i + 1); unsigned char a_min[ADDR_RAW_BUF_LEN], a_max[ADDR_RAW_BUF_LEN]; unsigned char b_min[ADDR_RAW_BUF_LEN], b_max[ADDR_RAW_BUF_LEN]; if (!extract_min_max(a, a_min, a_max, length) || !extract_min_max(b, b_min, b_max, length)) return 0; /* * Punt inverted ranges. */ if (memcmp(a_min, a_max, length) > 0 || memcmp(b_min, b_max, length) > 0) return 0; /* * Punt overlaps. */ if (memcmp(a_max, b_min, length) >= 0) return 0; /* * Merge if a and b are adjacent. We check for * adjacency by subtracting one from b_min first. */ for (j = length - 1; j >= 0 && b_min[j]-- == 0x00; j--) ; if (memcmp(a_max, b_min, length) == 0) { IPAddressOrRange *merged; if (!make_addressRange(&merged, a_min, b_max, length)) return 0; (void)sk_IPAddressOrRange_set(aors, i, merged); (void)sk_IPAddressOrRange_delete(aors, i + 1); IPAddressOrRange_free(a); IPAddressOrRange_free(b); --i; continue; } } /* * Check for inverted final range. */ j = sk_IPAddressOrRange_num(aors) - 1; { IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, j); if (a != NULL && a->type == IPAddressOrRange_addressRange) { unsigned char a_min[ADDR_RAW_BUF_LEN], a_max[ADDR_RAW_BUF_LEN]; if (!extract_min_max(a, a_min, a_max, length)) return 0; if (memcmp(a_min, a_max, length) > 0) return 0; } } return 1; }
377,428
0
static int length_from_afi(const unsigned afi) { switch (afi) { case IANA_AFI_IPV4: return 4; case IANA_AFI_IPV6: return 16; default: return 0; } }
377,429
0
static int IPAddressOrRange_cmp(const IPAddressOrRange *a, const IPAddressOrRange *b, const int length) { unsigned char addr_a[ADDR_RAW_BUF_LEN], addr_b[ADDR_RAW_BUF_LEN]; int prefixlen_a = 0, prefixlen_b = 0; int r; switch (a->type) { case IPAddressOrRange_addressPrefix: if (!addr_expand(addr_a, a->u.addressPrefix, length, 0x00)) return -1; prefixlen_a = addr_prefixlen(a->u.addressPrefix); break; case IPAddressOrRange_addressRange: if (!addr_expand(addr_a, a->u.addressRange->min, length, 0x00)) return -1; prefixlen_a = length * 8; break; } switch (b->type) { case IPAddressOrRange_addressPrefix: if (!addr_expand(addr_b, b->u.addressPrefix, length, 0x00)) return -1; prefixlen_b = addr_prefixlen(b->u.addressPrefix); break; case IPAddressOrRange_addressRange: if (!addr_expand(addr_b, b->u.addressRange->min, length, 0x00)) return -1; prefixlen_b = length * 8; break; } if ((r = memcmp(addr_a, addr_b, length)) != 0) return r; else return prefixlen_a - prefixlen_b; }
377,430
0
static int addr_contains(IPAddressOrRanges *parent, IPAddressOrRanges *child, int length) { unsigned char p_min[ADDR_RAW_BUF_LEN], p_max[ADDR_RAW_BUF_LEN]; unsigned char c_min[ADDR_RAW_BUF_LEN], c_max[ADDR_RAW_BUF_LEN]; int p, c; if (child == NULL || parent == child) return 1; if (parent == NULL) return 0; p = 0; for (c = 0; c < sk_IPAddressOrRange_num(child); c++) { if (!extract_min_max(sk_IPAddressOrRange_value(child, c), c_min, c_max, length)) return -1; for (;; p++) { if (p >= sk_IPAddressOrRange_num(parent)) return 0; if (!extract_min_max(sk_IPAddressOrRange_value(parent, p), p_min, p_max, length)) return 0; if (memcmp(p_max, c_max, length) < 0) continue; if (memcmp(p_min, c_min, length) > 0) return 0; break; } } return 1; }
377,432
0
static IPAddressFamily *make_IPAddressFamily(IPAddrBlocks *addr, const unsigned afi, const unsigned *safi) { IPAddressFamily *f; unsigned char key[3]; int keylen; int i; key[0] = (afi >> 8) & 0xFF; key[1] = afi & 0xFF; if (safi != NULL) { key[2] = *safi & 0xFF; keylen = 3; } else { keylen = 2; } for (i = 0; i < sk_IPAddressFamily_num(addr); i++) { f = sk_IPAddressFamily_value(addr, i); OPENSSL_assert(f->addressFamily->data != NULL); if (f->addressFamily->length == keylen && !memcmp(f->addressFamily->data, key, keylen)) return f; } if ((f = IPAddressFamily_new()) == NULL) goto err; if (f->ipAddressChoice == NULL && (f->ipAddressChoice = IPAddressChoice_new()) == NULL) goto err; if (f->addressFamily == NULL && (f->addressFamily = ASN1_OCTET_STRING_new()) == NULL) goto err; if (!ASN1_OCTET_STRING_set(f->addressFamily, key, keylen)) goto err; if (!sk_IPAddressFamily_push(addr, f)) goto err; return f; err: IPAddressFamily_free(f); return NULL; }
377,434
0
unsigned int X509v3_addr_get_afi(const IPAddressFamily *f) { if (f == NULL || f->addressFamily == NULL || f->addressFamily->data == NULL || f->addressFamily->length < 2) return 0; return (f->addressFamily->data[0] << 8) | f->addressFamily->data[1]; }
377,436
0
int X509v3_addr_add_prefix(IPAddrBlocks *addr, const unsigned afi, const unsigned *safi, unsigned char *a, const int prefixlen) { IPAddressOrRanges *aors = make_prefix_or_range(addr, afi, safi); IPAddressOrRange *aor; if (aors == NULL || !make_addressPrefix(&aor, a, prefixlen)) return 0; if (sk_IPAddressOrRange_push(aors, aor)) return 1; IPAddressOrRange_free(aor); return 0; }
377,437
0
static int i2r_address(BIO *out, const unsigned afi, const unsigned char fill, const ASN1_BIT_STRING *bs) { unsigned char addr[ADDR_RAW_BUF_LEN]; int i, n; if (bs->length < 0) return 0; switch (afi) { case IANA_AFI_IPV4: if (!addr_expand(addr, bs, 4, fill)) return 0; BIO_printf(out, "%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]); break; case IANA_AFI_IPV6: if (!addr_expand(addr, bs, 16, fill)) return 0; for (n = 16; n > 1 && addr[n - 1] == 0x00 && addr[n - 2] == 0x00; n -= 2) ; for (i = 0; i < n; i += 2) BIO_printf(out, "%x%s", (addr[i] << 8) | addr[i + 1], (i < 14 ? ":" : "")); if (i < 16) BIO_puts(out, ":"); if (i == 0) BIO_puts(out, ":"); break; default: for (i = 0; i < bs->length; i++) BIO_printf(out, "%s%02x", (i > 0 ? ":" : ""), bs->data[i]); BIO_printf(out, "[%d]", (int)(bs->flags & 7)); break; } return 1; }
377,439
0
int X509v3_addr_canonize(IPAddrBlocks *addr) { int i; for (i = 0; i < sk_IPAddressFamily_num(addr); i++) { IPAddressFamily *f = sk_IPAddressFamily_value(addr, i); if (f->ipAddressChoice->type == IPAddressChoice_addressesOrRanges && !IPAddressOrRanges_canonize(f->ipAddressChoice-> u.addressesOrRanges, X509v3_addr_get_afi(f))) return 0; } (void)sk_IPAddressFamily_set_cmp_func(addr, IPAddressFamily_cmp); sk_IPAddressFamily_sort(addr); OPENSSL_assert(X509v3_addr_is_canonical(addr)); return 1; }
377,440
0
static int v4IPAddressOrRange_cmp(const IPAddressOrRange *const *a, const IPAddressOrRange *const *b) { return IPAddressOrRange_cmp(*a, *b, 4); }
377,441
0
static int range_should_be_prefix(const unsigned char *min, const unsigned char *max, const int length) { unsigned char mask; int i, j; OPENSSL_assert(memcmp(min, max, length) <= 0); for (i = 0; i < length && min[i] == max[i]; i++) ; for (j = length - 1; j >= 0 && min[j] == 0x00 && max[j] == 0xFF; j--) ; if (i < j) return -1; if (i > j) return i * 8; mask = min[i] ^ max[i]; switch (mask) { case 0x01: j = 7; break; case 0x03: j = 6; break; case 0x07: j = 5; break; case 0x0F: j = 4; break; case 0x1F: j = 3; break; case 0x3F: j = 2; break; case 0x7F: j = 1; break; default: return -1; } if ((min[i] & mask) != 0 || (max[i] & mask) != mask) return -1; else return i * 8 + j; }
377,442
0
static int addr_expand(unsigned char *addr, const ASN1_BIT_STRING *bs, const int length, const unsigned char fill) { if (bs->length < 0 || bs->length > length) return 0; if (bs->length > 0) { memcpy(addr, bs->data, bs->length); if ((bs->flags & 7) != 0) { unsigned char mask = 0xFF >> (8 - (bs->flags & 7)); if (fill == 0) addr[bs->length - 1] &= ~mask; else addr[bs->length - 1] |= mask; } } memset(addr + bs->length, fill, length - bs->length); return 1; }
377,444
0
static IPAddressOrRanges *make_prefix_or_range(IPAddrBlocks *addr, const unsigned afi, const unsigned *safi) { IPAddressFamily *f = make_IPAddressFamily(addr, afi, safi); IPAddressOrRanges *aors = NULL; if (f == NULL || f->ipAddressChoice == NULL || (f->ipAddressChoice->type == IPAddressChoice_inherit && f->ipAddressChoice->u.inherit != NULL)) return NULL; if (f->ipAddressChoice->type == IPAddressChoice_addressesOrRanges) aors = f->ipAddressChoice->u.addressesOrRanges; if (aors != NULL) return aors; if ((aors = sk_IPAddressOrRange_new_null()) == NULL) return NULL; switch (afi) { case IANA_AFI_IPV4: (void)sk_IPAddressOrRange_set_cmp_func(aors, v4IPAddressOrRange_cmp); break; case IANA_AFI_IPV6: (void)sk_IPAddressOrRange_set_cmp_func(aors, v6IPAddressOrRange_cmp); break; } f->ipAddressChoice->type = IPAddressChoice_addressesOrRanges; f->ipAddressChoice->u.addressesOrRanges = aors; return aors; }
377,446
0
int X509v3_addr_add_inherit(IPAddrBlocks *addr, const unsigned afi, const unsigned *safi) { IPAddressFamily *f = make_IPAddressFamily(addr, afi, safi); if (f == NULL || f->ipAddressChoice == NULL || (f->ipAddressChoice->type == IPAddressChoice_addressesOrRanges && f->ipAddressChoice->u.addressesOrRanges != NULL)) return 0; if (f->ipAddressChoice->type == IPAddressChoice_inherit && f->ipAddressChoice->u.inherit != NULL) return 1; if (f->ipAddressChoice->u.inherit == NULL && (f->ipAddressChoice->u.inherit = ASN1_NULL_new()) == NULL) return 0; f->ipAddressChoice->type = IPAddressChoice_inherit; return 1; }
377,447
0
int X509v3_addr_subset(IPAddrBlocks *a, IPAddrBlocks *b) { int i; if (a == NULL || a == b) return 1; if (b == NULL || X509v3_addr_inherits(a) || X509v3_addr_inherits(b)) return 0; (void)sk_IPAddressFamily_set_cmp_func(b, IPAddressFamily_cmp); for (i = 0; i < sk_IPAddressFamily_num(a); i++) { IPAddressFamily *fa = sk_IPAddressFamily_value(a, i); int j = sk_IPAddressFamily_find(b, fa); IPAddressFamily *fb; fb = sk_IPAddressFamily_value(b, j); if (fb == NULL) return 0; if (!addr_contains(fb->ipAddressChoice->u.addressesOrRanges, fa->ipAddressChoice->u.addressesOrRanges, length_from_afi(X509v3_addr_get_afi(fb)))) return 0; } return 1; }
377,448
0
int X509v3_addr_inherits(IPAddrBlocks *addr) { int i; if (addr == NULL) return 0; for (i = 0; i < sk_IPAddressFamily_num(addr); i++) { IPAddressFamily *f = sk_IPAddressFamily_value(addr, i); if (f->ipAddressChoice->type == IPAddressChoice_inherit) return 1; } return 0; }
377,449
0
static int make_addressPrefix(IPAddressOrRange **result, unsigned char *addr, const int prefixlen) { int bytelen = (prefixlen + 7) / 8, bitlen = prefixlen % 8; IPAddressOrRange *aor = IPAddressOrRange_new(); if (aor == NULL) return 0; aor->type = IPAddressOrRange_addressPrefix; if (aor->u.addressPrefix == NULL && (aor->u.addressPrefix = ASN1_BIT_STRING_new()) == NULL) goto err; if (!ASN1_BIT_STRING_set(aor->u.addressPrefix, addr, bytelen)) goto err; aor->u.addressPrefix->flags &= ~7; aor->u.addressPrefix->flags |= ASN1_STRING_FLAG_BITS_LEFT; if (bitlen > 0) { aor->u.addressPrefix->data[bytelen - 1] &= ~(0xFF >> bitlen); aor->u.addressPrefix->flags |= 8 - bitlen; } *result = aor; return 1; err: IPAddressOrRange_free(aor); return 0; }
377,450
0
static void *v2i_IPAddrBlocks(const struct v3_ext_method *method, struct v3_ext_ctx *ctx, STACK_OF(CONF_VALUE) *values) { static const char v4addr_chars[] = "0123456789."; static const char v6addr_chars[] = "0123456789.:abcdefABCDEF"; IPAddrBlocks *addr = NULL; char *s = NULL, *t; int i; if ((addr = sk_IPAddressFamily_new(IPAddressFamily_cmp)) == NULL) { X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE); return NULL; } for (i = 0; i < sk_CONF_VALUE_num(values); i++) { CONF_VALUE *val = sk_CONF_VALUE_value(values, i); unsigned char min[ADDR_RAW_BUF_LEN], max[ADDR_RAW_BUF_LEN]; unsigned afi, *safi = NULL, safi_; const char *addr_chars = NULL; int prefixlen, i1, i2, delim, length; if (!name_cmp(val->name, "IPv4")) { afi = IANA_AFI_IPV4; } else if (!name_cmp(val->name, "IPv6")) { afi = IANA_AFI_IPV6; } else if (!name_cmp(val->name, "IPv4-SAFI")) { afi = IANA_AFI_IPV4; safi = &safi_; } else if (!name_cmp(val->name, "IPv6-SAFI")) { afi = IANA_AFI_IPV6; safi = &safi_; } else { X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_EXTENSION_NAME_ERROR); X509V3_conf_err(val); goto err; } switch (afi) { case IANA_AFI_IPV4: addr_chars = v4addr_chars; break; case IANA_AFI_IPV6: addr_chars = v6addr_chars; break; } length = length_from_afi(afi); /* * Handle SAFI, if any, and OPENSSL_strdup() so we can null-terminate * the other input values. */ if (safi != NULL) { *safi = strtoul(val->value, &t, 0); t += strspn(t, " \t"); if (*safi > 0xFF || *t++ != ':') { X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_INVALID_SAFI); X509V3_conf_err(val); goto err; } t += strspn(t, " \t"); s = OPENSSL_strdup(t); } else { s = OPENSSL_strdup(val->value); } if (s == NULL) { X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE); goto err; } /* * Check for inheritance. Not worth additional complexity to * optimize this (seldom-used) case. */ if (strcmp(s, "inherit") == 0) { if (!X509v3_addr_add_inherit(addr, afi, safi)) { X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_INVALID_INHERITANCE); X509V3_conf_err(val); goto err; } OPENSSL_free(s); s = NULL; continue; } i1 = strspn(s, addr_chars); i2 = i1 + strspn(s + i1, " \t"); delim = s[i2++]; s[i1] = '\0'; if (a2i_ipadd(min, s) != length) { X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_INVALID_IPADDRESS); X509V3_conf_err(val); goto err; } switch (delim) { case '/': prefixlen = (int)strtoul(s + i2, &t, 10); if (t == s + i2 || *t != '\0') { X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_EXTENSION_VALUE_ERROR); X509V3_conf_err(val); goto err; } if (!X509v3_addr_add_prefix(addr, afi, safi, min, prefixlen)) { X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE); goto err; } break; case '-': i1 = i2 + strspn(s + i2, " \t"); i2 = i1 + strspn(s + i1, addr_chars); if (i1 == i2 || s[i2] != '\0') { X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_EXTENSION_VALUE_ERROR); X509V3_conf_err(val); goto err; } if (a2i_ipadd(max, s + i1) != length) { X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_INVALID_IPADDRESS); X509V3_conf_err(val); goto err; } if (memcmp(min, max, length_from_afi(afi)) > 0) { X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_EXTENSION_VALUE_ERROR); X509V3_conf_err(val); goto err; } if (!X509v3_addr_add_range(addr, afi, safi, min, max)) { X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE); goto err; } break; case '\0': if (!X509v3_addr_add_prefix(addr, afi, safi, min, length * 8)) { X509V3err(X509V3_F_V2I_IPADDRBLOCKS, ERR_R_MALLOC_FAILURE); goto err; } break; default: X509V3err(X509V3_F_V2I_IPADDRBLOCKS, X509V3_R_EXTENSION_VALUE_ERROR); X509V3_conf_err(val); goto err; } OPENSSL_free(s); s = NULL; } /* * Canonize the result, then we're done. */ if (!X509v3_addr_canonize(addr)) goto err; return addr; err: OPENSSL_free(s); sk_IPAddressFamily_pop_free(addr, IPAddressFamily_free); return NULL; }
377,452
0
void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n, int tna, int tnb, BN_ULONG *t) { int i,j,n2=n*2; int c1,c2,neg,zero; BN_ULONG ln,lo,*p; # ifdef BN_COUNT fprintf(stderr," bn_mul_part_recursive (%d%+d) * (%d%+d)\n", n, tna, n, tnb); # endif if (n < 8) { bn_mul_normal(r,a,n+tna,b,n+tnb); return; } /* r=(a[0]-a[1])*(b[1]-b[0]) */ c1=bn_cmp_part_words(a,&(a[n]),tna,n-tna); c2=bn_cmp_part_words(&(b[n]),b,tnb,tnb-n); zero=neg=0; switch (c1*3+c2) { case -4: bn_sub_part_words(t, &(a[n]),a, tna,tna-n); /* - */ bn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb); /* - */ break; case -3: zero=1; /* break; */ case -2: bn_sub_part_words(t, &(a[n]),a, tna,tna-n); /* - */ bn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n); /* + */ neg=1; break; case -1: case 0: case 1: zero=1; /* break; */ case 2: bn_sub_part_words(t, a, &(a[n]),tna,n-tna); /* + */ bn_sub_part_words(&(t[n]),b, &(b[n]),tnb,n-tnb); /* - */ neg=1; break; case 3: zero=1; /* break; */ case 4: bn_sub_part_words(t, a, &(a[n]),tna,n-tna); bn_sub_part_words(&(t[n]),&(b[n]),b, tnb,tnb-n); break; } /* The zero case isn't yet implemented here. The speedup would probably be negligible. */ # if 0 if (n == 4) { bn_mul_comba4(&(t[n2]),t,&(t[n])); bn_mul_comba4(r,a,b); bn_mul_normal(&(r[n2]),&(a[n]),tn,&(b[n]),tn); memset(&(r[n2+tn*2]),0,sizeof(BN_ULONG)*(n2-tn*2)); } else # endif if (n == 8) { bn_mul_comba8(&(t[n2]),t,&(t[n])); bn_mul_comba8(r,a,b); bn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb); memset(&(r[n2+tna+tnb]),0,sizeof(BN_ULONG)*(n2-tna-tnb)); } else { p= &(t[n2*2]); bn_mul_recursive(&(t[n2]),t,&(t[n]),n,0,0,p); bn_mul_recursive(r,a,b,n,0,0,p); i=n/2; /* If there is only a bottom half to the number, * just do it */ if (tna > tnb) j = tna - i; else j = tnb - i; if (j == 0) { bn_mul_recursive(&(r[n2]),&(a[n]),&(b[n]), i,tna-i,tnb-i,p); memset(&(r[n2+i*2]),0,sizeof(BN_ULONG)*(n2-i*2)); } else if (j > 0) /* eg, n == 16, i == 8 and tn == 11 */ { bn_mul_part_recursive(&(r[n2]),&(a[n]),&(b[n]), i,tna-i,tnb-i,p); memset(&(r[n2+tna+tnb]),0, sizeof(BN_ULONG)*(n2-tna-tnb)); } else /* (j < 0) eg, n == 16, i == 8 and tn == 5 */ { memset(&(r[n2]),0,sizeof(BN_ULONG)*n2); if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) { bn_mul_normal(&(r[n2]),&(a[n]),tna,&(b[n]),tnb); } else { for (;;) { i/=2; /* these simplified conditions work * exclusively because difference * between tna and tnb is 1 or 0 */ if (i < tna || i < tnb) { bn_mul_part_recursive(&(r[n2]), &(a[n]),&(b[n]), i,tna-i,tnb-i,p); break; } else if (i == tna || i == tnb) { bn_mul_recursive(&(r[n2]), &(a[n]),&(b[n]), i,tna-i,tnb-i,p); break; } } } } } /* t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign * r[10] holds (a[0]*b[0]) * r[32] holds (b[1]*b[1]) */ c1=(int)(bn_add_words(t,r,&(r[n2]),n2)); if (neg) /* if t[32] is negative */ { c1-=(int)(bn_sub_words(&(t[n2]),t,&(t[n2]),n2)); } else { /* Might have a carry */ c1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),t,n2)); } /* t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1]) * r[10] holds (a[0]*b[0]) * r[32] holds (b[1]*b[1]) * c1 holds the carry bits */ c1+=(int)(bn_add_words(&(r[n]),&(r[n]),&(t[n2]),n2)); if (c1) { p= &(r[n+n2]); lo= *p; ln=(lo+c1)&BN_MASK2; *p=ln; /* The overflow will stop before we over write * words we should not overwrite */ if (ln < (BN_ULONG)c1) { do { p++; lo= *p; ln=(lo+1)&BN_MASK2; *p=ln; } while (ln == 0); } } }
377,453
0
BN_ULONG bn_add_part_words(BN_ULONG *r, const BN_ULONG *a, const BN_ULONG *b, int cl, int dl) { BN_ULONG c, l, t; assert(cl >= 0); c = bn_add_words(r, a, b, cl); if (dl == 0) return c; r += cl; a += cl; b += cl; if (dl < 0) { int save_dl = dl; #ifdef BN_COUNT fprintf(stderr, " bn_add_part_words %d + %d (dl < 0, c = %d)\n", cl, dl, c); #endif while (c) { l=(c+b[0])&BN_MASK2; c=(l < c); r[0]=l; if (++dl >= 0) break; l=(c+b[1])&BN_MASK2; c=(l < c); r[1]=l; if (++dl >= 0) break; l=(c+b[2])&BN_MASK2; c=(l < c); r[2]=l; if (++dl >= 0) break; l=(c+b[3])&BN_MASK2; c=(l < c); r[3]=l; if (++dl >= 0) break; save_dl = dl; b+=4; r+=4; } if (dl < 0) { #ifdef BN_COUNT fprintf(stderr, " bn_add_part_words %d + %d (dl < 0, c == 0)\n", cl, dl); #endif if (save_dl < dl) { switch (dl - save_dl) { case 1: r[1] = b[1]; if (++dl >= 0) break; case 2: r[2] = b[2]; if (++dl >= 0) break; case 3: r[3] = b[3]; if (++dl >= 0) break; } b += 4; r += 4; } } if (dl < 0) { #ifdef BN_COUNT fprintf(stderr, " bn_add_part_words %d + %d (dl < 0, copy)\n", cl, dl); #endif for(;;) { r[0] = b[0]; if (++dl >= 0) break; r[1] = b[1]; if (++dl >= 0) break; r[2] = b[2]; if (++dl >= 0) break; r[3] = b[3]; if (++dl >= 0) break; b += 4; r += 4; } } } else { int save_dl = dl; #ifdef BN_COUNT fprintf(stderr, " bn_add_part_words %d + %d (dl > 0)\n", cl, dl); #endif while (c) { t=(a[0]+c)&BN_MASK2; c=(t < c); r[0]=t; if (--dl <= 0) break; t=(a[1]+c)&BN_MASK2; c=(t < c); r[1]=t; if (--dl <= 0) break; t=(a[2]+c)&BN_MASK2; c=(t < c); r[2]=t; if (--dl <= 0) break; t=(a[3]+c)&BN_MASK2; c=(t < c); r[3]=t; if (--dl <= 0) break; save_dl = dl; a+=4; r+=4; } #ifdef BN_COUNT fprintf(stderr, " bn_add_part_words %d + %d (dl > 0, c == 0)\n", cl, dl); #endif if (dl > 0) { if (save_dl > dl) { switch (save_dl - dl) { case 1: r[1] = a[1]; if (--dl <= 0) break; case 2: r[2] = a[2]; if (--dl <= 0) break; case 3: r[3] = a[3]; if (--dl <= 0) break; } a += 4; r += 4; } } if (dl > 0) { #ifdef BN_COUNT fprintf(stderr, " bn_add_part_words %d + %d (dl > 0, copy)\n", cl, dl); #endif for(;;) { r[0] = a[0]; if (--dl <= 0) break; r[1] = a[1]; if (--dl <= 0) break; r[2] = a[2]; if (--dl <= 0) break; r[3] = a[3]; if (--dl <= 0) break; a += 4; r += 4; } } } return c; }
377,454
0
void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2, BN_ULONG *t) { int n=n2/2; # ifdef BN_COUNT fprintf(stderr," bn_mul_low_recursive %d * %d\n",n2,n2); # endif bn_mul_recursive(r,a,b,n,0,0,&(t[0])); if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL) { bn_mul_low_recursive(&(t[0]),&(a[0]),&(b[n]),n,&(t[n2])); bn_add_words(&(r[n]),&(r[n]),&(t[0]),n); bn_mul_low_recursive(&(t[0]),&(a[n]),&(b[0]),n,&(t[n2])); bn_add_words(&(r[n]),&(r[n]),&(t[0]),n); } else { bn_mul_low_normal(&(t[0]),&(a[0]),&(b[n]),n); bn_mul_low_normal(&(t[n]),&(a[n]),&(b[0]),n); bn_add_words(&(r[n]),&(r[n]),&(t[0]),n); bn_add_words(&(r[n]),&(r[n]),&(t[n]),n); } }
377,455
0
int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx) { int ret=0; int top,al,bl; BIGNUM *rr; #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) int i; #endif #ifdef BN_RECURSION BIGNUM *t=NULL; int j=0,k; #endif #ifdef BN_COUNT fprintf(stderr,"BN_mul %d * %d\n",a->top,b->top); #endif bn_check_top(a); bn_check_top(b); bn_check_top(r); al=a->top; bl=b->top; if ((al == 0) || (bl == 0)) { BN_zero(r); return(1); } top=al+bl; BN_CTX_start(ctx); if ((r == a) || (r == b)) { if ((rr = BN_CTX_get(ctx)) == NULL) goto err; } else rr = r; rr->neg=a->neg^b->neg; #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) i = al-bl; #endif #ifdef BN_MUL_COMBA if (i == 0) { # if 0 if (al == 4) { if (bn_wexpand(rr,8) == NULL) goto err; rr->top=8; bn_mul_comba4(rr->d,a->d,b->d); goto end; } # endif if (al == 8) { if (bn_wexpand(rr,16) == NULL) goto err; rr->top=16; bn_mul_comba8(rr->d,a->d,b->d); goto end; } } #endif /* BN_MUL_COMBA */ #ifdef BN_RECURSION if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) { if (i >= -1 && i <= 1) { int sav_j =0; /* Find out the power of two lower or equal to the longest of the two numbers */ if (i >= 0) { j = BN_num_bits_word((BN_ULONG)al); } if (i == -1) { j = BN_num_bits_word((BN_ULONG)bl); } sav_j = j; j = 1<<(j-1); assert(j <= al || j <= bl); k = j+j; t = BN_CTX_get(ctx); if (t == NULL) goto err; if (al > j || bl > j) { if (bn_wexpand(t,k*4) == NULL) goto err; if (bn_wexpand(rr,k*4) == NULL) goto err; bn_mul_part_recursive(rr->d,a->d,b->d, j,al-j,bl-j,t->d); } else /* al <= j || bl <= j */ { if (bn_wexpand(t,k*2) == NULL) goto err; if (bn_wexpand(rr,k*2) == NULL) goto err; bn_mul_recursive(rr->d,a->d,b->d, j,al-j,bl-j,t->d); } rr->top=top; goto end; } #if 0 if (i == 1 && !BN_get_flags(b,BN_FLG_STATIC_DATA)) { BIGNUM *tmp_bn = (BIGNUM *)b; if (bn_wexpand(tmp_bn,al) == NULL) goto err; tmp_bn->d[bl]=0; bl++; i--; } else if (i == -1 && !BN_get_flags(a,BN_FLG_STATIC_DATA)) { BIGNUM *tmp_bn = (BIGNUM *)a; if (bn_wexpand(tmp_bn,bl) == NULL) goto err; tmp_bn->d[al]=0; al++; i++; } if (i == 0) { /* symmetric and > 4 */ /* 16 or larger */ j=BN_num_bits_word((BN_ULONG)al); j=1<<(j-1); k=j+j; t = BN_CTX_get(ctx); if (al == j) /* exact multiple */ { if (bn_wexpand(t,k*2) == NULL) goto err; if (bn_wexpand(rr,k*2) == NULL) goto err; bn_mul_recursive(rr->d,a->d,b->d,al,t->d); } else { if (bn_wexpand(t,k*4) == NULL) goto err; if (bn_wexpand(rr,k*4) == NULL) goto err; bn_mul_part_recursive(rr->d,a->d,b->d,al-j,j,t->d); } rr->top=top; goto end; } #endif } #endif /* BN_RECURSION */ if (bn_wexpand(rr,top) == NULL) goto err; rr->top=top; bn_mul_normal(rr->d,a->d,al,b->d,bl); #if defined(BN_MUL_COMBA) || defined(BN_RECURSION) end: #endif bn_correct_top(rr); if (r != rr) BN_copy(r,rr); ret=1; err: bn_check_top(r); BN_CTX_end(ctx); return(ret); }
377,456
0
void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n) { #ifdef BN_COUNT fprintf(stderr," bn_mul_low_normal %d * %d\n",n,n); #endif bn_mul_words(r,a,n,b[0]); for (;;) { if (--n <= 0) return; bn_mul_add_words(&(r[1]),a,n,b[1]); if (--n <= 0) return; bn_mul_add_words(&(r[2]),a,n,b[2]); if (--n <= 0) return; bn_mul_add_words(&(r[3]),a,n,b[3]); if (--n <= 0) return; bn_mul_add_words(&(r[4]),a,n,b[4]); r+=4; b+=4; } }
377,457
0
void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l, int n2, BN_ULONG *t) { int i,n; int c1,c2; int neg,oneg,zero; BN_ULONG ll,lc,*lp,*mp; # ifdef BN_COUNT fprintf(stderr," bn_mul_high %d * %d\n",n2,n2); # endif n=n2/2; /* Calculate (al-ah)*(bh-bl) */ neg=zero=0; c1=bn_cmp_words(&(a[0]),&(a[n]),n); c2=bn_cmp_words(&(b[n]),&(b[0]),n); switch (c1*3+c2) { case -4: bn_sub_words(&(r[0]),&(a[n]),&(a[0]),n); bn_sub_words(&(r[n]),&(b[0]),&(b[n]),n); break; case -3: zero=1; break; case -2: bn_sub_words(&(r[0]),&(a[n]),&(a[0]),n); bn_sub_words(&(r[n]),&(b[n]),&(b[0]),n); neg=1; break; case -1: case 0: case 1: zero=1; break; case 2: bn_sub_words(&(r[0]),&(a[0]),&(a[n]),n); bn_sub_words(&(r[n]),&(b[0]),&(b[n]),n); neg=1; break; case 3: zero=1; break; case 4: bn_sub_words(&(r[0]),&(a[0]),&(a[n]),n); bn_sub_words(&(r[n]),&(b[n]),&(b[0]),n); break; } oneg=neg; /* t[10] = (a[0]-a[1])*(b[1]-b[0]) */ /* r[10] = (a[1]*b[1]) */ # ifdef BN_MUL_COMBA if (n == 8) { bn_mul_comba8(&(t[0]),&(r[0]),&(r[n])); bn_mul_comba8(r,&(a[n]),&(b[n])); } else # endif { bn_mul_recursive(&(t[0]),&(r[0]),&(r[n]),n,0,0,&(t[n2])); bn_mul_recursive(r,&(a[n]),&(b[n]),n,0,0,&(t[n2])); } /* s0 == low(al*bl) * s1 == low(ah*bh)+low((al-ah)*(bh-bl))+low(al*bl)+high(al*bl) * We know s0 and s1 so the only unknown is high(al*bl) * high(al*bl) == s1 - low(ah*bh+s0+(al-ah)*(bh-bl)) * high(al*bl) == s1 - (r[0]+l[0]+t[0]) */ if (l != NULL) { lp= &(t[n2+n]); c1=(int)(bn_add_words(lp,&(r[0]),&(l[0]),n)); } else { c1=0; lp= &(r[0]); } if (neg) neg=(int)(bn_sub_words(&(t[n2]),lp,&(t[0]),n)); else { bn_add_words(&(t[n2]),lp,&(t[0]),n); neg=0; } if (l != NULL) { bn_sub_words(&(t[n2+n]),&(l[n]),&(t[n2]),n); } else { lp= &(t[n2+n]); mp= &(t[n2]); for (i=0; i<n; i++) lp[i]=((~mp[i])+1)&BN_MASK2; } /* s[0] = low(al*bl) * t[3] = high(al*bl) * t[10] = (a[0]-a[1])*(b[1]-b[0]) neg is the sign * r[10] = (a[1]*b[1]) */ /* R[10] = al*bl * R[21] = al*bl + ah*bh + (a[0]-a[1])*(b[1]-b[0]) * R[32] = ah*bh */ /* R[1]=t[3]+l[0]+r[0](+-)t[0] (have carry/borrow) * R[2]=r[0]+t[3]+r[1](+-)t[1] (have carry/borrow) * R[3]=r[1]+(carry/borrow) */ if (l != NULL) { lp= &(t[n2]); c1= (int)(bn_add_words(lp,&(t[n2+n]),&(l[0]),n)); } else { lp= &(t[n2+n]); c1=0; } c1+=(int)(bn_add_words(&(t[n2]),lp, &(r[0]),n)); if (oneg) c1-=(int)(bn_sub_words(&(t[n2]),&(t[n2]),&(t[0]),n)); else c1+=(int)(bn_add_words(&(t[n2]),&(t[n2]),&(t[0]),n)); c2 =(int)(bn_add_words(&(r[0]),&(r[0]),&(t[n2+n]),n)); c2+=(int)(bn_add_words(&(r[0]),&(r[0]),&(r[n]),n)); if (oneg) c2-=(int)(bn_sub_words(&(r[0]),&(r[0]),&(t[n]),n)); else c2+=(int)(bn_add_words(&(r[0]),&(r[0]),&(t[n]),n)); if (c1 != 0) /* Add starting at r[0], could be +ve or -ve */ { i=0; if (c1 > 0) { lc=c1; do { ll=(r[i]+lc)&BN_MASK2; r[i++]=ll; lc=(lc > ll); } while (lc); } else { lc= -c1; do { ll=r[i]; r[i++]=(ll-lc)&BN_MASK2; lc=(lc > ll); } while (lc); } } if (c2 != 0) /* Add starting at r[1] */ { i=n; if (c2 > 0) { lc=c2; do { ll=(r[i]+lc)&BN_MASK2; r[i++]=ll; lc=(lc > ll); } while (lc); } else { lc= -c2; do { ll=r[i]; r[i++]=(ll-lc)&BN_MASK2; lc=(lc > ll); } while (lc); } } }
377,458
0
void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb) { BN_ULONG *rr; #ifdef BN_COUNT fprintf(stderr," bn_mul_normal %d * %d\n",na,nb); #endif if (na < nb) { int itmp; BN_ULONG *ltmp; itmp=na; na=nb; nb=itmp; ltmp=a; a=b; b=ltmp; } rr= &(r[na]); if (nb <= 0) { (void)bn_mul_words(r,a,na,0); return; } else rr[0]=bn_mul_words(r,a,na,b[0]); for (;;) { if (--nb <= 0) return; rr[1]=bn_mul_add_words(&(r[1]),a,na,b[1]); if (--nb <= 0) return; rr[2]=bn_mul_add_words(&(r[2]),a,na,b[2]); if (--nb <= 0) return; rr[3]=bn_mul_add_words(&(r[3]),a,na,b[3]); if (--nb <= 0) return; rr[4]=bn_mul_add_words(&(r[4]),a,na,b[4]); rr+=4; r+=4; b+=4; } }
377,461
0
static int pkcs7_encode_rinfo(PKCS7_RECIP_INFO *ri, unsigned char *key, int keylen) { EVP_PKEY_CTX *pctx = NULL; EVP_PKEY *pkey = NULL; unsigned char *ek = NULL; int ret = 0; size_t eklen; pkey = X509_get_pubkey(ri->cert); if (!pkey) return 0; pctx = EVP_PKEY_CTX_new(pkey, NULL); if (!pctx) return 0; if (EVP_PKEY_encrypt_init(pctx) <= 0) goto err; if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_PKCS7_ENCRYPT, 0, ri) <= 0) { PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, PKCS7_R_CTRL_ERROR); goto err; } if (EVP_PKEY_encrypt(pctx, NULL, &eklen, key, keylen) <= 0) goto err; ek = OPENSSL_malloc(eklen); if (ek == NULL) { PKCS7err(PKCS7_F_PKCS7_ENCODE_RINFO, ERR_R_MALLOC_FAILURE); goto err; } if (EVP_PKEY_encrypt(pctx, ek, &eklen, key, keylen) <= 0) goto err; ASN1_STRING_set0(ri->enc_key, ek, eklen); ek = NULL; ret = 1; err: EVP_PKEY_free(pkey); EVP_PKEY_CTX_free(pctx); OPENSSL_free(ek); return ret; }
377,462
0
BIO *PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert) { int i, j; BIO *out = NULL, *btmp = NULL, *etmp = NULL, *bio = NULL; X509_ALGOR *xa; ASN1_OCTET_STRING *data_body = NULL; const EVP_MD *evp_md; const EVP_CIPHER *evp_cipher = NULL; EVP_CIPHER_CTX *evp_ctx = NULL; X509_ALGOR *enc_alg = NULL; STACK_OF(X509_ALGOR) *md_sk = NULL; STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL; PKCS7_RECIP_INFO *ri = NULL; unsigned char *ek = NULL, *tkey = NULL; int eklen = 0, tkeylen = 0; if (p7 == NULL) { PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_INVALID_NULL_POINTER); return NULL; } if (p7->d.ptr == NULL) { PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT); return NULL; } i = OBJ_obj2nid(p7->type); p7->state = PKCS7_S_HEADER; switch (i) { case NID_pkcs7_signed: /* * p7->d.sign->contents is a PKCS7 structure consisting of a contentType * field and optional content. * data_body is NULL if that structure has no (=detached) content * or if the contentType is wrong (i.e., not "data"). */ data_body = PKCS7_get_octet_string(p7->d.sign->contents); if (!PKCS7_is_detached(p7) && data_body == NULL) { PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_INVALID_SIGNED_DATA_TYPE); goto err; } md_sk = p7->d.sign->md_algs; break; case NID_pkcs7_signedAndEnveloped: rsk = p7->d.signed_and_enveloped->recipientinfo; md_sk = p7->d.signed_and_enveloped->md_algs; /* data_body is NULL if the optional EncryptedContent is missing. */ data_body = p7->d.signed_and_enveloped->enc_data->enc_data; enc_alg = p7->d.signed_and_enveloped->enc_data->algorithm; evp_cipher = EVP_get_cipherbyobj(enc_alg->algorithm); if (evp_cipher == NULL) { PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_UNSUPPORTED_CIPHER_TYPE); goto err; } break; case NID_pkcs7_enveloped: rsk = p7->d.enveloped->recipientinfo; enc_alg = p7->d.enveloped->enc_data->algorithm; /* data_body is NULL if the optional EncryptedContent is missing. */ data_body = p7->d.enveloped->enc_data->enc_data; evp_cipher = EVP_get_cipherbyobj(enc_alg->algorithm); if (evp_cipher == NULL) { PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_UNSUPPORTED_CIPHER_TYPE); goto err; } break; default: PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); goto err; } /* Detached content must be supplied via in_bio instead. */ if (data_body == NULL && in_bio == NULL) { PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_CONTENT); goto err; } /* We will be checking the signature */ if (md_sk != NULL) { for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) { xa = sk_X509_ALGOR_value(md_sk, i); if ((btmp = BIO_new(BIO_f_md())) == NULL) { PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB); goto err; } j = OBJ_obj2nid(xa->algorithm); evp_md = EVP_get_digestbynid(j); if (evp_md == NULL) { PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_UNKNOWN_DIGEST_TYPE); goto err; } BIO_set_md(btmp, evp_md); if (out == NULL) out = btmp; else BIO_push(out, btmp); btmp = NULL; } } if (evp_cipher != NULL) { if ((etmp = BIO_new(BIO_f_cipher())) == NULL) { PKCS7err(PKCS7_F_PKCS7_DATADECODE, ERR_R_BIO_LIB); goto err; } /* * It was encrypted, we need to decrypt the secret key with the * private key */ /* * Find the recipientInfo which matches the passed certificate (if * any) */ if (pcert) { for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) { ri = sk_PKCS7_RECIP_INFO_value(rsk, i); if (!pkcs7_cmp_ri(ri, pcert)) break; ri = NULL; } if (ri == NULL) { PKCS7err(PKCS7_F_PKCS7_DATADECODE, PKCS7_R_NO_RECIPIENT_MATCHES_CERTIFICATE); goto err; } } /* If we haven't got a certificate try each ri in turn */ if (pcert == NULL) { /* * Always attempt to decrypt all rinfo even after success as a * defence against MMA timing attacks. */ for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) { ri = sk_PKCS7_RECIP_INFO_value(rsk, i); if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey) < 0) goto err; ERR_clear_error(); } } else { /* Only exit on fatal errors, not decrypt failure */ if (pkcs7_decrypt_rinfo(&ek, &eklen, ri, pkey) < 0) goto err; ERR_clear_error(); } evp_ctx = NULL; BIO_get_cipher_ctx(etmp, &evp_ctx); if (EVP_CipherInit_ex(evp_ctx, evp_cipher, NULL, NULL, NULL, 0) <= 0) goto err; if (EVP_CIPHER_asn1_to_param(evp_ctx, enc_alg->parameter) < 0) goto err; /* Generate random key as MMA defence */ tkeylen = EVP_CIPHER_CTX_key_length(evp_ctx); tkey = OPENSSL_malloc(tkeylen); if (!tkey) goto err; if (EVP_CIPHER_CTX_rand_key(evp_ctx, tkey) <= 0) goto err; if (ek == NULL) { ek = tkey; eklen = tkeylen; tkey = NULL; } if (eklen != EVP_CIPHER_CTX_key_length(evp_ctx)) { /* * Some S/MIME clients don't use the same key and effective key * length. The key length is determined by the size of the * decrypted RSA key. */ if (!EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen)) { /* Use random key as MMA defence */ OPENSSL_clear_free(ek, eklen); ek = tkey; eklen = tkeylen; tkey = NULL; } } /* Clear errors so we don't leak information useful in MMA */ ERR_clear_error(); if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0) goto err; OPENSSL_clear_free(ek, eklen); ek = NULL; OPENSSL_clear_free(tkey, tkeylen); tkey = NULL; if (out == NULL) out = etmp; else BIO_push(out, etmp); etmp = NULL; } if (in_bio != NULL) { bio = in_bio; } else { if (data_body->length > 0) bio = BIO_new_mem_buf(data_body->data, data_body->length); else { bio = BIO_new(BIO_s_mem()); BIO_set_mem_eof_return(bio, 0); } if (bio == NULL) goto err; } BIO_push(out, bio); bio = NULL; return out; err: OPENSSL_clear_free(ek, eklen); OPENSSL_clear_free(tkey, tkeylen); BIO_free_all(out); BIO_free_all(btmp); BIO_free_all(etmp); BIO_free_all(bio); return NULL; }
377,465
0
int PKCS7_SIGNER_INFO_sign(PKCS7_SIGNER_INFO *si) { EVP_MD_CTX mctx; EVP_PKEY_CTX *pctx; unsigned char *abuf = NULL; int alen; size_t siglen; const EVP_MD *md = NULL; md = EVP_get_digestbyobj(si->digest_alg->algorithm); if (md == NULL) return 0; EVP_MD_CTX_init(&mctx); if (EVP_DigestSignInit(&mctx, &pctx, md, NULL, si->pkey) <= 0) goto err; if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, EVP_PKEY_CTRL_PKCS7_SIGN, 0, si) <= 0) { PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR); goto err; } alen = ASN1_item_i2d((ASN1_VALUE *)si->auth_attr, &abuf, ASN1_ITEM_rptr(PKCS7_ATTR_SIGN)); if (!abuf) goto err; if (EVP_DigestSignUpdate(&mctx, abuf, alen) <= 0) goto err; OPENSSL_free(abuf); abuf = NULL; if (EVP_DigestSignFinal(&mctx, NULL, &siglen) <= 0) goto err; abuf = OPENSSL_malloc(siglen); if (!abuf) goto err; if (EVP_DigestSignFinal(&mctx, abuf, &siglen) <= 0) goto err; if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, EVP_PKEY_CTRL_PKCS7_SIGN, 1, si) <= 0) { PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SIGN, PKCS7_R_CTRL_ERROR); goto err; } EVP_MD_CTX_cleanup(&mctx); ASN1_STRING_set0(si->enc_digest, abuf, siglen); return 1; err: OPENSSL_free(abuf); EVP_MD_CTX_cleanup(&mctx); return 0; }
377,466
0
int PKCS7_signatureVerify(BIO *bio, PKCS7 *p7, PKCS7_SIGNER_INFO *si, X509 *x509) { ASN1_OCTET_STRING *os; EVP_MD_CTX mdc_tmp, *mdc; int ret = 0, i; int md_type; STACK_OF(X509_ATTRIBUTE) *sk; BIO *btmp; EVP_PKEY *pkey; EVP_MD_CTX_init(&mdc_tmp); if (!PKCS7_type_is_signed(p7) && !PKCS7_type_is_signedAndEnveloped(p7)) { PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_WRONG_PKCS7_TYPE); goto err; } md_type = OBJ_obj2nid(si->digest_alg->algorithm); btmp = bio; for (;;) { if ((btmp == NULL) || ((btmp = BIO_find_type(btmp, BIO_TYPE_MD)) == NULL)) { PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); goto err; } BIO_get_md_ctx(btmp, &mdc); if (mdc == NULL) { PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_INTERNAL_ERROR); goto err; } if (EVP_MD_CTX_type(mdc) == md_type) break; /* * Workaround for some broken clients that put the signature OID * instead of the digest OID in digest_alg->algorithm */ if (EVP_MD_pkey_type(EVP_MD_CTX_md(mdc)) == md_type) break; btmp = BIO_next(btmp); } /* * mdc is the digest ctx that we want, unless there are attributes, in * which case the digest is the signed attributes */ if (!EVP_MD_CTX_copy_ex(&mdc_tmp, mdc)) goto err; sk = si->auth_attr; if ((sk != NULL) && (sk_X509_ATTRIBUTE_num(sk) != 0)) { unsigned char md_dat[EVP_MAX_MD_SIZE], *abuf = NULL; unsigned int md_len; int alen; ASN1_OCTET_STRING *message_digest; if (!EVP_DigestFinal_ex(&mdc_tmp, md_dat, &md_len)) goto err; message_digest = PKCS7_digest_from_attributes(sk); if (!message_digest) { PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_UNABLE_TO_FIND_MESSAGE_DIGEST); goto err; } if ((message_digest->length != (int)md_len) || (memcmp(message_digest->data, md_dat, md_len))) { PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_DIGEST_FAILURE); ret = -1; goto err; } if (!EVP_VerifyInit_ex(&mdc_tmp, EVP_get_digestbynid(md_type), NULL)) goto err; alen = ASN1_item_i2d((ASN1_VALUE *)sk, &abuf, ASN1_ITEM_rptr(PKCS7_ATTR_VERIFY)); if (alen <= 0) { PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, ERR_R_ASN1_LIB); ret = -1; goto err; } if (!EVP_VerifyUpdate(&mdc_tmp, abuf, alen)) goto err; OPENSSL_free(abuf); } os = si->enc_digest; pkey = X509_get_pubkey(x509); if (!pkey) { ret = -1; goto err; } i = EVP_VerifyFinal(&mdc_tmp, os->data, os->length, pkey); EVP_PKEY_free(pkey); if (i <= 0) { PKCS7err(PKCS7_F_PKCS7_SIGNATUREVERIFY, PKCS7_R_SIGNATURE_FAILURE); ret = -1; goto err; } ret = 1; err: EVP_MD_CTX_cleanup(&mdc_tmp); return (ret); }
377,467
0
BIO *PKCS7_dataInit(PKCS7 *p7, BIO *bio) { int i; BIO *out = NULL, *btmp = NULL; X509_ALGOR *xa = NULL; const EVP_CIPHER *evp_cipher = NULL; STACK_OF(X509_ALGOR) *md_sk = NULL; STACK_OF(PKCS7_RECIP_INFO) *rsk = NULL; X509_ALGOR *xalg = NULL; PKCS7_RECIP_INFO *ri = NULL; ASN1_OCTET_STRING *os = NULL; if (p7 == NULL) { PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_INVALID_NULL_POINTER); return NULL; } /* * The content field in the PKCS7 ContentInfo is optional, but that really * only applies to inner content (precisely, detached signatures). * * When reading content, missing outer content is therefore treated as an * error. * * When creating content, PKCS7_content_new() must be called before * calling this method, so a NULL p7->d is always an error. */ if (p7->d.ptr == NULL) { PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_NO_CONTENT); return NULL; } i = OBJ_obj2nid(p7->type); p7->state = PKCS7_S_HEADER; switch (i) { case NID_pkcs7_signed: md_sk = p7->d.sign->md_algs; os = PKCS7_get_octet_string(p7->d.sign->contents); break; case NID_pkcs7_signedAndEnveloped: rsk = p7->d.signed_and_enveloped->recipientinfo; md_sk = p7->d.signed_and_enveloped->md_algs; xalg = p7->d.signed_and_enveloped->enc_data->algorithm; evp_cipher = p7->d.signed_and_enveloped->enc_data->cipher; if (evp_cipher == NULL) { PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_CIPHER_NOT_INITIALIZED); goto err; } break; case NID_pkcs7_enveloped: rsk = p7->d.enveloped->recipientinfo; xalg = p7->d.enveloped->enc_data->algorithm; evp_cipher = p7->d.enveloped->enc_data->cipher; if (evp_cipher == NULL) { PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_CIPHER_NOT_INITIALIZED); goto err; } break; case NID_pkcs7_digest: xa = p7->d.digest->md; os = PKCS7_get_octet_string(p7->d.digest->contents); break; case NID_pkcs7_data: break; default: PKCS7err(PKCS7_F_PKCS7_DATAINIT, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); goto err; } for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) if (!PKCS7_bio_add_digest(&out, sk_X509_ALGOR_value(md_sk, i))) goto err; if (xa && !PKCS7_bio_add_digest(&out, xa)) goto err; if (evp_cipher != NULL) { unsigned char key[EVP_MAX_KEY_LENGTH]; unsigned char iv[EVP_MAX_IV_LENGTH]; int keylen, ivlen; EVP_CIPHER_CTX *ctx; if ((btmp = BIO_new(BIO_f_cipher())) == NULL) { PKCS7err(PKCS7_F_PKCS7_DATAINIT, ERR_R_BIO_LIB); goto err; } BIO_get_cipher_ctx(btmp, &ctx); keylen = EVP_CIPHER_key_length(evp_cipher); ivlen = EVP_CIPHER_iv_length(evp_cipher); xalg->algorithm = OBJ_nid2obj(EVP_CIPHER_type(evp_cipher)); if (ivlen > 0) if (RAND_bytes(iv, ivlen) <= 0) goto err; if (EVP_CipherInit_ex(ctx, evp_cipher, NULL, NULL, NULL, 1) <= 0) goto err; if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0) goto err; if (EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, 1) <= 0) goto err; if (ivlen > 0) { if (xalg->parameter == NULL) { xalg->parameter = ASN1_TYPE_new(); if (xalg->parameter == NULL) goto err; } if (EVP_CIPHER_param_to_asn1(ctx, xalg->parameter) < 0) goto err; } /* Lets do the pub key stuff :-) */ for (i = 0; i < sk_PKCS7_RECIP_INFO_num(rsk); i++) { ri = sk_PKCS7_RECIP_INFO_value(rsk, i); if (pkcs7_encode_rinfo(ri, key, keylen) <= 0) goto err; } OPENSSL_cleanse(key, keylen); if (out == NULL) out = btmp; else BIO_push(out, btmp); btmp = NULL; } if (bio == NULL) { if (PKCS7_is_detached(p7)) bio = BIO_new(BIO_s_null()); else if (os && os->length > 0) bio = BIO_new_mem_buf(os->data, os->length); if (bio == NULL) { bio = BIO_new(BIO_s_mem()); if (bio == NULL) goto err; BIO_set_mem_eof_return(bio, 0); } } if (out) BIO_push(out, bio); else out = bio; return out; err: BIO_free_all(out); BIO_free_all(btmp); return NULL; }
377,468
0
static bool auth_request_proxy_is_self(struct auth_request *request) { const char *port = NULL; /* check if the port is the same */ port = auth_fields_find(request->fields.extra_fields, "port"); if (port != NULL && !str_uint_equals(port, request->fields.local_port)) return FALSE; /* don't check destuser. in some systems destuser is intentionally changed to proxied connections, but that shouldn't affect the proxying decision. it's unlikely any systems would actually want to proxy a connection to itself only to change the username, since it can already be done without proxying by changing the "user" field. */ return TRUE; }
377,469
0
passdb_result_to_string(enum passdb_result result) { switch (result) { case PASSDB_RESULT_INTERNAL_FAILURE: return "internal_failure"; case PASSDB_RESULT_SCHEME_NOT_AVAILABLE: return "scheme_not_available"; case PASSDB_RESULT_USER_UNKNOWN: return "user_unknown"; case PASSDB_RESULT_USER_DISABLED: return "user_disabled"; case PASSDB_RESULT_PASS_EXPIRED: return "pass_expired"; case PASSDB_RESULT_NEXT: return "next"; case PASSDB_RESULT_PASSWORD_MISMATCH: return "password_mismatch"; case PASSDB_RESULT_OK: return "ok"; } i_unreached(); }
377,470
0
void auth_request_log_password_mismatch(struct auth_request *request, const char *subsystem) { auth_request_log_login_failure(request, subsystem, AUTH_LOG_MSG_PASSWORD_MISMATCH); }
377,471
0
void auth_request_log_error(struct auth_request *auth_request, const char *subsystem, const char *format, ...) { struct event *event = get_request_event(auth_request, subsystem); va_list va; va_start(va, format); T_BEGIN { string_t *str = t_str_new(128); str_vprintfa(str, format, va); e_error(event, "%s", str_c(str)); } T_END; va_end(va); }
377,474
0
void auth_request_unref(struct auth_request **_request) { struct auth_request *request = *_request; *_request = NULL; i_assert(request->refcount > 0); if (--request->refcount > 0) return; i_assert(array_count(&request->authdb_event) == 0); if (request->handler_pending_reply) auth_request_handler_abort(request); event_unref(&request->mech_event); event_unref(&request->event); auth_request_state_count[request->state]--; auth_refresh_proctitle(); if (request->mech_password != NULL) { safe_memset(request->mech_password, 0, strlen(request->mech_password)); } if (request->dns_lookup_ctx != NULL) dns_lookup_abort(&request->dns_lookup_ctx->dns_lookup); timeout_remove(&request->to_abort); timeout_remove(&request->to_penalty); if (request->mech != NULL) request->mech->auth_free(request); else pool_unref(&request->pool); }
377,476
0
auth_request_new(const struct mech_module *mech, struct event *parent_event) { struct auth_request *request; request = mech->auth_new(); request->mech = mech; auth_request_post_alloc_init(request, parent_event); return request; }
377,477
0
void auth_request_continue(struct auth_request *request, const unsigned char *data, size_t data_size) { i_assert(request->state == AUTH_REQUEST_STATE_MECH_CONTINUE); if (request->fields.successful) { auth_request_success(request, "", 0); return; } if (auth_request_fail_on_nuls(request, data, data_size)) return; auth_request_refresh_last_access(request); request->mech->auth_continue(request, data, data_size); }
377,479
0
static void log_password_failure(struct auth_request *request, const char *plain_password, const char *crypted_password, const char *scheme, const struct password_generate_params *params, const char *subsystem) { struct event *event = get_request_event(request, subsystem); static bool scheme_ok = FALSE; string_t *str = t_str_new(256); const char *working_scheme; str_printfa(str, "%s(%s) != '%s'", scheme, plain_password, crypted_password); if (!scheme_ok) { /* perhaps the scheme is wrong - see if we can find a working one */ working_scheme = password_scheme_detect(plain_password, crypted_password, params); if (working_scheme != NULL) { str_printfa(str, ", try %s scheme instead", working_scheme); } } e_debug(event, "%s", str_c(str)); }
377,482
0
void auth_request_userdb_lookup_end(struct auth_request *request, enum userdb_result result) { i_assert(array_count(&request->authdb_event) > 0); struct event *event = authdb_event(request); struct event_passthrough *e = event_create_passthrough(event)-> set_name("auth_userdb_request_finished")-> add_str("result", userdb_result_to_string(result)); if (request->userdb_cache_result != AUTH_REQUEST_CACHE_NONE && request->set->cache_ttl != 0 && request->set->cache_size != 0) e->add_str("cache", auth_request_cache_result_to_str(request->userdb_cache_result)); e_debug(e->event(), "Finished userdb lookup"); event_unref(&event); array_pop_back(&request->authdb_event); }
377,484
0
void auth_request_lookup_credentials_policy_continue(struct auth_request *request, lookup_credentials_callback_t *callback) { struct auth_passdb *passdb; const char *cache_key, *cache_cred, *cache_scheme, *error; enum passdb_result result; i_assert(request->state == AUTH_REQUEST_STATE_MECH_CONTINUE); if (auth_request_is_disabled_master_user(request)) { callback(PASSDB_RESULT_USER_UNKNOWN, NULL, 0, request); return; } passdb = request->passdb; while (passdb != NULL && auth_request_want_skip_passdb(request, passdb)) passdb = passdb->next; request->passdb = passdb; if (passdb == NULL) { auth_request_log_error(request, request->mech != NULL ? AUTH_SUBSYS_MECH : "none", "All password databases were skipped"); callback(PASSDB_RESULT_INTERNAL_FAILURE, NULL, 0, request); return; } auth_request_passdb_lookup_begin(request); request->private_callback.lookup_credentials = callback; cache_key = passdb_cache == NULL ? NULL : passdb->cache_key; if (cache_key != NULL) { if (passdb_cache_lookup_credentials(request, cache_key, &cache_cred, &cache_scheme, &result, FALSE)) { request->passdb_cache_result = AUTH_REQUEST_CACHE_HIT; passdb_handle_credentials( result, cache_cred, cache_scheme, auth_request_lookup_credentials_finish, request); return; } else { request->passdb_cache_result = AUTH_REQUEST_CACHE_MISS; } } auth_request_set_state(request, AUTH_REQUEST_STATE_PASSDB); if (passdb->passdb->iface.lookup_credentials == NULL) { /* this passdb doesn't support credentials */ e_debug(authdb_event(request), "passdb doesn't support credential lookups"); auth_request_lookup_credentials_callback( PASSDB_RESULT_SCHEME_NOT_AVAILABLE, uchar_empty_ptr, 0, request); } else if (passdb->passdb->blocking) { passdb_blocking_lookup_credentials(request); } else if (passdb_template_export(passdb->default_fields_tmpl, request, &error) < 0) { e_error(authdb_event(request), "Failed to expand default_fields: %s", error); auth_request_lookup_credentials_callback( PASSDB_RESULT_INTERNAL_FAILURE, uchar_empty_ptr, 0, request); } else { passdb->passdb->iface.lookup_credentials(request, auth_request_lookup_credentials_callback); } }
377,486
0
void auth_request_success_continue(struct auth_policy_check_ctx *ctx) { struct auth_request *request = ctx->request; i_assert(request->state == AUTH_REQUEST_STATE_MECH_CONTINUE); timeout_remove(&request->to_penalty); if (request->failed || !request->passdb_success) { /* password was valid, but some other check failed. */ auth_request_fail(request); return; } auth_request_set_auth_successful(request); /* log before delay */ auth_request_log_finished(request); if (request->delay_until > ioloop_time) { unsigned int delay_secs = request->delay_until - ioloop_time; request->to_penalty = timeout_add(delay_secs * 1000, auth_request_success_continue, ctx); return; } if (ctx->success_data->used > 0 && !request->fields.final_resp_ok) { /* we'll need one more SASL round, since client doesn't support the final SASL response */ auth_request_handler_reply_continue(request, ctx->success_data->data, ctx->success_data->used); return; } auth_request_set_state(request, AUTH_REQUEST_STATE_FINISHED); auth_request_refresh_last_access(request); auth_request_handler_reply(request, AUTH_CLIENT_RESULT_SUCCESS, ctx->success_data->data, ctx->success_data->used); }
377,488
0
get_updated_username(const char *old_username, const char *name, const char *value) { const char *p; if (strcmp(name, "user") == 0) { /* replace the whole username */ return value; } p = strchr(old_username, '@'); if (strcmp(name, "username") == 0) { if (strchr(value, '@') != NULL) return value; /* preserve the current @domain */ return t_strconcat(value, p, NULL); } if (strcmp(name, "domain") == 0) { if (p == NULL) { /* add the domain */ return t_strconcat(old_username, "@", value, NULL); } else { /* replace the existing domain */ p = t_strdup_until(old_username, p + 1); return t_strconcat(p, value, NULL); } } return NULL; }
377,489
0
void auth_request_lookup_credentials(struct auth_request *request, const char *scheme, lookup_credentials_callback_t *callback) { struct auth_policy_check_ctx *ctx; i_assert(request->state == AUTH_REQUEST_STATE_MECH_CONTINUE); if (request->wanted_credentials_scheme == NULL) request->wanted_credentials_scheme = p_strdup(request->pool, scheme); request->user_changed_by_lookup = FALSE; if (request->policy_processed || !request->set->policy_check_before_auth) auth_request_lookup_credentials_policy_continue(request, callback); else { ctx = p_new(request->pool, struct auth_policy_check_ctx, 1); ctx->request = request; ctx->callback_lookup = callback; ctx->type = AUTH_POLICY_CHECK_TYPE_LOOKUP; auth_policy_check(request, ctx->request->mech_password, auth_request_policy_check_callback, ctx); } }
377,491
0
static void auth_passdb_init(struct auth_passdb *passdb) { passdb_init(passdb->passdb); i_assert(passdb->passdb->default_pass_scheme != NULL || passdb->cache_key == NULL); }
377,493
0
void auth_request_verify_plain(struct auth_request *request, const char *password, verify_plain_callback_t *callback) { struct auth_policy_check_ctx *ctx; i_assert(request->state == AUTH_REQUEST_STATE_MECH_CONTINUE); if (request->mech_password == NULL) request->mech_password = p_strdup(request->pool, password); else i_assert(request->mech_password == password); request->user_changed_by_lookup = FALSE; if (request->policy_processed || !request->set->policy_check_before_auth) { request->handler->verify_plain_continue_callback(request, callback); } else { ctx = p_new(request->pool, struct auth_policy_check_ctx, 1); ctx->request = request; ctx->callback_plain = callback; ctx->type = AUTH_POLICY_CHECK_TYPE_PLAIN; auth_policy_check(request, request->mech_password, auth_request_policy_check_callback, ctx); } }
377,494
0
void auth_request_set_field(struct auth_request *request, const char *name, const char *value, const char *default_scheme) { const char *suffix; size_t name_len = strlen(name); i_assert(*name != '\0'); i_assert(value != NULL); i_assert(request->passdb != NULL); if (name_len > 10 && strcmp(name+name_len-10, ":protected") == 0) { /* set this field only if it hasn't been set before */ name = t_strndup(name, name_len-10); if (auth_fields_exists(request->fields.extra_fields, name)) return; } else if (name_len > 7 && strcmp(name+name_len-7, ":remove") == 0) { /* remove this field entirely */ name = t_strndup(name, name_len-7); auth_fields_remove(request->fields.extra_fields, name); return; } if (strcmp(name, "password") == 0) { auth_request_set_password(request, value, default_scheme, FALSE); return; } if (strcmp(name, "password_noscheme") == 0) { auth_request_set_password(request, value, default_scheme, TRUE); return; } if (auth_request_try_update_username(request, name, value)) { /* don't change the original value so it gets saved correctly to cache. */ } else if (strcmp(name, "login_user") == 0) { auth_request_set_login_username_forced(request, value); } else if (strcmp(name, "allow_nets") == 0) { auth_request_validate_networks(request, name, value, &request->fields.remote_ip); } else if (strcmp(name, "fail") == 0) { request->failed = TRUE; } else if (strcmp(name, "delay_until") == 0) { time_t timestamp; unsigned int extra_secs = 0; const char *p; p = strchr(value, '+'); if (p != NULL) { value = t_strdup_until(value, p++); if (str_to_uint(p, &extra_secs) < 0) { e_error(authdb_event(request), "Invalid delay_until randomness number '%s'", p); request->failed = TRUE; } else { extra_secs = i_rand_limit(extra_secs); } } if (str_to_time(value, &timestamp) < 0) { e_error(authdb_event(request), "Invalid delay_until timestamp '%s'", value); request->failed = TRUE; } else if (timestamp <= ioloop_time) { /* no more delays */ } else if (timestamp - ioloop_time > AUTH_REQUEST_MAX_DELAY_SECS) { e_error(authdb_event(request), "delay_until timestamp %s is too much in the future, failing", value); request->failed = TRUE; } else { /* add randomness, but not too much of it */ timestamp += extra_secs; if (timestamp - ioloop_time > AUTH_REQUEST_MAX_DELAY_SECS) timestamp = ioloop_time + AUTH_REQUEST_MAX_DELAY_SECS; request->delay_until = timestamp; } } else if (strcmp(name, "allow_real_nets") == 0) { auth_request_validate_networks(request, name, value, &request->fields.real_remote_ip); } else if (str_begins(name, "userdb_", &suffix)) { /* for prefetch userdb */ request->userdb_prefetch_set = TRUE; if (request->fields.userdb_reply == NULL) auth_request_init_userdb_reply(request, TRUE); if (strcmp(name, "userdb_userdb_import") == 0) { /* we can't put the whole userdb_userdb_import value to extra_cache_fields or it doesn't work properly. so handle this explicitly. */ auth_request_passdb_import(request, value, "userdb_", default_scheme); return; } auth_request_set_userdb_field(request, suffix, value); } else if (strcmp(name, "noauthenticate") == 0) { /* add "nopassword" also so that passdbs won't try to verify the password. */ auth_fields_add(request->fields.extra_fields, name, value, 0); auth_fields_add(request->fields.extra_fields, "nopassword", NULL, 0); } else if (strcmp(name, "nopassword") == 0) { /* NULL password - anything goes */ const char *password = request->passdb_password; if (password != NULL && !auth_fields_exists(request->fields.extra_fields, "noauthenticate")) { (void)password_get_scheme(&password); if (*password != '\0') { e_error(authdb_event(request), "nopassword set but password is " "non-empty"); return; } } request->passdb_password = NULL; auth_fields_add(request->fields.extra_fields, name, value, 0); return; } else if (strcmp(name, "passdb_import") == 0) { auth_request_passdb_import(request, value, "", default_scheme); return; } else { /* these fields are returned to client */ auth_fields_add(request->fields.extra_fields, name, value, 0); return; } /* add the field unconditionally to extra_fields. this is required if a) auth cache is used, b) if we're a worker and we'll need to send this to the main auth process that can store it in the cache, c) for easily checking :protected fields' existence. */ auth_fields_add(request->fields.extra_fields, name, value, AUTH_FIELD_FLAG_HIDDEN); }
377,495
0
static enum auth_passdb_skip auth_passdb_skip_parse(const char *str) { if (strcmp(str, "never") == 0) return AUTH_PASSDB_SKIP_NEVER; if (strcmp(str, "authenticated") == 0) return AUTH_PASSDB_SKIP_AUTHENTICATED; if (strcmp(str, "unauthenticated") == 0) return AUTH_PASSDB_SKIP_UNAUTHENTICATED; i_unreached(); }
377,497
0
void auth_request_lookup_user(struct auth_request *request, userdb_callback_t *callback) { struct auth_userdb *userdb = request->userdb; const char *cache_key, *error; request->private_callback.userdb = callback; request->user_changed_by_lookup = FALSE; request->userdb_lookup = TRUE; request->userdb_cache_result = AUTH_REQUEST_CACHE_NONE; if (request->fields.userdb_reply == NULL) auth_request_init_userdb_reply(request, TRUE); else { /* we still want to set default_fields. these override any existing fields set by previous userdbs (because if that is unwanted, ":protected" can be used). */ if (userdb_template_export(userdb->default_fields_tmpl, request, &error) < 0) { e_error(authdb_event(request), "Failed to expand default_fields: %s", error); auth_request_userdb_callback( USERDB_RESULT_INTERNAL_FAILURE, request); return; } } auth_request_userdb_lookup_begin(request); /* (for now) auth_cache is shared between passdb and userdb */ cache_key = passdb_cache == NULL ? NULL : userdb->cache_key; if (cache_key != NULL) { enum userdb_result result; if (auth_request_lookup_user_cache(request, cache_key, &result, FALSE)) { request->userdb_cache_result = AUTH_REQUEST_CACHE_HIT; auth_request_userdb_callback(result, request); return; } else { request->userdb_cache_result = AUTH_REQUEST_CACHE_MISS; } } if (userdb->userdb->iface->lookup == NULL) { /* we are deinitializing */ auth_request_userdb_callback(USERDB_RESULT_INTERNAL_FAILURE, request); } else if (userdb->userdb->blocking) userdb_blocking_lookup(request); else userdb->userdb->iface->lookup(request, auth_request_userdb_callback); }
377,498
0
void auth_request_log_unknown_user(struct auth_request *request, const char *subsystem) { auth_request_log_login_failure(request, subsystem, "unknown user"); }
377,499
0
auth_request_passdb_import(struct auth_request *request, const char *args, const char *key_prefix, const char *default_scheme) { const char *const *arg, *field; for (arg = t_strsplit(args, "\t"); *arg != NULL; arg++) { field = t_strconcat(key_prefix, *arg, NULL); auth_request_set_field_keyvalue(request, field, default_scheme); } }
377,500
0
void passdb_deinit(struct passdb_module *passdb) { unsigned int idx; i_assert(passdb->init_refcount > 0); if (--passdb->init_refcount > 0) return; if (passdb_find(passdb->iface.name, passdb->args, &idx) == NULL) i_unreached(); array_delete(&passdb_modules, idx, 1); if (passdb->iface.deinit != NULL) passdb->iface.deinit(passdb); /* make sure passdb isn't accessed again */ passdb->iface = passdb_iface_deinit; }
377,501
0
static void auth_init(struct auth *auth) { struct auth_passdb *passdb; struct auth_userdb *userdb; struct dns_lookup_settings dns_set; for (passdb = auth->masterdbs; passdb != NULL; passdb = passdb->next) auth_passdb_init(passdb); for (passdb = auth->passdbs; passdb != NULL; passdb = passdb->next) auth_passdb_init(passdb); for (userdb = auth->userdbs; userdb != NULL; userdb = userdb->next) userdb_init(userdb->userdb); i_zero(&dns_set); dns_set.dns_client_socket_path = AUTH_DNS_SOCKET_PATH; dns_set.timeout_msecs = AUTH_DNS_DEFAULT_TIMEOUT_MSECS; dns_set.idle_timeout_msecs = AUTH_DNS_IDLE_TIMEOUT_MSECS; dns_set.cache_ttl_secs = AUTH_DNS_CACHE_TTL_SECS; auth->dns_client = dns_client_init(&dns_set); }
377,502
0
static bool auth_request_is_disabled_master_user(struct auth_request *request) { if (request->fields.requested_login_user == NULL || request->passdb != NULL) return FALSE; /* no masterdbs, master logins not supported */ e_info(request->mech_event, "Attempted master login with no master passdbs " "(trying to log in as user: %s)", request->fields.requested_login_user); return TRUE; }
377,503
0
int auth_request_password_verify(struct auth_request *request, const char *plain_password, const char *crypted_password, const char *scheme, const char *subsystem) { return auth_request_password_verify_log(request, plain_password, crypted_password, scheme, subsystem, TRUE); }
377,505
0
void auth_request_ref(struct auth_request *request) { request->refcount++; }
377,506