2016-05-17 18:51:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 18:51:34 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2012-04-19 16:19:56 +00:00
|
|
|
#include <limits.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2016-06-04 11:48:00 +00:00
|
|
|
#include "internal/numbers.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/buffer.h>
|
2015-03-30 19:31:49 +00:00
|
|
|
#include <openssl/asn1.h>
|
2019-09-27 21:57:58 +00:00
|
|
|
#include "crypto/asn1.h"
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2000-12-30 02:40:26 +00:00
|
|
|
#ifndef NO_OLD_ASN1
|
2015-01-22 03:40:55 +00:00
|
|
|
# ifndef OPENSSL_NO_STDIO
|
2000-12-30 02:40:26 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
void *ASN1_d2i_fp(void *(*xnew) (void), d2i_of_void *d2i, FILE *in, void **x)
|
|
|
|
{
|
|
|
|
BIO *b;
|
|
|
|
void *ret;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_FP, ERR_R_BUF_LIB);
|
2017-10-17 14:04:09 +00:00
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
BIO_set_fp(b, in, BIO_NOCLOSE);
|
|
|
|
ret = ASN1_d2i_bio(xnew, d2i, b, x);
|
|
|
|
BIO_free(b);
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
# endif
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
void *ASN1_d2i_bio(void *(*xnew) (void), d2i_of_void *d2i, BIO *in, void **x)
|
|
|
|
{
|
|
|
|
BUF_MEM *b = NULL;
|
|
|
|
const unsigned char *p;
|
|
|
|
void *ret = NULL;
|
|
|
|
int len;
|
2000-12-30 02:40:26 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
len = asn1_d2i_read_bio(in, &b);
|
|
|
|
if (len < 0)
|
|
|
|
goto err;
|
2000-12-30 02:40:26 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
p = (unsigned char *)b->data;
|
|
|
|
ret = d2i(x, &p, len);
|
|
|
|
err:
|
2015-05-01 18:37:16 +00:00
|
|
|
BUF_MEM_free(b);
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2000-12-30 02:40:26 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void *ASN1_item_d2i_bio(const ASN1_ITEM *it, BIO *in, void *x)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
BUF_MEM *b = NULL;
|
|
|
|
const unsigned char *p;
|
|
|
|
void *ret = NULL;
|
|
|
|
int len;
|
2000-12-30 02:40:26 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
len = asn1_d2i_read_bio(in, &b);
|
|
|
|
if (len < 0)
|
|
|
|
goto err;
|
2000-12-30 02:40:26 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
p = (const unsigned char *)b->data;
|
|
|
|
ret = ASN1_item_d2i(x, &p, len, it);
|
|
|
|
err:
|
2015-05-01 18:37:16 +00:00
|
|
|
BUF_MEM_free(b);
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2000-12-30 02:40:26 +00:00
|
|
|
|
2015-01-14 20:57:28 +00:00
|
|
|
#ifndef OPENSSL_NO_STDIO
|
2000-12-30 02:40:26 +00:00
|
|
|
void *ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
BIO *b;
|
|
|
|
char *ret;
|
2000-12-30 02:40:26 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if ((b = BIO_new(BIO_s_file())) == NULL) {
|
|
|
|
ASN1err(ASN1_F_ASN1_ITEM_D2I_FP, ERR_R_BUF_LIB);
|
2017-10-17 14:04:09 +00:00
|
|
|
return NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
BIO_set_fp(b, in, BIO_NOCLOSE);
|
|
|
|
ret = ASN1_item_d2i_bio(it, b, x);
|
|
|
|
BIO_free(b);
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2000-12-30 02:40:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define HEADER_SIZE 8
|
2016-04-11 12:57:20 +00:00
|
|
|
#define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
|
2016-11-18 17:17:20 +00:00
|
|
|
int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
BUF_MEM *b;
|
|
|
|
unsigned char *p;
|
|
|
|
int i;
|
|
|
|
size_t want = HEADER_SIZE;
|
2016-06-04 11:48:00 +00:00
|
|
|
uint32_t eos = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
size_t off = 0;
|
|
|
|
size_t len = 0;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-04-03 17:28:06 +00:00
|
|
|
const unsigned char *q;
|
|
|
|
long slen;
|
|
|
|
int inf, tag, xclass;
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
b = BUF_MEM_new();
|
|
|
|
if (b == NULL) {
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
|
|
|
|
return -1;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ERR_clear_error();
|
|
|
|
for (;;) {
|
|
|
|
if (want >= (len - off)) {
|
|
|
|
want -= (len - off);
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) {
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
i = BIO_read(in, &(b->data[len]), want);
|
|
|
|
if ((i < 0) && ((len - off) == 0)) {
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_NOT_ENOUGH_DATA);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (i > 0) {
|
|
|
|
if (len + i < len) {
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
len += i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* else data already loaded */
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
p = (unsigned char *)&(b->data[off]);
|
2015-04-03 17:28:06 +00:00
|
|
|
q = p;
|
|
|
|
inf = ASN1_get_object(&q, &slen, &tag, &xclass, len - off);
|
|
|
|
if (inf & 0x80) {
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned long e;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
e = ERR_GET_REASON(ERR_peek_error());
|
|
|
|
if (e != ASN1_R_TOO_LONG)
|
|
|
|
goto err;
|
|
|
|
else
|
|
|
|
ERR_clear_error(); /* clear error */
|
|
|
|
}
|
2015-04-03 17:28:06 +00:00
|
|
|
i = q - p; /* header length */
|
2015-01-22 03:40:55 +00:00
|
|
|
off += i; /* end of data */
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-04-03 17:28:06 +00:00
|
|
|
if (inf & 1) {
|
2015-01-22 03:40:55 +00:00
|
|
|
/* no data body so go round again */
|
2016-06-04 11:48:00 +00:00
|
|
|
if (eos == UINT32_MAX) {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_HEADER_TOO_LONG);
|
|
|
|
goto err;
|
|
|
|
}
|
2016-06-04 11:48:00 +00:00
|
|
|
eos++;
|
2015-01-22 03:40:55 +00:00
|
|
|
want = HEADER_SIZE;
|
2015-04-03 17:28:06 +00:00
|
|
|
} else if (eos && (slen == 0) && (tag == V_ASN1_EOC)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
/* eos value, so go back and read another header */
|
|
|
|
eos--;
|
2016-06-04 11:48:00 +00:00
|
|
|
if (eos == 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
break;
|
|
|
|
else
|
|
|
|
want = HEADER_SIZE;
|
|
|
|
} else {
|
2015-04-03 17:28:06 +00:00
|
|
|
/* suck in slen bytes of data */
|
|
|
|
want = slen;
|
2015-01-22 03:40:55 +00:00
|
|
|
if (want > (len - off)) {
|
2016-04-11 12:57:20 +00:00
|
|
|
size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
want -= (len - off);
|
|
|
|
if (want > INT_MAX /* BIO_read takes an int length */ ||
|
|
|
|
len + want < len) {
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
while (want > 0) {
|
2016-04-11 12:57:20 +00:00
|
|
|
/*
|
|
|
|
* Read content in chunks of increasing size
|
|
|
|
* so we can return an error for EOF without
|
|
|
|
* having to allocate the entire content length
|
|
|
|
* in one go.
|
|
|
|
*/
|
|
|
|
size_t chunk = want > chunk_max ? chunk_max : want;
|
|
|
|
|
|
|
|
if (!BUF_MEM_grow_clean(b, len + chunk)) {
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2016-04-11 12:57:20 +00:00
|
|
|
want -= chunk;
|
|
|
|
while (chunk > 0) {
|
|
|
|
i = BIO_read(in, &(b->data[len]), chunk);
|
|
|
|
if (i <= 0) {
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
|
|
|
|
ASN1_R_NOT_ENOUGH_DATA);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* This can't overflow because |len+want| didn't
|
|
|
|
* overflow.
|
|
|
|
*/
|
2016-04-11 12:57:20 +00:00
|
|
|
len += i;
|
|
|
|
chunk -= i;
|
|
|
|
}
|
|
|
|
if (chunk_max < INT_MAX/2)
|
|
|
|
chunk_max *= 2;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-03 17:28:06 +00:00
|
|
|
if (off + slen < off) {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-04-03 17:28:06 +00:00
|
|
|
off += slen;
|
2016-06-04 11:48:00 +00:00
|
|
|
if (eos == 0) {
|
2015-01-22 03:40:55 +00:00
|
|
|
break;
|
|
|
|
} else
|
|
|
|
want = HEADER_SIZE;
|
|
|
|
}
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (off > INT_MAX) {
|
|
|
|
ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
|
|
|
|
goto err;
|
|
|
|
}
|
2012-04-19 16:19:56 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
*pb = b;
|
|
|
|
return off;
|
|
|
|
err:
|
2015-05-01 18:37:16 +00:00
|
|
|
BUF_MEM_free(b);
|
2015-01-22 03:40:55 +00:00
|
|
|
return -1;
|
|
|
|
}
|