Skip to content

Commit 5eaab70

Browse files
committed
Update TLCP
1 parent f754b12 commit 5eaab70

File tree

9 files changed

+1248
-744
lines changed

9 files changed

+1248
-744
lines changed

include/gmssl/tls.h

Lines changed: 159 additions & 166 deletions
Large diffs are not rendered by default.

include/gmssl/x509.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,11 @@ int x509_certs_get_subjects(const uint8_t *certs, size_t certslen, uint8_t *name
379379
int x509_certs_print(FILE *fp, int fmt, int ind, const char *label, const uint8_t *d, size_t dlen);
380380

381381

382+
int x509_cert_new_from_file(uint8_t **out, size_t *outlen, const char *file);
383+
int x509_certs_new_from_file(uint8_t **out, size_t *outlen, const char *file);
384+
385+
386+
382387

383388
#ifdef __cplusplus
384389
}

src/asn1.c

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ int asn1_ia5_string_check(const char *a, size_t alen)
173173

174174
int asn1_tag_to_der(int tag, uint8_t **out, size_t *outlen)
175175
{
176-
if (out) {
176+
if (out && *out) {
177177
*(*out)++ = (uint8_t)tag;
178178
}
179179
(*outlen)++;
@@ -183,7 +183,7 @@ int asn1_tag_to_der(int tag, uint8_t **out, size_t *outlen)
183183
int asn1_length_to_der(size_t len, uint8_t **out, size_t *outlen)
184184
{
185185
if (len < 128) {
186-
if (out) {
186+
if (out && *out) {
187187
*(*out)++ = (uint8_t)len;
188188
}
189189
(*outlen)++;
@@ -198,7 +198,7 @@ int asn1_length_to_der(size_t len, uint8_t **out, size_t *outlen)
198198
else if (len < (1 << 24)) i = 3;
199199
else i = 4;
200200

201-
if (out) {
201+
if (out && *out) {
202202
*(*out)++ = 0x80 + i;
203203
memcpy(*out, buf + 4 - i, i);
204204
(*out) += i;
@@ -211,7 +211,7 @@ int asn1_length_to_der(size_t len, uint8_t **out, size_t *outlen)
211211
// 提供返回值是为了和其他to_der函数一致
212212
int asn1_data_to_der(const uint8_t *data, size_t datalen, uint8_t **out, size_t *outlen)
213213
{
214-
if (out) {
214+
if (out && *out) {
215215
memcpy(*out, data, datalen);
216216
*out += datalen;
217217
}
@@ -301,7 +301,7 @@ int asn1_data_from_der(const uint8_t **data, size_t datalen, const uint8_t **in,
301301

302302
int asn1_header_to_der(int tag, size_t len, uint8_t **out, size_t *outlen)
303303
{
304-
if ((out && !(*out)) || !outlen) {
304+
if (!outlen) {
305305
error_print();
306306
return -1;
307307
}
@@ -429,15 +429,16 @@ int asn1_boolean_from_name(int *val, const char *name)
429429

430430
int asn1_boolean_to_der_ex(int tag, int val, uint8_t **out, size_t *outlen)
431431
{
432-
if ((out && !(*out)) || !outlen) {
432+
if (!outlen) {
433+
error_print();
433434
return -1;
434435
}
435436

436437
if (val < 0) {
437438
return 0;
438439
}
439440

440-
if (out) {
441+
if (out && *out) {
441442
*(*out)++ = tag;
442443
*(*out)++ = 0x01;
443444
*(*out)++ = val ? 0xff : 0x00;
@@ -448,22 +449,20 @@ int asn1_boolean_to_der_ex(int tag, int val, uint8_t **out, size_t *outlen)
448449

449450
int asn1_integer_to_der_ex(int tag, const uint8_t *a, size_t alen, uint8_t **out, size_t *outlen)
450451
{
451-
if (!a) {
452-
return 0;
452+
if (!outlen) {
453+
error_print();
454+
return -1;
453455
}
454456

455-
456-
457-
if (alen <= 0 || alen > INT_MAX || (out && !(*out)) || !outlen) {
457+
if (alen <= 0 || alen > INT_MAX) {
458458
error_print();
459459
return -1;
460460
}
461+
if (!a) {
462+
return 0;
463+
}
461464

462-
463-
464-
465-
466-
if (out)
465+
if (out && *out)
467466
*(*out)++ = tag;
468467
(*outlen)++;
469468

@@ -474,15 +473,15 @@ int asn1_integer_to_der_ex(int tag, const uint8_t *a, size_t alen, uint8_t **out
474473

475474
if (a[0] & 0x80) {
476475
asn1_length_to_der(alen + 1, out, outlen);
477-
if (out) {
476+
if (out && *out) {
478477
*(*out)++ = 0x00;
479478
memcpy(*out, a, alen);
480479
(*out) += alen;
481480
}
482481
(*outlen) += 1 + alen;
483482
} else {
484483
asn1_length_to_der(alen, out ,outlen);
485-
if (out) {
484+
if (out && *out) {
486485
memcpy(*out, a, alen);
487486
(*out) += alen;
488487
}
@@ -571,11 +570,11 @@ const char *asn1_null_name(void)
571570

572571
int asn1_null_to_der(uint8_t **out, size_t *outlen)
573572
{
574-
if ((out && !(*out)) || !outlen) {
573+
if (!outlen) {
574+
error_print();
575575
return -1;
576576
}
577-
578-
if (out) {
577+
if (out && *out) {
579578
*(*out)++ = ASN1_TAG_NULL;
580579
*(*out)++ = 0x00;
581580
}
@@ -597,7 +596,7 @@ static void asn1_oid_node_to_base128(uint32_t a, uint8_t **out, size_t *outlen)
597596
}
598597

599598
while (n--) {
600-
if (out)
599+
if (out && *out)
601600
*(*out)++ = buf[n];
602601
(*outlen)++;
603602
}
@@ -639,10 +638,14 @@ static int asn1_oid_node_from_base128(uint32_t *a, const uint8_t **in, size_t *i
639638

640639
int asn1_object_identifier_to_octets(const uint32_t *nodes, size_t nodes_cnt, uint8_t *out, size_t *outlen)
641640
{
641+
if (!outlen) {
642+
error_print();
643+
return -1;
644+
}
642645
if (nodes_cnt < 2 || nodes_cnt > 32) {
643646
return -1;
644647
}
645-
if (out)
648+
if (out && *out)
646649
*out++ = (uint8_t)(nodes[0] * 40 + nodes[1]);
647650
(*outlen) = 1;
648651
nodes += 2;
@@ -705,19 +708,20 @@ int asn1_object_identifier_to_der_ex(int tag, const uint32_t *nodes, size_t node
705708
uint8_t octets[32];
706709
size_t octetslen = 0;
707710

708-
if ((out && !(*out)) || !outlen) {
711+
if (!outlen) {
712+
error_print();
709713
return -1;
710714
}
711715

712-
if (out)
716+
if (out && *out)
713717
*(*out)++ = tag;
714718
(*outlen)++;
715719

716720
asn1_object_identifier_to_octets(nodes, nodes_cnt, octets, &octetslen);
717721

718722
asn1_length_to_der(octetslen, out, outlen);
719723

720-
if (out) {
724+
if (out && *out) {
721725
// 注意:If out == NULL, *out ==> Segment Fault
722726
memcpy(*out, octets, octetslen);
723727
*out += octetslen;
@@ -824,18 +828,19 @@ int asn1_utc_time_to_der_ex(int tag, time_t a, uint8_t **out, size_t *outlen)
824828
struct tm tm_val;
825829
char buf[ASN1_UTC_TIME_LEN + 1];
826830

827-
if ((out && !(*out)) || !outlen) {
831+
if (!outlen) {
832+
error_print();
828833
return -1;
829834
}
830835

831836
gmtime_r(&a, &tm_val);
832837
strftime(buf, sizeof(buf), "%y%m%d%H%M%SZ", &tm_val);
833838

834-
if (out)
839+
if (out && *out)
835840
*(*out)++ = tag;
836841
(*outlen)++;
837842
asn1_length_to_der(sizeof(buf)-1, out, outlen);
838-
if (out) {
843+
if (out && *out) {
839844
memcpy(*out, buf, sizeof(buf)-1);
840845
(*out) += sizeof(buf)-1;
841846
}
@@ -850,7 +855,7 @@ int asn1_generalized_time_to_der_ex(int tag, time_t a, uint8_t **out, size_t *ou
850855
struct tm tm_val;
851856
char buf[ASN1_GENERALIZED_TIME_LEN + 1];
852857

853-
if ((out && !(*out)) || !outlen) {
858+
if (!outlen) {
854859
error_print();
855860
return -1;
856861
}
@@ -859,11 +864,11 @@ int asn1_generalized_time_to_der_ex(int tag, time_t a, uint8_t **out, size_t *ou
859864
strftime(buf, sizeof(buf), "%Y%m%d%H%M%SZ", &tm_val);
860865
//printf("%s %d: generalized time : %s\n", __FILE__, __LINE__, buf);
861866

862-
if (out)
867+
if (out && *out)
863868
*(*out)++ = tag;
864869
(*outlen)++;
865870
asn1_length_to_der(ASN1_GENERALIZED_TIME_LEN, out, outlen);
866-
if (out) {
871+
if (out && *out) {
867872
memcpy(*out, buf, ASN1_GENERALIZED_TIME_LEN);
868873
(*out) += ASN1_GENERALIZED_TIME_LEN;
869874
}

0 commit comments

Comments
 (0)