1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
12e1051a39Sopenharmony_ci#include <openssl/asn1.h>
13e1051a39Sopenharmony_ci#include <openssl/asn1t.h>
14e1051a39Sopenharmony_ci#include <openssl/conf.h>
15e1051a39Sopenharmony_ci#include <openssl/x509v3.h>
16e1051a39Sopenharmony_ci#include "ext_dat.h"
17e1051a39Sopenharmony_ci#include "x509_local.h"
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_cistatic STACK_OF(CONF_VALUE) *i2v_BASIC_CONSTRAINTS(X509V3_EXT_METHOD *method,
20e1051a39Sopenharmony_ci                                                   BASIC_CONSTRAINTS *bcons,
21e1051a39Sopenharmony_ci                                                   STACK_OF(CONF_VALUE)
22e1051a39Sopenharmony_ci                                                   *extlist);
23e1051a39Sopenharmony_cistatic BASIC_CONSTRAINTS *v2i_BASIC_CONSTRAINTS(X509V3_EXT_METHOD *method,
24e1051a39Sopenharmony_ci                                                X509V3_CTX *ctx,
25e1051a39Sopenharmony_ci                                                STACK_OF(CONF_VALUE) *values);
26e1051a39Sopenharmony_ci
27e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_bcons = {
28e1051a39Sopenharmony_ci    NID_basic_constraints, 0,
29e1051a39Sopenharmony_ci    ASN1_ITEM_ref(BASIC_CONSTRAINTS),
30e1051a39Sopenharmony_ci    0, 0, 0, 0,
31e1051a39Sopenharmony_ci    0, 0,
32e1051a39Sopenharmony_ci    (X509V3_EXT_I2V) i2v_BASIC_CONSTRAINTS,
33e1051a39Sopenharmony_ci    (X509V3_EXT_V2I)v2i_BASIC_CONSTRAINTS,
34e1051a39Sopenharmony_ci    NULL, NULL,
35e1051a39Sopenharmony_ci    NULL
36e1051a39Sopenharmony_ci};
37e1051a39Sopenharmony_ci
38e1051a39Sopenharmony_ciASN1_SEQUENCE(BASIC_CONSTRAINTS) = {
39e1051a39Sopenharmony_ci        ASN1_OPT(BASIC_CONSTRAINTS, ca, ASN1_FBOOLEAN),
40e1051a39Sopenharmony_ci        ASN1_OPT(BASIC_CONSTRAINTS, pathlen, ASN1_INTEGER)
41e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END(BASIC_CONSTRAINTS)
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ciIMPLEMENT_ASN1_FUNCTIONS(BASIC_CONSTRAINTS)
44e1051a39Sopenharmony_ci
45e1051a39Sopenharmony_cistatic STACK_OF(CONF_VALUE) *i2v_BASIC_CONSTRAINTS(X509V3_EXT_METHOD *method,
46e1051a39Sopenharmony_ci                                                   BASIC_CONSTRAINTS *bcons,
47e1051a39Sopenharmony_ci                                                   STACK_OF(CONF_VALUE)
48e1051a39Sopenharmony_ci                                                   *extlist)
49e1051a39Sopenharmony_ci{
50e1051a39Sopenharmony_ci    X509V3_add_value_bool("CA", bcons->ca, &extlist);
51e1051a39Sopenharmony_ci    X509V3_add_value_int("pathlen", bcons->pathlen, &extlist);
52e1051a39Sopenharmony_ci    return extlist;
53e1051a39Sopenharmony_ci}
54e1051a39Sopenharmony_ci
55e1051a39Sopenharmony_cistatic BASIC_CONSTRAINTS *v2i_BASIC_CONSTRAINTS(X509V3_EXT_METHOD *method,
56e1051a39Sopenharmony_ci                                                X509V3_CTX *ctx,
57e1051a39Sopenharmony_ci                                                STACK_OF(CONF_VALUE) *values)
58e1051a39Sopenharmony_ci{
59e1051a39Sopenharmony_ci    BASIC_CONSTRAINTS *bcons = NULL;
60e1051a39Sopenharmony_ci    CONF_VALUE *val;
61e1051a39Sopenharmony_ci    int i;
62e1051a39Sopenharmony_ci
63e1051a39Sopenharmony_ci    if ((bcons = BASIC_CONSTRAINTS_new()) == NULL) {
64e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
65e1051a39Sopenharmony_ci        return NULL;
66e1051a39Sopenharmony_ci    }
67e1051a39Sopenharmony_ci    for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
68e1051a39Sopenharmony_ci        val = sk_CONF_VALUE_value(values, i);
69e1051a39Sopenharmony_ci        if (strcmp(val->name, "CA") == 0) {
70e1051a39Sopenharmony_ci            if (!X509V3_get_value_bool(val, &bcons->ca))
71e1051a39Sopenharmony_ci                goto err;
72e1051a39Sopenharmony_ci        } else if (strcmp(val->name, "pathlen") == 0) {
73e1051a39Sopenharmony_ci            if (!X509V3_get_value_int(val, &bcons->pathlen))
74e1051a39Sopenharmony_ci                goto err;
75e1051a39Sopenharmony_ci        } else {
76e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NAME);
77e1051a39Sopenharmony_ci            X509V3_conf_add_error_name_value(val);
78e1051a39Sopenharmony_ci            goto err;
79e1051a39Sopenharmony_ci        }
80e1051a39Sopenharmony_ci    }
81e1051a39Sopenharmony_ci    return bcons;
82e1051a39Sopenharmony_ci err:
83e1051a39Sopenharmony_ci    BASIC_CONSTRAINTS_free(bcons);
84e1051a39Sopenharmony_ci    return NULL;
85e1051a39Sopenharmony_ci}
86