1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2015-2022 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 "e_os.h"
11e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
12e1051a39Sopenharmony_ci#include <stdio.h>
13e1051a39Sopenharmony_ci#include <openssl/asn1t.h>
14e1051a39Sopenharmony_ci#include <openssl/conf.h>
15e1051a39Sopenharmony_ci#include <openssl/x509v3.h>
16e1051a39Sopenharmony_ci#include "ext_dat.h"
17e1051a39Sopenharmony_ci#include "x509_local.h"
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_cistatic STACK_OF(CONF_VALUE) *i2v_TLS_FEATURE(const X509V3_EXT_METHOD *method,
20e1051a39Sopenharmony_ci                                             TLS_FEATURE *tls_feature,
21e1051a39Sopenharmony_ci                                             STACK_OF(CONF_VALUE) *ext_list);
22e1051a39Sopenharmony_cistatic TLS_FEATURE *v2i_TLS_FEATURE(const X509V3_EXT_METHOD *method,
23e1051a39Sopenharmony_ci                                    X509V3_CTX *ctx,
24e1051a39Sopenharmony_ci                                    STACK_OF(CONF_VALUE) *nval);
25e1051a39Sopenharmony_ci
26e1051a39Sopenharmony_ciASN1_ITEM_TEMPLATE(TLS_FEATURE) =
27e1051a39Sopenharmony_ci        ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, TLS_FEATURE, ASN1_INTEGER)
28e1051a39Sopenharmony_cistatic_ASN1_ITEM_TEMPLATE_END(TLS_FEATURE)
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_ciIMPLEMENT_ASN1_ALLOC_FUNCTIONS(TLS_FEATURE)
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_tls_feature = {
33e1051a39Sopenharmony_ci    NID_tlsfeature, 0,
34e1051a39Sopenharmony_ci    ASN1_ITEM_ref(TLS_FEATURE),
35e1051a39Sopenharmony_ci    0, 0, 0, 0,
36e1051a39Sopenharmony_ci    0, 0,
37e1051a39Sopenharmony_ci    (X509V3_EXT_I2V)i2v_TLS_FEATURE,
38e1051a39Sopenharmony_ci    (X509V3_EXT_V2I)v2i_TLS_FEATURE,
39e1051a39Sopenharmony_ci    0, 0,
40e1051a39Sopenharmony_ci    NULL
41e1051a39Sopenharmony_ci};
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ci
44e1051a39Sopenharmony_citypedef struct {
45e1051a39Sopenharmony_ci    long num;
46e1051a39Sopenharmony_ci    const char *name;
47e1051a39Sopenharmony_ci} TLS_FEATURE_NAME;
48e1051a39Sopenharmony_ci
49e1051a39Sopenharmony_cistatic TLS_FEATURE_NAME tls_feature_tbl[] = {
50e1051a39Sopenharmony_ci    { 5, "status_request" },
51e1051a39Sopenharmony_ci    { 17, "status_request_v2" }
52e1051a39Sopenharmony_ci};
53e1051a39Sopenharmony_ci
54e1051a39Sopenharmony_ci/*
55e1051a39Sopenharmony_ci * i2v_TLS_FEATURE converts the TLS_FEATURE structure tls_feature into the
56e1051a39Sopenharmony_ci * STACK_OF(CONF_VALUE) structure ext_list. STACK_OF(CONF_VALUE) is the format
57e1051a39Sopenharmony_ci * used by the CONF library to represent a multi-valued extension.  ext_list is
58e1051a39Sopenharmony_ci * returned.
59e1051a39Sopenharmony_ci */
60e1051a39Sopenharmony_cistatic STACK_OF(CONF_VALUE) *i2v_TLS_FEATURE(const X509V3_EXT_METHOD *method,
61e1051a39Sopenharmony_ci                                             TLS_FEATURE *tls_feature,
62e1051a39Sopenharmony_ci                                             STACK_OF(CONF_VALUE) *ext_list)
63e1051a39Sopenharmony_ci{
64e1051a39Sopenharmony_ci    int i;
65e1051a39Sopenharmony_ci    size_t j;
66e1051a39Sopenharmony_ci    ASN1_INTEGER *ai;
67e1051a39Sopenharmony_ci    long tlsextid;
68e1051a39Sopenharmony_ci    for (i = 0; i < sk_ASN1_INTEGER_num(tls_feature); i++) {
69e1051a39Sopenharmony_ci        ai = sk_ASN1_INTEGER_value(tls_feature, i);
70e1051a39Sopenharmony_ci        tlsextid = ASN1_INTEGER_get(ai);
71e1051a39Sopenharmony_ci        for (j = 0; j < OSSL_NELEM(tls_feature_tbl); j++)
72e1051a39Sopenharmony_ci            if (tlsextid == tls_feature_tbl[j].num)
73e1051a39Sopenharmony_ci                break;
74e1051a39Sopenharmony_ci        if (j < OSSL_NELEM(tls_feature_tbl))
75e1051a39Sopenharmony_ci            X509V3_add_value(NULL, tls_feature_tbl[j].name, &ext_list);
76e1051a39Sopenharmony_ci        else
77e1051a39Sopenharmony_ci            X509V3_add_value_int(NULL, ai, &ext_list);
78e1051a39Sopenharmony_ci    }
79e1051a39Sopenharmony_ci    return ext_list;
80e1051a39Sopenharmony_ci}
81e1051a39Sopenharmony_ci
82e1051a39Sopenharmony_ci/*
83e1051a39Sopenharmony_ci * v2i_TLS_FEATURE converts the multi-valued extension nval into a TLS_FEATURE
84e1051a39Sopenharmony_ci * structure, which is returned if the conversion is successful.  In case of
85e1051a39Sopenharmony_ci * error, NULL is returned.
86e1051a39Sopenharmony_ci */
87e1051a39Sopenharmony_cistatic TLS_FEATURE *v2i_TLS_FEATURE(const X509V3_EXT_METHOD *method,
88e1051a39Sopenharmony_ci                                    X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
89e1051a39Sopenharmony_ci{
90e1051a39Sopenharmony_ci    TLS_FEATURE *tlsf;
91e1051a39Sopenharmony_ci    char *extval, *endptr;
92e1051a39Sopenharmony_ci    ASN1_INTEGER *ai = NULL;
93e1051a39Sopenharmony_ci    CONF_VALUE *val;
94e1051a39Sopenharmony_ci    int i;
95e1051a39Sopenharmony_ci    size_t j;
96e1051a39Sopenharmony_ci    long tlsextid;
97e1051a39Sopenharmony_ci
98e1051a39Sopenharmony_ci    if ((tlsf = sk_ASN1_INTEGER_new_null()) == NULL) {
99e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
100e1051a39Sopenharmony_ci        return NULL;
101e1051a39Sopenharmony_ci    }
102e1051a39Sopenharmony_ci
103e1051a39Sopenharmony_ci    for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
104e1051a39Sopenharmony_ci        val = sk_CONF_VALUE_value(nval, i);
105e1051a39Sopenharmony_ci        if (val->value)
106e1051a39Sopenharmony_ci            extval = val->value;
107e1051a39Sopenharmony_ci        else
108e1051a39Sopenharmony_ci            extval = val->name;
109e1051a39Sopenharmony_ci
110e1051a39Sopenharmony_ci        for (j = 0; j < OSSL_NELEM(tls_feature_tbl); j++)
111e1051a39Sopenharmony_ci            if (OPENSSL_strcasecmp(extval, tls_feature_tbl[j].name) == 0)
112e1051a39Sopenharmony_ci                break;
113e1051a39Sopenharmony_ci        if (j < OSSL_NELEM(tls_feature_tbl))
114e1051a39Sopenharmony_ci            tlsextid = tls_feature_tbl[j].num;
115e1051a39Sopenharmony_ci        else {
116e1051a39Sopenharmony_ci            tlsextid = strtol(extval, &endptr, 10);
117e1051a39Sopenharmony_ci            if (((*endptr) != '\0') || (extval == endptr) || (tlsextid < 0) ||
118e1051a39Sopenharmony_ci                (tlsextid > 65535)) {
119e1051a39Sopenharmony_ci                ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_SYNTAX);
120e1051a39Sopenharmony_ci                X509V3_conf_add_error_name_value(val);
121e1051a39Sopenharmony_ci                goto err;
122e1051a39Sopenharmony_ci            }
123e1051a39Sopenharmony_ci        }
124e1051a39Sopenharmony_ci
125e1051a39Sopenharmony_ci        if ((ai = ASN1_INTEGER_new()) == NULL
126e1051a39Sopenharmony_ci                || !ASN1_INTEGER_set(ai, tlsextid)
127e1051a39Sopenharmony_ci                || sk_ASN1_INTEGER_push(tlsf, ai) <= 0) {
128e1051a39Sopenharmony_ci            ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
129e1051a39Sopenharmony_ci            goto err;
130e1051a39Sopenharmony_ci        }
131e1051a39Sopenharmony_ci        /* So it doesn't get purged if an error occurs next time around */
132e1051a39Sopenharmony_ci        ai = NULL;
133e1051a39Sopenharmony_ci    }
134e1051a39Sopenharmony_ci    return tlsf;
135e1051a39Sopenharmony_ci
136e1051a39Sopenharmony_ci err:
137e1051a39Sopenharmony_ci    sk_ASN1_INTEGER_pop_free(tlsf, ASN1_INTEGER_free);
138e1051a39Sopenharmony_ci    ASN1_INTEGER_free(ai);
139e1051a39Sopenharmony_ci    return NULL;
140e1051a39Sopenharmony_ci}
141