Update ASN.1

This commit is contained in:
Zhi Guan
2025-12-08 16:50:56 +08:00
parent d69783aaa5
commit 2b67dca44a
3 changed files with 19 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2025 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
@@ -37,6 +37,7 @@ extern "C" {
enum ASN1_TAG {
ASN1_TAG_END_OF_CONTENTS = 0,
ASN1_TAG_BOOLEAN = 1,
ASN1_TAG_INTEGER = 2,
ASN1_TAG_BIT_STRING = 3,
@@ -68,6 +69,10 @@ enum ASN1_TAG {
ASN1_TAG_EXPLICIT = 0xa0,
};
#define ASN1_R_OK 1
#define ASN1_R_EOF 0
#define ASN1_R_ERROR -1
#define ASN1_R_TRUNCATED_DATA -2
const char *asn1_tag_name(int tag);
int asn1_tag_is_cstring(int tag);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2025 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
@@ -44,6 +44,7 @@ const char *asn1_tag_name(int tag)
}
switch (tag) {
case ASN1_TAG_END_OF_CONTENTS: return "End-of-contents";
case ASN1_TAG_BOOLEAN: return "BOOLEAN";
case ASN1_TAG_INTEGER: return "INTEGER";
case ASN1_TAG_BIT_STRING: return "BIT STRING";
@@ -221,7 +222,7 @@ int asn1_length_from_der(size_t *len, const uint8_t **in, size_t *inlen)
// check if the left input is enough for reading (d,dlen)
if (*inlen < *len) {
error_print();
return -2; // Special error for test_asn1_length() // TODO: fix asn1test.c test vector
return ASN1_R_TRUNCATED_DATA;
}
return 1;
}
@@ -382,7 +383,7 @@ int asn1_any_type_from_der(int *tag, const uint8_t **d, size_t *dlen, const uint
}
if (*inlen == 0) {
*tag = - 1;
*tag = -1; // -1 means not initialized
*d = NULL;
*dlen = 0;
return 0;
@@ -402,7 +403,7 @@ int asn1_any_type_from_der(int *tag, const uint8_t **d, size_t *dlen, const uint
return 1;
}
// we need to check this is an asn.1 type
// caller gaurantees (a, alen) is an asn.1 object (tlv)
int asn1_any_to_der(const uint8_t *a, size_t alen, uint8_t **out, size_t *outlen)
{
if (!outlen) {
@@ -410,13 +411,13 @@ int asn1_any_to_der(const uint8_t *a, size_t alen, uint8_t **out, size_t *outlen
return -1;
}
if (!alen) {
return 0;
}
if (!a) {
if (a) {
error_print();
return -1;
}
return 0;
}
if (out && *out) {
memcpy(*out, a, alen);
@@ -1157,7 +1158,7 @@ int asn1_object_identifier_print(FILE *fp, int format, int indent, const char *l
for (i = 0; i < nodes_cnt - 1; i++) {
fprintf(fp, "%d.", (int)nodes[i]);
}
fprintf(fp, "%d)", nodes[i]);
fprintf(fp, "%d)", (int)nodes[i]);
}
fprintf(fp, "\n");
return 1;

View File

@@ -103,7 +103,8 @@ static int test_asn1_length(void)
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
int ret;
ret = asn1_length_from_der(&length, &cp, &len);
if (ret != 1 && ret != -2) {
// TLV in test vectors have no Value, asn1_length_from_der will return ASN1_R_TRUNCATED_DATA (-2)
if (ret != 1 && ret != ASN1_R_TRUNCATED_DATA) {
error_print();
return -1;
}