1 /* $OpenBSD: x_req.c,v 1.18 2021/11/01 20:53:08 tb 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_lcl.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 139 140 X509_REQ_INFO * 141 d2i_X509_REQ_INFO(X509_REQ_INFO **a, const unsigned char **in, long len) 142 { 143 return (X509_REQ_INFO *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, 144 &X509_REQ_INFO_it); 145 } 146 147 int 148 i2d_X509_REQ_INFO(X509_REQ_INFO *a, unsigned char **out) 149 { 150 return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_REQ_INFO_it); 151 } 152 153 X509_REQ_INFO * 154 X509_REQ_INFO_new(void) 155 { 156 return (X509_REQ_INFO *)ASN1_item_new(&X509_REQ_INFO_it); 157 } 158 159 void 160 X509_REQ_INFO_free(X509_REQ_INFO *a) 161 { 162 ASN1_item_free((ASN1_VALUE *)a, &X509_REQ_INFO_it); 163 } 164 165 static const ASN1_AUX X509_REQ_aux = { 166 .app_data = NULL, 167 .flags = ASN1_AFLG_REFCOUNT, 168 .ref_offset = offsetof(X509_REQ, references), 169 .ref_lock = CRYPTO_LOCK_X509_REQ, 170 }; 171 static const ASN1_TEMPLATE X509_REQ_seq_tt[] = { 172 { 173 .offset = offsetof(X509_REQ, req_info), 174 .field_name = "req_info", 175 .item = &X509_REQ_INFO_it, 176 }, 177 { 178 .offset = offsetof(X509_REQ, sig_alg), 179 .field_name = "sig_alg", 180 .item = &X509_ALGOR_it, 181 }, 182 { 183 .offset = offsetof(X509_REQ, signature), 184 .field_name = "signature", 185 .item = &ASN1_BIT_STRING_it, 186 }, 187 }; 188 189 const ASN1_ITEM X509_REQ_it = { 190 .itype = ASN1_ITYPE_SEQUENCE, 191 .utype = V_ASN1_SEQUENCE, 192 .templates = X509_REQ_seq_tt, 193 .tcount = sizeof(X509_REQ_seq_tt) / sizeof(ASN1_TEMPLATE), 194 .funcs = &X509_REQ_aux, 195 .size = sizeof(X509_REQ), 196 .sname = "X509_REQ", 197 }; 198 199 200 X509_REQ * 201 d2i_X509_REQ(X509_REQ **a, const unsigned char **in, long len) 202 { 203 return (X509_REQ *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, 204 &X509_REQ_it); 205 } 206 207 int 208 i2d_X509_REQ(X509_REQ *a, unsigned char **out) 209 { 210 return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_REQ_it); 211 } 212 213 X509_REQ * 214 X509_REQ_new(void) 215 { 216 return (X509_REQ *)ASN1_item_new(&X509_REQ_it); 217 } 218 219 void 220 X509_REQ_free(X509_REQ *a) 221 { 222 ASN1_item_free((ASN1_VALUE *)a, &X509_REQ_it); 223 } 224 225 X509_REQ * 226 X509_REQ_dup(X509_REQ *x) 227 { 228 return ASN1_item_dup(&X509_REQ_it, x); 229 } 230 231 int 232 X509_REQ_get_signature_nid(const X509_REQ *req) 233 { 234 return OBJ_obj2nid(req->sig_alg->algorithm); 235 } 236 237 void 238 X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig, 239 const X509_ALGOR **palg) 240 { 241 if (psig != NULL) 242 *psig = req->signature; 243 if (palg != NULL) 244 *palg = req->sig_alg; 245 } 246