]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/v3_sxnet.c
threads_pthread.c: change inline to ossl_inline
[thirdparty/openssl.git] / crypto / x509 / v3_sxnet.c
CommitLineData
0f113f3e 1/*
b6461792 2 * Copyright 1999-2024 The OpenSSL Project Authors. All Rights Reserved.
2f0eae31 3 *
4286ca47 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
d2e9e320
RS
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
2f0eae31
DSH
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822
BM
12#include <openssl/conf.h>
13#include <openssl/asn1.h>
9d6b1ce6 14#include <openssl/asn1t.h>
ec577822 15#include <openssl/x509v3.h>
df2ee0e2 16#include "ext_dat.h"
2f0eae31
DSH
17
18/* Support for Thawte strong extranet extension */
19
28a98809
DSH
20#define SXNET_TEST
21
0f113f3e
MC
22static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,
23 int indent);
28a98809 24#ifdef SXNET_TEST
0f113f3e
MC
25static SXNET *sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
26 STACK_OF(CONF_VALUE) *nval);
28a98809 27#endif
47864aea 28const X509V3_EXT_METHOD ossl_v3_sxnet = {
0f113f3e
MC
29 NID_sxnet, X509V3_EXT_MULTILINE, ASN1_ITEM_ref(SXNET),
30 0, 0, 0, 0,
31 0, 0,
32 0,
28a98809 33#ifdef SXNET_TEST
0f113f3e 34 (X509V3_EXT_V2I)sxnet_v2i,
28a98809 35#else
0f113f3e 36 0,
28a98809 37#endif
0f113f3e
MC
38 (X509V3_EXT_I2R)sxnet_i2r,
39 0,
40 NULL
2f0eae31
DSH
41};
42
9d6b1ce6 43ASN1_SEQUENCE(SXNETID) = {
0f113f3e
MC
44 ASN1_SIMPLE(SXNETID, zone, ASN1_INTEGER),
45 ASN1_SIMPLE(SXNETID, user, ASN1_OCTET_STRING)
d339187b 46} ASN1_SEQUENCE_END(SXNETID)
2f0eae31 47
9d6b1ce6 48IMPLEMENT_ASN1_FUNCTIONS(SXNETID)
2f0eae31 49
9d6b1ce6 50ASN1_SEQUENCE(SXNET) = {
0f113f3e
MC
51 ASN1_SIMPLE(SXNET, version, ASN1_INTEGER),
52 ASN1_SEQUENCE_OF(SXNET, ids, SXNETID)
d339187b 53} ASN1_SEQUENCE_END(SXNET)
2f0eae31 54
9d6b1ce6 55IMPLEMENT_ASN1_FUNCTIONS(SXNET)
2f0eae31 56
6b691a5c 57static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,
0f113f3e 58 int indent)
2f0eae31 59{
61b0fead 60 int64_t v;
0f113f3e
MC
61 char *tmp;
62 SXNETID *id;
63 int i;
61b0fead
MC
64
65 /*
66 * Since we add 1 to the version number to display it, we don't support
67 * LONG_MAX since that would cause on overflow.
68 */
69 if (!ASN1_INTEGER_get_int64(&v, sx->version)
70 || v >= LONG_MAX
71 || v < LONG_MIN) {
72 BIO_printf(out, "%*sVersion: <unsupported>", indent, "");
73 } else {
74 long vl = (long)v;
75
76 BIO_printf(out, "%*sVersion: %ld (0x%lX)", indent, "", vl + 1, vl);
77 }
0f113f3e
MC
78 for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
79 id = sk_SXNETID_value(sx->ids, i);
80 tmp = i2s_ASN1_INTEGER(NULL, id->zone);
9ef1f848 81 if (tmp == NULL)
82 return 0;
0f113f3e
MC
83 BIO_printf(out, "\n%*sZone: %s, User: ", indent, "", tmp);
84 OPENSSL_free(tmp);
f422a514 85 ASN1_STRING_print(out, id->user);
0f113f3e
MC
86 }
87 return 1;
2f0eae31 88}
28a98809
DSH
89
90#ifdef SXNET_TEST
91
0f113f3e
MC
92/*
93 * NBB: this is used for testing only. It should *not* be used for anything
28a98809
DSH
94 * else because it will just take static IDs from the configuration file and
95 * they should really be separate values for each user.
96 */
97
0f113f3e
MC
98static SXNET *sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
99 STACK_OF(CONF_VALUE) *nval)
28a98809 100{
0f113f3e
MC
101 CONF_VALUE *cnf;
102 SXNET *sx = NULL;
103 int i;
104 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
105 cnf = sk_CONF_VALUE_value(nval, i);
0151e772
BE
106 if (!SXNET_add_id_asc(&sx, cnf->name, cnf->value, -1)) {
107 SXNET_free(sx);
0f113f3e 108 return NULL;
0151e772 109 }
0f113f3e
MC
110 }
111 return sx;
28a98809 112}
0f113f3e 113
28a98809
DSH
114#endif
115
116/* Strong Extranet utility functions */
117
118/* Add an id given the zone as an ASCII number */
119
0aa25a68 120int SXNET_add_id_asc(SXNET **psx, const char *zone, const char *user, int userlen)
28a98809 121{
75ebbd9a
RS
122 ASN1_INTEGER *izone;
123
124 if ((izone = s2i_ASN1_INTEGER(NULL, zone)) == NULL) {
9311d0c4 125 ERR_raise(ERR_LIB_X509V3, X509V3_R_ERROR_CONVERTING_ZONE);
0f113f3e
MC
126 return 0;
127 }
7054fc1c
TM
128 if (!SXNET_add_id_INTEGER(psx, izone, user, userlen)) {
129 ASN1_INTEGER_free(izone);
130 return 0;
131 }
132 return 1;
28a98809
DSH
133}
134
135/* Add an id given the zone as an unsigned long */
136
0aa25a68 137int SXNET_add_id_ulong(SXNET **psx, unsigned long lzone, const char *user,
0f113f3e 138 int userlen)
28a98809 139{
75ebbd9a
RS
140 ASN1_INTEGER *izone;
141
142 if ((izone = ASN1_INTEGER_new()) == NULL
143 || !ASN1_INTEGER_set(izone, lzone)) {
e077455e 144 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
f422a514 145 ASN1_INTEGER_free(izone);
0f113f3e
MC
146 return 0;
147 }
7054fc1c
TM
148 if (!SXNET_add_id_INTEGER(psx, izone, user, userlen)) {
149 ASN1_INTEGER_free(izone);
150 return 0;
151 }
152 return 1;
28a98809
DSH
153}
154
0f113f3e
MC
155/*
156 * Add an id given the zone as an ASN1_INTEGER. Note this version uses the
157 * passed integer and doesn't make a copy so don't free it up afterwards.
28a98809
DSH
158 */
159
0aa25a68 160int SXNET_add_id_INTEGER(SXNET **psx, ASN1_INTEGER *zone, const char *user,
0f113f3e 161 int userlen)
28a98809 162{
0f113f3e
MC
163 SXNET *sx = NULL;
164 SXNETID *id = NULL;
12a765a5
RS
165
166 if (psx == NULL || zone == NULL || user == NULL) {
9311d0c4 167 ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NULL_ARGUMENT);
0f113f3e
MC
168 return 0;
169 }
170 if (userlen == -1)
171 userlen = strlen(user);
172 if (userlen > 64) {
9311d0c4 173 ERR_raise(ERR_LIB_X509V3, X509V3_R_USER_TOO_LONG);
0f113f3e
MC
174 return 0;
175 }
75ebbd9a 176 if (*psx == NULL) {
e077455e
RL
177 if ((sx = SXNET_new()) == NULL) {
178 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
0f113f3e 179 goto err;
e077455e
RL
180 }
181 if (!ASN1_INTEGER_set(sx->version, 0)) {
182 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
0f113f3e 183 goto err;
e077455e 184 }
0f113f3e
MC
185 } else
186 sx = *psx;
187 if (SXNET_get_id_INTEGER(sx, zone)) {
9311d0c4 188 ERR_raise(ERR_LIB_X509V3, X509V3_R_DUPLICATE_ZONE_ID);
10481d33
PH
189 if (*psx == NULL)
190 SXNET_free(sx);
0f113f3e
MC
191 return 0;
192 }
28a98809 193
e077455e
RL
194 if ((id = SXNETID_new()) == NULL) {
195 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
0f113f3e 196 goto err;
e077455e 197 }
0f113f3e 198
e077455e
RL
199 if (!ASN1_OCTET_STRING_set(id->user, (const unsigned char *)user, userlen)){
200 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
0f113f3e 201 goto err;
e077455e
RL
202 }
203 if (!sk_SXNETID_push(sx->ids, id)) {
204 ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);
0f113f3e 205 goto err;
e077455e 206 }
39801184 207 ASN1_INTEGER_free(id->zone);
0f113f3e 208 id->zone = zone;
10481d33 209 *psx = sx;
0f113f3e
MC
210 return 1;
211
212 err:
0f113f3e 213 SXNETID_free(id);
10481d33
PH
214 if (*psx == NULL)
215 SXNET_free(sx);
0f113f3e 216 return 0;
28a98809
DSH
217}
218
0aa25a68 219ASN1_OCTET_STRING *SXNET_get_id_asc(SXNET *sx, const char *zone)
28a98809 220{
75ebbd9a 221 ASN1_INTEGER *izone;
0f113f3e 222 ASN1_OCTET_STRING *oct;
75ebbd9a
RS
223
224 if ((izone = s2i_ASN1_INTEGER(NULL, zone)) == NULL) {
9311d0c4 225 ERR_raise(ERR_LIB_X509V3, X509V3_R_ERROR_CONVERTING_ZONE);
0f113f3e
MC
226 return NULL;
227 }
228 oct = SXNET_get_id_INTEGER(sx, izone);
f422a514 229 ASN1_INTEGER_free(izone);
0f113f3e 230 return oct;
28a98809
DSH
231}
232
6b691a5c 233ASN1_OCTET_STRING *SXNET_get_id_ulong(SXNET *sx, unsigned long lzone)
28a98809 234{
75ebbd9a 235 ASN1_INTEGER *izone;
0f113f3e 236 ASN1_OCTET_STRING *oct;
75ebbd9a
RS
237
238 if ((izone = ASN1_INTEGER_new()) == NULL
239 || !ASN1_INTEGER_set(izone, lzone)) {
e077455e 240 ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);
f422a514 241 ASN1_INTEGER_free(izone);
0f113f3e
MC
242 return NULL;
243 }
244 oct = SXNET_get_id_INTEGER(sx, izone);
f422a514 245 ASN1_INTEGER_free(izone);
0f113f3e 246 return oct;
28a98809
DSH
247}
248
6b691a5c 249ASN1_OCTET_STRING *SXNET_get_id_INTEGER(SXNET *sx, ASN1_INTEGER *zone)
28a98809 250{
0f113f3e
MC
251 SXNETID *id;
252 int i;
253 for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
254 id = sk_SXNETID_value(sx->ids, i);
f422a514 255 if (!ASN1_INTEGER_cmp(id->zone, zone))
0f113f3e
MC
256 return id->user;
257 }
258 return NULL;
28a98809 259}