1
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
40
41
42
43
44
45
49
50
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
101
102
103
108
109
115
116
117
124
125
126
127
128
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
149
153
154
157
158
159
162
163
167
168
169
170
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
191
192
193
194
195
196
197
201
202
203
204
205
206
207
208
209
210
214
215
216
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
522
523
524
529
530
531
532
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
628
629
630
636
637
638
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/* ... */
/* ... */
#ifndef MBEDTLS_X509_CRT_H
#define MBEDTLS_X509_CRT_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "x509.h"
#include "x509_crl.h"
/* ... */
#ifdef __cplusplus
extern "C" {
#endif
/* ... */
/* ... */
typedef struct mbedtls_x509_crt
{
mbedtls_x509_buf raw;
mbedtls_x509_buf tbs;
int version;
mbedtls_x509_buf serial;
mbedtls_x509_buf sig_oid;
mbedtls_x509_buf issuer_raw;
mbedtls_x509_buf subject_raw;
mbedtls_x509_name issuer;
mbedtls_x509_name subject;
mbedtls_x509_time valid_from;
mbedtls_x509_time valid_to;
mbedtls_pk_context pk;
mbedtls_x509_buf issuer_id;
mbedtls_x509_buf subject_id;
mbedtls_x509_buf v3_ext;
mbedtls_x509_sequence subject_alt_names;
int ext_types;
int ca_istrue;
int max_pathlen;
unsigned int key_usage;
mbedtls_x509_sequence ext_key_usage;
unsigned char ns_cert_type;
mbedtls_x509_buf sig;
mbedtls_md_type_t sig_md;
mbedtls_pk_type_t sig_pk;
void *sig_opts;
struct mbedtls_x509_crt *next;
...}
mbedtls_x509_crt;
/* ... */
#define MBEDTLS_X509_ID_FLAG( id ) ( 1 << ( (id) - 1 ) )
/* ... */
typedef struct mbedtls_x509_crt_profile
{
uint32_t allowed_mds;
uint32_t allowed_pks;
uint32_t allowed_curves;
uint32_t rsa_min_bitlen;
...}
mbedtls_x509_crt_profile;
#define MBEDTLS_X509_CRT_VERSION_1 0
#define MBEDTLS_X509_CRT_VERSION_2 1
#define MBEDTLS_X509_CRT_VERSION_3 2
#define MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN 32
#define MBEDTLS_X509_RFC5280_UTC_TIME_LEN 15
5 defines
#if !defined( MBEDTLS_X509_MAX_FILE_PATH_LEN )
#define MBEDTLS_X509_MAX_FILE_PATH_LEN 512
#endif
/* ... */
typedef struct mbedtls_x509write_cert
{
int version;
mbedtls_mpi serial;
mbedtls_pk_context *subject_key;
mbedtls_pk_context *issuer_key;
mbedtls_asn1_named_data *subject;
mbedtls_asn1_named_data *issuer;
mbedtls_md_type_t md_alg;
char not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
char not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN + 1];
mbedtls_asn1_named_data *extensions;
...}
mbedtls_x509write_cert;
/* ... */
typedef struct {
mbedtls_x509_crt *crt;
uint32_t flags;
...} mbedtls_x509_crt_verify_chain_item;
/* ... */
#define MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
/* ... */
typedef struct
{
mbedtls_x509_crt_verify_chain_item items[MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE];
unsigned len;
...} mbedtls_x509_crt_verify_chain;
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
/* ... */
typedef struct
{
mbedtls_pk_restart_ctx pk;
mbedtls_x509_crt *parent;
mbedtls_x509_crt *fallback_parent;
int fallback_signature_is_good;
int parent_is_trusted;
enum {
x509_crt_rs_none,
x509_crt_rs_find_parent,
...} in_progress;
int self_cnt;
mbedtls_x509_crt_verify_chain ver_chain;
...} mbedtls_x509_crt_restart_ctx;
/* ... */
#else
typedef void mbedtls_x509_crt_restart_ctx;
/* ... */
#endif
#if defined(MBEDTLS_X509_CRT_PARSE_C)
/* ... */
extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default;
/* ... */
extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next;
/* ... */
extern const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb;
/* ... */
int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
size_t buflen );
/* ... */
int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen );
#if defined(MBEDTLS_FS_IO)
/* ... */
int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path );
/* ... */
int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path );/* ... */
#endif
/* ... */
int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
const mbedtls_x509_crt *crt );
/* ... */
int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
uint32_t flags );
/* ... */
int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
mbedtls_x509_crt *trust_ca,
mbedtls_x509_crl *ca_crl,
const char *cn, uint32_t *flags,
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
void *p_vrfy );
/* ... */
int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
mbedtls_x509_crt *trust_ca,
mbedtls_x509_crl *ca_crl,
const mbedtls_x509_crt_profile *profile,
const char *cn, uint32_t *flags,
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
void *p_vrfy );
/* ... */
int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
mbedtls_x509_crt *trust_ca,
mbedtls_x509_crl *ca_crl,
const mbedtls_x509_crt_profile *profile,
const char *cn, uint32_t *flags,
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
void *p_vrfy,
mbedtls_x509_crt_restart_ctx *rs_ctx );
#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
/* ... */
int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
unsigned int usage );/* ... */
#endif
#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
/* ... */
int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
const char *usage_oid,
size_t usage_len );/* ... */
#endif
#if defined(MBEDTLS_X509_CRL_PARSE_C)
/* ... */
int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl );/* ... */
#endif
/* ... */
void mbedtls_x509_crt_init( mbedtls_x509_crt *crt );
/* ... */
void mbedtls_x509_crt_free( mbedtls_x509_crt *crt );
#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
/* ... */
void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx );
/* ... */
void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx );/* ... */
#endif /* ... */
#endif
#if defined(MBEDTLS_X509_CRT_WRITE_C)
/* ... */
void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx );
/* ... */
void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, int version );
/* ... */
int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, const mbedtls_mpi *serial );
/* ... */
int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, const char *not_before,
const char *not_after );
/* ... */
int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
const char *issuer_name );
/* ... */
int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
const char *subject_name );
/* ... */
void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
/* ... */
void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, mbedtls_pk_context *key );
/* ... */
void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, mbedtls_md_type_t md_alg );
/* ... */
int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
const char *oid, size_t oid_len,
int critical,
const unsigned char *val, size_t val_len );
/* ... */
int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
int is_ca, int max_pathlen );
#if defined(MBEDTLS_SHA1_C)
/* ... */
int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx );
/* ... */
int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx );/* ... */
#endif
/* ... */
int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
unsigned int key_usage );
/* ... */
int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
unsigned char ns_cert_type );
/* ... */
void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx );
/* ... */
int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );
#if defined(MBEDTLS_PEM_WRITE_C)
/* ... */
int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *ctx, unsigned char *buf, size_t size,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng );/* ... */
#endif /* ... */
#endif
#ifdef __cplusplus
}extern "C" { ... }
#endif
/* ... */
#endif