155714Skris/* v3_bitst.c */
2280297Sjkim/*
3280297Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4280297Sjkim * 1999.
555714Skris */
655714Skris/* ====================================================================
755714Skris * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
855714Skris *
955714Skris * Redistribution and use in source and binary forms, with or without
1055714Skris * modification, are permitted provided that the following conditions
1155714Skris * are met:
1255714Skris *
1355714Skris * 1. Redistributions of source code must retain the above copyright
14280297Sjkim *    notice, this list of conditions and the following disclaimer.
1555714Skris *
1655714Skris * 2. Redistributions in binary form must reproduce the above copyright
1755714Skris *    notice, this list of conditions and the following disclaimer in
1855714Skris *    the documentation and/or other materials provided with the
1955714Skris *    distribution.
2055714Skris *
2155714Skris * 3. All advertising materials mentioning features or use of this
2255714Skris *    software must display the following acknowledgment:
2355714Skris *    "This product includes software developed by the OpenSSL Project
2455714Skris *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2555714Skris *
2655714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2755714Skris *    endorse or promote products derived from this software without
2855714Skris *    prior written permission. For written permission, please contact
2955714Skris *    licensing@OpenSSL.org.
3055714Skris *
3155714Skris * 5. Products derived from this software may not be called "OpenSSL"
3255714Skris *    nor may "OpenSSL" appear in their names without prior written
3355714Skris *    permission of the OpenSSL Project.
3455714Skris *
3555714Skris * 6. Redistributions of any form whatsoever must retain the following
3655714Skris *    acknowledgment:
3755714Skris *    "This product includes software developed by the OpenSSL Project
3855714Skris *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3955714Skris *
4055714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4155714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4255714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4355714Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4455714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4555714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4655714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4755714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4955714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5055714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5155714Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
5255714Skris * ====================================================================
5355714Skris *
5455714Skris * This product includes cryptographic software written by Eric Young
5555714Skris * (eay@cryptsoft.com).  This product includes software written by Tim
5655714Skris * Hudson (tjh@cryptsoft.com).
5755714Skris *
5855714Skris */
5955714Skris
6055714Skris#include <stdio.h>
6155714Skris#include "cryptlib.h"
6255714Skris#include <openssl/conf.h>
6355714Skris#include <openssl/x509v3.h>
6455714Skris
6555714Skrisstatic BIT_STRING_BITNAME ns_cert_type_table[] = {
66280297Sjkim    {0, "SSL Client", "client"},
67280297Sjkim    {1, "SSL Server", "server"},
68280297Sjkim    {2, "S/MIME", "email"},
69280297Sjkim    {3, "Object Signing", "objsign"},
70280297Sjkim    {4, "Unused", "reserved"},
71280297Sjkim    {5, "SSL CA", "sslCA"},
72280297Sjkim    {6, "S/MIME CA", "emailCA"},
73280297Sjkim    {7, "Object Signing CA", "objCA"},
74280297Sjkim    {-1, NULL, NULL}
7555714Skris};
7655714Skris
7755714Skrisstatic BIT_STRING_BITNAME key_usage_type_table[] = {
78280297Sjkim    {0, "Digital Signature", "digitalSignature"},
79280297Sjkim    {1, "Non Repudiation", "nonRepudiation"},
80280297Sjkim    {2, "Key Encipherment", "keyEncipherment"},
81280297Sjkim    {3, "Data Encipherment", "dataEncipherment"},
82280297Sjkim    {4, "Key Agreement", "keyAgreement"},
83280297Sjkim    {5, "Certificate Sign", "keyCertSign"},
84280297Sjkim    {6, "CRL Sign", "cRLSign"},
85280297Sjkim    {7, "Encipher Only", "encipherOnly"},
86280297Sjkim    {8, "Decipher Only", "decipherOnly"},
87280297Sjkim    {-1, NULL, NULL}
8855714Skris};
8955714Skris
90280297Sjkimconst X509V3_EXT_METHOD v3_nscert =
91280297SjkimEXT_BITSTRING(NID_netscape_cert_type, ns_cert_type_table);
92280297Sjkimconst X509V3_EXT_METHOD v3_key_usage =
93280297SjkimEXT_BITSTRING(NID_key_usage, key_usage_type_table);
9455714Skris
95160814SsimonSTACK_OF(CONF_VALUE) *i2v_ASN1_BIT_STRING(X509V3_EXT_METHOD *method,
96280297Sjkim                                          ASN1_BIT_STRING *bits,
97280297Sjkim                                          STACK_OF(CONF_VALUE) *ret)
9855714Skris{
99280297Sjkim    BIT_STRING_BITNAME *bnam;
100280297Sjkim    for (bnam = method->usr_data; bnam->lname; bnam++) {
101280297Sjkim        if (ASN1_BIT_STRING_get_bit(bits, bnam->bitnum))
102280297Sjkim            X509V3_add_value(bnam->lname, NULL, &ret);
103280297Sjkim    }
104280297Sjkim    return ret;
10555714Skris}
106280297Sjkim
107160814SsimonASN1_BIT_STRING *v2i_ASN1_BIT_STRING(X509V3_EXT_METHOD *method,
108280297Sjkim                                     X509V3_CTX *ctx,
109280297Sjkim                                     STACK_OF(CONF_VALUE) *nval)
11055714Skris{
111280297Sjkim    CONF_VALUE *val;
112280297Sjkim    ASN1_BIT_STRING *bs;
113280297Sjkim    int i;
114280297Sjkim    BIT_STRING_BITNAME *bnam;
115280297Sjkim    if (!(bs = M_ASN1_BIT_STRING_new())) {
116280297Sjkim        X509V3err(X509V3_F_V2I_ASN1_BIT_STRING, ERR_R_MALLOC_FAILURE);
117280297Sjkim        return NULL;
118280297Sjkim    }
119280297Sjkim    for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
120280297Sjkim        val = sk_CONF_VALUE_value(nval, i);
121280297Sjkim        for (bnam = method->usr_data; bnam->lname; bnam++) {
122280297Sjkim            if (!strcmp(bnam->sname, val->name) ||
123280297Sjkim                !strcmp(bnam->lname, val->name)) {
124280297Sjkim                if (!ASN1_BIT_STRING_set_bit(bs, bnam->bitnum, 1)) {
125280297Sjkim                    X509V3err(X509V3_F_V2I_ASN1_BIT_STRING,
126280297Sjkim                              ERR_R_MALLOC_FAILURE);
127280297Sjkim                    M_ASN1_BIT_STRING_free(bs);
128280297Sjkim                    return NULL;
129280297Sjkim                }
130280297Sjkim                break;
131280297Sjkim            }
132280297Sjkim        }
133280297Sjkim        if (!bnam->lname) {
134280297Sjkim            X509V3err(X509V3_F_V2I_ASN1_BIT_STRING,
135280297Sjkim                      X509V3_R_UNKNOWN_BIT_STRING_ARGUMENT);
136280297Sjkim            X509V3_conf_err(val);
137280297Sjkim            M_ASN1_BIT_STRING_free(bs);
138280297Sjkim            return NULL;
139280297Sjkim        }
140280297Sjkim    }
141280297Sjkim    return bs;
14255714Skris}
143