1 /* $OpenBSD: x509_extku.c,v 1.3 2023/02/16 08:38:17 tb Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project 1999. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 59 #include <stdio.h> 60 61 #include <openssl/asn1t.h> 62 #include <openssl/conf.h> 63 #include <openssl/err.h> 64 #include <openssl/x509v3.h> 65 66 static void *v2i_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method, 67 X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval); 68 static STACK_OF(CONF_VALUE) *i2v_EXTENDED_KEY_USAGE( 69 const X509V3_EXT_METHOD *method, void *eku, STACK_OF(CONF_VALUE) *extlist); 70 71 const X509V3_EXT_METHOD v3_ext_ku = { 72 .ext_nid = NID_ext_key_usage, 73 .ext_flags = 0, 74 .it = &EXTENDED_KEY_USAGE_it, 75 .ext_new = NULL, 76 .ext_free = NULL, 77 .d2i = NULL, 78 .i2d = NULL, 79 .i2s = NULL, 80 .s2i = NULL, 81 .i2v = i2v_EXTENDED_KEY_USAGE, 82 .v2i = v2i_EXTENDED_KEY_USAGE, 83 .i2r = NULL, 84 .r2i = NULL, 85 .usr_data = NULL, 86 }; 87 88 /* NB OCSP acceptable responses also is a SEQUENCE OF OBJECT */ 89 const X509V3_EXT_METHOD v3_ocsp_accresp = { 90 .ext_nid = NID_id_pkix_OCSP_acceptableResponses, 91 .ext_flags = 0, 92 .it = &EXTENDED_KEY_USAGE_it, 93 .ext_new = NULL, 94 .ext_free = NULL, 95 .d2i = NULL, 96 .i2d = NULL, 97 .i2s = NULL, 98 .s2i = NULL, 99 .i2v = i2v_EXTENDED_KEY_USAGE, 100 .v2i = v2i_EXTENDED_KEY_USAGE, 101 .i2r = NULL, 102 .r2i = NULL, 103 .usr_data = NULL, 104 }; 105 106 static const ASN1_TEMPLATE EXTENDED_KEY_USAGE_item_tt = { 107 .flags = ASN1_TFLG_SEQUENCE_OF, 108 .tag = 0, 109 .offset = 0, 110 .field_name = "EXTENDED_KEY_USAGE", 111 .item = &ASN1_OBJECT_it, 112 }; 113 114 const ASN1_ITEM EXTENDED_KEY_USAGE_it = { 115 .itype = ASN1_ITYPE_PRIMITIVE, 116 .utype = -1, 117 .templates = &EXTENDED_KEY_USAGE_item_tt, 118 .tcount = 0, 119 .funcs = NULL, 120 .size = 0, 121 .sname = "EXTENDED_KEY_USAGE", 122 }; 123 124 125 EXTENDED_KEY_USAGE * 126 d2i_EXTENDED_KEY_USAGE(EXTENDED_KEY_USAGE **a, const unsigned char **in, long len) 127 { 128 return (EXTENDED_KEY_USAGE *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, 129 &EXTENDED_KEY_USAGE_it); 130 } 131 LCRYPTO_ALIAS(d2i_EXTENDED_KEY_USAGE); 132 133 int 134 i2d_EXTENDED_KEY_USAGE(EXTENDED_KEY_USAGE *a, unsigned char **out) 135 { 136 return ASN1_item_i2d((ASN1_VALUE *)a, out, &EXTENDED_KEY_USAGE_it); 137 } 138 LCRYPTO_ALIAS(i2d_EXTENDED_KEY_USAGE); 139 140 EXTENDED_KEY_USAGE * 141 EXTENDED_KEY_USAGE_new(void) 142 { 143 return (EXTENDED_KEY_USAGE *)ASN1_item_new(&EXTENDED_KEY_USAGE_it); 144 } 145 LCRYPTO_ALIAS(EXTENDED_KEY_USAGE_new); 146 147 void 148 EXTENDED_KEY_USAGE_free(EXTENDED_KEY_USAGE *a) 149 { 150 ASN1_item_free((ASN1_VALUE *)a, &EXTENDED_KEY_USAGE_it); 151 } 152 LCRYPTO_ALIAS(EXTENDED_KEY_USAGE_free); 153 154 static STACK_OF(CONF_VALUE) * 155 i2v_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method, void *a, 156 STACK_OF(CONF_VALUE) *extlist) 157 { 158 ASN1_OBJECT *obj; 159 EXTENDED_KEY_USAGE *eku = a; 160 STACK_OF(CONF_VALUE) *free_extlist = NULL; 161 char obj_tmp[80]; 162 int i; 163 164 if (extlist == NULL) { 165 if ((free_extlist = extlist = sk_CONF_VALUE_new_null()) == NULL) 166 return NULL; 167 } 168 169 for (i = 0; i < sk_ASN1_OBJECT_num(eku); i++) { 170 if ((obj = sk_ASN1_OBJECT_value(eku, i)) == NULL) 171 goto err; 172 if (!i2t_ASN1_OBJECT(obj_tmp, sizeof obj_tmp, obj)) 173 goto err; 174 if (!X509V3_add_value(NULL, obj_tmp, &extlist)) 175 goto err; 176 } 177 178 return extlist; 179 180 err: 181 sk_CONF_VALUE_pop_free(free_extlist, X509V3_conf_free); 182 183 return NULL; 184 } 185 186 static void * 187 v2i_EXTENDED_KEY_USAGE(const X509V3_EXT_METHOD *method, X509V3_CTX *ctx, 188 STACK_OF(CONF_VALUE) *nval) 189 { 190 EXTENDED_KEY_USAGE *extku; 191 char *extval; 192 ASN1_OBJECT *objtmp; 193 CONF_VALUE *val; 194 int i; 195 196 if (!(extku = sk_ASN1_OBJECT_new_null())) { 197 X509V3error(ERR_R_MALLOC_FAILURE); 198 return NULL; 199 } 200 201 for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { 202 val = sk_CONF_VALUE_value(nval, i); 203 if (val->value) 204 extval = val->value; 205 else 206 extval = val->name; 207 if (!(objtmp = OBJ_txt2obj(extval, 0))) { 208 sk_ASN1_OBJECT_pop_free(extku, ASN1_OBJECT_free); 209 X509V3error(X509V3_R_INVALID_OBJECT_IDENTIFIER); 210 X509V3_conf_err(val); 211 return NULL; 212 } 213 if (sk_ASN1_OBJECT_push(extku, objtmp) == 0) { 214 ASN1_OBJECT_free(objtmp); 215 sk_ASN1_OBJECT_pop_free(extku, ASN1_OBJECT_free); 216 X509V3error(ERR_R_MALLOC_FAILURE); 217 return NULL; 218 } 219 } 220 return extku; 221 } 222