From 2b67dca44a76f6a68dd3008c85d9ade5ab81bda8 Mon Sep 17 00:00:00 2001 From: Zhi Guan Date: Mon, 8 Dec 2025 16:50:56 +0800 Subject: [PATCH] Update ASN.1 --- include/gmssl/asn1.h | 7 ++++++- src/asn1.c | 21 +++++++++++---------- tests/asn1test.c | 3 ++- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/include/gmssl/asn1.h b/include/gmssl/asn1.h index 77b66fc2..5f8a154f 100644 --- a/include/gmssl/asn1.h +++ b/include/gmssl/asn1.h @@ -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); diff --git a/src/asn1.c b/src/asn1.c index affaee4e..be9370f4 100644 --- a/src/asn1.c +++ b/src/asn1.c @@ -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 (!a) { - if (a) { - error_print(); - return -1; - } + if (!alen) { return 0; } + if (!a) { + error_print(); + return -1; + } 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; diff --git a/tests/asn1test.c b/tests/asn1test.c index 8bd69705..f1265e8e 100644 --- a/tests/asn1test.c +++ b/tests/asn1test.c @@ -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; }