2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/x509.h>
14 #include "crypto/x509.h"
16 DEFINE_STACK_OF(X509_ATTRIBUTE)
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.
26 * This means that the field will be correctly encoded unless
27 * we NULL out the field.
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.
37 static int rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
40 X509_REQ_INFO *rinf = (X509_REQ_INFO *)*pval;
42 if (operation == ASN1_OP_NEW_POST) {
43 rinf->attributes = sk_X509_ATTRIBUTE_new_null();
44 if (!rinf->attributes)
50 static int req_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
53 #ifndef OPENSSL_NO_SM2
54 X509_REQ *ret = (X509_REQ *)*pval;
58 ASN1_OCTET_STRING_free(ret->distinguishing_id);
60 case ASN1_OP_NEW_POST:
61 ret->distinguishing_id = NULL;
64 case ASN1_OP_FREE_POST:
65 ASN1_OCTET_STRING_free(ret->distinguishing_id);
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
80 ASN1_IMP_SET_OF_OPT(X509_REQ_INFO, attributes, X509_ATTRIBUTE, 0)
81 } ASN1_SEQUENCE_END_enc(X509_REQ_INFO, X509_REQ_INFO)
83 IMPLEMENT_ASN1_FUNCTIONS(X509_REQ_INFO)
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)
91 IMPLEMENT_ASN1_FUNCTIONS(X509_REQ)
93 IMPLEMENT_ASN1_DUP_FUNCTION(X509_REQ)
95 void X509_REQ_set0_distinguishing_id(X509_REQ *x, ASN1_OCTET_STRING *d_id)
97 ASN1_OCTET_STRING_free(x->distinguishing_id);
98 x->distinguishing_id = d_id;
101 ASN1_OCTET_STRING *X509_REQ_get0_distinguishing_id(X509_REQ *x)
103 return x->distinguishing_id;