1 /* $OpenBSD: x_req.c,v 1.23 2024/07/08 14:48:49 beck Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 #include <stdio.h> 60 61 #include <openssl/asn1t.h> 62 #include <openssl/x509.h> 63 64 #include "x509_local.h" 65 66 /* X509_REQ_INFO is handled in an unusual way to get round 67 * invalid encodings. Some broken certificate requests don't 68 * encode the attributes field if it is empty. This is in 69 * violation of PKCS#10 but we need to tolerate it. We do 70 * this by making the attributes field OPTIONAL then using 71 * the callback to initialise it to an empty STACK. 72 * 73 * This means that the field will be correctly encoded unless 74 * we NULL out the field. 75 * 76 * As a result we no longer need the req_kludge field because 77 * the information is now contained in the attributes field: 78 * 1. If it is NULL then it's the invalid omission. 79 * 2. If it is empty it is the correct encoding. 80 * 3. If it is not empty then some attributes are present. 81 * 82 */ 83 84 static int 85 rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg) 86 { 87 X509_REQ_INFO *rinf = (X509_REQ_INFO *)*pval; 88 89 if (operation == ASN1_OP_NEW_POST) { 90 rinf->attributes = sk_X509_ATTRIBUTE_new_null(); 91 if (!rinf->attributes) 92 return 0; 93 } 94 return 1; 95 } 96 97 static const ASN1_AUX X509_REQ_INFO_aux = { 98 .flags = ASN1_AFLG_ENCODING, 99 .asn1_cb = rinf_cb, 100 .enc_offset = offsetof(X509_REQ_INFO, enc), 101 }; 102 static const ASN1_TEMPLATE X509_REQ_INFO_seq_tt[] = { 103 { 104 .offset = offsetof(X509_REQ_INFO, version), 105 .field_name = "version", 106 .item = &ASN1_INTEGER_it, 107 }, 108 { 109 .offset = offsetof(X509_REQ_INFO, subject), 110 .field_name = "subject", 111 .item = &X509_NAME_it, 112 }, 113 { 114 .offset = offsetof(X509_REQ_INFO, pubkey), 115 .field_name = "pubkey", 116 .item = &X509_PUBKEY_it, 117 }, 118 /* This isn't really OPTIONAL but it gets round invalid 119 * encodings 120 */ 121 { 122 .flags = ASN1_TFLG_IMPLICIT | ASN1_TFLG_SET_OF | ASN1_TFLG_OPTIONAL, 123 .offset = offsetof(X509_REQ_INFO, attributes), 124 .field_name = "attributes", 125 .item = &X509_ATTRIBUTE_it, 126 }, 127 }; 128 129 const ASN1_ITEM X509_REQ_INFO_it = { 130 .itype = ASN1_ITYPE_SEQUENCE, 131 .utype = V_ASN1_SEQUENCE, 132 .templates = X509_REQ_INFO_seq_tt, 133 .tcount = sizeof(X509_REQ_INFO_seq_tt) / sizeof(ASN1_TEMPLATE), 134 .funcs = &X509_REQ_INFO_aux, 135 .size = sizeof(X509_REQ_INFO), 136 .sname = "X509_REQ_INFO", 137 }; 138 LCRYPTO_ALIAS(X509_REQ_INFO_it); 139 140 141 X509_REQ_INFO * 142 d2i_X509_REQ_INFO(X509_REQ_INFO **a, const unsigned char **in, long len) 143 { 144 return (X509_REQ_INFO *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, 145 &X509_REQ_INFO_it); 146 } 147 LCRYPTO_ALIAS(d2i_X509_REQ_INFO); 148 149 int 150 i2d_X509_REQ_INFO(X509_REQ_INFO *a, unsigned char **out) 151 { 152 return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_REQ_INFO_it); 153 } 154 LCRYPTO_ALIAS(i2d_X509_REQ_INFO); 155 156 X509_REQ_INFO * 157 X509_REQ_INFO_new(void) 158 { 159 return (X509_REQ_INFO *)ASN1_item_new(&X509_REQ_INFO_it); 160 } 161 LCRYPTO_ALIAS(X509_REQ_INFO_new); 162 163 void 164 X509_REQ_INFO_free(X509_REQ_INFO *a) 165 { 166 ASN1_item_free((ASN1_VALUE *)a, &X509_REQ_INFO_it); 167 } 168 LCRYPTO_ALIAS(X509_REQ_INFO_free); 169 170 static const ASN1_AUX X509_REQ_aux = { 171 .app_data = NULL, 172 .flags = ASN1_AFLG_REFCOUNT, 173 .ref_offset = offsetof(X509_REQ, references), 174 .ref_lock = CRYPTO_LOCK_X509_REQ, 175 }; 176 static const ASN1_TEMPLATE X509_REQ_seq_tt[] = { 177 { 178 .offset = offsetof(X509_REQ, req_info), 179 .field_name = "req_info", 180 .item = &X509_REQ_INFO_it, 181 }, 182 { 183 .offset = offsetof(X509_REQ, sig_alg), 184 .field_name = "sig_alg", 185 .item = &X509_ALGOR_it, 186 }, 187 { 188 .offset = offsetof(X509_REQ, signature), 189 .field_name = "signature", 190 .item = &ASN1_BIT_STRING_it, 191 }, 192 }; 193 194 const ASN1_ITEM X509_REQ_it = { 195 .itype = ASN1_ITYPE_SEQUENCE, 196 .utype = V_ASN1_SEQUENCE, 197 .templates = X509_REQ_seq_tt, 198 .tcount = sizeof(X509_REQ_seq_tt) / sizeof(ASN1_TEMPLATE), 199 .funcs = &X509_REQ_aux, 200 .size = sizeof(X509_REQ), 201 .sname = "X509_REQ", 202 }; 203 LCRYPTO_ALIAS(X509_REQ_it); 204 205 206 X509_REQ * 207 d2i_X509_REQ(X509_REQ **a, const unsigned char **in, long len) 208 { 209 return (X509_REQ *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, 210 &X509_REQ_it); 211 } 212 LCRYPTO_ALIAS(d2i_X509_REQ); 213 214 int 215 i2d_X509_REQ(X509_REQ *a, unsigned char **out) 216 { 217 return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_REQ_it); 218 } 219 LCRYPTO_ALIAS(i2d_X509_REQ); 220 221 X509_REQ * 222 X509_REQ_new(void) 223 { 224 return (X509_REQ *)ASN1_item_new(&X509_REQ_it); 225 } 226 LCRYPTO_ALIAS(X509_REQ_new); 227 228 void 229 X509_REQ_free(X509_REQ *a) 230 { 231 ASN1_item_free((ASN1_VALUE *)a, &X509_REQ_it); 232 } 233 LCRYPTO_ALIAS(X509_REQ_free); 234 235 X509_REQ * 236 X509_REQ_dup(X509_REQ *x) 237 { 238 return ASN1_item_dup(&X509_REQ_it, x); 239 } 240 LCRYPTO_ALIAS(X509_REQ_dup); 241 242 int 243 X509_REQ_get_signature_nid(const X509_REQ *req) 244 { 245 return OBJ_obj2nid(req->sig_alg->algorithm); 246 } 247 LCRYPTO_ALIAS(X509_REQ_get_signature_nid); 248 249 void 250 X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig, 251 const X509_ALGOR **palg) 252 { 253 if (psig != NULL) 254 *psig = req->signature; 255 if (palg != NULL) 256 *palg = req->sig_alg; 257 } 258 LCRYPTO_ALIAS(X509_REQ_get0_signature); 259