v3_bitst.c revision 160814
133965Sjdp/* v3_bitst.c */
278828Sobrien/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3218822Sdim * project 1999.
438889Sjdp */
533965Sjdp/* ====================================================================
633965Sjdp * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
733965Sjdp *
833965Sjdp * Redistribution and use in source and binary forms, with or without
933965Sjdp * modification, are permitted provided that the following conditions
1033965Sjdp * are met:
1133965Sjdp *
1233965Sjdp * 1. Redistributions of source code must retain the above copyright
1333965Sjdp *    notice, this list of conditions and the following disclaimer.
1433965Sjdp *
1533965Sjdp * 2. Redistributions in binary form must reproduce the above copyright
1633965Sjdp *    notice, this list of conditions and the following disclaimer in
1733965Sjdp *    the documentation and/or other materials provided with the
1833965Sjdp *    distribution.
1933965Sjdp *
20218822Sdim * 3. All advertising materials mentioning features or use of this
21218822Sdim *    software must display the following acknowledgment:
2233965Sjdp *    "This product includes software developed by the OpenSSL Project
23218822Sdim *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2433965Sjdp *
2533965Sjdp * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2633965Sjdp *    endorse or promote products derived from this software without
2733965Sjdp *    prior written permission. For written permission, please contact
28218822Sdim *    licensing@OpenSSL.org.
2933965Sjdp *
3061843Sobrien * 5. Products derived from this software may not be called "OpenSSL"
31130561Sobrien *    nor may "OpenSSL" appear in their names without prior written
32130561Sobrien *    permission of the OpenSSL Project.
3333965Sjdp *
34218822Sdim * 6. Redistributions of any form whatsoever must retain the following
3533965Sjdp *    acknowledgment:
3638889Sjdp *    "This product includes software developed by the OpenSSL Project
3738889Sjdp *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3838889Sjdp *
39104834Sobrien * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4038889Sjdp * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4138889Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4238889Sjdp * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4338889Sjdp * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4438889Sjdp * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4538889Sjdp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4638889Sjdp * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4760484Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4860484Sobrien * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4960484Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5060484Sobrien * OF THE POSSIBILITY OF SUCH DAMAGE.
5160484Sobrien * ====================================================================
5260484Sobrien *
5360484Sobrien * This product includes cryptographic software written by Eric Young
5460484Sobrien * (eay@cryptsoft.com).  This product includes software written by Tim
5589857Sobrien * Hudson (tjh@cryptsoft.com).
5689857Sobrien *
5789857Sobrien */
5889857Sobrien
5989857Sobrien#include <stdio.h>
6089857Sobrien#include "cryptlib.h"
6189857Sobrien#include <openssl/conf.h>
6289857Sobrien#include <openssl/x509v3.h>
6389857Sobrien
6489857Sobrienstatic BIT_STRING_BITNAME ns_cert_type_table[] = {
65130561Sobrien{0, "SSL Client", "client"},
6689857Sobrien{1, "SSL Server", "server"},
6760484Sobrien{2, "S/MIME", "email"},
6833965Sjdp{3, "Object Signing", "objsign"},
69130561Sobrien{4, "Unused", "reserved"},
70130561Sobrien{5, "SSL CA", "sslCA"},
7133965Sjdp{6, "S/MIME CA", "emailCA"},
7233965Sjdp{7, "Object Signing CA", "objCA"},
7333965Sjdp{-1, NULL, NULL}
7433965Sjdp};
7533965Sjdp
76130561Sobrienstatic BIT_STRING_BITNAME key_usage_type_table[] = {
77130561Sobrien{0, "Digital Signature", "digitalSignature"},
7833965Sjdp{1, "Non Repudiation", "nonRepudiation"},
7933965Sjdp{2, "Key Encipherment", "keyEncipherment"},
8033965Sjdp{3, "Data Encipherment", "dataEncipherment"},
8133965Sjdp{4, "Key Agreement", "keyAgreement"},
8260484Sobrien{5, "Certificate Sign", "keyCertSign"},
83130561Sobrien{6, "CRL Sign", "cRLSign"},
84130561Sobrien{7, "Encipher Only", "encipherOnly"},
85130561Sobrien{8, "Decipher Only", "decipherOnly"},
86130561Sobrien{-1, NULL, NULL}
87130561Sobrien};
8833965Sjdp
8933965Sjdp
90104834Sobrien
9133965SjdpX509V3_EXT_METHOD v3_nscert = EXT_BITSTRING(NID_netscape_cert_type, ns_cert_type_table);
9233965SjdpX509V3_EXT_METHOD v3_key_usage = EXT_BITSTRING(NID_key_usage, key_usage_type_table);
9333965Sjdp
9433965SjdpSTACK_OF(CONF_VALUE) *i2v_ASN1_BIT_STRING(X509V3_EXT_METHOD *method,
9560484Sobrien	     ASN1_BIT_STRING *bits, STACK_OF(CONF_VALUE) *ret)
96130561Sobrien{
97130561Sobrien	BIT_STRING_BITNAME *bnam;
9833965Sjdp	for(bnam =method->usr_data; bnam->lname; bnam++) {
9933965Sjdp		if(ASN1_BIT_STRING_get_bit(bits, bnam->bitnum))
10060484Sobrien			X509V3_add_value(bnam->lname, NULL, &ret);
10133965Sjdp	}
10233965Sjdp	return ret;
10360484Sobrien}
10460484Sobrien
10560484SobrienASN1_BIT_STRING *v2i_ASN1_BIT_STRING(X509V3_EXT_METHOD *method,
10660484Sobrien	     X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval)
10760484Sobrien{
10860484Sobrien	CONF_VALUE *val;
10960484Sobrien	ASN1_BIT_STRING *bs;
11060484Sobrien	int i;
11133965Sjdp	BIT_STRING_BITNAME *bnam;
11233965Sjdp	if(!(bs = M_ASN1_BIT_STRING_new())) {
11333965Sjdp		X509V3err(X509V3_F_V2I_ASN1_BIT_STRING,ERR_R_MALLOC_FAILURE);
114130561Sobrien		return NULL;
115130561Sobrien	}
116130561Sobrien	for(i = 0; i < sk_CONF_VALUE_num(nval); i++) {
117130561Sobrien		val = sk_CONF_VALUE_value(nval, i);
118130561Sobrien		for(bnam = method->usr_data; bnam->lname; bnam++) {
119130561Sobrien			if(!strcmp(bnam->sname, val->name) ||
120130561Sobrien				!strcmp(bnam->lname, val->name) ) {
121130561Sobrien				if(!ASN1_BIT_STRING_set_bit(bs, bnam->bitnum, 1)) {
122130561Sobrien					X509V3err(X509V3_F_V2I_ASN1_BIT_STRING,
123130561Sobrien						ERR_R_MALLOC_FAILURE);
124130561Sobrien					M_ASN1_BIT_STRING_free(bs);
12533965Sjdp					return NULL;
12633965Sjdp				}
12760484Sobrien				break;
12889857Sobrien			}
129130561Sobrien		}
130130561Sobrien		if(!bnam->lname) {
13189857Sobrien			X509V3err(X509V3_F_V2I_ASN1_BIT_STRING,
132130561Sobrien					X509V3_R_UNKNOWN_BIT_STRING_ARGUMENT);
133130561Sobrien			X509V3_conf_err(val);
13433965Sjdp			M_ASN1_BIT_STRING_free(bs);
13560484Sobrien			return NULL;
13660484Sobrien		}
137130561Sobrien	}
13833965Sjdp	return bs;
13933965Sjdp}
14060484Sobrien
14160484Sobrien
14233965Sjdp