1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1999-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_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method,
18e1051a39Sopenharmony_ci                                    X509V3_CTX *ctx,
19e1051a39Sopenharmony_ci                                    STACK_OF(CONF_VALUE) *nval);
20e1051a39Sopenharmony_cistatic STACK_OF(CONF_VALUE) *i2v_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD
21e1051a39Sopenharmony_ci                                                    *method, void *eku, STACK_OF(CONF_VALUE)
22e1051a39Sopenharmony_ci                                                    *extlist);
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_ext_ku = {
25e1051a39Sopenharmony_ci    NID_ext_key_usage, 0,
26e1051a39Sopenharmony_ci    ASN1_ITEM_ref(EXTENDED_KEY_USAGE),
27e1051a39Sopenharmony_ci    0, 0, 0, 0,
28e1051a39Sopenharmony_ci    0, 0,
29e1051a39Sopenharmony_ci    i2v_EXTENDED_KEY_USAGE,
30e1051a39Sopenharmony_ci    v2i_EXTENDED_KEY_USAGE,
31e1051a39Sopenharmony_ci    0, 0,
32e1051a39Sopenharmony_ci    NULL
33e1051a39Sopenharmony_ci};
34e1051a39Sopenharmony_ci
35e1051a39Sopenharmony_ci/* NB OCSP acceptable responses also is a SEQUENCE OF OBJECT */
36e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_ocsp_accresp = {
37e1051a39Sopenharmony_ci    NID_id_pkix_OCSP_acceptableResponses, 0,
38e1051a39Sopenharmony_ci    ASN1_ITEM_ref(EXTENDED_KEY_USAGE),
39e1051a39Sopenharmony_ci    0, 0, 0, 0,
40e1051a39Sopenharmony_ci    0, 0,
41e1051a39Sopenharmony_ci    i2v_EXTENDED_KEY_USAGE,
42e1051a39Sopenharmony_ci    v2i_EXTENDED_KEY_USAGE,
43e1051a39Sopenharmony_ci    0, 0,
44e1051a39Sopenharmony_ci    NULL
45e1051a39Sopenharmony_ci};
46e1051a39Sopenharmony_ci
47e1051a39Sopenharmony_ciASN1_ITEM_TEMPLATE(EXTENDED_KEY_USAGE) =
48e1051a39Sopenharmony_ci        ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, EXTENDED_KEY_USAGE, ASN1_OBJECT)
49e1051a39Sopenharmony_ciASN1_ITEM_TEMPLATE_END(EXTENDED_KEY_USAGE)
50e1051a39Sopenharmony_ci
51e1051a39Sopenharmony_ciIMPLEMENT_ASN1_FUNCTIONS(EXTENDED_KEY_USAGE)
52e1051a39Sopenharmony_ci
53e1051a39Sopenharmony_cistatic STACK_OF(CONF_VALUE) *i2v_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD
54e1051a39Sopenharmony_ci                                                    *method, void *a, STACK_OF(CONF_VALUE)
55e1051a39Sopenharmony_ci                                                    *ext_list)
56e1051a39Sopenharmony_ci{
57e1051a39Sopenharmony_ci    EXTENDED_KEY_USAGE *eku = a;
58e1051a39Sopenharmony_ci    int i;
59e1051a39Sopenharmony_ci    ASN1_OBJECT *obj;
60e1051a39Sopenharmony_ci    char obj_tmp[80];
61e1051a39Sopenharmony_ci    for (i = 0; i < sk_ASN1_OBJECT_num(eku); i++) {
62e1051a39Sopenharmony_ci        obj = sk_ASN1_OBJECT_value(eku, i);
63e1051a39Sopenharmony_ci        i2t_ASN1_OBJECT(obj_tmp, 80, obj);
64e1051a39Sopenharmony_ci        X509V3_add_value(NULL, obj_tmp, &ext_list);
65e1051a39Sopenharmony_ci    }
66e1051a39Sopenharmony_ci    return ext_list;
67e1051a39Sopenharmony_ci}
68e1051a39Sopenharmony_ci
69e1051a39Sopenharmony_cistatic void *v2i_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method,
70e1051a39Sopenharmony_ci                                    X509V3_CTX *ctx,
71e1051a39Sopenharmony_ci                                    STACK_OF(CONF_VALUE) *nval)
72e1051a39Sopenharmony_ci{
73e1051a39Sopenharmony_ci    EXTENDED_KEY_USAGE *extku;
74e1051a39Sopenharmony_ci    char *extval;
75e1051a39Sopenharmony_ci    ASN1_OBJECT *objtmp;
76e1051a39Sopenharmony_ci    CONF_VALUE *val;
77e1051a39Sopenharmony_ci    const int num = sk_CONF_VALUE_num(nval);
78e1051a39Sopenharmony_ci    int i;
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_ci    extku = sk_ASN1_OBJECT_new_reserve(NULL, num);
81e1051a39Sopenharmony_ci    if (extku == NULL) {
82e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
83e1051a39Sopenharmony_ci        sk_ASN1_OBJECT_free(extku);
84e1051a39Sopenharmony_ci        return NULL;
85e1051a39Sopenharmony_ci    }
86e1051a39Sopenharmony_ci
87e1051a39Sopenharmony_ci    for (i = 0; i < num; i++) {
88e1051a39Sopenharmony_ci        val = sk_CONF_VALUE_value(nval, i);
89e1051a39Sopenharmony_ci        if (val->value)
90e1051a39Sopenharmony_ci            extval = val->value;
91e1051a39Sopenharmony_ci        else
92e1051a39Sopenharmony_ci            extval = val->name;
93e1051a39Sopenharmony_ci        if ((objtmp = OBJ_txt2obj(extval, 0)) == NULL) {
94e1051a39Sopenharmony_ci            sk_ASN1_OBJECT_pop_free(extku, ASN1_OBJECT_free);
95e1051a39Sopenharmony_ci            ERR_raise_data(ERR_LIB_X509V3, X509V3_R_INVALID_OBJECT_IDENTIFIER,
96e1051a39Sopenharmony_ci                           "%s", extval);
97e1051a39Sopenharmony_ci            return NULL;
98e1051a39Sopenharmony_ci        }
99e1051a39Sopenharmony_ci        sk_ASN1_OBJECT_push(extku, objtmp);  /* no failure as it was reserved */
100e1051a39Sopenharmony_ci    }
101e1051a39Sopenharmony_ci    return extku;
102e1051a39Sopenharmony_ci}
103