1 // Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdio.h>
16 #include <string.h>
17
18 #include <openssl/conf.h>
19 #include <openssl/err.h>
20 #include <openssl/obj.h>
21 #include <openssl/x509.h>
22
23 #include "internal.h"
24
25
26 static const BIT_STRING_BITNAME ns_cert_type_table[] = {
27 {0, "SSL Client", "client"},
28 {1, "SSL Server", "server"},
29 {2, "S/MIME", "email"},
30 {3, "Object Signing", "objsign"},
31 {4, "Unused", "reserved"},
32 {5, "SSL CA", "sslCA"},
33 {6, "S/MIME CA", "emailCA"},
34 {7, "Object Signing CA", "objCA"},
35 {-1, NULL, NULL}};
36
37 static const BIT_STRING_BITNAME key_usage_type_table[] = {
38 {0, "Digital Signature", "digitalSignature"},
39 {1, "Non Repudiation", "nonRepudiation"},
40 {2, "Key Encipherment", "keyEncipherment"},
41 {3, "Data Encipherment", "dataEncipherment"},
42 {4, "Key Agreement", "keyAgreement"},
43 {5, "Certificate Sign", "keyCertSign"},
44 {6, "CRL Sign", "cRLSign"},
45 {7, "Encipher Only", "encipherOnly"},
46 {8, "Decipher Only", "decipherOnly"},
47 {-1, NULL, NULL}};
48
STACK_OF(CONF_VALUE)49 static STACK_OF(CONF_VALUE) *i2v_ASN1_BIT_STRING(
50 const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *ret) {
51 const ASN1_BIT_STRING *bits = reinterpret_cast<ASN1_BIT_STRING *>(ext);
52 const BIT_STRING_BITNAME *bnam;
53 for (bnam = reinterpret_cast<const BIT_STRING_BITNAME *>(method->usr_data);
54 bnam->lname; bnam++) {
55 if (ASN1_BIT_STRING_get_bit(bits, bnam->bitnum)) {
56 X509V3_add_value(bnam->lname, NULL, &ret);
57 }
58 }
59 return ret;
60 }
61
v2i_ASN1_BIT_STRING(const X509V3_EXT_METHOD * method,const X509V3_CTX * ctx,const STACK_OF (CONF_VALUE)* nval)62 static void *v2i_ASN1_BIT_STRING(const X509V3_EXT_METHOD *method,
63 const X509V3_CTX *ctx,
64 const STACK_OF(CONF_VALUE) *nval) {
65 ASN1_BIT_STRING *bs;
66 if (!(bs = ASN1_BIT_STRING_new())) {
67 return NULL;
68 }
69 for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) {
70 const CONF_VALUE *val = sk_CONF_VALUE_value(nval, i);
71 const BIT_STRING_BITNAME *bnam;
72 for (bnam = reinterpret_cast<const BIT_STRING_BITNAME *>(method->usr_data);
73 bnam->lname; bnam++) {
74 if (!strcmp(bnam->sname, val->name) || !strcmp(bnam->lname, val->name)) {
75 if (!ASN1_BIT_STRING_set_bit(bs, bnam->bitnum, 1)) {
76 ASN1_BIT_STRING_free(bs);
77 return NULL;
78 }
79 break;
80 }
81 }
82 if (!bnam->lname) {
83 OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_BIT_STRING_ARGUMENT);
84 X509V3_conf_err(val);
85 ASN1_BIT_STRING_free(bs);
86 return NULL;
87 }
88 }
89 return bs;
90 }
91
92 #define EXT_BITSTRING(nid, table) \
93 { \
94 nid, 0, ASN1_ITEM_ref(ASN1_BIT_STRING), 0, 0, 0, 0, 0, 0, \
95 i2v_ASN1_BIT_STRING, v2i_ASN1_BIT_STRING, NULL, NULL, (void *)(table) \
96 }
97
98 const X509V3_EXT_METHOD v3_nscert =
99 EXT_BITSTRING(NID_netscape_cert_type, ns_cert_type_table);
100 const X509V3_EXT_METHOD v3_key_usage =
101 EXT_BITSTRING(NID_key_usage, key_usage_type_table);
102