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)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
61 #include <openssl/asn1t.h>
62 #include <openssl/x509.h>
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.
73 * This means that the field will be correctly encoded unless
74 * we NULL out the field.
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.
85 rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, void *exarg)
87 X509_REQ_INFO *rinf = (X509_REQ_INFO *)*pval;
89 if (operation == ASN1_OP_NEW_POST) {
90 rinf->attributes = sk_X509_ATTRIBUTE_new_null();
91 if (!rinf->attributes)
97 static const ASN1_AUX X509_REQ_INFO_aux = {
98 .flags = ASN1_AFLG_ENCODING,
100 .enc_offset = offsetof(X509_REQ_INFO, enc),
102 static const ASN1_TEMPLATE X509_REQ_INFO_seq_tt[] = {
104 .offset = offsetof(X509_REQ_INFO, version),
105 .field_name = "version",
106 .item = &ASN1_INTEGER_it,
109 .offset = offsetof(X509_REQ_INFO, subject),
110 .field_name = "subject",
111 .item = &X509_NAME_it,
114 .offset = offsetof(X509_REQ_INFO, pubkey),
115 .field_name = "pubkey",
116 .item = &X509_PUBKEY_it,
118 /* This isn't really OPTIONAL but it gets round invalid
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,
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",
141 d2i_X509_REQ_INFO(X509_REQ_INFO **a, const unsigned char **in, long len)
143 return (X509_REQ_INFO *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
148 i2d_X509_REQ_INFO(X509_REQ_INFO *a, unsigned char **out)
150 return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_REQ_INFO_it);
154 X509_REQ_INFO_new(void)
156 return (X509_REQ_INFO *)ASN1_item_new(&X509_REQ_INFO_it);
160 X509_REQ_INFO_free(X509_REQ_INFO *a)
162 ASN1_item_free((ASN1_VALUE *)a, &X509_REQ_INFO_it);
165 static const ASN1_AUX X509_REQ_aux = {
167 .flags = ASN1_AFLG_REFCOUNT,
168 .ref_offset = offsetof(X509_REQ, references),
169 .ref_lock = CRYPTO_LOCK_X509_REQ,
171 static const ASN1_TEMPLATE X509_REQ_seq_tt[] = {
173 .offset = offsetof(X509_REQ, req_info),
174 .field_name = "req_info",
175 .item = &X509_REQ_INFO_it,
178 .offset = offsetof(X509_REQ, sig_alg),
179 .field_name = "sig_alg",
180 .item = &X509_ALGOR_it,
183 .offset = offsetof(X509_REQ, signature),
184 .field_name = "signature",
185 .item = &ASN1_BIT_STRING_it,
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),
201 d2i_X509_REQ(X509_REQ **a, const unsigned char **in, long len)
203 return (X509_REQ *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
208 i2d_X509_REQ(X509_REQ *a, unsigned char **out)
210 return ASN1_item_i2d((ASN1_VALUE *)a, out, &X509_REQ_it);
216 return (X509_REQ *)ASN1_item_new(&X509_REQ_it);
220 X509_REQ_free(X509_REQ *a)
222 ASN1_item_free((ASN1_VALUE *)a, &X509_REQ_it);
226 X509_REQ_dup(X509_REQ *x)
228 return ASN1_item_dup(&X509_REQ_it, x);
232 X509_REQ_get_signature_nid(const X509_REQ *req)
234 return OBJ_obj2nid(req->sig_alg->algorithm);
238 X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig,
239 const X509_ALGOR **palg)
242 *psig = req->signature;
244 *palg = req->sig_alg;