mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-09-23 22:03:48 +08:00
Refine ASN.1 functions
This commit is contained in:
@@ -11,6 +11,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <assert.h>
|
||||
#include <gmssl/asn1.h>
|
||||
#include <gmssl/error.h>
|
||||
@@ -80,7 +81,8 @@ static int test_asn1_length(void)
|
||||
344,
|
||||
65537,
|
||||
1<<23,
|
||||
(size_t)1<<31,
|
||||
INT_MAX, // INT_MAX = 2^31 - 1
|
||||
//(size_t)1<<31, // default int value of (1<<31) is -2^31, 2^31 is larger than the INT_MAX limit
|
||||
};
|
||||
size_t length;
|
||||
uint8_t buf[256];
|
||||
@@ -684,6 +686,96 @@ static int test_asn1_generalized_time(void)
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_asn1_from_der_null_args(void)
|
||||
{
|
||||
uint8_t buf[100];
|
||||
const uint8_t *cp = NULL;
|
||||
size_t len = 100;
|
||||
|
||||
int val;
|
||||
const char *str;
|
||||
const uint8_t *d;
|
||||
size_t dlen;
|
||||
time_t t;
|
||||
uint32_t nodes[32];
|
||||
size_t nodes_cnt;
|
||||
|
||||
fprintf(stderr, "%s: *inlen = 0\n", __FUNCTION__);
|
||||
cp = buf;
|
||||
len = 0;
|
||||
if (asn1_boolean_from_der(&val, &cp, &len) != 0) { error_print(); return -1; }
|
||||
if (asn1_int_from_der(&val, &cp, &len) != 0) { error_print(); return -1; }
|
||||
if (asn1_bits_from_der(&val, &cp, &len) != 0) { error_print(); return -1; }
|
||||
if (asn1_null_from_der(&cp, &len) != 0) { error_print(); return -1; }
|
||||
if (asn1_object_identifier_from_der(nodes, &nodes_cnt, &cp, &len) != 0) { error_print(); return -1; }
|
||||
if (asn1_utf8_string_from_der(&str, &dlen, &cp, &len) != 0) { error_print(); return -1; }
|
||||
if (asn1_printable_string_from_der(&str, &dlen, &cp, &len) != 0) { error_print(); return -1; }
|
||||
if (asn1_ia5_string_from_der(&str, &dlen, &cp, &len) != 0) { error_print(); return -1; }
|
||||
if (asn1_utc_time_from_der(&t, &cp, &len) != 0) { error_print(); return -1; }
|
||||
if (asn1_generalized_time_from_der(&t, &cp, &len) != 0) { error_print(); return -1; }
|
||||
fprintf(stderr, "%s: result = NULL\n", __FUNCTION__);
|
||||
|
||||
cp = NULL;
|
||||
len = 100;
|
||||
if (asn1_boolean_from_der(NULL, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_int_from_der(NULL, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_bits_from_der(NULL, &cp, &len) != -1) { error_print(); return -1; }
|
||||
//if (asn1_null_from_der(&cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_object_identifier_from_der(NULL, &nodes_cnt, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_utf8_string_from_der(NULL, &dlen, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_printable_string_from_der(NULL, &dlen, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_ia5_string_from_der(NULL, &dlen, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_utc_time_from_der(NULL, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_generalized_time_from_der(NULL, &cp, &len) != -1) { error_print(); return -1; }
|
||||
|
||||
fprintf(stderr, "%s: *inlen = 0\n", __FUNCTION__);
|
||||
cp = buf;
|
||||
len = 0;
|
||||
if (asn1_boolean_from_der(&val, &cp, &len) != 0) { error_print(); return -1; }
|
||||
fprintf(stderr, "%s: in = NULL\n", __FUNCTION__);
|
||||
len = 100;
|
||||
if (asn1_boolean_from_der(&val, NULL, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_int_from_der(&val, NULL, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_bits_from_der(&val, NULL, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_null_from_der(NULL, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_object_identifier_from_der(nodes, &nodes_cnt, NULL, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_utf8_string_from_der(&str, &dlen, NULL, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_printable_string_from_der(&str, &dlen, NULL, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_ia5_string_from_der(&str, &dlen, NULL, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_utc_time_from_der(&t, NULL, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_generalized_time_from_der(&t, NULL, &len) != -1) { error_print(); return -1; }
|
||||
|
||||
fprintf(stderr, "%s: inlen = NULL\n", __FUNCTION__);
|
||||
cp = buf;
|
||||
if (asn1_boolean_from_der(&val, &cp, NULL) != -1) { error_print(); return -1; }
|
||||
if (asn1_int_from_der(&val, &cp, NULL) != -1) { error_print(); return -1; }
|
||||
if (asn1_bits_from_der(&val, &cp, NULL) != -1) { error_print(); return -1; }
|
||||
if (asn1_null_from_der(&cp, NULL) != -1) { error_print(); return -1; }
|
||||
if (asn1_object_identifier_from_der(nodes, &nodes_cnt, &cp, NULL) != -1) { error_print(); return -1; }
|
||||
if (asn1_utf8_string_from_der(&str, &dlen, &cp, NULL) != -1) { error_print(); return -1; }
|
||||
if (asn1_printable_string_from_der(&str, &dlen, &cp, NULL) != -1) { error_print(); return -1; }
|
||||
if (asn1_ia5_string_from_der(&str, &dlen, &cp, NULL) != -1) { error_print(); return -1; }
|
||||
if (asn1_utc_time_from_der(&t, &cp, NULL) != -1) { error_print(); return -1; }
|
||||
if (asn1_generalized_time_from_der(&t, &cp, NULL) != -1) { error_print(); return -1; }
|
||||
|
||||
fprintf(stderr, "%s: *in = NULL\n", __FUNCTION__);
|
||||
cp = NULL;
|
||||
len = 100;
|
||||
if (asn1_boolean_from_der(&val, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_int_from_der(&val, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_bits_from_der(&val, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_null_from_der(&cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_object_identifier_from_der(nodes, &nodes_cnt, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_utf8_string_from_der(&str, &dlen, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_printable_string_from_der(&str, &dlen, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_ia5_string_from_der(&str, &dlen, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_utc_time_from_der(&t, &cp, &len) != -1) { error_print(); return -1; }
|
||||
if (asn1_generalized_time_from_der(&t, &cp, &len) != -1) { error_print(); return -1; }
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
if (test_asn1_tag() != 1) goto err;
|
||||
@@ -700,6 +792,7 @@ int main(void)
|
||||
if (test_asn1_time() != 1) goto err;
|
||||
if (test_asn1_utc_time() != 1) goto err;
|
||||
if (test_asn1_generalized_time() != 1) goto err;
|
||||
if (test_asn1_from_der_null_args() != 1) goto err;
|
||||
printf("%s all tests passed\n", __FILE__);
|
||||
return 0;
|
||||
err:
|
||||
|
||||
Reference in New Issue
Block a user