1/* BEGIN_HEADER */
2#include "mbedtls/bignum.h"
3#include "mbedtls/x509.h"
4#include "mbedtls/x509_crt.h"
5#include "mbedtls/x509_crl.h"
6#include "mbedtls/x509_csr.h"
7#include "x509_internal.h"
8#include "mbedtls/pem.h"
9#include "mbedtls/oid.h"
10#include "x509_oid.h"
11#include "mbedtls/base64.h"
12#include "mbedtls/error.h"
13#include "mbedtls/pk.h"
14#if defined(MBEDTLS_PK_HAVE_PRIVATE_HEADER)
15#include <mbedtls/private/pk_private.h>
16#endif /* MBEDTLS_PK_HAVE_PRIVATE_HEADER */
17#include "mbedtls/asn1.h"
18#include "mbedtls/asn1write.h"
19#include "string.h"
20
21#if MBEDTLS_X509_MAX_INTERMEDIATE_CA > 19
22#error "The value of MBEDTLS_X509_MAX_INTERMEDIATE_C is larger \
23    than the current threshold 19. To test larger values, please \
24    adapt the script framework/data_files/dir-max/long.sh."
25#endif
26
27/* Test-only profile allowing all digests, PK algorithms, and curves. */
28const mbedtls_x509_crt_profile profile_all =
29{
30    0xFFFFFFFF, /* Any MD        */
31    0xFFFFFFFF, /* Any PK alg    */
32    0xFFFFFFFF, /* Any curve     */
33    1024,
34};
35
36/* Profile for backward compatibility. Allows SHA-1, unlike the default
37   profile. */
38const mbedtls_x509_crt_profile compat_profile =
39{
40    MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
41    MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
42    MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
43    MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
44    MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
45    MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
46    0xFFFFFFFF, /* Any PK alg    */
47    0xFFFFFFFF, /* Any curve     */
48    1024,
49};
50
51const mbedtls_x509_crt_profile profile_rsa3072 =
52{
53    MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
54    MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
55    MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
56    MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_RSA),
57    0,
58    3072,
59};
60
61const mbedtls_x509_crt_profile profile_sha512 =
62{
63    MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
64    0xFFFFFFFF, /* Any PK alg    */
65    0xFFFFFFFF, /* Any curve     */
66    1024,
67};
68
69#if defined(MBEDTLS_X509_CRT_PARSE_C)
70
71#if defined(MBEDTLS_FS_IO)
72static int verify_none(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
73{
74    ((void) data);
75    ((void) crt);
76    ((void) certificate_depth);
77    *flags |= MBEDTLS_X509_BADCERT_OTHER;
78
79    return 0;
80}
81
82static int verify_all(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
83{
84    ((void) data);
85    ((void) crt);
86    ((void) certificate_depth);
87    *flags = 0;
88
89    return 0;
90}
91
92#if defined(MBEDTLS_X509_CRL_PARSE_C) && \
93    defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
94static int ca_callback_fail(void *data, mbedtls_x509_crt const *child,
95                            mbedtls_x509_crt **candidates)
96{
97    ((void) data);
98    ((void) child);
99    ((void) candidates);
100
101    return -1;
102}
103
104static int ca_callback(void *data, mbedtls_x509_crt const *child,
105                       mbedtls_x509_crt **candidates)
106{
107    int ret = 0;
108    mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
109    mbedtls_x509_crt *first;
110
111    /* This is a test-only implementation of the CA callback
112     * which always returns the entire list of trusted certificates.
113     * Production implementations managing a large number of CAs
114     * should use an efficient presentation and lookup for the
115     * set of trusted certificates (such as a hashtable) and only
116     * return those trusted certificates which satisfy basic
117     * parental checks, such as the matching of child `Issuer`
118     * and parent `Subject` field. */
119    ((void) child);
120
121    first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
122    if (first == NULL) {
123        ret = -1;
124        goto exit;
125    }
126    mbedtls_x509_crt_init(first);
127
128    if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
129        ret = -1;
130        goto exit;
131    }
132
133    while (ca->next != NULL) {
134        ca = ca->next;
135        if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
136            ret = -1;
137            goto exit;
138        }
139    }
140
141exit:
142
143    if (ret != 0) {
144        mbedtls_x509_crt_free(first);
145        mbedtls_free(first);
146        first = NULL;
147    }
148
149    *candidates = first;
150    return ret;
151}
152#endif /* MBEDTLS_X509_CRL_PARSE_C && MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
153
154static int verify_fatal(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
155{
156    int *levels = (int *) data;
157
158    ((void) crt);
159    ((void) certificate_depth);
160
161    /* Simulate a fatal error in the callback */
162    if (*levels & (1 << certificate_depth)) {
163        *flags |= (1 << certificate_depth);
164        return -1 - certificate_depth;
165    }
166
167    return 0;
168}
169
170/* strsep() not available on Windows */
171static char *mystrsep(char **stringp, const char *delim)
172{
173    const char *p;
174    char *ret = *stringp;
175
176    if (*stringp == NULL) {
177        return NULL;
178    }
179
180    for (;; (*stringp)++) {
181        if (**stringp == '\0') {
182            *stringp = NULL;
183            goto done;
184        }
185
186        for (p = delim; *p != '\0'; p++) {
187            if (**stringp == *p) {
188                **stringp = '\0';
189                (*stringp)++;
190                goto done;
191            }
192        }
193    }
194
195done:
196    return ret;
197}
198
199typedef struct {
200    char buf[512];
201    char *p;
202} verify_print_context;
203
204static void verify_print_init(verify_print_context *ctx)
205{
206    memset(ctx, 0, sizeof(verify_print_context));
207    ctx->p = ctx->buf;
208}
209
210static int verify_print(void *data, mbedtls_x509_crt *crt, int certificate_depth, uint32_t *flags)
211{
212    int ret;
213    verify_print_context *ctx = (verify_print_context *) data;
214    char *p = ctx->p;
215    size_t n = ctx->buf + sizeof(ctx->buf) - ctx->p;
216    ((void) flags);
217
218    ret = mbedtls_snprintf(p, n, "depth %d - serial ", certificate_depth);
219    MBEDTLS_X509_SAFE_SNPRINTF;
220
221    ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
222    MBEDTLS_X509_SAFE_SNPRINTF;
223
224    ret = mbedtls_snprintf(p, n, " - subject ");
225    MBEDTLS_X509_SAFE_SNPRINTF;
226
227    ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
228    MBEDTLS_X509_SAFE_SNPRINTF;
229
230    ret = mbedtls_snprintf(p, n, " - flags 0x%08x\n", *flags);
231    MBEDTLS_X509_SAFE_SNPRINTF;
232
233    ctx->p = p;
234
235    return 0;
236}
237
238static int verify_parse_san(mbedtls_x509_subject_alternative_name *san,
239                            char **buf, size_t *size)
240{
241    int ret;
242    size_t i;
243    char *p = *buf;
244    size_t n = *size;
245
246    ret = mbedtls_snprintf(p, n, "type : %d", san->type);
247    MBEDTLS_X509_SAFE_SNPRINTF;
248
249    switch (san->type) {
250        case (MBEDTLS_X509_SAN_OTHER_NAME):
251            ret = mbedtls_snprintf(p, n, "\notherName :");
252            MBEDTLS_X509_SAFE_SNPRINTF;
253
254            if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
255                                &san->san.other_name.type_id) == 0) {
256                ret = mbedtls_snprintf(p, n, " hardware module name :");
257                MBEDTLS_X509_SAFE_SNPRINTF;
258                ret = mbedtls_snprintf(p, n, " hardware type : ");
259                MBEDTLS_X509_SAFE_SNPRINTF;
260
261                ret = mbedtls_oid_get_numeric_string(p,
262                                                     n,
263                                                     &san->san.other_name.value.hardware_module_name
264                                                     .oid);
265                MBEDTLS_X509_SAFE_SNPRINTF;
266
267                ret = mbedtls_snprintf(p, n, ", hardware serial number : ");
268                MBEDTLS_X509_SAFE_SNPRINTF;
269
270                for (i = 0; i < san->san.other_name.value.hardware_module_name.val.len; i++) {
271                    ret = mbedtls_snprintf(p,
272                                           n,
273                                           "%02X",
274                                           san->san.other_name.value.hardware_module_name.val.p[i]);
275                    MBEDTLS_X509_SAFE_SNPRINTF;
276                }
277            }
278            break;/* MBEDTLS_OID_ON_HW_MODULE_NAME */
279        case (MBEDTLS_X509_SAN_DNS_NAME):
280            ret = mbedtls_snprintf(p, n, "\ndNSName : ");
281            MBEDTLS_X509_SAFE_SNPRINTF;
282            if (san->san.unstructured_name.len >= n) {
283                *p = '\0';
284                return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
285            }
286            n -= san->san.unstructured_name.len;
287            for (i = 0; i < san->san.unstructured_name.len; i++) {
288                *p++ = san->san.unstructured_name.p[i];
289            }
290            break;/* MBEDTLS_X509_SAN_DNS_NAME */
291        case (MBEDTLS_X509_SAN_RFC822_NAME):
292            ret = mbedtls_snprintf(p, n, "\nrfc822Name : ");
293            MBEDTLS_X509_SAFE_SNPRINTF;
294            if (san->san.unstructured_name.len >= n) {
295                *p = '\0';
296                return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
297            }
298            n -= san->san.unstructured_name.len;
299            for (i = 0; i < san->san.unstructured_name.len; i++) {
300                *p++ = san->san.unstructured_name.p[i];
301            }
302            break;/* MBEDTLS_X509_SAN_RFC822_NAME */
303        case (MBEDTLS_X509_SAN_DIRECTORY_NAME):
304            ret = mbedtls_snprintf(p, n, "\ndirectoryName : ");
305            MBEDTLS_X509_SAFE_SNPRINTF;
306            ret = mbedtls_x509_dn_gets(p, n, &san->san.directory_name);
307            if (ret < 0) {
308                return ret;
309            }
310
311            p += ret;
312            n -= ret;
313            break;/* MBEDTLS_X509_SAN_DIRECTORY_NAME */
314        default:
315            /*
316             * Should not happen.
317             */
318            return -1;
319    }
320    ret = mbedtls_snprintf(p, n, "\n");
321    MBEDTLS_X509_SAFE_SNPRINTF;
322
323    *size = n;
324    *buf = p;
325
326    return 0;
327}
328#endif /* MBEDTLS_FS_IO */
329
330static int parse_crt_ext_cb(void *p_ctx, mbedtls_x509_crt const *crt, mbedtls_x509_buf const *oid,
331                            int critical, const unsigned char *cp, const unsigned char *end)
332{
333    (void) crt;
334    (void) critical;
335    mbedtls_x509_buf *new_oid = (mbedtls_x509_buf *) p_ctx;
336    if (oid->tag == MBEDTLS_ASN1_OID &&
337        MBEDTLS_OID_CMP(MBEDTLS_OID_CERTIFICATE_POLICIES, oid) == 0) {
338        /* Handle unknown certificate policy */
339        int ret, parse_ret = 0;
340        size_t len;
341        unsigned char **p = (unsigned char **) &cp;
342
343        /* Get main sequence tag */
344        ret = mbedtls_asn1_get_tag(p, end, &len,
345                                   MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
346        if (ret != 0) {
347            return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
348        }
349
350        if (*p + len != end) {
351            return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
352                                     MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
353        }
354
355        /*
356         * Cannot be an empty sequence.
357         */
358        if (len == 0) {
359            return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
360                                     MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
361        }
362
363        while (*p < end) {
364            const unsigned char *policy_end;
365
366            /*
367             * Get the policy sequence
368             */
369            if ((ret = mbedtls_asn1_get_tag(p, end, &len,
370                                            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
371                0) {
372                return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
373            }
374
375            policy_end = *p + len;
376
377            if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
378                                            MBEDTLS_ASN1_OID)) != 0) {
379                return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
380            }
381
382            /*
383             * Recognize exclusively the policy with OID 1
384             */
385            if (len != 1 || *p[0] != 1) {
386                parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
387            }
388
389            *p += len;
390
391            /*
392             * If there is an optional qualifier, then *p < policy_end
393             * Check the Qualifier len to verify it doesn't exceed policy_end.
394             */
395            if (*p < policy_end) {
396                if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
397                                                MBEDTLS_ASN1_CONSTRUCTED |
398                                                MBEDTLS_ASN1_SEQUENCE)) != 0) {
399                    return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
400                }
401                /*
402                 * Skip the optional policy qualifiers.
403                 */
404                *p += len;
405            }
406
407            if (*p != policy_end) {
408                return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
409                                         MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
410            }
411        }
412
413        if (*p != end) {
414            return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
415                                     MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
416        }
417
418        return parse_ret;
419    } else if (new_oid != NULL && new_oid->tag == oid->tag && new_oid->len == oid->len &&
420               memcmp(new_oid->p, oid->p, oid->len) == 0) {
421        return 0;
422    } else {
423        return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
424                                 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
425    }
426}
427#endif /* MBEDTLS_X509_CRT_PARSE_C */
428
429#if defined(MBEDTLS_X509_CSR_PARSE_C) && \
430    !defined(MBEDTLS_X509_REMOVE_INFO)
431static int parse_csr_ext_accept_cb(void *p_ctx,
432                                   mbedtls_x509_csr const *csr,
433                                   mbedtls_x509_buf const *oid,
434                                   int critical,
435                                   const unsigned char *cp,
436                                   const unsigned char *end)
437{
438    (void) p_ctx;
439    (void) csr;
440    (void) oid;
441    (void) critical;
442    (void) cp;
443    (void) end;
444
445    return 0;
446}
447
448static int parse_csr_ext_reject_cb(void *p_ctx,
449                                   mbedtls_x509_csr const *csr,
450                                   mbedtls_x509_buf const *oid,
451                                   int critical,
452                                   const unsigned char *cp,
453                                   const unsigned char *end)
454{
455    (void) p_ctx;
456    (void) csr;
457    (void) oid;
458    (void) critical;
459    (void) cp;
460    (void) end;
461
462    return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
463                             MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
464}
465#endif /* MBEDTLS_X509_CSR_PARSE_C && !MBEDTLS_X509_REMOVE_INFO */
466/* END_HEADER */
467
468/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
469void x509_accessor_ext_types(int ext_type, int has_ext_type)
470{
471    mbedtls_x509_crt crt;
472    int expected_result = ext_type & has_ext_type;
473
474    mbedtls_x509_crt_init(&crt);
475    USE_PSA_INIT();
476
477    crt.ext_types = ext_type;
478
479    TEST_EQUAL(mbedtls_x509_crt_has_ext_type(&crt, has_ext_type), expected_result);
480
481exit:
482    mbedtls_x509_crt_free(&crt);
483    USE_PSA_DONE();
484}
485/* END_CASE */
486
487/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_TEST_HOOKS */
488void x509_crt_parse_cn_inet_pton(const char *cn, data_t *exp, int ref_ret)
489{
490    uint32_t addr[4];
491    size_t addrlen = mbedtls_x509_crt_parse_cn_inet_pton(cn, addr);
492    TEST_EQUAL(addrlen, (size_t) ref_ret);
493
494    if (addrlen) {
495        TEST_MEMORY_COMPARE(exp->x, exp->len, addr, addrlen);
496    }
497}
498/* END_CASE */
499
500/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
501void x509_parse_san(char *crt_file, char *result_str, int parse_result)
502{
503    int ret;
504    mbedtls_x509_crt   crt;
505    mbedtls_x509_subject_alternative_name san;
506    mbedtls_x509_sequence *cur = NULL;
507    char buf[2000];
508    char *p = buf;
509    size_t n = sizeof(buf);
510
511    mbedtls_x509_crt_init(&crt);
512    USE_PSA_INIT();
513    memset(buf, 0, 2000);
514
515    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), parse_result);
516
517    if (parse_result != 0) {
518        goto exit;
519    }
520    if (crt.ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
521        cur = &crt.subject_alt_names;
522        while (cur != NULL) {
523            ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
524            TEST_ASSERT(ret == 0 || ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
525            /*
526             * If san type not supported, ignore.
527             */
528            if (ret == 0) {
529                ret = verify_parse_san(&san, &p, &n);
530                mbedtls_x509_free_subject_alt_name(&san);
531                TEST_EQUAL(ret, 0);
532            }
533            cur = cur->next;
534        }
535    }
536
537    TEST_EQUAL(strcmp(buf, result_str), 0);
538
539exit:
540    mbedtls_x509_crt_free(&crt);
541    USE_PSA_DONE();
542}
543/* END_CASE */
544
545/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:!MBEDTLS_X509_REMOVE_INFO:MBEDTLS_X509_CRT_PARSE_C */
546void x509_cert_info(char *crt_file, char *result_str)
547{
548    mbedtls_x509_crt   crt;
549    char buf[2000];
550    int res;
551
552    mbedtls_x509_crt_init(&crt);
553    USE_PSA_INIT();
554    memset(buf, 0, 2000);
555
556    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
557    res = mbedtls_x509_crt_info(buf, 2000, "", &crt);
558
559    TEST_ASSERT(res != -1);
560    TEST_ASSERT(res != -2);
561
562    TEST_EQUAL(strcmp(buf, result_str), 0);
563
564exit:
565    mbedtls_x509_crt_free(&crt);
566    USE_PSA_DONE();
567}
568/* END_CASE */
569
570/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
571void mbedtls_x509_crl_info(char *crl_file, char *result_str)
572{
573    mbedtls_x509_crl   crl;
574    char buf[2000];
575    int res;
576
577    mbedtls_x509_crl_init(&crl);
578    USE_PSA_INIT();
579    memset(buf, 0, 2000);
580
581    TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
582    res = mbedtls_x509_crl_info(buf, 2000, "", &crl);
583
584    TEST_ASSERT(res != -1);
585    TEST_ASSERT(res != -2);
586
587    TEST_EQUAL(strcmp(buf, result_str), 0);
588
589exit:
590    mbedtls_x509_crl_free(&crl);
591    USE_PSA_DONE();
592}
593/* END_CASE */
594
595/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRL_PARSE_C */
596void mbedtls_x509_crl_parse(char *crl_file, int result)
597{
598    mbedtls_x509_crl   crl;
599    char buf[2000];
600
601    mbedtls_x509_crl_init(&crl);
602    USE_PSA_INIT();
603    memset(buf, 0, 2000);
604
605    TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), result);
606
607exit:
608    mbedtls_x509_crl_free(&crl);
609    USE_PSA_DONE();
610}
611/* END_CASE */
612
613/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
614void mbedtls_x509_csr_info(char *csr_file, char *result_str)
615{
616    mbedtls_x509_csr   csr;
617    char buf[2000];
618    int res;
619
620    mbedtls_x509_csr_init(&csr);
621    USE_PSA_INIT();
622    memset(buf, 0, 2000);
623
624    TEST_EQUAL(mbedtls_x509_csr_parse_file(&csr, csr_file), 0);
625    res = mbedtls_x509_csr_info(buf, 2000, "", &csr);
626
627    TEST_ASSERT(res != -1);
628    TEST_ASSERT(res != -2);
629
630    TEST_EQUAL(strcmp(buf, result_str), 0);
631
632exit:
633    mbedtls_x509_csr_free(&csr);
634    USE_PSA_DONE();
635}
636/* END_CASE */
637
638/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
639void x509_verify_info(int flags, char *prefix, char *result_str)
640{
641    char buf[2000];
642    int res;
643
644    USE_PSA_INIT();
645    memset(buf, 0, sizeof(buf));
646
647    res = mbedtls_x509_crt_verify_info(buf, sizeof(buf), prefix, flags);
648
649    TEST_ASSERT(res >= 0);
650
651    TEST_EQUAL(strcmp(buf, result_str), 0);
652
653exit:
654    USE_PSA_DONE();
655}
656/* END_CASE */
657
658/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_ECP_RESTARTABLE:MBEDTLS_ECDSA_C */
659void x509_verify_restart(char *crt_file, char *ca_file,
660                         int result, int flags_result,
661                         int max_ops, int min_restart, int max_restart)
662{
663    int ret, cnt_restart;
664    mbedtls_x509_crt_restart_ctx rs_ctx;
665    mbedtls_x509_crt crt;
666    mbedtls_x509_crt ca;
667    uint32_t flags = 0;
668
669    /*
670     * See comments on ecp_test_vect_restart() for op count precision.
671     *
672     * For reference, with Mbed TLS 2.6 and default settings:
673     * - ecdsa_verify() for P-256:  ~  6700
674     * - ecdsa_verify() for P-384:  ~ 18800
675     * - x509_verify() for server5 -> test-ca2:             ~ 18800
676     * - x509_verify() for server10 -> int-ca3 -> int-ca2:  ~ 25500
677     */
678    mbedtls_x509_crt_restart_init(&rs_ctx);
679    mbedtls_x509_crt_init(&crt);
680    mbedtls_x509_crt_init(&ca);
681    MD_OR_USE_PSA_INIT();
682
683    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
684    TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
685
686    psa_interruptible_set_max_ops(max_ops);
687
688    cnt_restart = 0;
689    do {
690        ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
691                                                  &mbedtls_x509_crt_profile_default, NULL, &flags,
692                                                  NULL, NULL, &rs_ctx);
693    } while (ret == MBEDTLS_ERR_ECP_IN_PROGRESS && ++cnt_restart);
694
695    TEST_EQUAL(ret, result);
696    TEST_EQUAL(flags, (uint32_t) flags_result);
697
698    TEST_ASSERT(cnt_restart >= min_restart);
699    TEST_ASSERT(cnt_restart <= max_restart);
700
701    /* Do we leak memory when aborting? */
702    ret = mbedtls_x509_crt_verify_restartable(&crt, &ca, NULL,
703                                              &mbedtls_x509_crt_profile_default, NULL, &flags,
704                                              NULL, NULL, &rs_ctx);
705    TEST_ASSERT(ret == result || ret == MBEDTLS_ERR_ECP_IN_PROGRESS);
706
707exit:
708    mbedtls_x509_crt_restart_free(&rs_ctx);
709    mbedtls_x509_crt_free(&crt);
710    mbedtls_x509_crt_free(&ca);
711    MD_OR_USE_PSA_DONE();
712}
713/* END_CASE */
714
715/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C */
716void x509_verify(char *crt_file, char *ca_file, char *crl_file,
717                 char *cn_name_str, int result, int flags_result,
718                 char *profile_str,
719                 char *verify_callback)
720{
721    mbedtls_x509_crt   crt;
722    mbedtls_x509_crt   ca;
723    mbedtls_x509_crl    crl;
724    uint32_t         flags = 0;
725    int         res;
726    int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *) = NULL;
727    char *cn_name = NULL;
728    const mbedtls_x509_crt_profile *profile;
729
730    mbedtls_x509_crt_init(&crt);
731    mbedtls_x509_crt_init(&ca);
732    mbedtls_x509_crl_init(&crl);
733    MD_OR_USE_PSA_INIT();
734
735    if (strcmp(cn_name_str, "NULL") != 0) {
736        cn_name = cn_name_str;
737    }
738
739    if (strcmp(profile_str, "") == 0) {
740        profile = &mbedtls_x509_crt_profile_default;
741    } else if (strcmp(profile_str, "next") == 0) {
742        profile = &mbedtls_x509_crt_profile_next;
743    } else if (strcmp(profile_str, "suite_b") == 0) {
744        profile = &mbedtls_x509_crt_profile_suiteb;
745    } else if (strcmp(profile_str, "compat") == 0) {
746        profile = &compat_profile;
747    } else if (strcmp(profile_str, "all") == 0) {
748        profile = &profile_all;
749    } else {
750        TEST_FAIL("Unknown algorithm profile");
751    }
752
753    if (strcmp(verify_callback, "NULL") == 0) {
754        f_vrfy = NULL;
755    } else if (strcmp(verify_callback, "verify_none") == 0) {
756        f_vrfy = verify_none;
757    } else if (strcmp(verify_callback, "verify_all") == 0) {
758        f_vrfy = verify_all;
759    } else {
760        TEST_FAIL("No known verify callback selected");
761    }
762
763    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
764    TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
765    TEST_EQUAL(mbedtls_x509_crl_parse_file(&crl, crl_file), 0);
766
767    res = mbedtls_x509_crt_verify_with_profile(&crt,
768                                               &ca,
769                                               &crl,
770                                               profile,
771                                               cn_name,
772                                               &flags,
773                                               f_vrfy,
774                                               NULL);
775
776    TEST_EQUAL(res, result);
777    TEST_EQUAL(flags, (uint32_t) flags_result);
778
779#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
780    /* CRLs aren't supported with CA callbacks, so skip the CA callback
781     * version of the test if CRLs are in use. */
782    if (strcmp(crl_file, "") == 0) {
783        flags = 0;
784
785        res = mbedtls_x509_crt_verify_with_ca_cb(&crt,
786                                                 ca_callback,
787                                                 &ca,
788                                                 profile,
789                                                 cn_name,
790                                                 &flags,
791                                                 f_vrfy,
792                                                 NULL);
793
794        TEST_EQUAL(res, result);
795        TEST_EQUAL(flags, (uint32_t) (flags_result));
796    }
797#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
798exit:
799    mbedtls_x509_crt_free(&crt);
800    mbedtls_x509_crt_free(&ca);
801    mbedtls_x509_crl_free(&crl);
802    MD_OR_USE_PSA_DONE();
803}
804/* END_CASE */
805
806/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_CRL_PARSE_C:MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
807void x509_verify_ca_cb_failure(char *crt_file, char *ca_file, char *name,
808                               int exp_ret)
809{
810    int ret;
811    mbedtls_x509_crt crt;
812    mbedtls_x509_crt ca;
813    uint32_t flags = 0;
814
815    mbedtls_x509_crt_init(&crt);
816    mbedtls_x509_crt_init(&ca);
817    USE_PSA_INIT();
818
819    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
820    TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
821
822    if (strcmp(name, "NULL") == 0) {
823        name = NULL;
824    }
825
826    ret = mbedtls_x509_crt_verify_with_ca_cb(&crt, ca_callback_fail, &ca,
827                                             &compat_profile, name, &flags,
828                                             NULL, NULL);
829
830    TEST_EQUAL(ret, exp_ret);
831    TEST_EQUAL(flags, (uint32_t) (-1));
832exit:
833    mbedtls_x509_crt_free(&crt);
834    mbedtls_x509_crt_free(&ca);
835    USE_PSA_DONE();
836}
837/* END_CASE */
838
839/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
840void x509_verify_callback(char *crt_file, char *ca_file, char *name,
841                          int exp_ret, char *exp_vrfy_out)
842{
843    int ret;
844    mbedtls_x509_crt crt;
845    mbedtls_x509_crt ca;
846    uint32_t flags = 0;
847    verify_print_context vrfy_ctx;
848
849    mbedtls_x509_crt_init(&crt);
850    mbedtls_x509_crt_init(&ca);
851    MD_OR_USE_PSA_INIT();
852
853    verify_print_init(&vrfy_ctx);
854
855    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
856    TEST_EQUAL(mbedtls_x509_crt_parse_file(&ca, ca_file), 0);
857
858    if (strcmp(name, "NULL") == 0) {
859        name = NULL;
860    }
861
862    ret = mbedtls_x509_crt_verify_with_profile(&crt, &ca, NULL,
863                                               &compat_profile,
864                                               name, &flags,
865                                               verify_print, &vrfy_ctx);
866
867    TEST_EQUAL(ret, exp_ret);
868    TEST_EQUAL(strcmp(vrfy_ctx.buf, exp_vrfy_out), 0);
869
870exit:
871    mbedtls_x509_crt_free(&crt);
872    mbedtls_x509_crt_free(&ca);
873    MD_OR_USE_PSA_DONE();
874}
875/* END_CASE */
876
877/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
878void mbedtls_x509_dn_gets_subject_replace(char *crt_file,
879                                          char *new_subject_ou,
880                                          char *result_str,
881                                          int ret)
882{
883    mbedtls_x509_crt   crt;
884    char buf[2000];
885    int res = 0;
886
887    mbedtls_x509_crt_init(&crt);
888    USE_PSA_INIT();
889
890    memset(buf, 0, 2000);
891
892    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
893    crt.subject.next->val.p = (unsigned char *) new_subject_ou;
894    crt.subject.next->val.len = strlen(new_subject_ou);
895
896    res =  mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
897
898    if (ret != 0) {
899        TEST_EQUAL(res, ret);
900    } else {
901        TEST_ASSERT(res != -1);
902        TEST_ASSERT(res != -2);
903        TEST_EQUAL(strcmp(buf, result_str), 0);
904    }
905exit:
906    mbedtls_x509_crt_free(&crt);
907    USE_PSA_DONE();
908}
909/* END_CASE */
910
911/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
912void mbedtls_x509_dn_gets(char *crt_file, char *entity, char *result_str)
913{
914    mbedtls_x509_crt   crt;
915    char buf[2000];
916    int res = 0;
917
918    mbedtls_x509_crt_init(&crt);
919    USE_PSA_INIT();
920
921    memset(buf, 0, 2000);
922
923    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
924    if (strcmp(entity, "subject") == 0) {
925        res =  mbedtls_x509_dn_gets(buf, 2000, &crt.subject);
926    } else if (strcmp(entity, "issuer") == 0) {
927        res =  mbedtls_x509_dn_gets(buf, 2000, &crt.issuer);
928    } else {
929        TEST_FAIL("Unknown entity");
930    }
931
932    TEST_ASSERT(res != -1);
933    TEST_ASSERT(res != -2);
934
935    TEST_EQUAL(strcmp(buf, result_str), 0);
936
937exit:
938    mbedtls_x509_crt_free(&crt);
939    USE_PSA_DONE();
940}
941/* END_CASE */
942
943/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
944void mbedtls_x509_get_name(char *rdn_sequence, int exp_ret)
945{
946    unsigned char *name = NULL;
947    unsigned char *p;
948    size_t name_len;
949    mbedtls_x509_name head;
950    int ret;
951
952    USE_PSA_INIT();
953    memset(&head, 0, sizeof(head));
954
955    name = mbedtls_test_unhexify_alloc(rdn_sequence, &name_len);
956    p = name;
957
958    ret = mbedtls_x509_get_name(&p, (name + name_len), &head);
959    if (ret == 0) {
960        mbedtls_asn1_free_named_data_list_shallow(head.next);
961    }
962
963    TEST_EQUAL(ret, exp_ret);
964
965exit:
966    mbedtls_free(name);
967    USE_PSA_DONE();
968}
969/* END_CASE */
970
971/* BEGIN_CASE depends_on:MBEDTLS_X509_CREATE_C:MBEDTLS_X509_USE_C:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
972void mbedtls_x509_dn_get_next(char *name_str,
973                              int next_merged,
974                              char *expected_oids,
975                              int exp_count,
976                              char *exp_dn_gets)
977{
978    int ret = 0, i;
979    size_t len = 0, out_size;
980    mbedtls_asn1_named_data *names = NULL;
981    mbedtls_x509_name parsed;
982    memset(&parsed, 0, sizeof(parsed));
983    mbedtls_x509_name *parsed_cur;
984    // Size of buf is maximum required for test cases
985    unsigned char buf[80] = { 0 };
986    unsigned char *out = NULL;
987    unsigned char *c = buf + sizeof(buf);
988    const char *short_name;
989
990    USE_PSA_INIT();
991
992    // Additional size required for trailing space
993    out_size = strlen(expected_oids) + 2;
994    TEST_CALLOC(out, out_size);
995
996    TEST_EQUAL(mbedtls_x509_string_to_names(&names, name_str), 0);
997
998    ret = mbedtls_x509_write_names(&c, buf, names);
999    TEST_LE_S(0, ret);
1000
1001    TEST_EQUAL(mbedtls_asn1_get_tag(&c, buf + sizeof(buf), &len,
1002                                    MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE), 0);
1003    TEST_EQUAL(mbedtls_x509_get_name(&c, buf + sizeof(buf), &parsed), 0);
1004
1005    // Iterate over names and set next_merged nodes
1006    parsed_cur = &parsed;
1007    for (; next_merged != 0 && parsed_cur != NULL; next_merged = next_merged >> 1) {
1008        parsed_cur->next_merged = next_merged & 0x01;
1009        parsed_cur = parsed_cur->next;
1010    }
1011
1012    // Iterate over RDN nodes and print OID of first element to buffer
1013    parsed_cur = &parsed;
1014    len = 0;
1015    for (i = 0; parsed_cur != NULL; i++) {
1016        TEST_EQUAL(mbedtls_x509_oid_get_attr_short_name(&parsed_cur->oid,
1017                                                        &short_name), 0);
1018        len += mbedtls_snprintf((char *) out + len, out_size - len, "%s ", short_name);
1019        parsed_cur = mbedtls_x509_dn_get_next(parsed_cur);
1020    }
1021    out[len-1] = 0;
1022
1023    TEST_EQUAL(exp_count, i);
1024    TEST_EQUAL(strcmp((char *) out, expected_oids), 0);
1025    mbedtls_free(out);
1026    out = NULL;
1027
1028    out_size = strlen(exp_dn_gets) + 1;
1029    TEST_CALLOC(out, out_size);
1030
1031    TEST_LE_S(0, mbedtls_x509_dn_gets((char *) out, out_size, &parsed));
1032    TEST_EQUAL(strcmp((char *) out, exp_dn_gets), 0);
1033exit:
1034    mbedtls_free(out);
1035    mbedtls_asn1_free_named_data_list(&names);
1036    mbedtls_asn1_free_named_data_list_shallow(parsed.next);
1037    USE_PSA_DONE();
1038}
1039/* END_CASE */
1040
1041/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
1042void mbedtls_x509_time_is_past(char *crt_file, char *entity, int result)
1043{
1044    mbedtls_x509_crt   crt;
1045
1046    mbedtls_x509_crt_init(&crt);
1047    USE_PSA_INIT();
1048
1049    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
1050
1051    if (strcmp(entity, "valid_from") == 0) {
1052        TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_from), result);
1053    } else if (strcmp(entity, "valid_to") == 0) {
1054        TEST_EQUAL(mbedtls_x509_time_is_past(&crt.valid_to), result);
1055    } else {
1056        TEST_FAIL("Unknown entity");
1057    }
1058
1059exit:
1060    mbedtls_x509_crt_free(&crt);
1061    USE_PSA_DONE();
1062}
1063/* END_CASE */
1064
1065/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
1066void mbedtls_x509_time_is_future(char *crt_file, char *entity, int result)
1067{
1068    mbedtls_x509_crt   crt;
1069
1070    mbedtls_x509_crt_init(&crt);
1071    USE_PSA_INIT();
1072
1073    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
1074
1075    if (strcmp(entity, "valid_from") == 0) {
1076        TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_from), result);
1077    } else if (strcmp(entity, "valid_to") == 0) {
1078        TEST_EQUAL(mbedtls_x509_time_is_future(&crt.valid_to), result);
1079    } else {
1080        TEST_FAIL("Unknown entity");
1081    }
1082
1083exit:
1084    mbedtls_x509_crt_free(&crt);
1085    USE_PSA_DONE();
1086}
1087/* END_CASE */
1088
1089/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
1090void x509parse_crt_file(char *crt_file, int result)
1091{
1092    mbedtls_x509_crt crt;
1093
1094    mbedtls_x509_crt_init(&crt);
1095    USE_PSA_INIT();
1096
1097    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), result);
1098
1099exit:
1100    mbedtls_x509_crt_free(&crt);
1101    USE_PSA_DONE();
1102}
1103/* END_CASE */
1104
1105/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
1106void mbedtls_x509_get_ca_istrue(char *crt_file, int result)
1107{
1108    mbedtls_x509_crt   crt;
1109    mbedtls_x509_crt_init(&crt);
1110    USE_PSA_INIT();
1111
1112    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
1113    TEST_EQUAL(mbedtls_x509_crt_get_ca_istrue(&crt), result);
1114exit:
1115    mbedtls_x509_crt_free(&crt);
1116    USE_PSA_DONE();
1117}
1118/* END_CASE */
1119
1120/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
1121void x509parse_crt(data_t *buf, char *result_str, int result)
1122{
1123    mbedtls_x509_crt   crt;
1124#if !defined(MBEDTLS_X509_REMOVE_INFO)
1125    unsigned char output[2000] = { 0 };
1126#else
1127    ((void) result_str);
1128#endif
1129    /* Tests whose result is MBEDTLS_ERR_PK_INVALID_PUBKEY might return
1130     * MBEDTLS_ERR_ASN1_UNEXPECTED_TAG until psa#308 is merged. This variable
1131     * is therefore used for backward compatiblity and will be removed in
1132     * mbedtls#10229. */
1133    int result_back_comp = result;
1134    int res;
1135
1136#if !defined(MBEDTLS_PK_USE_PSA_RSA_DATA)
1137    /* Support for mbedtls#10213 before psa#308. Once psa#308 will be
1138     * merged this dirty fix can be removed. */
1139    if (result == MBEDTLS_ERR_PK_INVALID_PUBKEY) {
1140        result_back_comp = MBEDTLS_ERR_ASN1_UNEXPECTED_TAG;
1141    }
1142#endif /* MBEDTLS_PK_USE_PSA_RSA_DATA */
1143
1144    mbedtls_x509_crt_init(&crt);
1145    USE_PSA_INIT();
1146
1147    res = mbedtls_x509_crt_parse_der(&crt, buf->x, buf->len);
1148    TEST_ASSERT((res == result) || (res == result_back_comp));
1149#if !defined(MBEDTLS_X509_REMOVE_INFO)
1150    if ((result) == 0) {
1151        res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1152        TEST_ASSERT(res != -1);
1153        TEST_ASSERT(res != -2);
1154
1155        TEST_EQUAL(strcmp((char *) output, result_str), 0);
1156    }
1157    memset(output, 0, 2000);
1158#endif
1159
1160    mbedtls_x509_crt_free(&crt);
1161    mbedtls_x509_crt_init(&crt);
1162
1163    res = mbedtls_x509_crt_parse_der_nocopy(&crt, buf->x, buf->len);
1164    TEST_ASSERT((res == result) || (res == result_back_comp));
1165#if !defined(MBEDTLS_X509_REMOVE_INFO)
1166    if ((result) == 0) {
1167        memset(output, 0, 2000);
1168
1169        res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1170
1171        TEST_ASSERT(res != -1);
1172        TEST_ASSERT(res != -2);
1173
1174        TEST_EQUAL(strcmp((char *) output, result_str), 0);
1175    }
1176    memset(output, 0, 2000);
1177#endif /* !MBEDTLS_X509_REMOVE_INFO */
1178
1179    mbedtls_x509_crt_free(&crt);
1180    mbedtls_x509_crt_init(&crt);
1181
1182    res = mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, NULL, NULL);
1183    TEST_ASSERT((res == result) || (res == result_back_comp));
1184#if !defined(MBEDTLS_X509_REMOVE_INFO)
1185    if ((result) == 0) {
1186        res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1187
1188        TEST_ASSERT(res != -1);
1189        TEST_ASSERT(res != -2);
1190
1191        TEST_EQUAL(strcmp((char *) output, result_str), 0);
1192    }
1193    memset(output, 0, 2000);
1194#endif /* !MBEDTLS_X509_REMOVE_INFO */
1195
1196    mbedtls_x509_crt_free(&crt);
1197    mbedtls_x509_crt_init(&crt);
1198
1199    res = mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, NULL, NULL);
1200    TEST_ASSERT((res == result) || (res == result_back_comp));
1201#if !defined(MBEDTLS_X509_REMOVE_INFO)
1202    if ((result) == 0) {
1203        res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1204
1205        TEST_ASSERT(res != -1);
1206        TEST_ASSERT(res != -2);
1207
1208        TEST_EQUAL(strcmp((char *) output, result_str), 0);
1209    }
1210#endif /* !MBEDTLS_X509_REMOVE_INFO */
1211
1212exit:
1213    mbedtls_x509_crt_free(&crt);
1214    USE_PSA_DONE();
1215}
1216/* END_CASE */
1217
1218/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
1219void x509parse_crt_cb(data_t *buf, char *result_str, int result)
1220{
1221    mbedtls_x509_crt   crt;
1222    mbedtls_x509_buf   oid;
1223
1224#if !defined(MBEDTLS_X509_REMOVE_INFO)
1225    unsigned char output[2000] = { 0 };
1226    int res;
1227#else
1228    ((void) result_str);
1229#endif
1230
1231    oid.tag = MBEDTLS_ASN1_OID;
1232    oid.len = MBEDTLS_OID_SIZE(MBEDTLS_OID_PKIX "\x01\x1F");
1233    oid.p = (unsigned char *) MBEDTLS_OID_PKIX "\x01\x1F";
1234
1235    mbedtls_x509_crt_init(&crt);
1236    USE_PSA_INIT();
1237
1238    TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 0, parse_crt_ext_cb,
1239                                                      &oid), result);
1240#if !defined(MBEDTLS_X509_REMOVE_INFO)
1241    if ((result) == 0) {
1242        res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1243
1244        TEST_ASSERT(res != -1);
1245        TEST_ASSERT(res != -2);
1246
1247        TEST_EQUAL(strcmp((char *) output, result_str), 0);
1248    }
1249    memset(output, 0, 2000);
1250#endif /* !MBEDTLS_X509_REMOVE_INFO */
1251
1252    mbedtls_x509_crt_free(&crt);
1253    mbedtls_x509_crt_init(&crt);
1254
1255    TEST_EQUAL(mbedtls_x509_crt_parse_der_with_ext_cb(&crt, buf->x, buf->len, 1, parse_crt_ext_cb,
1256                                                      &oid), (result));
1257#if !defined(MBEDTLS_X509_REMOVE_INFO)
1258    if ((result) == 0) {
1259        res = mbedtls_x509_crt_info((char *) output, 2000, "", &crt);
1260
1261        TEST_ASSERT(res != -1);
1262        TEST_ASSERT(res != -2);
1263
1264        TEST_EQUAL(strcmp((char *) output, result_str), 0);
1265    }
1266#endif /* !MBEDTLS_X509_REMOVE_INFO */
1267
1268exit:
1269    mbedtls_x509_crt_free(&crt);
1270    USE_PSA_DONE();
1271}
1272/* END_CASE */
1273
1274/* BEGIN_CASE depends_on:MBEDTLS_X509_CRL_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1275void x509parse_crl(data_t *buf, char *result_str, int result)
1276{
1277    mbedtls_x509_crl   crl;
1278    unsigned char output[2000];
1279    int res;
1280
1281    mbedtls_x509_crl_init(&crl);
1282    USE_PSA_INIT();
1283
1284    memset(output, 0, 2000);
1285
1286
1287    TEST_EQUAL(mbedtls_x509_crl_parse(&crl, buf->x, buf->len), (result));
1288    if ((result) == 0) {
1289        res = mbedtls_x509_crl_info((char *) output, 2000, "", &crl);
1290
1291        TEST_ASSERT(res != -1);
1292        TEST_ASSERT(res != -2);
1293
1294        TEST_EQUAL(strcmp((char *) output, result_str), 0);
1295    }
1296
1297exit:
1298    mbedtls_x509_crl_free(&crl);
1299    USE_PSA_DONE();
1300}
1301/* END_CASE */
1302
1303/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1304void mbedtls_x509_csr_parse(data_t *csr_der, char *ref_out, int ref_ret)
1305{
1306    mbedtls_x509_csr csr;
1307    char my_out[1000];
1308    int my_ret;
1309
1310    mbedtls_x509_csr_init(&csr);
1311    USE_PSA_INIT();
1312
1313    memset(my_out, 0, sizeof(my_out));
1314
1315    my_ret = mbedtls_x509_csr_parse_der(&csr, csr_der->x, csr_der->len);
1316    TEST_EQUAL(my_ret, ref_ret);
1317
1318    if (ref_ret == 0) {
1319        size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1320        TEST_EQUAL(my_out_len, strlen(ref_out));
1321        TEST_EQUAL(strcmp(my_out, ref_out), 0);
1322    }
1323
1324exit:
1325    mbedtls_x509_csr_free(&csr);
1326    USE_PSA_DONE();
1327}
1328/* END_CASE */
1329
1330/* BEGIN_CASE depends_on:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1331void mbedtls_x509_csr_parse_with_ext_cb(data_t *csr_der, char *ref_out, int ref_ret, int accept)
1332{
1333    mbedtls_x509_csr csr;
1334    char my_out[1000];
1335    int my_ret;
1336
1337    mbedtls_x509_csr_init(&csr);
1338    USE_PSA_INIT();
1339
1340    memset(my_out, 0, sizeof(my_out));
1341
1342    my_ret = mbedtls_x509_csr_parse_der_with_ext_cb(&csr, csr_der->x, csr_der->len,
1343                                                    accept ? parse_csr_ext_accept_cb :
1344                                                    parse_csr_ext_reject_cb,
1345                                                    NULL);
1346    TEST_EQUAL(my_ret, ref_ret);
1347
1348    if (ref_ret == 0) {
1349        size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1350        TEST_EQUAL(my_out_len, strlen(ref_out));
1351        TEST_EQUAL(strcmp(my_out, ref_out), 0);
1352    }
1353
1354exit:
1355    mbedtls_x509_csr_free(&csr);
1356    USE_PSA_DONE();
1357}
1358/* END_CASE */
1359
1360/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CSR_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1361void mbedtls_x509_csr_parse_file(char *csr_file, char *ref_out, int ref_ret)
1362{
1363    mbedtls_x509_csr csr;
1364    char my_out[1000];
1365    int my_ret;
1366
1367    mbedtls_x509_csr_init(&csr);
1368    USE_PSA_INIT();
1369
1370    memset(my_out, 0, sizeof(my_out));
1371
1372    my_ret = mbedtls_x509_csr_parse_file(&csr, csr_file);
1373    TEST_EQUAL(my_ret, ref_ret);
1374
1375    if (ref_ret == 0) {
1376        size_t my_out_len = mbedtls_x509_csr_info(my_out, sizeof(my_out), "", &csr);
1377        TEST_EQUAL(my_out_len, strlen(ref_out));
1378        TEST_EQUAL(strcmp(my_out, ref_out), 0);
1379    }
1380
1381exit:
1382    mbedtls_x509_csr_free(&csr);
1383    USE_PSA_DONE();
1384}
1385/* END_CASE */
1386
1387/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
1388void mbedtls_x509_crt_parse_file(char *crt_path, int ret, int nb_crt)
1389{
1390    mbedtls_x509_crt chain, *cur;
1391    int i;
1392
1393    mbedtls_x509_crt_init(&chain);
1394    USE_PSA_INIT();
1395
1396    TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, crt_path), ret);
1397
1398    /* Check how many certs we got */
1399    for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1400        if (cur->raw.p != NULL) {
1401            i++;
1402        }
1403    }
1404
1405    TEST_EQUAL(i, nb_crt);
1406
1407exit:
1408    mbedtls_x509_crt_free(&chain);
1409    USE_PSA_DONE();
1410}
1411/* END_CASE */
1412
1413/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
1414void mbedtls_x509_crt_parse_path(char *crt_path, int ret, int nb_crt)
1415{
1416    mbedtls_x509_crt chain, *cur;
1417    int i;
1418
1419    mbedtls_x509_crt_init(&chain);
1420    USE_PSA_INIT();
1421
1422    TEST_EQUAL(mbedtls_x509_crt_parse_path(&chain, crt_path), ret);
1423
1424    /* Check how many certs we got */
1425    for (i = 0, cur = &chain; cur != NULL; cur = cur->next) {
1426        if (cur->raw.p != NULL) {
1427            i++;
1428        }
1429    }
1430
1431    TEST_EQUAL(i, nb_crt);
1432
1433exit:
1434    mbedtls_x509_crt_free(&chain);
1435    USE_PSA_DONE();
1436}
1437/* END_CASE */
1438
1439/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
1440void mbedtls_x509_crt_verify_max(char *ca_file, char *chain_dir, int nb_int,
1441                                 int ret_chk, int flags_chk)
1442{
1443    char file_buf[128];
1444    int ret;
1445    uint32_t flags;
1446    mbedtls_x509_crt trusted, chain;
1447
1448    /*
1449     * We expect chain_dir to contain certificates 00.crt, 01.crt, etc.
1450     * with NN.crt signed by NN-1.crt
1451     */
1452    mbedtls_x509_crt_init(&trusted);
1453    mbedtls_x509_crt_init(&chain);
1454    MD_OR_USE_PSA_INIT();
1455
1456    /* Load trusted root */
1457    TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, ca_file), 0);
1458
1459    /* Load a chain with nb_int intermediates (from 01 to nb_int),
1460     * plus one "end-entity" cert (nb_int + 1) */
1461    ret = mbedtls_snprintf(file_buf, sizeof(file_buf), "%s/c%02d.pem", chain_dir,
1462                           nb_int + 1);
1463    TEST_ASSERT(ret > 0 && (size_t) ret < sizeof(file_buf));
1464    TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, file_buf), 0);
1465
1466    /* Try to verify that chain */
1467    ret = mbedtls_x509_crt_verify(&chain, &trusted, NULL, NULL, &flags,
1468                                  NULL, NULL);
1469    TEST_EQUAL(ret, ret_chk);
1470    TEST_EQUAL(flags, (uint32_t) flags_chk);
1471
1472exit:
1473    mbedtls_x509_crt_free(&chain);
1474    mbedtls_x509_crt_free(&trusted);
1475    MD_OR_USE_PSA_DONE();
1476}
1477/* END_CASE */
1478
1479/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
1480void mbedtls_x509_crt_verify_chain(char *chain_paths, char *trusted_ca,
1481                                   int flags_result, int result,
1482                                   char *profile_name, int vrfy_fatal_lvls)
1483{
1484    char *act;
1485    uint32_t flags;
1486    int res;
1487    mbedtls_x509_crt trusted, chain;
1488    const mbedtls_x509_crt_profile *profile = NULL;
1489
1490    mbedtls_x509_crt_init(&chain);
1491    mbedtls_x509_crt_init(&trusted);
1492    MD_OR_USE_PSA_INIT();
1493
1494    while ((act = mystrsep(&chain_paths, " ")) != NULL) {
1495        TEST_EQUAL(mbedtls_x509_crt_parse_file(&chain, act), 0);
1496    }
1497    TEST_EQUAL(mbedtls_x509_crt_parse_file(&trusted, trusted_ca), 0);
1498
1499    if (strcmp(profile_name, "") == 0) {
1500        profile = &mbedtls_x509_crt_profile_default;
1501    } else if (strcmp(profile_name, "next") == 0) {
1502        profile = &mbedtls_x509_crt_profile_next;
1503    } else if (strcmp(profile_name, "suiteb") == 0) {
1504        profile = &mbedtls_x509_crt_profile_suiteb;
1505    } else if (strcmp(profile_name, "rsa3072") == 0) {
1506        profile = &profile_rsa3072;
1507    } else if (strcmp(profile_name, "sha512") == 0) {
1508        profile = &profile_sha512;
1509    }
1510
1511    res = mbedtls_x509_crt_verify_with_profile(&chain, &trusted, NULL, profile,
1512                                               NULL, &flags, verify_fatal, &vrfy_fatal_lvls);
1513
1514    TEST_EQUAL(res, (result));
1515    TEST_EQUAL(flags, (uint32_t) (flags_result));
1516
1517exit:
1518    mbedtls_x509_crt_free(&trusted);
1519    mbedtls_x509_crt_free(&chain);
1520    MD_OR_USE_PSA_DONE();
1521}
1522/* END_CASE */
1523
1524/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:!MBEDTLS_X509_REMOVE_INFO */
1525void x509_oid_desc(data_t *buf, char *ref_desc)
1526{
1527    mbedtls_x509_buf oid;
1528    const char *desc = NULL;
1529    int ret;
1530
1531    USE_PSA_INIT();
1532
1533    oid.tag = MBEDTLS_ASN1_OID;
1534    oid.p   = buf->x;
1535    oid.len   = buf->len;
1536
1537    ret = mbedtls_x509_oid_get_extended_key_usage(&oid, &desc);
1538
1539    if (strcmp(ref_desc, "notfound") == 0) {
1540        TEST_ASSERT(ret != 0);
1541        TEST_ASSERT(desc == NULL);
1542    } else {
1543        TEST_EQUAL(ret, 0);
1544        TEST_ASSERT(desc != NULL);
1545        TEST_EQUAL(strcmp(desc, ref_desc), 0);
1546    }
1547
1548exit:
1549    USE_PSA_DONE();
1550}
1551/* END_CASE */
1552
1553/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
1554void x509_oid_numstr(data_t *oid_buf, char *numstr, int blen, int ret)
1555{
1556    mbedtls_x509_buf oid;
1557    char num_buf[100];
1558
1559    USE_PSA_INIT();
1560
1561    memset(num_buf, 0x2a, sizeof(num_buf));
1562
1563    oid.tag = MBEDTLS_ASN1_OID;
1564    oid.p   = oid_buf->x;
1565    oid.len   = oid_buf->len;
1566
1567    TEST_ASSERT((size_t) blen <= sizeof(num_buf));
1568
1569    TEST_EQUAL(mbedtls_oid_get_numeric_string(num_buf, blen, &oid), ret);
1570
1571    if (ret >= 0) {
1572        TEST_EQUAL(num_buf[ret], 0);
1573        TEST_EQUAL(strcmp(num_buf, numstr), 0);
1574    }
1575
1576exit:
1577    USE_PSA_DONE();
1578}
1579/* END_CASE */
1580
1581/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
1582void x509_check_key_usage(char *crt_file, int usage, int ret)
1583{
1584    mbedtls_x509_crt crt;
1585
1586    mbedtls_x509_crt_init(&crt);
1587    USE_PSA_INIT();
1588
1589    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
1590
1591    TEST_EQUAL(mbedtls_x509_crt_check_key_usage(&crt, usage), ret);
1592
1593exit:
1594    mbedtls_x509_crt_free(&crt);
1595    USE_PSA_DONE();
1596}
1597/* END_CASE */
1598
1599/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_X509_CRT_PARSE_C */
1600void x509_check_extended_key_usage(char *crt_file, data_t *oid, int ret
1601                                   )
1602{
1603    mbedtls_x509_crt crt;
1604
1605    mbedtls_x509_crt_init(&crt);
1606    USE_PSA_INIT();
1607
1608    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, crt_file), 0);
1609
1610    TEST_EQUAL(mbedtls_x509_crt_check_extended_key_usage(&crt, (const char *) oid->x, oid->len),
1611               ret);
1612
1613exit:
1614    mbedtls_x509_crt_free(&crt);
1615    USE_PSA_DONE();
1616}
1617/* END_CASE */
1618
1619/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
1620void x509_get_time(int tag, char *time_str, int ret, int year, int mon,
1621                   int day, int hour, int min, int sec)
1622{
1623    mbedtls_x509_time time;
1624    unsigned char buf[21];
1625    unsigned char *start = buf;
1626    unsigned char *end = buf;
1627
1628    USE_PSA_INIT();
1629    memset(&time, 0x00, sizeof(time));
1630    *end = (unsigned char) tag; end++;
1631    *end = strlen(time_str);
1632    TEST_ASSERT(*end < 20);
1633    end++;
1634    memcpy(end, time_str, (size_t) *(end - 1));
1635    end += *(end - 1);
1636
1637    TEST_EQUAL(mbedtls_x509_get_time(&start, end, &time), ret);
1638    if (ret == 0) {
1639        TEST_EQUAL(year, time.year);
1640        TEST_EQUAL(mon, time.mon);
1641        TEST_EQUAL(day, time.day);
1642        TEST_EQUAL(hour, time.hour);
1643        TEST_EQUAL(min, time.min);
1644        TEST_EQUAL(sec, time.sec);
1645    }
1646exit:
1647    USE_PSA_DONE();
1648}
1649/* END_CASE */
1650
1651/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_X509_RSASSA_PSS_SUPPORT */
1652void x509_parse_rsassa_pss_params(data_t *params, int params_tag,
1653                                  int ref_msg_md, int ref_mgf_md,
1654                                  int ref_salt_len, int ref_ret)
1655{
1656    int my_ret;
1657    mbedtls_x509_buf buf;
1658    mbedtls_md_type_t my_msg_md, my_mgf_md;
1659    int my_salt_len;
1660
1661    USE_PSA_INIT();
1662
1663    buf.p = params->x;
1664    buf.len = params->len;
1665    buf.tag = params_tag;
1666
1667    my_ret = mbedtls_x509_get_rsassa_pss_params(&buf, &my_msg_md, &my_mgf_md,
1668                                                &my_salt_len);
1669
1670    TEST_EQUAL(my_ret, ref_ret);
1671
1672    if (ref_ret == 0) {
1673        TEST_EQUAL(my_msg_md, (mbedtls_md_type_t) ref_msg_md);
1674        TEST_EQUAL(my_mgf_md, (mbedtls_md_type_t) ref_mgf_md);
1675        TEST_EQUAL(my_salt_len, ref_salt_len);
1676    }
1677
1678exit:
1679    USE_PSA_DONE();
1680}
1681/* END_CASE */
1682
1683/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
1684void x509_crt_parse_subjectkeyid(char *file, data_t *subjectKeyId, int ref_ret)
1685{
1686    mbedtls_x509_crt crt;
1687
1688    mbedtls_x509_crt_init(&crt);
1689    /* X509 relies on PK under the hood and the latter can use PSA to store keys
1690     * and perform operations so psa_crypto_init() must be called before. */
1691    USE_PSA_INIT();
1692
1693    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
1694
1695    if (ref_ret == 0) {
1696        TEST_EQUAL(crt.subject_key_id.tag, MBEDTLS_ASN1_OCTET_STRING);
1697        TEST_EQUAL(memcmp(crt.subject_key_id.p, subjectKeyId->x, subjectKeyId->len), 0);
1698        TEST_EQUAL(crt.subject_key_id.len, subjectKeyId->len);
1699    } else {
1700        TEST_EQUAL(crt.subject_key_id.tag, 0);
1701        TEST_EQUAL(crt.subject_key_id.len, 0);
1702    }
1703
1704exit:
1705    mbedtls_x509_crt_free(&crt);
1706    USE_PSA_DONE();
1707}
1708/* END_CASE */
1709
1710/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C:MBEDTLS_FS_IO */
1711void x509_crt_parse_authoritykeyid(char *file,
1712                                   data_t *keyId,
1713                                   char *authorityKeyId_issuer,
1714                                   data_t *serial,
1715                                   int ref_ret)
1716{
1717    mbedtls_x509_crt crt;
1718    mbedtls_x509_subject_alternative_name san;
1719    char name_buf[128];
1720
1721    mbedtls_x509_crt_init(&crt);
1722    /* X509 relies on PK under the hood and the latter can use PSA to store keys
1723     * and perform operations so psa_crypto_init() must be called before. */
1724    USE_PSA_INIT();
1725
1726    TEST_EQUAL(mbedtls_x509_crt_parse_file(&crt, file), ref_ret);
1727
1728    if (ref_ret == 0) {
1729        /* KeyId test */
1730        if (keyId->len > 0) {
1731            TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, MBEDTLS_ASN1_OCTET_STRING);
1732            TEST_EQUAL(memcmp(crt.authority_key_id.keyIdentifier.p, keyId->x, keyId->len), 0);
1733            TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, keyId->len);
1734        } else {
1735            TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1736            TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
1737        }
1738
1739
1740        /* Issuer test */
1741        if (strlen(authorityKeyId_issuer) > 0) {
1742            mbedtls_x509_sequence *issuerPtr = &crt.authority_key_id.authorityCertIssuer;
1743
1744            TEST_EQUAL(mbedtls_x509_parse_subject_alt_name(&issuerPtr->buf, &san), 0);
1745
1746            TEST_ASSERT(mbedtls_x509_dn_gets(name_buf, sizeof(name_buf),
1747                                             &san.san.directory_name)
1748                        > 0);
1749            TEST_EQUAL(strcmp(name_buf, authorityKeyId_issuer), 0);
1750
1751            mbedtls_x509_free_subject_alt_name(&san);
1752        }
1753
1754        /* Serial test */
1755        if (serial->len > 0) {
1756            TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag,
1757                       MBEDTLS_ASN1_INTEGER);
1758            TEST_EQUAL(memcmp(crt.authority_key_id.authorityCertSerialNumber.p,
1759                              serial->x, serial->len), 0);
1760            TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, serial->len);
1761        } else {
1762            TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1763            TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
1764        }
1765
1766    } else {
1767        TEST_EQUAL(crt.authority_key_id.keyIdentifier.tag, 0);
1768        TEST_EQUAL(crt.authority_key_id.keyIdentifier.len, 0);
1769
1770        TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.tag, 0);
1771        TEST_EQUAL(crt.authority_key_id.authorityCertSerialNumber.len, 0);
1772    }
1773
1774exit:
1775    mbedtls_x509_crt_free(&crt);
1776    USE_PSA_DONE();
1777}
1778/* END_CASE */
1779
1780/* BEGIN_CASE depends_on:MBEDTLS_X509_USE_C */
1781void oid_get_numeric_string(data_t *oid, int error_ret, char *result_str)
1782{
1783    char buf[256];
1784    mbedtls_asn1_buf input_oid = { 0, 0, NULL };
1785    int ret;
1786
1787    input_oid.tag = MBEDTLS_ASN1_OID;
1788    /* Test that an empty OID is not dereferenced */
1789    input_oid.p = oid->len ? oid->x : (void *) 1;
1790    input_oid.len = oid->len;
1791
1792    ret = mbedtls_oid_get_numeric_string(buf, sizeof(buf), &input_oid);
1793
1794    if (error_ret == 0) {
1795        TEST_EQUAL(ret, strlen(result_str));
1796        TEST_ASSERT(ret >= 3);
1797        TEST_EQUAL(strcmp(buf, result_str), 0);
1798    } else {
1799        TEST_EQUAL(ret, error_ret);
1800    }
1801}
1802/* END_CASE */
1803