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>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/buffer.h>
|
|
|
|
#include <openssl/objects.h>
|
1999-07-24 03:09:01 +00:00
|
|
|
#include <openssl/asn1.h>
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-09-03 13:27:19 +00:00
|
|
|
#ifndef ASN1_PARSE_MAXDEPTH
|
|
|
|
#define ASN1_PARSE_MAXDEPTH 128
|
|
|
|
#endif
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
|
|
|
|
int indent);
|
2008-11-12 03:58:08 +00:00
|
|
|
static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
|
2015-01-22 03:40:55 +00:00
|
|
|
int offset, int depth, int indent, int dump);
|
1999-04-19 21:31:43 +00:00
|
|
|
static int asn1_print_info(BIO *bp, int tag, int xclass, int constructed,
|
2015-01-22 03:40:55 +00:00
|
|
|
int indent)
|
|
|
|
{
|
|
|
|
static const char fmt[] = "%-18s";
|
|
|
|
char str[128];
|
|
|
|
const char *p;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (constructed & V_ASN1_CONSTRUCTED)
|
|
|
|
p = "cons: ";
|
|
|
|
else
|
|
|
|
p = "prim: ";
|
|
|
|
if (BIO_write(bp, p, 6) < 6)
|
|
|
|
goto err;
|
|
|
|
BIO_indent(bp, indent, 128);
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
p = str;
|
|
|
|
if ((xclass & V_ASN1_PRIVATE) == V_ASN1_PRIVATE)
|
|
|
|
BIO_snprintf(str, sizeof str, "priv [ %d ] ", tag);
|
|
|
|
else if ((xclass & V_ASN1_CONTEXT_SPECIFIC) == V_ASN1_CONTEXT_SPECIFIC)
|
|
|
|
BIO_snprintf(str, sizeof str, "cont [ %d ]", tag);
|
|
|
|
else if ((xclass & V_ASN1_APPLICATION) == V_ASN1_APPLICATION)
|
|
|
|
BIO_snprintf(str, sizeof str, "appl [ %d ]", tag);
|
|
|
|
else if (tag > 30)
|
|
|
|
BIO_snprintf(str, sizeof str, "<ASN1 %d>", tag);
|
|
|
|
else
|
|
|
|
p = ASN1_tag2str(tag);
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (BIO_printf(bp, fmt, p) <= 0)
|
|
|
|
goto err;
|
|
|
|
return (1);
|
|
|
|
err:
|
|
|
|
return (0);
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2008-11-12 03:58:08 +00:00
|
|
|
int ASN1_parse(BIO *bp, const unsigned char *pp, long len, int indent)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
return (asn1_parse2(bp, &pp, len, 0, 0, indent, 0));
|
|
|
|
}
|
2000-07-27 17:28:25 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
int ASN1_parse_dump(BIO *bp, const unsigned char *pp, long len, int indent,
|
|
|
|
int dump)
|
|
|
|
{
|
|
|
|
return (asn1_parse2(bp, &pp, len, 0, 0, indent, dump));
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
static int asn1_parse2(BIO *bp, const unsigned char **pp, long length,
|
|
|
|
int offset, int depth, int indent, int dump)
|
|
|
|
{
|
|
|
|
const unsigned char *p, *ep, *tot, *op, *opp;
|
|
|
|
long len;
|
|
|
|
int tag, xclass, ret = 0;
|
|
|
|
int nl, hl, j, r;
|
|
|
|
ASN1_OBJECT *o = NULL;
|
|
|
|
ASN1_OCTET_STRING *os = NULL;
|
|
|
|
/* ASN1_BMPSTRING *bmp=NULL; */
|
2016-02-10 23:54:52 +00:00
|
|
|
int dump_indent, dump_cont = 0;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-09-03 13:27:19 +00:00
|
|
|
if (depth > ASN1_PARSE_MAXDEPTH) {
|
|
|
|
BIO_puts(bp, "BAD RECURSION DEPTH\n");
|
2015-11-24 16:37:52 +00:00
|
|
|
return 0;
|
2015-09-03 13:27:19 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
dump_indent = 6; /* Because we know BIO_dump_indent() */
|
|
|
|
p = *pp;
|
|
|
|
tot = p + length;
|
2016-06-04 17:52:28 +00:00
|
|
|
while (length > 0) {
|
2015-01-22 03:40:55 +00:00
|
|
|
op = p;
|
|
|
|
j = ASN1_get_object(&p, &len, &tag, &xclass, length);
|
|
|
|
if (j & 0x80) {
|
|
|
|
if (BIO_write(bp, "Error in encoding\n", 18) <= 0)
|
|
|
|
goto end;
|
|
|
|
ret = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
hl = (p - op);
|
|
|
|
length -= hl;
|
|
|
|
/*
|
|
|
|
* if j == 0x21 it is a constructed indefinite length object
|
|
|
|
*/
|
|
|
|
if (BIO_printf(bp, "%5ld:", (long)offset + (long)(op - *pp))
|
|
|
|
<= 0)
|
|
|
|
goto end;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (j != (V_ASN1_CONSTRUCTED | 1)) {
|
|
|
|
if (BIO_printf(bp, "d=%-2d hl=%ld l=%4ld ",
|
|
|
|
depth, (long)hl, len) <= 0)
|
|
|
|
goto end;
|
|
|
|
} else {
|
|
|
|
if (BIO_printf(bp, "d=%-2d hl=%ld l=inf ", depth, (long)hl) <= 0)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (!asn1_print_info(bp, tag, xclass, j, (indent) ? depth : 0))
|
|
|
|
goto end;
|
|
|
|
if (j & V_ASN1_CONSTRUCTED) {
|
2016-03-29 18:37:57 +00:00
|
|
|
const unsigned char *sp = p;
|
2016-06-28 20:51:01 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ep = p + len;
|
|
|
|
if (BIO_write(bp, "\n", 1) <= 0)
|
|
|
|
goto end;
|
|
|
|
if (len > length) {
|
|
|
|
BIO_printf(bp, "length is greater than %ld\n", length);
|
|
|
|
ret = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if ((j == 0x21) && (len == 0)) {
|
|
|
|
for (;;) {
|
|
|
|
r = asn1_parse2(bp, &p, (long)(tot - p),
|
|
|
|
offset + (p - *pp), depth + 1,
|
|
|
|
indent, dump);
|
|
|
|
if (r == 0) {
|
|
|
|
ret = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
2016-03-29 18:37:57 +00:00
|
|
|
if ((r == 2) || (p >= tot)) {
|
|
|
|
len = p - sp;
|
2015-01-22 03:40:55 +00:00
|
|
|
break;
|
2016-03-29 18:37:57 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-03-29 18:37:57 +00:00
|
|
|
} else {
|
2016-04-14 03:14:43 +00:00
|
|
|
long tmp = len;
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
while (p < ep) {
|
2016-03-29 18:37:57 +00:00
|
|
|
sp = p;
|
2016-04-14 03:14:43 +00:00
|
|
|
r = asn1_parse2(bp, &p, tmp,
|
2015-01-22 03:40:55 +00:00
|
|
|
offset + (p - *pp), depth + 1,
|
|
|
|
indent, dump);
|
|
|
|
if (r == 0) {
|
|
|
|
ret = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
2016-04-14 03:14:43 +00:00
|
|
|
tmp -= p - sp;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-03-29 18:37:57 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
} else if (xclass != 0) {
|
|
|
|
p += len;
|
|
|
|
if (BIO_write(bp, "\n", 1) <= 0)
|
|
|
|
goto end;
|
|
|
|
} else {
|
|
|
|
nl = 0;
|
|
|
|
if ((tag == V_ASN1_PRINTABLESTRING) ||
|
|
|
|
(tag == V_ASN1_T61STRING) ||
|
|
|
|
(tag == V_ASN1_IA5STRING) ||
|
|
|
|
(tag == V_ASN1_VISIBLESTRING) ||
|
|
|
|
(tag == V_ASN1_NUMERICSTRING) ||
|
|
|
|
(tag == V_ASN1_UTF8STRING) ||
|
|
|
|
(tag == V_ASN1_UTCTIME) || (tag == V_ASN1_GENERALIZEDTIME)) {
|
|
|
|
if (BIO_write(bp, ":", 1) <= 0)
|
|
|
|
goto end;
|
|
|
|
if ((len > 0) && BIO_write(bp, (const char *)p, (int)len)
|
|
|
|
!= (int)len)
|
|
|
|
goto end;
|
|
|
|
} else if (tag == V_ASN1_OBJECT) {
|
|
|
|
opp = op;
|
|
|
|
if (d2i_ASN1_OBJECT(&o, &opp, len + hl) != NULL) {
|
|
|
|
if (BIO_write(bp, ":", 1) <= 0)
|
|
|
|
goto end;
|
|
|
|
i2a_ASN1_OBJECT(bp, o);
|
|
|
|
} else {
|
2016-02-11 00:23:17 +00:00
|
|
|
if (BIO_puts(bp, ":BAD OBJECT") <= 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto end;
|
2016-02-10 23:54:52 +00:00
|
|
|
dump_cont = 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
} else if (tag == V_ASN1_BOOLEAN) {
|
2015-03-14 18:06:59 +00:00
|
|
|
if (len != 1) {
|
2016-02-11 00:23:17 +00:00
|
|
|
if (BIO_puts(bp, ":BAD BOOLEAN") <= 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto end;
|
2016-02-10 23:54:52 +00:00
|
|
|
dump_cont = 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2016-03-29 18:37:57 +00:00
|
|
|
if (len > 0)
|
|
|
|
BIO_printf(bp, ":%u", p[0]);
|
2015-01-22 03:40:55 +00:00
|
|
|
} else if (tag == V_ASN1_BMPSTRING) {
|
|
|
|
/* do the BMP thang */
|
|
|
|
} else if (tag == V_ASN1_OCTET_STRING) {
|
|
|
|
int i, printable = 1;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
opp = op;
|
|
|
|
os = d2i_ASN1_OCTET_STRING(NULL, &opp, len + hl);
|
|
|
|
if (os != NULL && os->length > 0) {
|
|
|
|
opp = os->data;
|
|
|
|
/*
|
|
|
|
* testing whether the octet string is printable
|
|
|
|
*/
|
|
|
|
for (i = 0; i < os->length; i++) {
|
|
|
|
if (((opp[i] < ' ') &&
|
|
|
|
(opp[i] != '\n') &&
|
|
|
|
(opp[i] != '\r') &&
|
|
|
|
(opp[i] != '\t')) || (opp[i] > '~')) {
|
|
|
|
printable = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (printable)
|
|
|
|
/* printable string */
|
|
|
|
{
|
|
|
|
if (BIO_write(bp, ":", 1) <= 0)
|
|
|
|
goto end;
|
|
|
|
if (BIO_write(bp, (const char *)opp, os->length) <= 0)
|
|
|
|
goto end;
|
|
|
|
} else if (!dump)
|
|
|
|
/*
|
|
|
|
* not printable => print octet string as hex dump
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
if (BIO_write(bp, "[HEX DUMP]:", 11) <= 0)
|
|
|
|
goto end;
|
|
|
|
for (i = 0; i < os->length; i++) {
|
|
|
|
if (BIO_printf(bp, "%02X", opp[i]) <= 0)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
/* print the normal dump */
|
|
|
|
{
|
|
|
|
if (!nl) {
|
|
|
|
if (BIO_write(bp, "\n", 1) <= 0)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (BIO_dump_indent(bp,
|
|
|
|
(const char *)opp,
|
|
|
|
((dump == -1 || dump >
|
|
|
|
os->
|
|
|
|
length) ? os->length : dump),
|
|
|
|
dump_indent) <= 0)
|
|
|
|
goto end;
|
|
|
|
nl = 1;
|
|
|
|
}
|
|
|
|
}
|
2015-03-24 11:52:24 +00:00
|
|
|
ASN1_OCTET_STRING_free(os);
|
|
|
|
os = NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
} else if (tag == V_ASN1_INTEGER) {
|
|
|
|
ASN1_INTEGER *bs;
|
|
|
|
int i;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
opp = op;
|
|
|
|
bs = d2i_ASN1_INTEGER(NULL, &opp, len + hl);
|
|
|
|
if (bs != NULL) {
|
|
|
|
if (BIO_write(bp, ":", 1) <= 0)
|
|
|
|
goto end;
|
|
|
|
if (bs->type == V_ASN1_NEG_INTEGER)
|
|
|
|
if (BIO_write(bp, "-", 1) <= 0)
|
|
|
|
goto end;
|
|
|
|
for (i = 0; i < bs->length; i++) {
|
|
|
|
if (BIO_printf(bp, "%02X", bs->data[i]) <= 0)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (bs->length == 0) {
|
|
|
|
if (BIO_write(bp, "00", 2) <= 0)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
2016-02-11 00:23:17 +00:00
|
|
|
if (BIO_puts(bp, ":BAD INTEGER") <= 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto end;
|
2016-02-10 23:54:52 +00:00
|
|
|
dump_cont = 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2015-03-14 04:16:42 +00:00
|
|
|
ASN1_INTEGER_free(bs);
|
2015-01-22 03:40:55 +00:00
|
|
|
} else if (tag == V_ASN1_ENUMERATED) {
|
|
|
|
ASN1_ENUMERATED *bs;
|
|
|
|
int i;
|
1999-02-09 01:29:37 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
opp = op;
|
|
|
|
bs = d2i_ASN1_ENUMERATED(NULL, &opp, len + hl);
|
|
|
|
if (bs != NULL) {
|
|
|
|
if (BIO_write(bp, ":", 1) <= 0)
|
|
|
|
goto end;
|
|
|
|
if (bs->type == V_ASN1_NEG_ENUMERATED)
|
|
|
|
if (BIO_write(bp, "-", 1) <= 0)
|
|
|
|
goto end;
|
|
|
|
for (i = 0; i < bs->length; i++) {
|
|
|
|
if (BIO_printf(bp, "%02X", bs->data[i]) <= 0)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (bs->length == 0) {
|
|
|
|
if (BIO_write(bp, "00", 2) <= 0)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
} else {
|
2016-02-11 00:23:17 +00:00
|
|
|
if (BIO_puts(bp, ":BAD ENUMERATED") <= 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto end;
|
2016-02-10 23:54:52 +00:00
|
|
|
dump_cont = 1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2015-03-14 04:16:42 +00:00
|
|
|
ASN1_ENUMERATED_free(bs);
|
2015-01-22 03:40:55 +00:00
|
|
|
} else if (len > 0 && dump) {
|
|
|
|
if (!nl) {
|
|
|
|
if (BIO_write(bp, "\n", 1) <= 0)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (BIO_dump_indent(bp, (const char *)p,
|
|
|
|
((dump == -1 || dump > len) ? len : dump),
|
|
|
|
dump_indent) <= 0)
|
|
|
|
goto end;
|
|
|
|
nl = 1;
|
|
|
|
}
|
2016-02-10 23:54:52 +00:00
|
|
|
if (dump_cont) {
|
|
|
|
int i;
|
|
|
|
const unsigned char *tmp = op + hl;
|
|
|
|
if (BIO_puts(bp, ":[") <= 0)
|
|
|
|
goto end;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (BIO_printf(bp, "%02X", tmp[i]) <= 0)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (BIO_puts(bp, "]") <= 0)
|
|
|
|
goto end;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!nl) {
|
|
|
|
if (BIO_write(bp, "\n", 1) <= 0)
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
p += len;
|
|
|
|
if ((tag == V_ASN1_EOC) && (xclass == 0)) {
|
|
|
|
ret = 2; /* End of sequence */
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
length -= len;
|
|
|
|
}
|
|
|
|
ret = 1;
|
|
|
|
end:
|
2015-03-24 11:52:24 +00:00
|
|
|
ASN1_OBJECT_free(o);
|
|
|
|
ASN1_OCTET_STRING_free(os);
|
2015-01-22 03:40:55 +00:00
|
|
|
*pp = p;
|
|
|
|
return (ret);
|
|
|
|
}
|
1999-09-07 12:16:29 +00:00
|
|
|
|
|
|
|
const char *ASN1_tag2str(int tag)
|
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
static const char *const tag2str[] = {
|
2015-01-21 12:19:08 +00:00
|
|
|
/* 0-4 */
|
2015-01-22 03:40:55 +00:00
|
|
|
"EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING",
|
|
|
|
/* 5-9 */
|
|
|
|
"NULL", "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL",
|
2015-01-21 12:19:08 +00:00
|
|
|
/* 10-13 */
|
2015-01-22 03:40:55 +00:00
|
|
|
"ENUMERATED", "<ASN1 11>", "UTF8STRING", "<ASN1 13>",
|
|
|
|
/* 15-17 */
|
|
|
|
"<ASN1 14>", "<ASN1 15>", "SEQUENCE", "SET",
|
|
|
|
/* 18-20 */
|
|
|
|
"NUMERICSTRING", "PRINTABLESTRING", "T61STRING",
|
2015-01-21 12:19:08 +00:00
|
|
|
/* 21-24 */
|
2015-01-22 03:40:55 +00:00
|
|
|
"VIDEOTEXSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME",
|
2015-01-21 12:19:08 +00:00
|
|
|
/* 25-27 */
|
2015-01-22 03:40:55 +00:00
|
|
|
"GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING",
|
2015-01-21 12:19:08 +00:00
|
|
|
/* 28-30 */
|
2015-01-22 03:40:55 +00:00
|
|
|
"UNIVERSALSTRING", "<ASN1 29>", "BMPSTRING"
|
|
|
|
};
|
1999-09-07 12:16:29 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
|
|
|
|
tag &= ~0x100;
|
1999-09-07 12:16:29 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (tag < 0 || tag > 30)
|
|
|
|
return "(unknown)";
|
|
|
|
return tag2str[tag];
|
1999-09-07 12:16:29 +00:00
|
|
|
}
|