1 // Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdio.h>
16 #include <string.h>
17
18 #include <openssl/asn1.h>
19 #include <openssl/asn1t.h>
20 #include <openssl/conf.h>
21 #include <openssl/err.h>
22 #include <openssl/obj.h>
23 #include <openssl/x509.h>
24
25 #include "internal.h"
26
27
28 static STACK_OF(CONF_VALUE) *i2v_BASIC_CONSTRAINTS(
29 const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *extlist);
30 static void *v2i_BASIC_CONSTRAINTS(const X509V3_EXT_METHOD *method,
31 const X509V3_CTX *ctx,
32 const STACK_OF(CONF_VALUE) *values);
33
34 const X509V3_EXT_METHOD v3_bcons = {
35 NID_basic_constraints,
36 0,
37 ASN1_ITEM_ref(BASIC_CONSTRAINTS),
38 0,
39 0,
40 0,
41 0,
42 0,
43 0,
44 i2v_BASIC_CONSTRAINTS,
45 v2i_BASIC_CONSTRAINTS,
46 NULL,
47 NULL,
48 NULL,
49 };
50
ASN1_SEQUENCE(BASIC_CONSTRAINTS)51 ASN1_SEQUENCE(BASIC_CONSTRAINTS) = {
52 ASN1_OPT(BASIC_CONSTRAINTS, ca, ASN1_FBOOLEAN),
53 ASN1_OPT(BASIC_CONSTRAINTS, pathlen, ASN1_INTEGER),
54 } ASN1_SEQUENCE_END(BASIC_CONSTRAINTS)
55
56 IMPLEMENT_ASN1_FUNCTIONS_const(BASIC_CONSTRAINTS)
57
58 static STACK_OF(CONF_VALUE) *i2v_BASIC_CONSTRAINTS(
59 const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *extlist) {
60 const BASIC_CONSTRAINTS *bcons =
61 reinterpret_cast<const BASIC_CONSTRAINTS *>(ext);
62 X509V3_add_value_bool("CA", bcons->ca, &extlist);
63 X509V3_add_value_int("pathlen", bcons->pathlen, &extlist);
64 return extlist;
65 }
66
v2i_BASIC_CONSTRAINTS(const X509V3_EXT_METHOD * method,const X509V3_CTX * ctx,const STACK_OF (CONF_VALUE)* values)67 static void *v2i_BASIC_CONSTRAINTS(const X509V3_EXT_METHOD *method,
68 const X509V3_CTX *ctx,
69 const STACK_OF(CONF_VALUE) *values) {
70 BASIC_CONSTRAINTS *bcons = NULL;
71 if (!(bcons = BASIC_CONSTRAINTS_new())) {
72 return NULL;
73 }
74 for (size_t i = 0; i < sk_CONF_VALUE_num(values); i++) {
75 const CONF_VALUE *val = sk_CONF_VALUE_value(values, i);
76 if (!strcmp(val->name, "CA")) {
77 if (!X509V3_get_value_bool(val, &bcons->ca)) {
78 goto err;
79 }
80 } else if (!strcmp(val->name, "pathlen")) {
81 if (!X509V3_get_value_int(val, &bcons->pathlen)) {
82 goto err;
83 }
84 } else {
85 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NAME);
86 X509V3_conf_err(val);
87 goto err;
88 }
89 }
90 return bcons;
91 err:
92 BASIC_CONSTRAINTS_free(bcons);
93 return NULL;
94 }
95