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