v3_sxnet.c 7.6 KB
Newer Older
D
Dr. Stephen Henson 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/* v3_sxnet.c */
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
 * project 1999.
 */
/* ====================================================================
 * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    licensing@OpenSSL.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */

#include <stdio.h>
#include "cryptlib.h"
61 62
#include <openssl/conf.h>
#include <openssl/asn1.h>
D
 
Dr. Stephen Henson 已提交
63
#include <openssl/asn1t.h>
64
#include <openssl/x509v3.h>
D
Dr. Stephen Henson 已提交
65 66 67

/* Support for Thawte strong extranet extension */

68 69
#define SXNET_TEST

D
Dr. Stephen Henson 已提交
70
static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out, int indent);
71
#ifdef SXNET_TEST
72 73
static SXNET * sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
						STACK_OF(CONF_VALUE) *nval);
74
#endif
D
Dr. Stephen Henson 已提交
75
X509V3_EXT_METHOD v3_sxnet = {
76
NID_sxnet, X509V3_EXT_MULTILINE, ASN1_ITEM_ref(SXNET),
D
 
Dr. Stephen Henson 已提交
77 78 79
0,0,0,0,
0,0,
0, 
80 81 82
#ifdef SXNET_TEST
(X509V3_EXT_V2I)sxnet_v2i,
#else
D
 
Dr. Stephen Henson 已提交
83
0,
84
#endif
D
Dr. Stephen Henson 已提交
85
(X509V3_EXT_I2R)sxnet_i2r,
D
 
Dr. Stephen Henson 已提交
86
0,
D
Dr. Stephen Henson 已提交
87 88 89
NULL
};

D
 
Dr. Stephen Henson 已提交
90 91 92
ASN1_SEQUENCE(SXNETID) = {
	ASN1_SIMPLE(SXNETID, zone, ASN1_INTEGER),
	ASN1_SIMPLE(SXNETID, user, ASN1_OCTET_STRING)
93
} ASN1_SEQUENCE_END(SXNETID)
D
Dr. Stephen Henson 已提交
94

D
 
Dr. Stephen Henson 已提交
95
IMPLEMENT_ASN1_FUNCTIONS(SXNETID)
D
Dr. Stephen Henson 已提交
96

D
 
Dr. Stephen Henson 已提交
97 98 99
ASN1_SEQUENCE(SXNET) = {
	ASN1_SIMPLE(SXNET, version, ASN1_INTEGER),
	ASN1_SEQUENCE_OF(SXNET, ids, SXNETID)
100
} ASN1_SEQUENCE_END(SXNET)
D
Dr. Stephen Henson 已提交
101

D
 
Dr. Stephen Henson 已提交
102
IMPLEMENT_ASN1_FUNCTIONS(SXNET)
D
Dr. Stephen Henson 已提交
103

U
Ulf Möller 已提交
104 105
static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,
	     int indent)
D
Dr. Stephen Henson 已提交
106 107 108 109 110 111 112
{
	long v;
	char *tmp;
	SXNETID *id;
	int i;
	v = ASN1_INTEGER_get(sx->version);
	BIO_printf(out, "%*sVersion: %d (0x%X)", indent, "", v + 1, v);
B
Ben Laurie 已提交
113 114
	for(i = 0; i < sk_SXNETID_num(sx->ids); i++) {
		id = sk_SXNETID_value(sx->ids, i);
D
Dr. Stephen Henson 已提交
115 116
		tmp = i2s_ASN1_INTEGER(NULL, id->zone);
		BIO_printf(out, "\n%*sZone: %s, User: ", indent, "", tmp);
117
		OPENSSL_free(tmp);
118
		M_ASN1_OCTET_STRING_print(out, id->user);
D
Dr. Stephen Henson 已提交
119 120 121
	}
	return 1;
}
122 123 124 125 126 127 128 129 130

#ifdef SXNET_TEST

/* NBB: this is used for testing only. It should *not* be used for anything
 * else because it will just take static IDs from the configuration file and
 * they should really be separate values for each user.
 */


U
Ulf Möller 已提交
131
static SXNET * sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
132
	     STACK_OF(CONF_VALUE) *nval)
133 134 135 136
{
	CONF_VALUE *cnf;
	SXNET *sx = NULL;
	int i;
137 138
	for(i = 0; i < sk_CONF_VALUE_num(nval); i++) {
		cnf = sk_CONF_VALUE_value(nval, i);
139 140 141 142 143 144 145 146 147 148 149 150 151
		if(!SXNET_add_id_asc(&sx, cnf->name, cnf->value, -1))
								 return NULL;
	}
	return sx;
}
		
	
#endif

/* Strong Extranet utility functions */

/* Add an id given the zone as an ASCII number */

B
Ben Laurie 已提交
152
int SXNET_add_id_asc(SXNET **psx, char *zone, char *user,
U
Ulf Möller 已提交
153
	     int userlen)
154 155 156 157 158 159 160 161 162 163 164
{
	ASN1_INTEGER *izone = NULL;
	if(!(izone = s2i_ASN1_INTEGER(NULL, zone))) {
		X509V3err(X509V3_F_SXNET_ADD_ASC,X509V3_R_ERROR_CONVERTING_ZONE);
		return 0;
	}
	return SXNET_add_id_INTEGER(psx, izone, user, userlen);
}

/* Add an id given the zone as an unsigned long */

B
Ben Laurie 已提交
165
int SXNET_add_id_ulong(SXNET **psx, unsigned long lzone, char *user,
U
Ulf Möller 已提交
166
	     int userlen)
167 168
{
	ASN1_INTEGER *izone = NULL;
169
	if(!(izone = M_ASN1_INTEGER_new()) || !ASN1_INTEGER_set(izone, lzone)) {
170
		X509V3err(X509V3_F_SXNET_ADD_ID_ULONG,ERR_R_MALLOC_FAILURE);
171
		M_ASN1_INTEGER_free(izone);
172 173 174 175 176 177 178 179 180 181 182
		return 0;
	}
	return SXNET_add_id_INTEGER(psx, izone, user, userlen);
	
}

/* Add an id given the zone as an ASN1_INTEGER.
 * Note this version uses the passed integer and doesn't make a copy so don't
 * free it up afterwards.
 */

B
Ben Laurie 已提交
183
int SXNET_add_id_INTEGER(SXNET **psx, ASN1_INTEGER *zone, char *user,
U
Ulf Möller 已提交
184
	     int userlen)
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
{
	SXNET *sx = NULL;
	SXNETID *id = NULL;
	if(!psx || !zone || !user) {
		X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER,X509V3_R_INVALID_NULL_ARGUMENT);
		return 0;
	}
	if(userlen == -1) userlen = strlen(user);
	if(userlen > 64) {
		X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER,X509V3_R_USER_TOO_LONG);
		return 0;
	}
	if(!*psx) {
		if(!(sx = SXNET_new())) goto err;
		if(!ASN1_INTEGER_set(sx->version, 0)) goto err;
		*psx = sx;
	} else sx = *psx;
	if(SXNET_get_id_INTEGER(sx, zone)) {
		X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER,X509V3_R_DUPLICATE_ZONE_ID);
		return 0;
	}

	if(!(id = SXNETID_new())) goto err;
	if(userlen == -1) userlen = strlen(user);
		
210
	if(!M_ASN1_OCTET_STRING_set(id->user, user, userlen)) goto err;
B
Ben Laurie 已提交
211
	if(!sk_SXNETID_push(sx->ids, id)) goto err;
212 213 214 215 216 217 218 219 220 221 222
	id->zone = zone;
	return 1;
	
	err:
	X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER,ERR_R_MALLOC_FAILURE);
	SXNETID_free(id);
	SXNET_free(sx);
	*psx = NULL;
	return 0;
}

U
Ulf Möller 已提交
223
ASN1_OCTET_STRING *SXNET_get_id_asc(SXNET *sx, char *zone)
224 225 226 227 228 229 230 231
{
	ASN1_INTEGER *izone = NULL;
	ASN1_OCTET_STRING *oct;
	if(!(izone = s2i_ASN1_INTEGER(NULL, zone))) {
		X509V3err(X509V3_F_SXNET_GET_ID_ASC,X509V3_R_ERROR_CONVERTING_ZONE);
		return NULL;
	}
	oct = SXNET_get_id_INTEGER(sx, izone);
232
	M_ASN1_INTEGER_free(izone);
233 234 235
	return oct;
}

U
Ulf Möller 已提交
236
ASN1_OCTET_STRING *SXNET_get_id_ulong(SXNET *sx, unsigned long lzone)
237 238 239
{
	ASN1_INTEGER *izone = NULL;
	ASN1_OCTET_STRING *oct;
240
	if(!(izone = M_ASN1_INTEGER_new()) || !ASN1_INTEGER_set(izone, lzone)) {
241
		X509V3err(X509V3_F_SXNET_GET_ID_ULONG,ERR_R_MALLOC_FAILURE);
242
		M_ASN1_INTEGER_free(izone);
243 244 245
		return NULL;
	}
	oct = SXNET_get_id_INTEGER(sx, izone);
246
	M_ASN1_INTEGER_free(izone);
247 248 249
	return oct;
}

U
Ulf Möller 已提交
250
ASN1_OCTET_STRING *SXNET_get_id_INTEGER(SXNET *sx, ASN1_INTEGER *zone)
251 252 253
{
	SXNETID *id;
	int i;
B
Ben Laurie 已提交
254 255
	for(i = 0; i < sk_SXNETID_num(sx->ids); i++) {
		id = sk_SXNETID_value(sx->ids, i);
256
		if(!M_ASN1_INTEGER_cmp(id->zone, zone)) return id->user;
257 258 259
	}
	return NULL;
}
B
Ben Laurie 已提交
260 261 262

IMPLEMENT_STACK_OF(SXNETID)
IMPLEMENT_ASN1_SET_OF(SXNETID)