1e1051a39Sopenharmony_ci/* 2e1051a39Sopenharmony_ci * Copyright 2003-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 18e1051a39Sopenharmony_cistatic STACK_OF(CONF_VALUE) *i2v_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD 19e1051a39Sopenharmony_ci *method, void *bcons, STACK_OF(CONF_VALUE) 20e1051a39Sopenharmony_ci *extlist); 21e1051a39Sopenharmony_cistatic void *v2i_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD *method, 22e1051a39Sopenharmony_ci X509V3_CTX *ctx, 23e1051a39Sopenharmony_ci STACK_OF(CONF_VALUE) *values); 24e1051a39Sopenharmony_ci 25e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_policy_constraints = { 26e1051a39Sopenharmony_ci NID_policy_constraints, 0, 27e1051a39Sopenharmony_ci ASN1_ITEM_ref(POLICY_CONSTRAINTS), 28e1051a39Sopenharmony_ci 0, 0, 0, 0, 29e1051a39Sopenharmony_ci 0, 0, 30e1051a39Sopenharmony_ci i2v_POLICY_CONSTRAINTS, 31e1051a39Sopenharmony_ci v2i_POLICY_CONSTRAINTS, 32e1051a39Sopenharmony_ci NULL, NULL, 33e1051a39Sopenharmony_ci NULL 34e1051a39Sopenharmony_ci}; 35e1051a39Sopenharmony_ci 36e1051a39Sopenharmony_ciASN1_SEQUENCE(POLICY_CONSTRAINTS) = { 37e1051a39Sopenharmony_ci ASN1_IMP_OPT(POLICY_CONSTRAINTS, requireExplicitPolicy, ASN1_INTEGER,0), 38e1051a39Sopenharmony_ci ASN1_IMP_OPT(POLICY_CONSTRAINTS, inhibitPolicyMapping, ASN1_INTEGER,1) 39e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END(POLICY_CONSTRAINTS) 40e1051a39Sopenharmony_ci 41e1051a39Sopenharmony_ciIMPLEMENT_ASN1_ALLOC_FUNCTIONS(POLICY_CONSTRAINTS) 42e1051a39Sopenharmony_ci 43e1051a39Sopenharmony_cistatic STACK_OF(CONF_VALUE) *i2v_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD 44e1051a39Sopenharmony_ci *method, void *a, STACK_OF(CONF_VALUE) 45e1051a39Sopenharmony_ci *extlist) 46e1051a39Sopenharmony_ci{ 47e1051a39Sopenharmony_ci POLICY_CONSTRAINTS *pcons = a; 48e1051a39Sopenharmony_ci X509V3_add_value_int("Require Explicit Policy", 49e1051a39Sopenharmony_ci pcons->requireExplicitPolicy, &extlist); 50e1051a39Sopenharmony_ci X509V3_add_value_int("Inhibit Policy Mapping", 51e1051a39Sopenharmony_ci pcons->inhibitPolicyMapping, &extlist); 52e1051a39Sopenharmony_ci return extlist; 53e1051a39Sopenharmony_ci} 54e1051a39Sopenharmony_ci 55e1051a39Sopenharmony_cistatic void *v2i_POLICY_CONSTRAINTS(const X509V3_EXT_METHOD *method, 56e1051a39Sopenharmony_ci X509V3_CTX *ctx, 57e1051a39Sopenharmony_ci STACK_OF(CONF_VALUE) *values) 58e1051a39Sopenharmony_ci{ 59e1051a39Sopenharmony_ci POLICY_CONSTRAINTS *pcons = NULL; 60e1051a39Sopenharmony_ci CONF_VALUE *val; 61e1051a39Sopenharmony_ci int i; 62e1051a39Sopenharmony_ci 63e1051a39Sopenharmony_ci if ((pcons = POLICY_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, "requireExplicitPolicy") == 0) { 70e1051a39Sopenharmony_ci if (!X509V3_get_value_int(val, &pcons->requireExplicitPolicy)) 71e1051a39Sopenharmony_ci goto err; 72e1051a39Sopenharmony_ci } else if (strcmp(val->name, "inhibitPolicyMapping") == 0) { 73e1051a39Sopenharmony_ci if (!X509V3_get_value_int(val, &pcons->inhibitPolicyMapping)) 74e1051a39Sopenharmony_ci goto err; 75e1051a39Sopenharmony_ci } else { 76e1051a39Sopenharmony_ci ERR_raise_data(ERR_LIB_X509V3, X509V3_R_INVALID_NAME, 77e1051a39Sopenharmony_ci "%s", val->name); 78e1051a39Sopenharmony_ci goto err; 79e1051a39Sopenharmony_ci } 80e1051a39Sopenharmony_ci } 81e1051a39Sopenharmony_ci if (pcons->inhibitPolicyMapping == NULL 82e1051a39Sopenharmony_ci && pcons->requireExplicitPolicy == NULL) { 83e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509V3, X509V3_R_ILLEGAL_EMPTY_EXTENSION); 84e1051a39Sopenharmony_ci goto err; 85e1051a39Sopenharmony_ci } 86e1051a39Sopenharmony_ci 87e1051a39Sopenharmony_ci return pcons; 88e1051a39Sopenharmony_ci err: 89e1051a39Sopenharmony_ci POLICY_CONSTRAINTS_free(pcons); 90e1051a39Sopenharmony_ci return NULL; 91e1051a39Sopenharmony_ci} 92