GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/x509v3/v3_sxnet.c Lines: 0 80 0.0 %
Date: 2017-11-07 Branches: 0 44 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: v3_sxnet.c,v 1.19 2017/01/29 17:49:23 beck Exp $ */
2
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3
 * project 1999.
4
 */
5
/* ====================================================================
6
 * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 *
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in
17
 *    the documentation and/or other materials provided with the
18
 *    distribution.
19
 *
20
 * 3. All advertising materials mentioning features or use of this
21
 *    software must display the following acknowledgment:
22
 *    "This product includes software developed by the OpenSSL Project
23
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24
 *
25
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26
 *    endorse or promote products derived from this software without
27
 *    prior written permission. For written permission, please contact
28
 *    licensing@OpenSSL.org.
29
 *
30
 * 5. Products derived from this software may not be called "OpenSSL"
31
 *    nor may "OpenSSL" appear in their names without prior written
32
 *    permission of the OpenSSL Project.
33
 *
34
 * 6. Redistributions of any form whatsoever must retain the following
35
 *    acknowledgment:
36
 *    "This product includes software developed by the OpenSSL Project
37
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38
 *
39
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
51
 * ====================================================================
52
 *
53
 * This product includes cryptographic software written by Eric Young
54
 * (eay@cryptsoft.com).  This product includes software written by Tim
55
 * Hudson (tjh@cryptsoft.com).
56
 *
57
 */
58
59
#include <stdio.h>
60
#include <string.h>
61
62
#include <openssl/asn1.h>
63
#include <openssl/asn1t.h>
64
#include <openssl/conf.h>
65
#include <openssl/err.h>
66
#include <openssl/x509v3.h>
67
68
/* Support for Thawte strong extranet extension */
69
70
#define SXNET_TEST
71
72
static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,
73
    int indent);
74
#ifdef SXNET_TEST
75
static SXNET * sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
76
    STACK_OF(CONF_VALUE) *nval);
77
#endif
78
79
const X509V3_EXT_METHOD v3_sxnet = {
80
	.ext_nid = NID_sxnet,
81
	.ext_flags = X509V3_EXT_MULTILINE,
82
	.it = &SXNET_it,
83
	.ext_new = NULL,
84
	.ext_free = NULL,
85
	.d2i = NULL,
86
	.i2d = NULL,
87
	.i2s = NULL,
88
	.s2i = NULL,
89
	.i2v = NULL,
90
#ifdef SXNET_TEST
91
	.v2i = (X509V3_EXT_V2I)sxnet_v2i,
92
#else
93
	.v2i = NULL,
94
#endif
95
	.i2r = (X509V3_EXT_I2R)sxnet_i2r,
96
	.r2i = NULL,
97
	.usr_data = NULL,
98
};
99
100
static const ASN1_TEMPLATE SXNETID_seq_tt[] = {
101
	{
102
		.flags = 0,
103
		.tag = 0,
104
		.offset = offsetof(SXNETID, zone),
105
		.field_name = "zone",
106
		.item = &ASN1_INTEGER_it,
107
	},
108
	{
109
		.flags = 0,
110
		.tag = 0,
111
		.offset = offsetof(SXNETID, user),
112
		.field_name = "user",
113
		.item = &ASN1_OCTET_STRING_it,
114
	},
115
};
116
117
const ASN1_ITEM SXNETID_it = {
118
	.itype = ASN1_ITYPE_SEQUENCE,
119
	.utype = V_ASN1_SEQUENCE,
120
	.templates = SXNETID_seq_tt,
121
	.tcount = sizeof(SXNETID_seq_tt) / sizeof(ASN1_TEMPLATE),
122
	.funcs = NULL,
123
	.size = sizeof(SXNETID),
124
	.sname = "SXNETID",
125
};
126
127
128
SXNETID *
129
d2i_SXNETID(SXNETID **a, const unsigned char **in, long len)
130
{
131
	return (SXNETID *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
132
	    &SXNETID_it);
133
}
134
135
int
136
i2d_SXNETID(SXNETID *a, unsigned char **out)
137
{
138
	return ASN1_item_i2d((ASN1_VALUE *)a, out, &SXNETID_it);
139
}
140
141
SXNETID *
142
SXNETID_new(void)
143
{
144
	return (SXNETID *)ASN1_item_new(&SXNETID_it);
145
}
146
147
void
148
SXNETID_free(SXNETID *a)
149
{
150
	ASN1_item_free((ASN1_VALUE *)a, &SXNETID_it);
151
}
152
153
static const ASN1_TEMPLATE SXNET_seq_tt[] = {
154
	{
155
		.flags = 0,
156
		.tag = 0,
157
		.offset = offsetof(SXNET, version),
158
		.field_name = "version",
159
		.item = &ASN1_INTEGER_it,
160
	},
161
	{
162
		.flags = ASN1_TFLG_SEQUENCE_OF,
163
		.tag = 0,
164
		.offset = offsetof(SXNET, ids),
165
		.field_name = "ids",
166
		.item = &SXNETID_it,
167
	},
168
};
169
170
const ASN1_ITEM SXNET_it = {
171
	.itype = ASN1_ITYPE_SEQUENCE,
172
	.utype = V_ASN1_SEQUENCE,
173
	.templates = SXNET_seq_tt,
174
	.tcount = sizeof(SXNET_seq_tt) / sizeof(ASN1_TEMPLATE),
175
	.funcs = NULL,
176
	.size = sizeof(SXNET),
177
	.sname = "SXNET",
178
};
179
180
181
SXNET *
182
d2i_SXNET(SXNET **a, const unsigned char **in, long len)
183
{
184
	return (SXNET *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
185
	    &SXNET_it);
186
}
187
188
int
189
i2d_SXNET(SXNET *a, unsigned char **out)
190
{
191
	return ASN1_item_i2d((ASN1_VALUE *)a, out, &SXNET_it);
192
}
193
194
SXNET *
195
SXNET_new(void)
196
{
197
	return (SXNET *)ASN1_item_new(&SXNET_it);
198
}
199
200
void
201
SXNET_free(SXNET *a)
202
{
203
	ASN1_item_free((ASN1_VALUE *)a, &SXNET_it);
204
}
205
206
static int
207
sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out, int indent)
208
{
209
	long v;
210
	char *tmp;
211
	SXNETID *id;
212
	int i;
213
214
	v = ASN1_INTEGER_get(sx->version);
215
	BIO_printf(out, "%*sVersion: %ld (0x%lX)", indent, "", v + 1, v);
216
	for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
217
		id = sk_SXNETID_value(sx->ids, i);
218
		tmp = i2s_ASN1_INTEGER(NULL, id->zone);
219
		BIO_printf(out, "\n%*sZone: %s, User: ", indent, "", tmp);
220
		free(tmp);
221
		ASN1_STRING_print(out, id->user);
222
	}
223
	return 1;
224
}
225
226
#ifdef SXNET_TEST
227
228
/* NBB: this is used for testing only. It should *not* be used for anything
229
 * else because it will just take static IDs from the configuration file and
230
 * they should really be separate values for each user.
231
 */
232
233
static SXNET *
234
sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
235
    STACK_OF(CONF_VALUE) *nval)
236
{
237
	CONF_VALUE *cnf;
238
	SXNET *sx = NULL;
239
	int i;
240
241
	for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
242
		cnf = sk_CONF_VALUE_value(nval, i);
243
		if (!SXNET_add_id_asc(&sx, cnf->name, cnf->value, -1))
244
			return NULL;
245
	}
246
	return sx;
247
}
248
249
#endif
250
251
/* Strong Extranet utility functions */
252
253
/* Add an id given the zone as an ASCII number */
254
255
int
256
SXNET_add_id_asc(SXNET **psx, char *zone, char *user, int userlen)
257
{
258
	ASN1_INTEGER *izone = NULL;
259
260
	if (!(izone = s2i_ASN1_INTEGER(NULL, zone))) {
261
		X509V3error(X509V3_R_ERROR_CONVERTING_ZONE);
262
		return 0;
263
	}
264
	return SXNET_add_id_INTEGER(psx, izone, user, userlen);
265
}
266
267
/* Add an id given the zone as an unsigned long */
268
269
int
270
SXNET_add_id_ulong(SXNET **psx, unsigned long lzone, char *user, int userlen)
271
{
272
	ASN1_INTEGER *izone = NULL;
273
274
	if (!(izone = ASN1_INTEGER_new()) ||
275
	    !ASN1_INTEGER_set(izone, lzone)) {
276
		X509V3error(ERR_R_MALLOC_FAILURE);
277
		ASN1_INTEGER_free(izone);
278
		return 0;
279
	}
280
	return SXNET_add_id_INTEGER(psx, izone, user, userlen);
281
}
282
283
/* Add an id given the zone as an ASN1_INTEGER.
284
 * Note this version uses the passed integer and doesn't make a copy so don't
285
 * free it up afterwards.
286
 */
287
288
int
289
SXNET_add_id_INTEGER(SXNET **psx, ASN1_INTEGER *zone, char *user, int userlen)
290
{
291
	SXNET *sx = NULL;
292
	SXNETID *id = NULL;
293
294
	if (!psx || !zone || !user) {
295
		X509V3error(X509V3_R_INVALID_NULL_ARGUMENT);
296
		return 0;
297
	}
298
	if (userlen == -1)
299
		userlen = strlen(user);
300
	if (userlen > 64) {
301
		X509V3error(X509V3_R_USER_TOO_LONG);
302
		return 0;
303
	}
304
	if (!*psx) {
305
		if (!(sx = SXNET_new()))
306
			goto err;
307
		if (!ASN1_INTEGER_set(sx->version, 0))
308
			goto err;
309
		*psx = sx;
310
	} else
311
		sx = *psx;
312
	if (SXNET_get_id_INTEGER(sx, zone)) {
313
		X509V3error(X509V3_R_DUPLICATE_ZONE_ID);
314
		return 0;
315
	}
316
317
	if (!(id = SXNETID_new()))
318
		goto err;
319
	if (userlen == -1)
320
		userlen = strlen(user);
321
322
	if (!ASN1_STRING_set(id->user, user, userlen))
323
		goto err;
324
	if (!sk_SXNETID_push(sx->ids, id))
325
		goto err;
326
	id->zone = zone;
327
	return 1;
328
329
err:
330
	X509V3error(ERR_R_MALLOC_FAILURE);
331
	SXNETID_free(id);
332
	SXNET_free(sx);
333
	*psx = NULL;
334
	return 0;
335
}
336
337
ASN1_OCTET_STRING *
338
SXNET_get_id_asc(SXNET *sx, char *zone)
339
{
340
	ASN1_INTEGER *izone = NULL;
341
	ASN1_OCTET_STRING *oct;
342
343
	if (!(izone = s2i_ASN1_INTEGER(NULL, zone))) {
344
		X509V3error(X509V3_R_ERROR_CONVERTING_ZONE);
345
		return NULL;
346
	}
347
	oct = SXNET_get_id_INTEGER(sx, izone);
348
	ASN1_INTEGER_free(izone);
349
	return oct;
350
}
351
352
ASN1_OCTET_STRING *
353
SXNET_get_id_ulong(SXNET *sx, unsigned long lzone)
354
{
355
	ASN1_INTEGER *izone = NULL;
356
	ASN1_OCTET_STRING *oct;
357
358
	if (!(izone = ASN1_INTEGER_new()) ||
359
	    !ASN1_INTEGER_set(izone, lzone)) {
360
		X509V3error(ERR_R_MALLOC_FAILURE);
361
		ASN1_INTEGER_free(izone);
362
		return NULL;
363
	}
364
	oct = SXNET_get_id_INTEGER(sx, izone);
365
	ASN1_INTEGER_free(izone);
366
	return oct;
367
}
368
369
ASN1_OCTET_STRING *
370
SXNET_get_id_INTEGER(SXNET *sx, ASN1_INTEGER *zone)
371
{
372
	SXNETID *id;
373
	int i;
374
375
	for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
376
		id = sk_SXNETID_value(sx->ids, i);
377
		if (!ASN1_STRING_cmp(id->zone, zone))
378
			return id->user;
379
	}
380
	return NULL;
381
}