b0a807d35a82d6acf1b5daa394eddd2617a9e09a
[openssl.git] / crypto / x509 / v3_bitst.c
1 /*
2  * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/conf.h>
13 #include <openssl/x509v3.h>
14 #include "ext_dat.h"
15
16 DEFINE_STACK_OF(CONF_VALUE)
17
18 static BIT_STRING_BITNAME ns_cert_type_table[] = {
19     {0, "SSL Client", "client"},
20     {1, "SSL Server", "server"},
21     {2, "S/MIME", "email"},
22     {3, "Object Signing", "objsign"},
23     {4, "Unused", "reserved"},
24     {5, "SSL CA", "sslCA"},
25     {6, "S/MIME CA", "emailCA"},
26     {7, "Object Signing CA", "objCA"},
27     {-1, NULL, NULL}
28 };
29
30 static BIT_STRING_BITNAME key_usage_type_table[] = {
31     {0, "Digital Signature", "digitalSignature"},
32     {1, "Non Repudiation", "nonRepudiation"},
33     {2, "Key Encipherment", "keyEncipherment"},
34     {3, "Data Encipherment", "dataEncipherment"},
35     {4, "Key Agreement", "keyAgreement"},
36     {5, "Certificate Sign", "keyCertSign"},
37     {6, "CRL Sign", "cRLSign"},
38     {7, "Encipher Only", "encipherOnly"},
39     {8, "Decipher Only", "decipherOnly"},
40     {-1, NULL, NULL}
41 };
42
43 const X509V3_EXT_METHOD v3_nscert =
44 EXT_BITSTRING(NID_netscape_cert_type, ns_cert_type_table);
45 const X509V3_EXT_METHOD v3_key_usage =
46 EXT_BITSTRING(NID_key_usage, key_usage_type_table);
47
48 STACK_OF(CONF_VALUE) *i2v_ASN1_BIT_STRING(X509V3_EXT_METHOD *method,
49                                           ASN1_BIT_STRING *bits,
50                                           STACK_OF(CONF_VALUE) *ret)
51 {
52     BIT_STRING_BITNAME *bnam;
53     for (bnam = method->usr_data; bnam->lname; bnam++) {
54         if (ASN1_BIT_STRING_get_bit(bits, bnam->bitnum))
55             X509V3_add_value(bnam->lname, NULL, &ret);
56     }
57     return ret;
58 }
59
60 ASN1_BIT_STRING *v2i_ASN1_BIT_STRING(X509V3_EXT_METHOD *method,
61                                      X509V3_CTX *ctx,
62                                      STACK_OF(CONF_VALUE) *nval)
63 {
64     CONF_VALUE *val;
65     ASN1_BIT_STRING *bs;
66     int i;
67     BIT_STRING_BITNAME *bnam;
68     if ((bs = ASN1_BIT_STRING_new()) == NULL) {
69         X509V3err(X509V3_F_V2I_ASN1_BIT_STRING, ERR_R_MALLOC_FAILURE);
70         return NULL;
71     }
72     for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
73         val = sk_CONF_VALUE_value(nval, i);
74         for (bnam = method->usr_data; bnam->lname; bnam++) {
75             if (strcmp(bnam->sname, val->name) == 0
76                 || strcmp(bnam->lname, val->name) == 0) {
77                 if (!ASN1_BIT_STRING_set_bit(bs, bnam->bitnum, 1)) {
78                     X509V3err(X509V3_F_V2I_ASN1_BIT_STRING,
79                               ERR_R_MALLOC_FAILURE);
80                     ASN1_BIT_STRING_free(bs);
81                     return NULL;
82                 }
83                 break;
84             }
85         }
86         if (!bnam->lname) {
87             X509V3err(X509V3_F_V2I_ASN1_BIT_STRING,
88                       X509V3_R_UNKNOWN_BIT_STRING_ARGUMENT);
89             X509V3_conf_err(val);
90             ASN1_BIT_STRING_free(bs);
91             return NULL;
92         }
93     }
94     return bs;
95 }