1 /*
2 * Copyright 2003-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/conf.h>
14 #include <openssl/x509v3.h>
15 #include "ext_dat.h"
16
17 static void *v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method,
18 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval);
19 static STACK_OF(CONF_VALUE) *i2v_POLICY_MAPPINGS(const X509V3_EXT_METHOD
20 *method, void *pmps, STACK_OF(CONF_VALUE)
21 *extlist);
22
23 const X509V3_EXT_METHOD ossl_v3_policy_mappings = {
24 NID_policy_mappings, 0,
25 ASN1_ITEM_ref(POLICY_MAPPINGS),
26 0, 0, 0, 0,
27 0, 0,
28 i2v_POLICY_MAPPINGS,
29 v2i_POLICY_MAPPINGS,
30 0, 0,
31 NULL
32 };
33
34 ASN1_SEQUENCE(POLICY_MAPPING) = {
35 ASN1_SIMPLE(POLICY_MAPPING, issuerDomainPolicy, ASN1_OBJECT),
36 ASN1_SIMPLE(POLICY_MAPPING, subjectDomainPolicy, ASN1_OBJECT)
37 } ASN1_SEQUENCE_END(POLICY_MAPPING)
38
39 ASN1_ITEM_TEMPLATE(POLICY_MAPPINGS) =
40 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, POLICY_MAPPINGS,
41 POLICY_MAPPING)
42 ASN1_ITEM_TEMPLATE_END(POLICY_MAPPINGS)
43
44 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(POLICY_MAPPING)
45
46 static STACK_OF(CONF_VALUE) *i2v_POLICY_MAPPINGS(const X509V3_EXT_METHOD
47 *method, void *a, STACK_OF(CONF_VALUE)
48 *ext_list)
49 {
50 POLICY_MAPPINGS *pmaps = a;
51 POLICY_MAPPING *pmap;
52 int i;
53 char obj_tmp1[80];
54 char obj_tmp2[80];
55
56 for (i = 0; i < sk_POLICY_MAPPING_num(pmaps); i++) {
57 pmap = sk_POLICY_MAPPING_value(pmaps, i);
58 i2t_ASN1_OBJECT(obj_tmp1, 80, pmap->issuerDomainPolicy);
59 i2t_ASN1_OBJECT(obj_tmp2, 80, pmap->subjectDomainPolicy);
60 X509V3_add_value(obj_tmp1, obj_tmp2, &ext_list);
61 }
62 return ext_list;
63 }
64
v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD * method,X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* nval)65 static void *v2i_POLICY_MAPPINGS(const X509V3_EXT_METHOD *method,
66 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
67 {
68 POLICY_MAPPING *pmap = NULL;
69 ASN1_OBJECT *obj1 = NULL, *obj2 = NULL;
70 CONF_VALUE *val;
71 POLICY_MAPPINGS *pmaps;
72 const int num = sk_CONF_VALUE_num(nval);
73 int i;
74
75 if ((pmaps = sk_POLICY_MAPPING_new_reserve(NULL, num)) == NULL) {
76 ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
77 return NULL;
78 }
79
80 for (i = 0; i < num; i++) {
81 val = sk_CONF_VALUE_value(nval, i);
82 if (!val->value || !val->name) {
83 ERR_raise_data(ERR_LIB_X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER,
84 "%s", val->name);
85 goto err;
86 }
87 obj1 = OBJ_txt2obj(val->name, 0);
88 obj2 = OBJ_txt2obj(val->value, 0);
89 if (!obj1 || !obj2) {
90 ERR_raise_data(ERR_LIB_X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER,
91 "%s", val->name);
92 goto err;
93 }
94 pmap = POLICY_MAPPING_new();
95 if (pmap == NULL) {
96 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
97 goto err;
98 }
99 pmap->issuerDomainPolicy = obj1;
100 pmap->subjectDomainPolicy = obj2;
101 obj1 = obj2 = NULL;
102 sk_POLICY_MAPPING_push(pmaps, pmap); /* no failure as it was reserved */
103 }
104 return pmaps;
105 err:
106 ASN1_OBJECT_free(obj1);
107 ASN1_OBJECT_free(obj2);
108 sk_POLICY_MAPPING_pop_free(pmaps, POLICY_MAPPING_free);
109 return NULL;
110 }
111