xref: /openssl/crypto/x509/v3_extku.c (revision 7ed6de99)
1 /*
2  * Copyright 1999-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 <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_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method,
18                                     X509V3_CTX *ctx,
19                                     STACK_OF(CONF_VALUE) *nval);
20 static STACK_OF(CONF_VALUE) *i2v_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD
21                                                     *method, void *eku, STACK_OF(CONF_VALUE)
22                                                     *extlist);
23 
24 const X509V3_EXT_METHOD ossl_v3_ext_ku = {
25     NID_ext_key_usage, 0,
26     ASN1_ITEM_ref(EXTENDED_KEY_USAGE),
27     0, 0, 0, 0,
28     0, 0,
29     i2v_EXTENDED_KEY_USAGE,
30     v2i_EXTENDED_KEY_USAGE,
31     0, 0,
32     NULL
33 };
34 
35 /* NB OCSP acceptable responses also is a SEQUENCE OF OBJECT */
36 const X509V3_EXT_METHOD ossl_v3_ocsp_accresp = {
37     NID_id_pkix_OCSP_acceptableResponses, 0,
38     ASN1_ITEM_ref(EXTENDED_KEY_USAGE),
39     0, 0, 0, 0,
40     0, 0,
41     i2v_EXTENDED_KEY_USAGE,
42     v2i_EXTENDED_KEY_USAGE,
43     0, 0,
44     NULL
45 };
46 
47 /* Acceptable Certificate Policies also is a SEQUENCE OF OBJECT */
48 const X509V3_EXT_METHOD ossl_v3_acc_cert_policies = {
49     NID_acceptable_cert_policies, 0,
50     ASN1_ITEM_ref(EXTENDED_KEY_USAGE),
51     0, 0, 0, 0,
52     0, 0,
53     i2v_EXTENDED_KEY_USAGE,
54     v2i_EXTENDED_KEY_USAGE,
55     0, 0,
56     NULL
57 };
58 
59 /* Acceptable Privilege Policies also is a SEQUENCE OF OBJECT */
60 const X509V3_EXT_METHOD ossl_v3_acc_priv_policies = {
61     NID_acceptable_privilege_policies, 0,
62     ASN1_ITEM_ref(EXTENDED_KEY_USAGE),
63     0, 0, 0, 0,
64     0, 0,
65     i2v_EXTENDED_KEY_USAGE,
66     v2i_EXTENDED_KEY_USAGE,
67     0, 0,
68     NULL
69 };
70 
ASN1_ITEM_TEMPLATE(EXTENDED_KEY_USAGE)71 ASN1_ITEM_TEMPLATE(EXTENDED_KEY_USAGE) =
72         ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, EXTENDED_KEY_USAGE, ASN1_OBJECT)
73 ASN1_ITEM_TEMPLATE_END(EXTENDED_KEY_USAGE)
74 
75 IMPLEMENT_ASN1_FUNCTIONS(EXTENDED_KEY_USAGE)
76 
77 static STACK_OF(CONF_VALUE) *i2v_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD
78                                                     *method, void *a, STACK_OF(CONF_VALUE)
79                                                     *ext_list)
80 {
81     EXTENDED_KEY_USAGE *eku = a;
82     int i;
83     ASN1_OBJECT *obj;
84     char obj_tmp[80];
85     for (i = 0; i < sk_ASN1_OBJECT_num(eku); i++) {
86         obj = sk_ASN1_OBJECT_value(eku, i);
87         i2t_ASN1_OBJECT(obj_tmp, 80, obj);
88         X509V3_add_value(NULL, obj_tmp, &ext_list);
89     }
90     return ext_list;
91 }
92 
v2i_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD * method,X509V3_CTX * ctx,STACK_OF (CONF_VALUE)* nval)93 static void *v2i_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method,
94                                     X509V3_CTX *ctx,
95                                     STACK_OF(CONF_VALUE) *nval)
96 {
97     EXTENDED_KEY_USAGE *extku;
98     char *extval;
99     ASN1_OBJECT *objtmp;
100     CONF_VALUE *val;
101     const int num = sk_CONF_VALUE_num(nval);
102     int i;
103 
104     extku = sk_ASN1_OBJECT_new_reserve(NULL, num);
105     if (extku == NULL) {
106         ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
107         sk_ASN1_OBJECT_free(extku);
108         return NULL;
109     }
110 
111     for (i = 0; i < num; i++) {
112         val = sk_CONF_VALUE_value(nval, i);
113         if (val->value)
114             extval = val->value;
115         else
116             extval = val->name;
117         if ((objtmp = OBJ_txt2obj(extval, 0)) == NULL) {
118             sk_ASN1_OBJECT_pop_free(extku, ASN1_OBJECT_free);
119             ERR_raise_data(ERR_LIB_X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER,
120                            "%s", extval);
121             return NULL;
122         }
123         sk_ASN1_OBJECT_push(extku, objtmp);  /* no failure as it was reserved */
124     }
125     return extku;
126 }
127