1*66bae5e7Schristos /*
2*66bae5e7Schristos  * Copyright 2003-2021 The OpenSSL Project Authors. All Rights Reserved.
3*66bae5e7Schristos  *
4*66bae5e7Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*66bae5e7Schristos  * this file except in compliance with the License.  You can obtain a copy
6*66bae5e7Schristos  * in the file LICENSE in the source distribution or at
7*66bae5e7Schristos  * https://www.openssl.org/source/license.html
8*66bae5e7Schristos  */
9*66bae5e7Schristos 
10*66bae5e7Schristos #include <stdio.h>
11*66bae5e7Schristos #include "internal/cryptlib.h"
12*66bae5e7Schristos #include <openssl/asn1.h>
13*66bae5e7Schristos #include <openssl/asn1t.h>
14*66bae5e7Schristos #include <openssl/conf.h>
15*66bae5e7Schristos #include <openssl/x509v3.h>
16*66bae5e7Schristos #include "ext_dat.h"
17*66bae5e7Schristos 
18*66bae5e7Schristos static STACK_OF(CONF_VALUE) *i2v_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD
19*66bae5e7Schristos                                                     *method, void *bcons, STACK_OF(CONF_VALUE)
20*66bae5e7Schristos                                                     *extlist);
21*66bae5e7Schristos static void *v2i_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD *method,
22*66bae5e7Schristos                                     X509V3_CTX *ctx,
23*66bae5e7Schristos                                     STACK_OF(CONF_VALUE) *values);
24*66bae5e7Schristos 
25*66bae5e7Schristos const X509V3_EXT_METHOD ossl_v3_policy_constraints = {
26*66bae5e7Schristos     NID_policy_constraints, 0,
27*66bae5e7Schristos     ASN1_ITEM_ref(POLICY_CONSTRAINTS),
28*66bae5e7Schristos     0, 0, 0, 0,
29*66bae5e7Schristos     0, 0,
30*66bae5e7Schristos     i2v_POLICY_CONSTRAINTS,
31*66bae5e7Schristos     v2i_POLICY_CONSTRAINTS,
32*66bae5e7Schristos     NULL, NULL,
33*66bae5e7Schristos     NULL
34*66bae5e7Schristos };
35*66bae5e7Schristos 
36*66bae5e7Schristos ASN1_SEQUENCE(POLICY_CONSTRAINTS) = {
37*66bae5e7Schristos         ASN1_IMP_OPT(POLICY_CONSTRAINTS, requireExplicitPolicy, ASN1_INTEGER,0),
38*66bae5e7Schristos         ASN1_IMP_OPT(POLICY_CONSTRAINTS, inhibitPolicyMapping, ASN1_INTEGER,1)
39*66bae5e7Schristos } ASN1_SEQUENCE_END(POLICY_CONSTRAINTS)
40*66bae5e7Schristos 
41*66bae5e7Schristos IMPLEMENT_ASN1_ALLOC_FUNCTIONS(POLICY_CONSTRAINTS)
42*66bae5e7Schristos 
43*66bae5e7Schristos static STACK_OF(CONF_VALUE) *i2v_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD
44*66bae5e7Schristos                                                     *method, void *a, STACK_OF(CONF_VALUE)
45*66bae5e7Schristos                                                     *extlist)
46*66bae5e7Schristos {
47*66bae5e7Schristos     POLICY_CONSTRAINTS *pcons = a;
48*66bae5e7Schristos     X509V3_add_value_int("Require Explicit Policy",
49*66bae5e7Schristos                          pcons->requireExplicitPolicy, &extlist);
50*66bae5e7Schristos     X509V3_add_value_int("Inhibit Policy Mapping",
51*66bae5e7Schristos                          pcons->inhibitPolicyMapping, &extlist);
52*66bae5e7Schristos     return extlist;
53*66bae5e7Schristos }
54*66bae5e7Schristos 
v2i_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD * method,X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* values)55*66bae5e7Schristos static void *v2i_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD *method,
56*66bae5e7Schristos                                     X509V3_CTX *ctx,
57*66bae5e7Schristos                                     STACK_OF(CONF_VALUE) *values)
58*66bae5e7Schristos {
59*66bae5e7Schristos     POLICY_CONSTRAINTS *pcons = NULL;
60*66bae5e7Schristos     CONF_VALUE *val;
61*66bae5e7Schristos     int i;
62*66bae5e7Schristos 
63*66bae5e7Schristos     if ((pcons = POLICY_CONSTRAINTS_new()) == NULL) {
64*66bae5e7Schristos         ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
65*66bae5e7Schristos         return NULL;
66*66bae5e7Schristos     }
67*66bae5e7Schristos     for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
68*66bae5e7Schristos         val = sk_CONF_VALUE_value(values, i);
69*66bae5e7Schristos         if (strcmp(val->name, "requireExplicitPolicy") == 0) {
70*66bae5e7Schristos             if (!X509V3_get_value_int(val, &pcons->requireExplicitPolicy))
71*66bae5e7Schristos                 goto err;
72*66bae5e7Schristos         } else if (strcmp(val->name, "inhibitPolicyMapping") == 0) {
73*66bae5e7Schristos             if (!X509V3_get_value_int(val, &pcons->inhibitPolicyMapping))
74*66bae5e7Schristos                 goto err;
75*66bae5e7Schristos         } else {
76*66bae5e7Schristos             ERR_raise_data(ERR_LIB_X509V3, X509V3_R_INVALID_NAME,
77*66bae5e7Schristos                            "%s", val->name);
78*66bae5e7Schristos             goto err;
79*66bae5e7Schristos         }
80*66bae5e7Schristos     }
81*66bae5e7Schristos     if (pcons->inhibitPolicyMapping == NULL
82*66bae5e7Schristos             && pcons->requireExplicitPolicy == NULL) {
83*66bae5e7Schristos         ERR_raise(ERR_LIB_X509V3, X509V3_R_ILLEGAL_EMPTY_EXTENSION);
84*66bae5e7Schristos         goto err;
85*66bae5e7Schristos     }
86*66bae5e7Schristos 
87*66bae5e7Schristos     return pcons;
88*66bae5e7Schristos  err:
89*66bae5e7Schristos     POLICY_CONSTRAINTS_free(pcons);
90*66bae5e7Schristos     return NULL;
91*66bae5e7Schristos }
92