func
stringlengths 0
484k
| target
int64 0
1
| cwe
sequence | project
stringlengths 2
29
| commit_id
stringlengths 40
40
| hash
float64 1,215,700,430,453,689,100,000,000B
340,281,914,521,452,260,000,000,000,000B
| size
int64 1
24k
| message
stringlengths 0
13.3k
|
---|---|---|---|---|---|---|---|
static int zlib_stateful_init(COMP_CTX *ctx)
{
int err;
struct zlib_state *state =
(struct zlib_state *)OPENSSL_malloc(sizeof(struct zlib_state));
if (state == NULL)
goto err;
state->istream.zalloc = zlib_zalloc;
state->istream.zfree = zlib_zfree;
state->istream.opaque = Z_NULL;
state->istream.next_in = Z_NULL;
state->istream.next_out = Z_NULL;
state->istream.avail_in = 0;
state->istream.avail_out = 0;
err = inflateInit_(&state->istream,
ZLIB_VERSION, sizeof(z_stream));
if (err != Z_OK)
goto err;
state->ostream.zalloc = zlib_zalloc;
state->ostream.zfree = zlib_zfree;
state->ostream.opaque = Z_NULL;
state->ostream.next_in = Z_NULL;
state->ostream.next_out = Z_NULL;
state->ostream.avail_in = 0;
state->ostream.avail_out = 0;
err = deflateInit_(&state->ostream,Z_DEFAULT_COMPRESSION,
ZLIB_VERSION, sizeof(z_stream));
if (err != Z_OK)
goto err;
CRYPTO_new_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
CRYPTO_set_ex_data(&ctx->ex_data,zlib_stateful_ex_idx,state);
return 1;
err:
if (state) OPENSSL_free(state);
return 0;
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 239,679,627,525,714,950,000,000,000,000,000,000,000 | 40 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
static int bio_zlib_flush(BIO *b)
{
BIO_ZLIB_CTX *ctx;
int ret;
z_stream *zout;
ctx = (BIO_ZLIB_CTX *)b->ptr;
/* If no data written or already flush show success */
if(!ctx->obuf || (ctx->odone && !ctx->ocount)) return 1;
zout = &ctx->zout;
BIO_clear_retry_flags(b);
/* No more input data */
zout->next_in = NULL;
zout->avail_in = 0;
for(;;)
{
/* If data in output buffer write it first */
while(ctx->ocount)
{
ret = BIO_write(b->next_bio, ctx->optr, ctx->ocount);
if(ret <= 0)
{
BIO_copy_next_retry(b);
return ret;
}
ctx->optr += ret;
ctx->ocount -= ret;
}
if(ctx->odone) return 1;
/* Compress some more */
/* Reset buffer */
ctx->optr = ctx->obuf;
zout->next_out = ctx->obuf;
zout->avail_out = ctx->obufsize;
/* Compress some more */
ret = deflate(zout, Z_FINISH);
if(ret == Z_STREAM_END) ctx->odone = 1;
else if(ret != Z_OK)
{
COMPerr(COMP_F_BIO_ZLIB_FLUSH,
COMP_R_ZLIB_DEFLATE_ERROR);
ERR_add_error_data(2, "zlib error:", zError(ret));
return 0;
}
ctx->ocount = ctx->obufsize - zout->avail_out;
}
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 128,186,999,418,185,140,000,000,000,000,000,000,000 | 48 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
static int zlib_stateful_compress_block(COMP_CTX *ctx, unsigned char *out,
unsigned int olen, unsigned char *in, unsigned int ilen)
{
int err = Z_OK;
struct zlib_state *state =
(struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
zlib_stateful_ex_idx);
if (state == NULL)
return -1;
state->ostream.next_in = in;
state->ostream.avail_in = ilen;
state->ostream.next_out = out;
state->ostream.avail_out = olen;
if (ilen > 0)
err = deflate(&state->ostream, Z_SYNC_FLUSH);
if (err != Z_OK)
return -1;
#ifdef DEBUG_ZLIB
fprintf(stderr,"compress(%4d)->%4d %s\n",
ilen,olen - state->ostream.avail_out,
(ilen != olen - state->ostream.avail_out)?"zlib":"clear");
#endif
return olen - state->ostream.avail_out;
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 285,263,019,612,552,700,000,000,000,000,000,000,000 | 26 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
static void zlib_zfree(void* opaque, void* address)
{
OPENSSL_free(address);
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 19,614,568,512,461,916,000,000,000,000,000,000,000 | 4 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
static void zlib_stateful_finish(COMP_CTX *ctx)
{
struct zlib_state *state =
(struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
zlib_stateful_ex_idx);
inflateEnd(&state->istream);
deflateEnd(&state->ostream);
OPENSSL_free(state);
CRYPTO_free_ex_data(CRYPTO_EX_INDEX_COMP,ctx,&ctx->ex_data);
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 196,507,213,193,854,240,000,000,000,000,000,000,000 | 10 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
static int bio_zlib_free(BIO *bi)
{
BIO_ZLIB_CTX *ctx;
if(!bi) return 0;
ctx = (BIO_ZLIB_CTX *)bi->ptr;
if(ctx->ibuf)
{
/* Destroy decompress context */
inflateEnd(&ctx->zin);
OPENSSL_free(ctx->ibuf);
}
if(ctx->obuf)
{
/* Destroy compress context */
deflateEnd(&ctx->zout);
OPENSSL_free(ctx->obuf);
}
OPENSSL_free(ctx);
bi->ptr = NULL;
bi->init = 0;
bi->flags = 0;
return 1;
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 154,806,281,249,588,380,000,000,000,000,000,000,000 | 23 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
static long bio_zlib_ctrl(BIO *b, int cmd, long num, void *ptr)
{
BIO_ZLIB_CTX *ctx;
int ret, *ip;
int ibs, obs;
if(!b->next_bio) return 0;
ctx = (BIO_ZLIB_CTX *)b->ptr;
switch (cmd)
{
case BIO_CTRL_RESET:
ctx->ocount = 0;
ctx->odone = 0;
ret = 1;
break;
case BIO_CTRL_FLUSH:
ret = bio_zlib_flush(b);
if (ret > 0)
ret = BIO_flush(b->next_bio);
break;
case BIO_C_SET_BUFF_SIZE:
ibs = -1;
obs = -1;
if (ptr != NULL)
{
ip = ptr;
if (*ip == 0)
ibs = (int) num;
else
obs = (int) num;
}
else
{
ibs = (int)num;
obs = ibs;
}
if (ibs != -1)
{
if (ctx->ibuf)
{
OPENSSL_free(ctx->ibuf);
ctx->ibuf = NULL;
}
ctx->ibufsize = ibs;
}
if (obs != -1)
{
if (ctx->obuf)
{
OPENSSL_free(ctx->obuf);
ctx->obuf = NULL;
}
ctx->obufsize = obs;
}
ret = 1;
break;
case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags(b);
ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
BIO_copy_next_retry(b);
break;
default:
ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
break;
}
return ret;
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 34,260,442,730,243,084,000,000,000,000,000,000,000 | 75 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
static int zlib_stateful_expand_block(COMP_CTX *ctx, unsigned char *out,
unsigned int olen, unsigned char *in, unsigned int ilen)
{
int err = Z_OK;
struct zlib_state *state =
(struct zlib_state *)CRYPTO_get_ex_data(&ctx->ex_data,
zlib_stateful_ex_idx);
if (state == NULL)
return 0;
state->istream.next_in = in;
state->istream.avail_in = ilen;
state->istream.next_out = out;
state->istream.avail_out = olen;
if (ilen > 0)
err = inflate(&state->istream, Z_SYNC_FLUSH);
if (err != Z_OK)
return -1;
#ifdef DEBUG_ZLIB
fprintf(stderr,"expand(%4d)->%4d %s\n",
ilen,olen - state->istream.avail_out,
(ilen != olen - state->istream.avail_out)?"zlib":"clear");
#endif
return olen - state->istream.avail_out;
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 288,989,699,090,275,300,000,000,000,000,000,000,000 | 27 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
static int bio_zlib_read(BIO *b, char *out, int outl)
{
BIO_ZLIB_CTX *ctx;
int ret;
z_stream *zin;
if(!out || !outl) return 0;
ctx = (BIO_ZLIB_CTX *)b->ptr;
zin = &ctx->zin;
BIO_clear_retry_flags(b);
if(!ctx->ibuf)
{
ctx->ibuf = OPENSSL_malloc(ctx->ibufsize);
if(!ctx->ibuf)
{
COMPerr(COMP_F_BIO_ZLIB_READ, ERR_R_MALLOC_FAILURE);
return 0;
}
inflateInit(zin);
zin->next_in = ctx->ibuf;
zin->avail_in = 0;
}
/* Copy output data directly to supplied buffer */
zin->next_out = (unsigned char *)out;
zin->avail_out = (unsigned int)outl;
for(;;)
{
/* Decompress while data available */
while(zin->avail_in)
{
ret = inflate(zin, 0);
if((ret != Z_OK) && (ret != Z_STREAM_END))
{
COMPerr(COMP_F_BIO_ZLIB_READ,
COMP_R_ZLIB_INFLATE_ERROR);
ERR_add_error_data(2, "zlib error:",
zError(ret));
return 0;
}
/* If EOF or we've read everything then return */
if((ret == Z_STREAM_END) || !zin->avail_out)
return outl - zin->avail_out;
}
/* No data in input buffer try to read some in,
* if an error then return the total data read.
*/
ret = BIO_read(b->next_bio, ctx->ibuf, ctx->ibufsize);
if(ret <= 0)
{
/* Total data read */
int tot = outl - zin->avail_out;
BIO_copy_next_retry(b);
if(ret < 0) return (tot > 0) ? tot : ret;
return tot;
}
zin->avail_in = ret;
zin->next_in = ctx->ibuf;
}
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 149,119,966,792,032,790,000,000,000,000,000,000,000 | 60 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
static int zz_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source,
uLong sourceLen)
{
z_stream stream;
int err;
stream.next_in = (Bytef*)source;
stream.avail_in = (uInt)sourceLen;
/* Check for source > 64K on 16-bit machine: */
if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
stream.next_out = dest;
stream.avail_out = (uInt)*destLen;
if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
err = inflateInit_(&stream,
ZLIB_VERSION, sizeof(z_stream));
if (err != Z_OK) return err;
err = inflate(&stream, Z_FINISH);
if (err != Z_STREAM_END) {
inflateEnd(&stream);
return err;
}
*destLen = stream.total_out;
err = inflateEnd(&stream);
return err;
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 314,427,918,529,874,800,000,000,000,000,000,000,000 | 32 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
static void* zlib_zalloc(void* opaque, unsigned int no, unsigned int size)
{
void *p;
p=OPENSSL_malloc(no*size);
if (p)
memset(p, 0, no*size);
return p;
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 234,501,463,553,659,350,000,000,000,000,000,000,000 | 9 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
void COMP_zlib_cleanup(void)
{
#ifdef ZLIB_SHARED
if (zlib_dso)
DSO_free(zlib_dso);
#endif
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 9,971,663,184,966,114,000,000,000,000,000,000,000 | 7 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
static long bio_zlib_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
{
if(!b->next_bio)
return 0;
return
BIO_callback_ctrl(b->next_bio, cmd, fp);
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 98,438,626,371,639,220,000,000,000,000,000,000,000 | 7 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
BIO_METHOD *BIO_f_zlib(void)
{
return &bio_meth_zlib;
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 251,139,579,942,778,550,000,000,000,000,000,000,000 | 4 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
COMP_METHOD *COMP_zlib(void)
{
COMP_METHOD *meth = &zlib_method_nozlib;
#ifdef ZLIB_SHARED
if (!zlib_loaded)
{
#if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_WIN32)
zlib_dso = DSO_load(NULL, "ZLIB1", NULL, 0);
#else
zlib_dso = DSO_load(NULL, "z", NULL, 0);
#endif
if (zlib_dso != NULL)
{
p_compress
= (compress_ft) DSO_bind_func(zlib_dso,
"compress");
p_inflateEnd
= (inflateEnd_ft) DSO_bind_func(zlib_dso,
"inflateEnd");
p_inflate
= (inflate_ft) DSO_bind_func(zlib_dso,
"inflate");
p_inflateInit_
= (inflateInit__ft) DSO_bind_func(zlib_dso,
"inflateInit_");
p_deflateEnd
= (deflateEnd_ft) DSO_bind_func(zlib_dso,
"deflateEnd");
p_deflate
= (deflate_ft) DSO_bind_func(zlib_dso,
"deflate");
p_deflateInit_
= (deflateInit__ft) DSO_bind_func(zlib_dso,
"deflateInit_");
p_zError
= (zError__ft) DSO_bind_func(zlib_dso,
"zError");
if (p_compress && p_inflateEnd && p_inflate
&& p_inflateInit_ && p_deflateEnd
&& p_deflate && p_deflateInit_ && p_zError)
zlib_loaded++;
}
}
#endif
#ifdef ZLIB_SHARED
if (zlib_loaded)
#endif
#if defined(ZLIB) || defined(ZLIB_SHARED)
{
/* init zlib_stateful_ex_idx here so that in a multi-process
* application it's enough to intialize openssl before forking
* (idx will be inherited in all the children) */
if (zlib_stateful_ex_idx == -1)
{
CRYPTO_w_lock(CRYPTO_LOCK_COMP);
if (zlib_stateful_ex_idx == -1)
zlib_stateful_ex_idx =
CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_COMP,
0,NULL,NULL,NULL,NULL);
CRYPTO_w_unlock(CRYPTO_LOCK_COMP);
if (zlib_stateful_ex_idx == -1)
goto err;
}
meth = &zlib_stateful_method;
}
err:
#endif
return(meth);
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 270,648,158,121,396,070,000,000,000,000,000,000,000 | 74 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
static int zlib_compress_block(COMP_CTX *ctx, unsigned char *out,
unsigned int olen, unsigned char *in, unsigned int ilen)
{
unsigned long l;
int i;
int clear=1;
if (ilen > 128)
{
out[0]=1;
l=olen-1;
i=compress(&(out[1]),&l,in,(unsigned long)ilen);
if (i != Z_OK)
return(-1);
if (ilen > l)
{
clear=0;
l++;
}
}
if (clear)
{
out[0]=0;
memcpy(&(out[1]),in,ilen);
l=ilen+1;
}
#ifdef DEBUG_ZLIB
fprintf(stderr,"compress(%4d)->%4d %s\n",
ilen,(int)l,(clear)?"clear":"zlib");
#endif
return((int)l);
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 285,072,229,977,350,060,000,000,000,000,000,000,000 | 32 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
static int zlib_expand_block(COMP_CTX *ctx, unsigned char *out,
unsigned int olen, unsigned char *in, unsigned int ilen)
{
unsigned long l;
int i;
if (in[0])
{
l=olen;
i=zz_uncompress(out,&l,&(in[1]),(unsigned long)ilen-1);
if (i != Z_OK)
return(-1);
}
else
{
memcpy(out,&(in[1]),ilen-1);
l=ilen-1;
}
#ifdef DEBUG_ZLIB
fprintf(stderr,"expand (%4d)->%4d %s\n",
ilen,(int)l,in[0]?"zlib":"clear");
#endif
return((int)l);
} | 0 | [
"CWE-399"
] | openssl | 1b31b5ad560b16e2fe1cad54a755e3e6b5e778a3 | 162,633,503,378,913,820,000,000,000,000,000,000,000 | 24 | Modify compression code so it avoids using ex_data free functions. This
stops applications that call CRYPTO_free_all_ex_data() prematurely leaking
memory. |
void bn_init(BIGNUM *a)
{
static BIGNUM nilbn;
*a = nilbn;
bn_check_top(a);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 165,014,393,623,226,940,000,000,000,000,000,000,000 | 7 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
BIGNUM *BN_new(void)
{
BIGNUM *ret;
if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);
return (NULL);
}
ret->flags = BN_FLG_MALLOCED;
bn_check_top(ret);
return (ret);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 218,070,981,470,574,900,000,000,000,000,000,000,000 | 12 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
int BN_is_negative(const BIGNUM *a)
{
return (a->neg != 0);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 297,499,807,439,864,170,000,000,000,000,000,000,000 | 4 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
BIGNUM *BN_lebin2bn(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);
s += len;
/* Skip trailing zeroes. */
for ( ; len > 0 && s[-1] == 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--) {
s--;
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;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 158,277,307,648,799,800,000,000,000,000,000,000,000 | 46 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
{
int i;
BN_ULONG l;
bn_check_top(a);
i = BN_num_bytes(a);
if (tolen < i)
return -1;
/* Add trailing zeroes if necessary */
if (tolen > i)
memset(to + i, 0, tolen - i);
to += i;
while (i--) {
l = a->d[i / BN_BYTES];
to--;
*to = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff;
}
return tolen;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 61,507,084,604,781,420,000,000,000,000,000,000,000 | 19 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
BIGNUM *bn_expand2(BIGNUM *b, int words)
{
bn_check_top(b);
if (words > b->dmax) {
BN_ULONG *a = bn_expand_internal(b, words);
if (!a)
return NULL;
if (b->d) {
OPENSSL_cleanse(b->d, b->dmax * sizeof(b->d[0]));
bn_free_d(b);
}
b->d = a;
b->dmax = words;
}
bn_check_top(b);
return b;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 305,058,020,335,332,730,000,000,000,000,000,000,000 | 19 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
int BN_security_bits(int L, int N)
{
int secbits, bits;
if (L >= 15360)
secbits = 256;
else if (L >= 7680)
secbits = 192;
else if (L >= 3072)
secbits = 128;
else if (L >= 2048)
secbits = 112;
else if (L >= 1024)
secbits = 80;
else
return 0;
if (N == -1)
return secbits;
bits = N / 2;
if (bits < 80)
return 0;
return bits >= secbits ? secbits : bits;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 13,117,852,724,642,590,000,000,000,000,000,000,000 | 22 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 288,486,101,575,260,870,000,000,000,000,000,000,000 | 66 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 156,096,050,011,771,020,000,000,000,000,000,000,000 | 49 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 318,154,373,327,991,600,000,000,000,000,000,000,000 | 8 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 140,781,878,392,111,570,000,000,000,000,000,000,000 | 44 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
int BN_is_word(const BIGNUM *a, const BN_ULONG w)
{
return BN_abs_is_word(a, w) && (!w || !a->neg);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 131,431,571,601,989,320,000,000,000,000,000,000,000 | 4 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
{
dest->d = b->d;
dest->top = b->top;
dest->dmax = b->dmax;
dest->neg = b->neg;
dest->flags = ((dest->flags & BN_FLG_MALLOCED)
| (b->flags & ~BN_FLG_MALLOCED)
| BN_FLG_STATIC_DATA | flags);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 11,634,317,094,595,527,000,000,000,000,000,000,000 | 10 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 290,428,363,320,557,160,000,000,000,000,000,000,000 | 11 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
size_t num, const EC_POINT *points[], const BIGNUM *scalars[],
BN_CTX *ctx)
{
if ((scalar != NULL) && (num == 0)) {
/* In this case we want to compute scalar * GeneratorPoint:
* this codepath is reached most prominently by (ephemeral) key
* generation of EC cryptosystems (i.e. ECDSA keygen and sign setup,
* ECDH keygen/first half), where the scalar is always secret.
* This is why we ignore if BN_FLG_CONSTTIME is actually set and we
* always call the constant time version.
*/
return ec_mul_consttime(group, r, scalar, NULL, ctx);
}
if ((scalar == NULL) && (num == 1)) {
/* In this case we want to compute scalar * GenericPoint:
* this codepath is reached most prominently by the second half of
* ECDH, where the secret scalar is multiplied by the peer's public
* point.
* To protect the secret scalar, we ignore if BN_FLG_CONSTTIME is
* actually set and we always call the constant time version.
*/
return ec_mul_consttime(group, r, scalars[0], points[0], ctx);
}
BN_CTX *new_ctx = NULL;
const EC_POINT *generator = NULL;
EC_POINT *tmp = NULL;
size_t totalnum;
size_t blocksize = 0, numblocks = 0; /* for wNAF splitting */
size_t pre_points_per_block = 0;
size_t i, j;
int k;
int r_is_inverted = 0;
int r_is_at_infinity = 1;
size_t *wsize = NULL; /* individual window sizes */
signed char **wNAF = NULL; /* individual wNAFs */
size_t *wNAF_len = NULL;
size_t max_len = 0;
size_t num_val;
EC_POINT **val = NULL; /* precomputation */
EC_POINT **v;
EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' or
* 'pre_comp->points' */
const EC_PRE_COMP *pre_comp = NULL;
int num_scalar = 0; /* flag: will be set to 1 if 'scalar' must be
* treated like other scalars, i.e.
* precomputation is not available */
int ret = 0;
if (group->meth != r->meth) {
ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
return 0;
}
if ((scalar == NULL) && (num == 0)) {
return EC_POINT_set_to_infinity(group, r);
}
for (i = 0; i < num; i++) {
if (group->meth != points[i]->meth) {
ECerr(EC_F_EC_WNAF_MUL, EC_R_INCOMPATIBLE_OBJECTS);
return 0;
}
}
if (ctx == NULL) {
ctx = new_ctx = BN_CTX_new();
if (ctx == NULL)
goto err;
}
if (scalar != NULL) {
generator = EC_GROUP_get0_generator(group);
if (generator == NULL) {
ECerr(EC_F_EC_WNAF_MUL, EC_R_UNDEFINED_GENERATOR);
goto err;
}
/* look if we can use precomputed multiples of generator */
pre_comp = group->pre_comp.ec;
if (pre_comp && pre_comp->numblocks
&& (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) ==
0)) {
blocksize = pre_comp->blocksize;
/*
* determine maximum number of blocks that wNAF splitting may
* yield (NB: maximum wNAF length is bit length plus one)
*/
numblocks = (BN_num_bits(scalar) / blocksize) + 1;
/*
* we cannot use more blocks than we have precomputation for
*/
if (numblocks > pre_comp->numblocks)
numblocks = pre_comp->numblocks;
pre_points_per_block = (size_t)1 << (pre_comp->w - 1);
/* check that pre_comp looks sane */
if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block)) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
goto err;
}
} else {
/* can't use precomputation */
pre_comp = NULL;
numblocks = 1;
num_scalar = 1; /* treat 'scalar' like 'num'-th element of
* 'scalars' */
}
}
totalnum = num + numblocks;
wsize = OPENSSL_malloc(totalnum * sizeof(wsize[0]));
wNAF_len = OPENSSL_malloc(totalnum * sizeof(wNAF_len[0]));
/* include space for pivot */
wNAF = OPENSSL_malloc((totalnum + 1) * sizeof(wNAF[0]));
val_sub = OPENSSL_malloc(totalnum * sizeof(val_sub[0]));
/* Ensure wNAF is initialised in case we end up going to err */
if (wNAF != NULL)
wNAF[0] = NULL; /* preliminary pivot */
if (wsize == NULL || wNAF_len == NULL || wNAF == NULL || val_sub == NULL) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
goto err;
}
/*
* num_val will be the total number of temporarily precomputed points
*/
num_val = 0;
for (i = 0; i < num + num_scalar; i++) {
size_t bits;
bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
wsize[i] = EC_window_bits_for_scalar_size(bits);
num_val += (size_t)1 << (wsize[i] - 1);
wNAF[i + 1] = NULL; /* make sure we always have a pivot */
wNAF[i] =
bn_compute_wNAF((i < num ? scalars[i] : scalar), wsize[i],
&wNAF_len[i]);
if (wNAF[i] == NULL)
goto err;
if (wNAF_len[i] > max_len)
max_len = wNAF_len[i];
}
if (numblocks) {
/* we go here iff scalar != NULL */
if (pre_comp == NULL) {
if (num_scalar != 1) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
goto err;
}
/* we have already generated a wNAF for 'scalar' */
} else {
signed char *tmp_wNAF = NULL;
size_t tmp_len = 0;
if (num_scalar != 0) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
goto err;
}
/*
* use the window size for which we have precomputation
*/
wsize[num] = pre_comp->w;
tmp_wNAF = bn_compute_wNAF(scalar, wsize[num], &tmp_len);
if (!tmp_wNAF)
goto err;
if (tmp_len <= max_len) {
/*
* One of the other wNAFs is at least as long as the wNAF
* belonging to the generator, so wNAF splitting will not buy
* us anything.
*/
numblocks = 1;
totalnum = num + 1; /* don't use wNAF splitting */
wNAF[num] = tmp_wNAF;
wNAF[num + 1] = NULL;
wNAF_len[num] = tmp_len;
/*
* pre_comp->points starts with the points that we need here:
*/
val_sub[num] = pre_comp->points;
} else {
/*
* don't include tmp_wNAF directly into wNAF array - use wNAF
* splitting and include the blocks
*/
signed char *pp;
EC_POINT **tmp_points;
if (tmp_len < numblocks * blocksize) {
/*
* possibly we can do with fewer blocks than estimated
*/
numblocks = (tmp_len + blocksize - 1) / blocksize;
if (numblocks > pre_comp->numblocks) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
OPENSSL_free(tmp_wNAF);
goto err;
}
totalnum = num + numblocks;
}
/* split wNAF in 'numblocks' parts */
pp = tmp_wNAF;
tmp_points = pre_comp->points;
for (i = num; i < totalnum; i++) {
if (i < totalnum - 1) {
wNAF_len[i] = blocksize;
if (tmp_len < blocksize) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
OPENSSL_free(tmp_wNAF);
goto err;
}
tmp_len -= blocksize;
} else
/*
* last block gets whatever is left (this could be
* more or less than 'blocksize'!)
*/
wNAF_len[i] = tmp_len;
wNAF[i + 1] = NULL;
wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
if (wNAF[i] == NULL) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
OPENSSL_free(tmp_wNAF);
goto err;
}
memcpy(wNAF[i], pp, wNAF_len[i]);
if (wNAF_len[i] > max_len)
max_len = wNAF_len[i];
if (*tmp_points == NULL) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
OPENSSL_free(tmp_wNAF);
goto err;
}
val_sub[i] = tmp_points;
tmp_points += pre_points_per_block;
pp += blocksize;
}
OPENSSL_free(tmp_wNAF);
}
}
}
/*
* All points we precompute now go into a single array 'val'.
* 'val_sub[i]' is a pointer to the subarray for the i-th point, or to a
* subarray of 'pre_comp->points' if we already have precomputation.
*/
val = OPENSSL_malloc((num_val + 1) * sizeof(val[0]));
if (val == NULL) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
goto err;
}
val[num_val] = NULL; /* pivot element */
/* allocate points for precomputation */
v = val;
for (i = 0; i < num + num_scalar; i++) {
val_sub[i] = v;
for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++) {
*v = EC_POINT_new(group);
if (*v == NULL)
goto err;
v++;
}
}
if (!(v == val + num_val)) {
ECerr(EC_F_EC_WNAF_MUL, ERR_R_INTERNAL_ERROR);
goto err;
}
if ((tmp = EC_POINT_new(group)) == NULL)
goto err;
/*-
* prepare precomputed values:
* val_sub[i][0] := points[i]
* val_sub[i][1] := 3 * points[i]
* val_sub[i][2] := 5 * points[i]
* ...
*/
for (i = 0; i < num + num_scalar; i++) {
if (i < num) {
if (!EC_POINT_copy(val_sub[i][0], points[i]))
goto err;
} else {
if (!EC_POINT_copy(val_sub[i][0], generator))
goto err;
}
if (wsize[i] > 1) {
if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx))
goto err;
for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++) {
if (!EC_POINT_add
(group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx))
goto err;
}
}
}
if (!EC_POINTs_make_affine(group, num_val, val, ctx))
goto err;
r_is_at_infinity = 1;
for (k = max_len - 1; k >= 0; k--) {
if (!r_is_at_infinity) {
if (!EC_POINT_dbl(group, r, r, ctx))
goto err;
}
for (i = 0; i < totalnum; i++) {
if (wNAF_len[i] > (size_t)k) {
int digit = wNAF[i][k];
int is_neg;
if (digit) {
is_neg = digit < 0;
if (is_neg)
digit = -digit;
if (is_neg != r_is_inverted) {
if (!r_is_at_infinity) {
if (!EC_POINT_invert(group, r, ctx))
goto err;
}
r_is_inverted = !r_is_inverted;
}
/* digit > 0 */
if (r_is_at_infinity) {
if (!EC_POINT_copy(r, val_sub[i][digit >> 1]))
goto err;
r_is_at_infinity = 0;
} else {
if (!EC_POINT_add
(group, r, r, val_sub[i][digit >> 1], ctx))
goto err;
}
}
}
}
}
if (r_is_at_infinity) {
if (!EC_POINT_set_to_infinity(group, r))
goto err;
} else {
if (r_is_inverted)
if (!EC_POINT_invert(group, r, ctx))
goto err;
}
ret = 1;
err:
BN_CTX_free(new_ctx);
EC_POINT_free(tmp);
OPENSSL_free(wsize);
OPENSSL_free(wNAF_len);
if (wNAF != NULL) {
signed char **w;
for (w = wNAF; *w != NULL; w++)
OPENSSL_free(*w);
OPENSSL_free(wNAF);
}
if (val != NULL) {
for (v = val; *v != NULL; v++)
EC_POINT_clear_free(*v);
OPENSSL_free(val);
}
OPENSSL_free(val_sub);
return ret;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 21,075,151,985,458,596,000,000,000,000,000,000,000 | 400 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
int BN_get_flags(const BIGNUM *b, int n)
{
return b->flags & n;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 228,650,380,737,746,660,000,000,000,000,000,000,000 | 4 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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:-)
*
* <[email protected]>
*/
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);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 189,552,957,092,578,200,000,000,000,000,000,000,000 | 71 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
int BN_bn2bin(const BIGNUM *a, unsigned char *to)
{
return bn2binpad(a, to, -1);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 86,188,004,496,562,570,000,000,000,000,000,000,000 | 4 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 241,805,550,961,835,900,000,000,000,000,000,000,000 | 7 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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;
}
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 113,599,173,233,389,000,000,000,000,000,000,000,000 | 16 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 272,287,623,817,459,300,000,000,000,000,000,000,000 | 18 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 298,691,067,765,111,400,000,000,000,000,000,000,000 | 22 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 157,873,829,975,173,090,000,000,000,000,000,000,000 | 21 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
void BN_zero_ex(BIGNUM *a)
{
a->top = 0;
a->neg = 0;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 337,520,505,781,731,900,000,000,000,000,000,000,000 | 5 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
BN_GENCB *BN_GENCB_new(void)
{
BN_GENCB *ret;
if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
BNerr(BN_F_BN_GENCB_NEW, ERR_R_MALLOC_FAILURE);
return (NULL);
}
return ret;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 332,854,432,515,879,850,000,000,000,000,000,000,000 | 11 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
void BN_GENCB_free(BN_GENCB *cb)
{
if (cb == NULL)
return;
OPENSSL_free(cb);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 275,097,277,783,071,900,000,000,000,000,000,000,000 | 6 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 308,521,194,842,360,700,000,000,000,000,000,000,000 | 8 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 269,769,600,452,801,160,000,000,000,000,000,000,000 | 8 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 190,341,726,467,004,160,000,000,000,000,000,000,000 | 17 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
void *BN_GENCB_get_arg(BN_GENCB *cb)
{
return cb->arg;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 90,150,276,290,610,760,000,000,000,000,000,000,000 | 4 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 278,450,007,848,038,870,000,000,000,000,000,000,000 | 133 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
{
if (tolen < 0)
return -1;
return bn2binpad(a, to, tolen);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 186,833,854,385,055,560,000,000,000,000,000,000,000 | 6 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
void BN_set_flags(BIGNUM *b, int n)
{
b->flags |= n;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 5,756,056,329,044,936,000,000,000,000,000,000,000 | 4 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
void BN_swap(BIGNUM *a, BIGNUM *b)
{
int flags_old_a, flags_old_b;
BN_ULONG *tmp_d;
int tmp_top, tmp_dmax, tmp_neg;
bn_check_top(a);
bn_check_top(b);
flags_old_a = a->flags;
flags_old_b = b->flags;
tmp_d = a->d;
tmp_top = a->top;
tmp_dmax = a->dmax;
tmp_neg = a->neg;
a->d = b->d;
a->top = b->top;
a->dmax = b->dmax;
a->neg = b->neg;
b->d = tmp_d;
b->top = tmp_top;
b->dmax = tmp_dmax;
b->neg = tmp_neg;
a->flags =
(flags_old_a & BN_FLG_MALLOCED) | (flags_old_b & BN_FLG_STATIC_DATA);
b->flags =
(flags_old_b & BN_FLG_MALLOCED) | (flags_old_a & BN_FLG_STATIC_DATA);
bn_check_top(a);
bn_check_top(b);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 74,599,252,352,417,120,000,000,000,000,000,000,000 | 34 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
int BN_is_zero(const BIGNUM *a)
{
return a->top == 0;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 231,812,123,924,147,650,000,000,000,000,000,000,000 | 4 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
BIGNUM *BN_secure_new(void)
{
BIGNUM *ret = BN_new();
if (ret != NULL)
ret->flags |= BN_FLG_SECURE;
return (ret);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 38,336,803,791,955,637,000,000,000,000,000,000,000 | 7 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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));
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 272,023,311,622,811,720,000,000,000,000,000,000,000 | 4 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
BIGNUM *bn_wexpand(BIGNUM *a, int words)
{
return (words <= a->dmax) ? a : bn_expand2(a, words);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 309,215,999,478,922,000,000,000,000,000,000,000,000 | 4 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
int BN_is_one(const BIGNUM *a)
{
return BN_abs_is_word(a, 1) && !a->neg;
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 222,841,968,349,573,250,000,000,000,000,000,000,000 | 4 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 69,557,488,577,388,370,000,000,000,000,000,000,000 | 17 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 139,912,457,571,811,050,000,000,000,000,000,000,000 | 5 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
int BN_is_odd(const BIGNUM *a)
{
return (a->top > 0) && (a->d[0] & 1);
} | 0 | [
"CWE-310"
] | openssl | aab7c770353b1dc4ba045938c8fb446dd1c4531e | 72,900,211,708,384,780,000,000,000,000,000,000,000 | 4 | Elliptic curve scalar multiplication with timing attack defenses
Co-authored-by: Nicola Tuveri <[email protected]>
Co-authored-by: Cesar Pereida Garcia <[email protected]>
Co-authored-by: Sohaib ul Hassan <[email protected]>
Reviewed-by: Andy Polyakov <[email protected]>
Reviewed-by: Matt Caswell <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/6009)
(cherry picked from commit 40e48e54582e46c1a01e184ecf5bd31f4f7f8294) |
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;
} | 0 | [
"CWE-399",
"CWE-703"
] | openssl | 4924b37ee01f71ae19c94a8934b80eeb2f677932 | 151,095,309,243,455,940,000,000,000,000,000,000,000 | 15 | bn/bn_gf2m.c: avoid infinite loop wich malformed ECParamters.
CVE-2015-1788
Reviewed-by: Matt Caswell <[email protected]> |
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;
} | 0 | [
"CWE-399",
"CWE-703"
] | openssl | 4924b37ee01f71ae19c94a8934b80eeb2f677932 | 28,699,120,715,648,597,000,000,000,000,000,000,000 | 22 | bn/bn_gf2m.c: avoid infinite loop wich malformed ECParamters.
CVE-2015-1788
Reviewed-by: Matt Caswell <[email protected]> |
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;
} | 0 | [
"CWE-399",
"CWE-703"
] | openssl | 4924b37ee01f71ae19c94a8934b80eeb2f677932 | 16,470,008,496,217,762,000,000,000,000,000,000,000 | 22 | bn/bn_gf2m.c: avoid infinite loop wich malformed ECParamters.
CVE-2015-1788
Reviewed-by: Matt Caswell <[email protected]> |
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;
} | 0 | [
"CWE-399",
"CWE-703"
] | openssl | 4924b37ee01f71ae19c94a8934b80eeb2f677932 | 133,242,002,620,538,940,000,000,000,000,000,000,000 | 21 | bn/bn_gf2m.c: avoid infinite loop wich malformed ECParamters.
CVE-2015-1788
Reviewed-by: Matt Caswell <[email protected]> |
int BN_GF2m_mod_sqr(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_SQR, BN_R_INVALID_LENGTH);
goto err;
}
ret = BN_GF2m_mod_sqr_arr(r, a, arr, ctx);
bn_check_top(r);
err:
OPENSSL_free(arr);
return ret;
} | 0 | [
"CWE-399",
"CWE-703"
] | openssl | 4924b37ee01f71ae19c94a8934b80eeb2f677932 | 238,965,249,919,811,850,000,000,000,000,000,000,000 | 21 | bn/bn_gf2m.c: avoid infinite loop wich malformed ECParamters.
CVE-2015-1788
Reviewed-by: Matt Caswell <[email protected]> |
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;
} | 0 | [
"CWE-399",
"CWE-703"
] | openssl | 4924b37ee01f71ae19c94a8934b80eeb2f677932 | 232,170,239,946,991,350,000,000,000,000,000,000,000 | 20 | bn/bn_gf2m.c: avoid infinite loop wich malformed ECParamters.
CVE-2015-1788
Reviewed-by: Matt Caswell <[email protected]> |
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;
} | 0 | [
"CWE-399",
"CWE-703"
] | openssl | 4924b37ee01f71ae19c94a8934b80eeb2f677932 | 190,531,339,232,326,740,000,000,000,000,000,000,000 | 91 | bn/bn_gf2m.c: avoid infinite loop wich malformed ECParamters.
CVE-2015-1788
Reviewed-by: Matt Caswell <[email protected]> |
int X509_verify_cert(X509_STORE_CTX *ctx)
{
X509 *x, *xtmp, *xtmp2, *chain_ss = NULL;
int bad_chain = 0;
X509_VERIFY_PARAM *param = ctx->param;
int depth, i, ok = 0;
int num, j, retry;
int (*cb) (int xok, X509_STORE_CTX *xctx);
STACK_OF(X509) *sktmp = NULL;
if (ctx->cert == NULL) {
X509err(X509_F_X509_VERIFY_CERT, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
return -1;
}
cb = ctx->verify_cb;
/*
* first we make sure the chain we are going to build is present and that
* the first entry is in place
*/
if (ctx->chain == NULL) {
if (((ctx->chain = sk_X509_new_null()) == NULL) ||
(!sk_X509_push(ctx->chain, ctx->cert))) {
X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
goto end;
}
CRYPTO_add(&ctx->cert->references, 1, CRYPTO_LOCK_X509);
ctx->last_untrusted = 1;
}
/* We use a temporary STACK so we can chop and hack at it */
if (ctx->untrusted != NULL
&& (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
goto end;
}
num = sk_X509_num(ctx->chain);
x = sk_X509_value(ctx->chain, num - 1);
depth = param->depth;
for (;;) {
/* If we have enough, we break */
if (depth < num)
break; /* FIXME: If this happens, we should take
* note of it and, if appropriate, use the
* X509_V_ERR_CERT_CHAIN_TOO_LONG error code
* later. */
/* If we are self signed, we break */
if (cert_self_signed(x))
break;
/*
* If asked see if we can find issuer in trusted store first
*/
if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) {
ok = ctx->get_issuer(&xtmp, ctx, x);
if (ok < 0)
return ok;
/*
* If successful for now free up cert so it will be picked up
* again later.
*/
if (ok > 0) {
X509_free(xtmp);
break;
}
}
/* If we were passed a cert chain, use it first */
if (ctx->untrusted != NULL) {
xtmp = find_issuer(ctx, sktmp, x);
if (xtmp != NULL) {
if (!sk_X509_push(ctx->chain, xtmp)) {
X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
goto end;
}
CRYPTO_add(&xtmp->references, 1, CRYPTO_LOCK_X509);
(void)sk_X509_delete_ptr(sktmp, xtmp);
ctx->last_untrusted++;
x = xtmp;
num++;
/*
* reparse the full chain for the next one
*/
continue;
}
}
break;
}
/* Remember how many untrusted certs we have */
j = num;
/*
* at this point, chain should contain a list of untrusted certificates.
* We now need to add at least one trusted one, if possible, otherwise we
* complain.
*/
do {
/*
* Examine last certificate in chain and see if it is self signed.
*/
i = sk_X509_num(ctx->chain);
x = sk_X509_value(ctx->chain, i - 1);
if (cert_self_signed(x)) {
/* we have a self signed certificate */
if (sk_X509_num(ctx->chain) == 1) {
/*
* We have a single self signed certificate: see if we can
* find it in the store. We must have an exact match to avoid
* possible impersonation.
*/
ok = ctx->get_issuer(&xtmp, ctx, x);
if ((ok <= 0) || X509_cmp(x, xtmp)) {
ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
ctx->current_cert = x;
ctx->error_depth = i - 1;
if (ok == 1)
X509_free(xtmp);
bad_chain = 1;
ok = cb(0, ctx);
if (!ok)
goto end;
} else {
/*
* We have a match: replace certificate with store
* version so we get any trust settings.
*/
X509_free(x);
x = xtmp;
(void)sk_X509_set(ctx->chain, i - 1, x);
ctx->last_untrusted = 0;
}
} else {
/*
* extract and save self signed certificate for later use
*/
chain_ss = sk_X509_pop(ctx->chain);
ctx->last_untrusted--;
num--;
j--;
x = sk_X509_value(ctx->chain, num - 1);
}
}
/* We now lookup certs from the certificate store */
for (;;) {
/* If we have enough, we break */
if (depth < num)
break;
/* If we are self signed, we break */
if (cert_self_signed(x))
break;
ok = ctx->get_issuer(&xtmp, ctx, x);
if (ok < 0)
return ok;
if (ok == 0)
break;
x = xtmp;
if (!sk_X509_push(ctx->chain, x)) {
X509_free(xtmp);
X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
return 0;
}
num++;
}
/* we now have our chain, lets check it... */
i = check_trust(ctx);
/* If explicitly rejected error */
if (i == X509_TRUST_REJECTED)
goto end;
/*
* If it's not explicitly trusted then check if there is an alternative
* chain that could be used. We only do this if we haven't already
* checked via TRUSTED_FIRST and the user hasn't switched off alternate
* chain checking
*/
retry = 0;
if (i != X509_TRUST_TRUSTED
&& !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
&& !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) {
while (j-- > 1) {
STACK_OF(X509) *chtmp = ctx->chain;
xtmp2 = sk_X509_value(ctx->chain, j - 1);
/*
* Temporarily set chain to NULL so we don't discount
* duplicates: the same certificate could be an untrusted
* CA found in the trusted store.
*/
ctx->chain = NULL;
ok = ctx->get_issuer(&xtmp, ctx, xtmp2);
ctx->chain = chtmp;
if (ok < 0)
goto end;
/* Check if we found an alternate chain */
if (ok > 0) {
/*
* Free up the found cert we'll add it again later
*/
X509_free(xtmp);
/*
* Dump all the certs above this point - we've found an
* alternate chain
*/
while (num > j) {
xtmp = sk_X509_pop(ctx->chain);
X509_free(xtmp);
num--;
ctx->last_untrusted--;
}
retry = 1;
break;
}
}
}
} while (retry);
/*
* If not explicitly trusted then indicate error unless it's a single
* self signed certificate in which case we've indicated an error already
* and set bad_chain == 1
*/
if (i != X509_TRUST_TRUSTED && !bad_chain) {
if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) {
if (ctx->last_untrusted >= num)
ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
else
ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
ctx->current_cert = x;
} else {
sk_X509_push(ctx->chain, chain_ss);
num++;
ctx->last_untrusted = num;
ctx->current_cert = chain_ss;
ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
chain_ss = NULL;
}
ctx->error_depth = num - 1;
bad_chain = 1;
ok = cb(0, ctx);
if (!ok)
goto end;
}
/* We have the chain complete: now we need to check its purpose */
ok = check_chain_extensions(ctx);
if (!ok)
goto end;
/* Check name constraints */
ok = check_name_constraints(ctx);
if (!ok)
goto end;
ok = check_id(ctx);
if (!ok)
goto end;
/* We may as well copy down any DSA parameters that are required */
X509_get_pubkey_parameters(NULL, ctx->chain);
/*
* Check revocation status: we do this after copying parameters because
* they may be needed for CRL signature verification.
*/
ok = ctx->check_revocation(ctx);
if (!ok)
goto end;
i = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
ctx->param->flags);
if (i != X509_V_OK) {
ctx->error = i;
ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth);
ok = cb(0, ctx);
if (!ok)
goto end;
}
/* At this point, we have a chain and need to verify it */
if (ctx->verify != NULL)
ok = ctx->verify(ctx);
else
ok = internal_verify(ctx);
if (!ok)
goto end;
/* RFC 3779 path validation, now that CRL check has been done */
ok = v3_asid_validate_path(ctx);
if (!ok)
goto end;
ok = v3_addr_validate_path(ctx);
if (!ok)
goto end;
/* If we get this far evaluate policies */
if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
ok = ctx->check_policy(ctx);
if (ok)
goto done;
end:
X509_get_pubkey_parameters(NULL, ctx->chain);
done:
sk_X509_free(sktmp);
X509_free(chain_ss);
return ok;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | f48b83b4fb7d6689584cf25f61ca63a4891f5b11 | 25,600,539,333,668,666,000,000,000,000,000,000,000 | 319 | Fix length checks in X509_cmp_time to avoid out-of-bounds reads.
Also tighten X509_cmp_time to reject more than three fractional
seconds in the time; and to reject trailing garbage after the offset.
CVE-2015-1789
Reviewed-by: Viktor Dukhovni <[email protected]>
Reviewed-by: Richard Levitte <[email protected]> |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | f48b83b4fb7d6689584cf25f61ca63a4891f5b11 | 250,612,911,160,755,850,000,000,000,000,000,000,000 | 142 | Fix length checks in X509_cmp_time to avoid out-of-bounds reads.
Also tighten X509_cmp_time to reject more than three fractional
seconds in the time; and to reject trailing garbage after the offset.
CVE-2015-1789
Reviewed-by: Viktor Dukhovni <[email protected]>
Reviewed-by: Richard Levitte <[email protected]> |
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));
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | f48b83b4fb7d6689584cf25f61ca63a4891f5b11 | 10,233,075,794,053,689,000,000,000,000,000,000,000 | 16 | Fix length checks in X509_cmp_time to avoid out-of-bounds reads.
Also tighten X509_cmp_time to reject more than three fractional
seconds in the time; and to reject trailing garbage after the offset.
CVE-2015-1789
Reviewed-by: Viktor Dukhovni <[email protected]>
Reviewed-by: Richard Levitte <[email protected]> |
X509_STORE_CTX *X509_STORE_CTX_new(void)
{
X509_STORE_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
if (!ctx) {
X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
return NULL;
}
memset(ctx, 0, sizeof(*ctx));
return ctx;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | f48b83b4fb7d6689584cf25f61ca63a4891f5b11 | 42,098,334,755,407,580,000,000,000,000,000,000,000 | 11 | Fix length checks in X509_cmp_time to avoid out-of-bounds reads.
Also tighten X509_cmp_time to reject more than three fractional
seconds in the time; and to reject trailing garbage after the offset.
CVE-2015-1789
Reviewed-by: Viktor Dukhovni <[email protected]>
Reviewed-by: Richard Levitte <[email protected]> |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | f48b83b4fb7d6689584cf25f61ca63a4891f5b11 | 72,112,709,357,633,620,000,000,000,000,000,000,000 | 38 | Fix length checks in X509_cmp_time to avoid out-of-bounds reads.
Also tighten X509_cmp_time to reject more than three fractional
seconds in the time; and to reject trailing garbage after the offset.
CVE-2015-1789
Reviewed-by: Viktor Dukhovni <[email protected]>
Reviewed-by: Richard Levitte <[email protected]> |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | f48b83b4fb7d6689584cf25f61ca63a4891f5b11 | 294,384,614,130,377,270,000,000,000,000,000,000,000 | 104 | Fix length checks in X509_cmp_time to avoid out-of-bounds reads.
Also tighten X509_cmp_time to reject more than three fractional
seconds in the time; and to reject trailing garbage after the offset.
CVE-2015-1789
Reviewed-by: Viktor Dukhovni <[email protected]>
Reviewed-by: Richard Levitte <[email protected]> |
int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
STACK_OF(X509) *chain)
{
int ret = 1;
ctx->ctx = store;
ctx->current_method = 0;
ctx->cert = x509;
ctx->untrusted = chain;
ctx->crls = NULL;
ctx->last_untrusted = 0;
ctx->other_ctx = NULL;
ctx->valid = 0;
ctx->chain = NULL;
ctx->error = 0;
ctx->explicit_policy = 0;
ctx->error_depth = 0;
ctx->current_cert = NULL;
ctx->current_issuer = NULL;
ctx->current_crl = NULL;
ctx->current_crl_score = 0;
ctx->current_reasons = 0;
ctx->tree = NULL;
ctx->parent = NULL;
ctx->param = X509_VERIFY_PARAM_new();
if (!ctx->param) {
X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
return 0;
}
/*
* Inherit callbacks and flags from X509_STORE if not set use defaults.
*/
if (store)
ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
else
ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;
if (store) {
ctx->verify_cb = store->verify_cb;
ctx->cleanup = store->cleanup;
} else
ctx->cleanup = 0;
if (ret)
ret = X509_VERIFY_PARAM_inherit(ctx->param,
X509_VERIFY_PARAM_lookup("default"));
if (ret == 0) {
X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
return 0;
}
if (store && store->check_issued)
ctx->check_issued = store->check_issued;
else
ctx->check_issued = check_issued;
if (store && store->get_issuer)
ctx->get_issuer = store->get_issuer;
else
ctx->get_issuer = X509_STORE_CTX_get1_issuer;
if (store && store->verify_cb)
ctx->verify_cb = store->verify_cb;
else
ctx->verify_cb = null_callback;
if (store && store->verify)
ctx->verify = store->verify;
else
ctx->verify = internal_verify;
if (store && store->check_revocation)
ctx->check_revocation = store->check_revocation;
else
ctx->check_revocation = check_revocation;
if (store && store->get_crl)
ctx->get_crl = store->get_crl;
else
ctx->get_crl = NULL;
if (store && store->check_crl)
ctx->check_crl = store->check_crl;
else
ctx->check_crl = check_crl;
if (store && store->cert_crl)
ctx->cert_crl = store->cert_crl;
else
ctx->cert_crl = cert_crl;
if (store && store->lookup_certs)
ctx->lookup_certs = store->lookup_certs;
else
ctx->lookup_certs = X509_STORE_get1_certs;
if (store && store->lookup_crls)
ctx->lookup_crls = store->lookup_crls;
else
ctx->lookup_crls = X509_STORE_get1_crls;
ctx->check_policy = check_policy;
/*
* Since X509_STORE_CTX_cleanup does a proper "free" on the ex_data, we
* put a corresponding "new" here.
*/
if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
&(ctx->ex_data))) {
OPENSSL_free(ctx);
X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
return 0;
}
return 1;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | f48b83b4fb7d6689584cf25f61ca63a4891f5b11 | 252,086,015,175,272,060,000,000,000,000,000,000,000 | 119 | Fix length checks in X509_cmp_time to avoid out-of-bounds reads.
Also tighten X509_cmp_time to reject more than three fractional
seconds in the time; and to reject trailing garbage after the offset.
CVE-2015-1789
Reviewed-by: Viktor Dukhovni <[email protected]>
Reviewed-by: Richard Levitte <[email protected]> |
static int check_hosts(X509 *x, X509_VERIFY_PARAM_ID *id)
{
int i;
int n = sk_OPENSSL_STRING_num(id->hosts);
char *name;
for (i = 0; i < n; ++i) {
name = sk_OPENSSL_STRING_value(id->hosts, i);
if (X509_check_host(x, name, 0, id->hostflags, &id->peername) > 0)
return 1;
}
return n == 0;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | f48b83b4fb7d6689584cf25f61ca63a4891f5b11 | 278,440,080,266,475,020,000,000,000,000,000,000,000 | 13 | Fix length checks in X509_cmp_time to avoid out-of-bounds reads.
Also tighten X509_cmp_time to reject more than three fractional
seconds in the time; and to reject trailing garbage after the offset.
CVE-2015-1789
Reviewed-by: Viktor Dukhovni <[email protected]>
Reviewed-by: Richard Levitte <[email protected]> |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | f48b83b4fb7d6689584cf25f61ca63a4891f5b11 | 17,095,133,595,264,929,000,000,000,000,000,000,000 | 50 | Fix length checks in X509_cmp_time to avoid out-of-bounds reads.
Also tighten X509_cmp_time to reject more than three fractional
seconds in the time; and to reject trailing garbage after the offset.
CVE-2015-1789
Reviewed-by: Viktor Dukhovni <[email protected]>
Reviewed-by: Richard Levitte <[email protected]> |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | f48b83b4fb7d6689584cf25f61ca63a4891f5b11 | 205,791,197,466,164,470,000,000,000,000,000,000,000 | 40 | Fix length checks in X509_cmp_time to avoid out-of-bounds reads.
Also tighten X509_cmp_time to reject more than three fractional
seconds in the time; and to reject trailing garbage after the offset.
CVE-2015-1789
Reviewed-by: Viktor Dukhovni <[email protected]>
Reviewed-by: Richard Levitte <[email protected]> |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | f48b83b4fb7d6689584cf25f61ca63a4891f5b11 | 264,179,929,735,403,350,000,000,000,000,000,000,000 | 88 | Fix length checks in X509_cmp_time to avoid out-of-bounds reads.
Also tighten X509_cmp_time to reject more than three fractional
seconds in the time; and to reject trailing garbage after the offset.
CVE-2015-1789
Reviewed-by: Viktor Dukhovni <[email protected]>
Reviewed-by: Richard Levitte <[email protected]> |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 292,100,783,852,987,060,000,000,000,000,000,000,000 | 27 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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);
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 52,203,733,591,535,640,000,000,000,000,000,000,000 | 11 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 285,450,892,687,770,700,000,000,000,000,000,000,000 | 56 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 40,765,622,824,777,833,000,000,000,000,000,000,000 | 103 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 12,894,762,779,492,082,000,000,000,000,000,000,000 | 17 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 229,875,971,871,453,000,000,000,000,000,000,000,000 | 71 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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;
}
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 202,677,492,404,977,800,000,000,000,000,000,000,000 | 11 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 106,372,837,446,230,300,000,000,000,000,000,000,000 | 38 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
int X509v3_addr_get_range(IPAddressOrRange *aor,
const unsigned afi,
unsigned char *min,
unsigned char *max, const int length)
{
int afi_length = length_from_afi(afi);
if (aor == NULL || min == NULL || max == NULL ||
afi_length == 0 || length < afi_length ||
(aor->type != IPAddressOrRange_addressPrefix &&
aor->type != IPAddressOrRange_addressRange) ||
!extract_min_max(aor, min, max, afi_length))
return 0;
return afi_length;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 184,328,700,057,760,700,000,000,000,000,000,000,000 | 15 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 74,487,212,512,330,440,000,000,000,000,000,000,000 | 33 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
static int v6IPAddressOrRange_cmp(const IPAddressOrRange *const *a,
const IPAddressOrRange *const *b)
{
return IPAddressOrRange_cmp(*a, *b, 16);
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 92,812,376,292,574,990,000,000,000,000,000,000,000 | 5 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 214,381,896,770,645,800,000,000,000,000,000,000,000 | 45 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
int X509v3_addr_is_canonical(IPAddrBlocks *addr)
{
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];
IPAddressOrRanges *aors;
int i, j, k;
/*
* Empty extension is canonical.
*/
if (addr == NULL)
return 1;
/*
* Check whether the top-level list is in order.
*/
for (i = 0; i < sk_IPAddressFamily_num(addr) - 1; i++) {
const IPAddressFamily *a = sk_IPAddressFamily_value(addr, i);
const IPAddressFamily *b = sk_IPAddressFamily_value(addr, i + 1);
if (IPAddressFamily_cmp(&a, &b) >= 0)
return 0;
}
/*
* Top level's ok, now check each address family.
*/
for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
IPAddressFamily *f = sk_IPAddressFamily_value(addr, i);
int length = length_from_afi(X509v3_addr_get_afi(f));
/*
* Inheritance is canonical. Anything other than inheritance or
* a SEQUENCE OF IPAddressOrRange is an ASN.1 error or something.
*/
if (f == NULL || f->ipAddressChoice == NULL)
return 0;
switch (f->ipAddressChoice->type) {
case IPAddressChoice_inherit:
continue;
case IPAddressChoice_addressesOrRanges:
break;
default:
return 0;
}
/*
* It's an IPAddressOrRanges sequence, check it.
*/
aors = f->ipAddressChoice->u.addressesOrRanges;
if (sk_IPAddressOrRange_num(aors) == 0)
return 0;
for (j = 0; j < sk_IPAddressOrRange_num(aors) - 1; j++) {
IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, j);
IPAddressOrRange *b = sk_IPAddressOrRange_value(aors, j + 1);
if (!extract_min_max(a, a_min, a_max, length) ||
!extract_min_max(b, b_min, b_max, length))
return 0;
/*
* Punt misordered list, overlapping start, or inverted range.
*/
if (memcmp(a_min, b_min, length) >= 0 ||
memcmp(a_min, a_max, length) > 0 ||
memcmp(b_min, b_max, length) > 0)
return 0;
/*
* Punt if adjacent or overlapping. Check for adjacency by
* subtracting one from b_min first.
*/
for (k = length - 1; k >= 0 && b_min[k]-- == 0x00; k--) ;
if (memcmp(a_max, b_min, length) >= 0)
return 0;
/*
* Check for range that should be expressed as a prefix.
*/
if (a->type == IPAddressOrRange_addressRange &&
range_should_be_prefix(a_min, a_max, length) >= 0)
return 0;
}
/*
* Check range to see if it's inverted or should be a
* prefix.
*/
j = sk_IPAddressOrRange_num(aors) - 1;
{
IPAddressOrRange *a = sk_IPAddressOrRange_value(aors, j);
if (a != NULL && a->type == IPAddressOrRange_addressRange) {
if (!extract_min_max(a, a_min, a_max, length))
return 0;
if (memcmp(a_min, a_max, length) > 0 ||
range_should_be_prefix(a_min, a_max, length) >= 0)
return 0;
}
}
}
/*
* If we made it through all that, we're happy.
*/
return 1;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 262,494,286,412,719,970,000,000,000,000,000,000,000 | 105 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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];
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 179,005,897,959,288,350,000,000,000,000,000,000,000 | 9 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 200,210,521,504,750,450,000,000,000,000,000,000,000 | 14 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
static int i2r_IPAddrBlocks(const X509V3_EXT_METHOD *method,
void *ext, BIO *out, int indent)
{
const IPAddrBlocks *addr = ext;
int i;
for (i = 0; i < sk_IPAddressFamily_num(addr); i++) {
IPAddressFamily *f = sk_IPAddressFamily_value(addr, i);
const unsigned int afi = X509v3_addr_get_afi(f);
switch (afi) {
case IANA_AFI_IPV4:
BIO_printf(out, "%*sIPv4", indent, "");
break;
case IANA_AFI_IPV6:
BIO_printf(out, "%*sIPv6", indent, "");
break;
default:
BIO_printf(out, "%*sUnknown AFI %u", indent, "", afi);
break;
}
if (f->addressFamily->length > 2) {
switch (f->addressFamily->data[2]) {
case 1:
BIO_puts(out, " (Unicast)");
break;
case 2:
BIO_puts(out, " (Multicast)");
break;
case 3:
BIO_puts(out, " (Unicast/Multicast)");
break;
case 4:
BIO_puts(out, " (MPLS)");
break;
case 64:
BIO_puts(out, " (Tunnel)");
break;
case 65:
BIO_puts(out, " (VPLS)");
break;
case 66:
BIO_puts(out, " (BGP MDT)");
break;
case 128:
BIO_puts(out, " (MPLS-labeled VPN)");
break;
default:
BIO_printf(out, " (Unknown SAFI %u)",
(unsigned)f->addressFamily->data[2]);
break;
}
}
switch (f->ipAddressChoice->type) {
case IPAddressChoice_inherit:
BIO_puts(out, ": inherit\n");
break;
case IPAddressChoice_addressesOrRanges:
BIO_puts(out, ":\n");
if (!i2r_IPAddressOrRanges(out,
indent + 2,
f->ipAddressChoice->
u.addressesOrRanges, afi))
return 0;
break;
}
}
return 1;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 199,205,484,802,727,830,000,000,000,000,000,000,000 | 67 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 208,144,480,708,555,040,000,000,000,000,000,000,000 | 36 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 226,213,497,331,838,780,000,000,000,000,000,000,000 | 16 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
static int v4IPAddressOrRange_cmp(const IPAddressOrRange *const *a,
const IPAddressOrRange *const *b)
{
return IPAddressOrRange_cmp(*a, *b, 4);
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 259,679,650,323,767,000,000,000,000,000,000,000,000 | 5 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 126,101,019,186,620,770,000,000,000,000,000,000,000 | 44 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
static int extract_min_max(IPAddressOrRange *aor,
unsigned char *min, unsigned char *max, int length)
{
if (aor == NULL || min == NULL || max == NULL)
return 0;
switch (aor->type) {
case IPAddressOrRange_addressPrefix:
return (addr_expand(min, aor->u.addressPrefix, length, 0x00) &&
addr_expand(max, aor->u.addressPrefix, length, 0xFF));
case IPAddressOrRange_addressRange:
return (addr_expand(min, aor->u.addressRange->min, length, 0x00) &&
addr_expand(max, aor->u.addressRange->max, length, 0xFF));
}
return 0;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 23,525,152,507,353,740,000,000,000,000,000,000,000 | 15 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
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;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 4,236,588,451,454,879,000,000,000,000,000,000,000 | 19 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
static int IPAddressFamily_cmp(const IPAddressFamily *const *a_,
const IPAddressFamily *const *b_)
{
const ASN1_OCTET_STRING *a = (*a_)->addressFamily;
const ASN1_OCTET_STRING *b = (*b_)->addressFamily;
int len = ((a->length <= b->length) ? a->length : b->length);
int cmp = memcmp(a->data, b->data, len);
return cmp ? cmp : a->length - b->length;
} | 0 | [
"CWE-119",
"CWE-787"
] | openssl | 068b963bb7afc57f5bdd723de0dd15e7795d5822 | 299,072,778,799,764,420,000,000,000,000,000,000,000 | 9 | Avoid out-of-bounds read
Fixes CVE 2017-3735
Reviewed-by: Kurt Roeckx <[email protected]>
(Merged from https://github.com/openssl/openssl/pull/4276)
(cherry picked from commit b23171744b01e473ebbfd6edad70c1c3825ffbcd) |
Subsets and Splits