GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/crypto/../../libssl/src/crypto/x509v3/v3_sxnet.c Lines: 0 90 0.0 %
Date: 2016-12-06 Branches: 0 46 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: v3_sxnet.c,v 1.17 2015/09/30 17:30:16 jsing 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 = ASN1_ITEM_ref(SXNET),
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
		X509V3err(X509V3_F_SXNET_ADD_ID_ASC,
262
		    X509V3_R_ERROR_CONVERTING_ZONE);
263
		return 0;
264
	}
265
	return SXNET_add_id_INTEGER(psx, izone, user, userlen);
266
}
267
268
/* Add an id given the zone as an unsigned long */
269
270
int
271
SXNET_add_id_ulong(SXNET **psx, unsigned long lzone, char *user, int userlen)
272
{
273
	ASN1_INTEGER *izone = NULL;
274
275
	if (!(izone = ASN1_INTEGER_new()) ||
276
	    !ASN1_INTEGER_set(izone, lzone)) {
277
		X509V3err(X509V3_F_SXNET_ADD_ID_ULONG, ERR_R_MALLOC_FAILURE);
278
		ASN1_INTEGER_free(izone);
279
		return 0;
280
	}
281
	return SXNET_add_id_INTEGER(psx, izone, user, userlen);
282
}
283
284
/* Add an id given the zone as an ASN1_INTEGER.
285
 * Note this version uses the passed integer and doesn't make a copy so don't
286
 * free it up afterwards.
287
 */
288
289
int
290
SXNET_add_id_INTEGER(SXNET **psx, ASN1_INTEGER *zone, char *user, int userlen)
291
{
292
	SXNET *sx = NULL;
293
	SXNETID *id = NULL;
294
295
	if (!psx || !zone || !user) {
296
		X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER,
297
		    X509V3_R_INVALID_NULL_ARGUMENT);
298
		return 0;
299
	}
300
	if (userlen == -1)
301
		userlen = strlen(user);
302
	if (userlen > 64) {
303
		X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER,
304
		    X509V3_R_USER_TOO_LONG);
305
		return 0;
306
	}
307
	if (!*psx) {
308
		if (!(sx = SXNET_new()))
309
			goto err;
310
		if (!ASN1_INTEGER_set(sx->version, 0))
311
			goto err;
312
		*psx = sx;
313
	} else
314
		sx = *psx;
315
	if (SXNET_get_id_INTEGER(sx, zone)) {
316
		X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER,
317
		    X509V3_R_DUPLICATE_ZONE_ID);
318
		return 0;
319
	}
320
321
	if (!(id = SXNETID_new()))
322
		goto err;
323
	if (userlen == -1)
324
		userlen = strlen(user);
325
326
	if (!ASN1_STRING_set(id->user, user, userlen))
327
		goto err;
328
	if (!sk_SXNETID_push(sx->ids, id))
329
		goto err;
330
	id->zone = zone;
331
	return 1;
332
333
err:
334
	X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER, ERR_R_MALLOC_FAILURE);
335
	SXNETID_free(id);
336
	SXNET_free(sx);
337
	*psx = NULL;
338
	return 0;
339
}
340
341
ASN1_OCTET_STRING *
342
SXNET_get_id_asc(SXNET *sx, char *zone)
343
{
344
	ASN1_INTEGER *izone = NULL;
345
	ASN1_OCTET_STRING *oct;
346
347
	if (!(izone = s2i_ASN1_INTEGER(NULL, zone))) {
348
		X509V3err(X509V3_F_SXNET_GET_ID_ASC,
349
		    X509V3_R_ERROR_CONVERTING_ZONE);
350
		return NULL;
351
	}
352
	oct = SXNET_get_id_INTEGER(sx, izone);
353
	ASN1_INTEGER_free(izone);
354
	return oct;
355
}
356
357
ASN1_OCTET_STRING *
358
SXNET_get_id_ulong(SXNET *sx, unsigned long lzone)
359
{
360
	ASN1_INTEGER *izone = NULL;
361
	ASN1_OCTET_STRING *oct;
362
363
	if (!(izone = ASN1_INTEGER_new()) ||
364
	    !ASN1_INTEGER_set(izone, lzone)) {
365
		X509V3err(X509V3_F_SXNET_GET_ID_ULONG, ERR_R_MALLOC_FAILURE);
366
		ASN1_INTEGER_free(izone);
367
		return NULL;
368
	}
369
	oct = SXNET_get_id_INTEGER(sx, izone);
370
	ASN1_INTEGER_free(izone);
371
	return oct;
372
}
373
374
ASN1_OCTET_STRING *
375
SXNET_get_id_INTEGER(SXNET *sx, ASN1_INTEGER *zone)
376
{
377
	SXNETID *id;
378
	int i;
379
380
	for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
381
		id = sk_SXNETID_value(sx->ids, i);
382
		if (!ASN1_STRING_cmp(id->zone, zone))
383
			return id->user;
384
	}
385
	return NULL;
386
}