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/asn1t.h> 13e1051a39Sopenharmony_ci#include <openssl/conf.h> 14e1051a39Sopenharmony_ci#include <openssl/x509v3.h> 15e1051a39Sopenharmony_ci#include "ext_dat.h" 16e1051a39Sopenharmony_ci 17e1051a39Sopenharmony_cistatic void *v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method, 18e1051a39Sopenharmony_ci X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval); 19e1051a39Sopenharmony_cistatic STACK_OF(CONF_VALUE) *i2v_POLICY_MAPPINGS(const X509V3_EXT_METHOD 20e1051a39Sopenharmony_ci *method, void *pmps, STACK_OF(CONF_VALUE) 21e1051a39Sopenharmony_ci *extlist); 22e1051a39Sopenharmony_ci 23e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_policy_mappings = { 24e1051a39Sopenharmony_ci NID_policy_mappings, 0, 25e1051a39Sopenharmony_ci ASN1_ITEM_ref(POLICY_MAPPINGS), 26e1051a39Sopenharmony_ci 0, 0, 0, 0, 27e1051a39Sopenharmony_ci 0, 0, 28e1051a39Sopenharmony_ci i2v_POLICY_MAPPINGS, 29e1051a39Sopenharmony_ci v2i_POLICY_MAPPINGS, 30e1051a39Sopenharmony_ci 0, 0, 31e1051a39Sopenharmony_ci NULL 32e1051a39Sopenharmony_ci}; 33e1051a39Sopenharmony_ci 34e1051a39Sopenharmony_ciASN1_SEQUENCE(POLICY_MAPPING) = { 35e1051a39Sopenharmony_ci ASN1_SIMPLE(POLICY_MAPPING, issuerDomainPolicy, ASN1_OBJECT), 36e1051a39Sopenharmony_ci ASN1_SIMPLE(POLICY_MAPPING, subjectDomainPolicy, ASN1_OBJECT) 37e1051a39Sopenharmony_ci} ASN1_SEQUENCE_END(POLICY_MAPPING) 38e1051a39Sopenharmony_ci 39e1051a39Sopenharmony_ciASN1_ITEM_TEMPLATE(POLICY_MAPPINGS) = 40e1051a39Sopenharmony_ci ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, POLICY_MAPPINGS, 41e1051a39Sopenharmony_ci POLICY_MAPPING) 42e1051a39Sopenharmony_ciASN1_ITEM_TEMPLATE_END(POLICY_MAPPINGS) 43e1051a39Sopenharmony_ci 44e1051a39Sopenharmony_ciIMPLEMENT_ASN1_ALLOC_FUNCTIONS(POLICY_MAPPING) 45e1051a39Sopenharmony_ci 46e1051a39Sopenharmony_cistatic STACK_OF(CONF_VALUE) *i2v_POLICY_MAPPINGS(const X509V3_EXT_METHOD 47e1051a39Sopenharmony_ci *method, void *a, STACK_OF(CONF_VALUE) 48e1051a39Sopenharmony_ci *ext_list) 49e1051a39Sopenharmony_ci{ 50e1051a39Sopenharmony_ci POLICY_MAPPINGS *pmaps = a; 51e1051a39Sopenharmony_ci POLICY_MAPPING *pmap; 52e1051a39Sopenharmony_ci int i; 53e1051a39Sopenharmony_ci char obj_tmp1[80]; 54e1051a39Sopenharmony_ci char obj_tmp2[80]; 55e1051a39Sopenharmony_ci 56e1051a39Sopenharmony_ci for (i = 0; i < sk_POLICY_MAPPING_num(pmaps); i++) { 57e1051a39Sopenharmony_ci pmap = sk_POLICY_MAPPING_value(pmaps, i); 58e1051a39Sopenharmony_ci i2t_ASN1_OBJECT(obj_tmp1, 80, pmap->issuerDomainPolicy); 59e1051a39Sopenharmony_ci i2t_ASN1_OBJECT(obj_tmp2, 80, pmap->subjectDomainPolicy); 60e1051a39Sopenharmony_ci X509V3_add_value(obj_tmp1, obj_tmp2, &ext_list); 61e1051a39Sopenharmony_ci } 62e1051a39Sopenharmony_ci return ext_list; 63e1051a39Sopenharmony_ci} 64e1051a39Sopenharmony_ci 65e1051a39Sopenharmony_cistatic void *v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method, 66e1051a39Sopenharmony_ci X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval) 67e1051a39Sopenharmony_ci{ 68e1051a39Sopenharmony_ci POLICY_MAPPING *pmap = NULL; 69e1051a39Sopenharmony_ci ASN1_OBJECT *obj1 = NULL, *obj2 = NULL; 70e1051a39Sopenharmony_ci CONF_VALUE *val; 71e1051a39Sopenharmony_ci POLICY_MAPPINGS *pmaps; 72e1051a39Sopenharmony_ci const int num = sk_CONF_VALUE_num(nval); 73e1051a39Sopenharmony_ci int i; 74e1051a39Sopenharmony_ci 75e1051a39Sopenharmony_ci if ((pmaps = sk_POLICY_MAPPING_new_reserve(NULL, num)) == NULL) { 76e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); 77e1051a39Sopenharmony_ci return NULL; 78e1051a39Sopenharmony_ci } 79e1051a39Sopenharmony_ci 80e1051a39Sopenharmony_ci for (i = 0; i < num; i++) { 81e1051a39Sopenharmony_ci val = sk_CONF_VALUE_value(nval, i); 82e1051a39Sopenharmony_ci if (!val->value || !val->name) { 83e1051a39Sopenharmony_ci ERR_raise_data(ERR_LIB_X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER, 84e1051a39Sopenharmony_ci "%s", val->name); 85e1051a39Sopenharmony_ci goto err; 86e1051a39Sopenharmony_ci } 87e1051a39Sopenharmony_ci obj1 = OBJ_txt2obj(val->name, 0); 88e1051a39Sopenharmony_ci obj2 = OBJ_txt2obj(val->value, 0); 89e1051a39Sopenharmony_ci if (!obj1 || !obj2) { 90e1051a39Sopenharmony_ci ERR_raise_data(ERR_LIB_X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER, 91e1051a39Sopenharmony_ci "%s", val->name); 92e1051a39Sopenharmony_ci goto err; 93e1051a39Sopenharmony_ci } 94e1051a39Sopenharmony_ci pmap = POLICY_MAPPING_new(); 95e1051a39Sopenharmony_ci if (pmap == NULL) { 96e1051a39Sopenharmony_ci ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE); 97e1051a39Sopenharmony_ci goto err; 98e1051a39Sopenharmony_ci } 99e1051a39Sopenharmony_ci pmap->issuerDomainPolicy = obj1; 100e1051a39Sopenharmony_ci pmap->subjectDomainPolicy = obj2; 101e1051a39Sopenharmony_ci obj1 = obj2 = NULL; 102e1051a39Sopenharmony_ci sk_POLICY_MAPPING_push(pmaps, pmap); /* no failure as it was reserved */ 103e1051a39Sopenharmony_ci } 104e1051a39Sopenharmony_ci return pmaps; 105e1051a39Sopenharmony_ci err: 106e1051a39Sopenharmony_ci ASN1_OBJECT_free(obj1); 107e1051a39Sopenharmony_ci ASN1_OBJECT_free(obj2); 108e1051a39Sopenharmony_ci sk_POLICY_MAPPING_pop_free(pmaps, POLICY_MAPPING_free); 109e1051a39Sopenharmony_ci return NULL; 110e1051a39Sopenharmony_ci} 111