mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-06-13 08:23:50 +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:
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
|
||||
* Copyright 2014-2023 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.
|
||||
@@ -504,7 +504,7 @@ static int test_x509_basic_constraints(void)
|
||||
|
||||
cp = p = buf; len = 0;
|
||||
if (x509_basic_constraints_to_der(-1, -1, &p, &len) != 1
|
||||
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|
||||
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != -1 // empty sequence is not allowed
|
||||
|| asn1_length_is_zero(len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
@@ -650,7 +650,7 @@ static int test_x509_policy_constraints(void)
|
||||
cp = p = buf; len = 0;
|
||||
val1 = val2 = 99;
|
||||
if (x509_policy_constraints_to_der(-1, -1, &p, &len) != 1
|
||||
|| x509_policy_constraints_from_der(&val1, &val2, &cp, &len) != 1
|
||||
|| x509_policy_constraints_from_der(&val1, &val2, &cp, &len) != -1 // empty sequence is not allowed
|
||||
|| asn1_check(val1 == -1) != 1
|
||||
|| asn1_check(val2 == -1) != 1
|
||||
|| asn1_length_is_zero(len) != 1) {
|
||||
|
||||
Reference in New Issue
Block a user