xref: /openssl/crypto/x509/v3_attrmap.c (revision 93b5275f)
1 /*
2  * Copyright 2024 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 <openssl/asn1t.h>
11 #include <openssl/x509v3.h>
12 #include <crypto/x509.h>
13 #include "ext_dat.h"
14 
15 ASN1_SEQUENCE(OSSL_ATAV) = {
16     ASN1_SIMPLE(OSSL_ATAV, type, ASN1_OBJECT),
17     ASN1_SIMPLE(OSSL_ATAV, value, ASN1_ANY)
18 } ASN1_SEQUENCE_END(OSSL_ATAV)
19 
20 ASN1_SEQUENCE(OSSL_ATTRIBUTE_TYPE_MAPPING) = {
21     ASN1_IMP(OSSL_ATTRIBUTE_TYPE_MAPPING, local, ASN1_OBJECT, 0),
22     ASN1_IMP(OSSL_ATTRIBUTE_TYPE_MAPPING, remote, ASN1_OBJECT, 1),
23 } ASN1_SEQUENCE_END(OSSL_ATTRIBUTE_TYPE_MAPPING)
24 
25 ASN1_SEQUENCE(OSSL_ATTRIBUTE_VALUE_MAPPING) = {
26     ASN1_IMP(OSSL_ATTRIBUTE_VALUE_MAPPING, local, OSSL_ATAV, 0),
27     ASN1_IMP(OSSL_ATTRIBUTE_VALUE_MAPPING, remote, OSSL_ATAV, 1),
28 } ASN1_SEQUENCE_END(OSSL_ATTRIBUTE_VALUE_MAPPING)
29 
30 ASN1_CHOICE(OSSL_ATTRIBUTE_MAPPING) = {
31     ASN1_IMP(OSSL_ATTRIBUTE_MAPPING, choice.typeMappings,
32              OSSL_ATTRIBUTE_TYPE_MAPPING, OSSL_ATTR_MAP_TYPE),
33     ASN1_IMP(OSSL_ATTRIBUTE_MAPPING, choice.typeValueMappings,
34              OSSL_ATTRIBUTE_VALUE_MAPPING, OSSL_ATTR_MAP_VALUE),
35 } ASN1_CHOICE_END(OSSL_ATTRIBUTE_MAPPING)
36 
37 ASN1_ITEM_TEMPLATE(OSSL_ATTRIBUTE_MAPPINGS) =
38     ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, OSSL_ATTRIBUTE_MAPPINGS, OSSL_ATTRIBUTE_MAPPING)
39 ASN1_ITEM_TEMPLATE_END(OSSL_ATTRIBUTE_MAPPINGS)
40 
41 IMPLEMENT_ASN1_FUNCTIONS(OSSL_ATAV)
42 IMPLEMENT_ASN1_FUNCTIONS(OSSL_ATTRIBUTE_TYPE_MAPPING)
43 IMPLEMENT_ASN1_FUNCTIONS(OSSL_ATTRIBUTE_VALUE_MAPPING)
44 IMPLEMENT_ASN1_FUNCTIONS(OSSL_ATTRIBUTE_MAPPING)
45 IMPLEMENT_ASN1_FUNCTIONS(OSSL_ATTRIBUTE_MAPPINGS)
46 
47 static int i2r_ATTRIBUTE_MAPPING(X509V3_EXT_METHOD *method,
48                                  OSSL_ATTRIBUTE_MAPPING *am,
49                                  BIO *out, int indent)
50 {
51     ASN1_OBJECT *local_type, *remote_type;
52     int local_attr_nid, remote_attr_nid;
53     ASN1_TYPE *local_val, *remote_val;
54 
55     switch (am->type) {
56     case (OSSL_ATTR_MAP_TYPE):
57         if (i2a_ASN1_OBJECT(out, am->choice.typeMappings->local) <= 0)
58             return 0;
59         if (BIO_puts(out, " == ") <= 0)
60             return 0;
61         return i2a_ASN1_OBJECT(out, am->choice.typeMappings->remote);
62     case (OSSL_ATTR_MAP_VALUE):
63         local_type = am->choice.typeValueMappings->local->type;
64         remote_type = am->choice.typeValueMappings->remote->type;
65         local_val = am->choice.typeValueMappings->local->value;
66         remote_val = am->choice.typeValueMappings->remote->value;
67         local_attr_nid = OBJ_obj2nid(local_type);
68         remote_attr_nid = OBJ_obj2nid(remote_type);
69         if (i2a_ASN1_OBJECT(out, local_type) <= 0)
70             return 0;
71         if (BIO_puts(out, ":") <= 0)
72             return 0;
73         if (ossl_print_attribute_value(out, local_attr_nid, local_val, 0) <= 0)
74             return 0;
75         if (BIO_puts(out, " == ") <= 0)
76             return 0;
77         if (i2a_ASN1_OBJECT(out, remote_type) <= 0)
78             return 0;
79         if (BIO_puts(out, ":") <= 0)
80             return 0;
81         return ossl_print_attribute_value(out, remote_attr_nid, remote_val, 0);
82     default:
83         return 0;
84     }
85     return 1;
86 }
87 
i2r_ATTRIBUTE_MAPPINGS(X509V3_EXT_METHOD * method,OSSL_ATTRIBUTE_MAPPINGS * ams,BIO * out,int indent)88 static int i2r_ATTRIBUTE_MAPPINGS(X509V3_EXT_METHOD *method,
89                                   OSSL_ATTRIBUTE_MAPPINGS *ams,
90                                   BIO *out, int indent)
91 {
92     int i;
93     OSSL_ATTRIBUTE_MAPPING *am;
94 
95     for (i = 0; i < sk_OSSL_ATTRIBUTE_MAPPING_num(ams); i++) {
96         am = sk_OSSL_ATTRIBUTE_MAPPING_value(ams, i);
97         if (BIO_printf(out, "%*s", indent, "") <= 0)
98             return 0;
99         if (i2r_ATTRIBUTE_MAPPING(method, am, out, indent + 4) <= 0)
100             return 0;
101         if (BIO_puts(out, "\n") <= 0)
102             return 0;
103     }
104     return 1;
105 }
106 
107 const X509V3_EXT_METHOD ossl_v3_attribute_mappings = {
108     NID_attribute_mappings, X509V3_EXT_MULTILINE,
109     ASN1_ITEM_ref(OSSL_ATTRIBUTE_MAPPINGS),
110     0, 0, 0, 0,
111     0, 0,
112     0,
113     0,
114     (X509V3_EXT_I2R)i2r_ATTRIBUTE_MAPPINGS,
115     0,
116     NULL
117 };
118