In OpenSSL builds, declare STACK for datatypes ...
[openssl.git] / crypto / x509 / x_req.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/x509.h>
14 #include "crypto/x509.h"
15
16 DEFINE_STACK_OF(X509_ATTRIBUTE)
17
18 /*-
19  * X509_REQ_INFO is handled in an unusual way to get round
20  * invalid encodings. Some broken certificate requests don't
21  * encode the attributes field if it is empty. This is in
22  * violation of PKCS#10 but we need to tolerate it. We do
23  * this by making the attributes field OPTIONAL then using
24  * the callback to initialise it to an empty STACK.
25  *
26  * This means that the field will be correctly encoded unless
27  * we NULL out the field.
28  *
29  * As a result we no longer need the req_kludge field because
30  * the information is now contained in the attributes field:
31  * 1. If it is NULL then it's the invalid omission.
32  * 2. If it is empty it is the correct encoding.
33  * 3. If it is not empty then some attributes are present.
34  *
35  */
36
37 static int rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
38                    void *exarg)
39 {
40     X509_REQ_INFO *rinf = (X509_REQ_INFO *)*pval;
41
42     if (operation == ASN1_OP_NEW_POST) {
43         rinf->attributes = sk_X509_ATTRIBUTE_new_null();
44         if (!rinf->attributes)
45             return 0;
46     }
47     return 1;
48 }
49
50 static int req_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
51                   void *exarg)
52 {
53 #ifndef OPENSSL_NO_SM2
54     X509_REQ *ret = (X509_REQ *)*pval;
55
56     switch (operation) {
57     case ASN1_OP_D2I_PRE:
58         ASN1_OCTET_STRING_free(ret->distinguishing_id);
59         /* fall thru */
60     case ASN1_OP_NEW_POST:
61         ret->distinguishing_id = NULL;
62         break;
63
64     case ASN1_OP_FREE_POST:
65         ASN1_OCTET_STRING_free(ret->distinguishing_id);
66         break;
67     }
68 #endif
69
70     return 1;
71 }
72
73 ASN1_SEQUENCE_enc(X509_REQ_INFO, enc, rinf_cb) = {
74         ASN1_SIMPLE(X509_REQ_INFO, version, ASN1_INTEGER),
75         ASN1_SIMPLE(X509_REQ_INFO, subject, X509_NAME),
76         ASN1_SIMPLE(X509_REQ_INFO, pubkey, X509_PUBKEY),
77         /* This isn't really OPTIONAL but it gets round invalid
78          * encodings
79          */
80         ASN1_IMP_SET_OF_OPT(X509_REQ_INFO, attributes, X509_ATTRIBUTE, 0)
81 } ASN1_SEQUENCE_END_enc(X509_REQ_INFO, X509_REQ_INFO)
82
83 IMPLEMENT_ASN1_FUNCTIONS(X509_REQ_INFO)
84
85 ASN1_SEQUENCE_ref(X509_REQ, req_cb) = {
86         ASN1_EMBED(X509_REQ, req_info, X509_REQ_INFO),
87         ASN1_EMBED(X509_REQ, sig_alg, X509_ALGOR),
88         ASN1_SIMPLE(X509_REQ, signature, ASN1_BIT_STRING)
89 } ASN1_SEQUENCE_END_ref(X509_REQ, X509_REQ)
90
91 IMPLEMENT_ASN1_FUNCTIONS(X509_REQ)
92
93 IMPLEMENT_ASN1_DUP_FUNCTION(X509_REQ)
94
95 void X509_REQ_set0_distinguishing_id(X509_REQ *x, ASN1_OCTET_STRING *d_id)
96 {
97     ASN1_OCTET_STRING_free(x->distinguishing_id);
98     x->distinguishing_id = d_id;
99 }
100
101 ASN1_OCTET_STRING *X509_REQ_get0_distinguishing_id(X509_REQ *x)
102 {
103     return x->distinguishing_id;
104 }