1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdio.h>
16
17 #include <openssl/asn1t.h>
18 #include <openssl/x509.h>
19
20 #include "internal.h"
21
22
23 // X509_REQ_INFO is handled in an unusual way to get round invalid encodings.
24 // Some broken certificate requests don't encode the attributes field if it
25 // is empty. This is in violation of PKCS#10 but we need to tolerate it. We
26 // do this by making the attributes field OPTIONAL then using the callback to
27 // initialise it to an empty STACK. This means that the field will be
28 // correctly encoded unless we NULL out the field.
29
rinf_cb(int operation,ASN1_VALUE ** pval,const ASN1_ITEM * it,void * exarg)30 static int rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
31 void *exarg) {
32 X509_REQ_INFO *rinf = (X509_REQ_INFO *)*pval;
33
34 if (operation == ASN1_OP_NEW_POST) {
35 rinf->attributes = sk_X509_ATTRIBUTE_new_null();
36 if (!rinf->attributes) {
37 return 0;
38 }
39 }
40
41 if (operation == ASN1_OP_D2I_POST) {
42 // The only defined CSR version is v1(0). For compatibility, we also accept
43 // a hypothetical v3(2). Although not defined, older versions of certbot
44 // use it. See https://github.com/certbot/certbot/pull/9334.
45 long version = ASN1_INTEGER_get(rinf->version);
46 if (version != X509_REQ_VERSION_1 && version != 2) {
47 OPENSSL_PUT_ERROR(X509, X509_R_INVALID_VERSION);
48 return 0;
49 }
50 }
51
52 return 1;
53 }
54
55 ASN1_SEQUENCE_enc(X509_REQ_INFO, enc, rinf_cb) = {
56 ASN1_SIMPLE(X509_REQ_INFO, version, ASN1_INTEGER),
57 ASN1_SIMPLE(X509_REQ_INFO, subject, X509_NAME),
58 ASN1_SIMPLE(X509_REQ_INFO, pubkey, X509_PUBKEY),
59 // This isn't really OPTIONAL but it gets around invalid encodings.
60 ASN1_IMP_SET_OF_OPT(X509_REQ_INFO, attributes, X509_ATTRIBUTE, 0),
61 } ASN1_SEQUENCE_END_enc(X509_REQ_INFO, X509_REQ_INFO)
62
63 IMPLEMENT_ASN1_FUNCTIONS(X509_REQ_INFO)
64
65 ASN1_SEQUENCE(X509_REQ) = {
66 ASN1_SIMPLE(X509_REQ, req_info, X509_REQ_INFO),
67 ASN1_SIMPLE(X509_REQ, sig_alg, X509_ALGOR),
68 ASN1_SIMPLE(X509_REQ, signature, ASN1_BIT_STRING),
69 } ASN1_SEQUENCE_END(X509_REQ)
70
71 IMPLEMENT_ASN1_FUNCTIONS(X509_REQ)
72
73 IMPLEMENT_ASN1_DUP_FUNCTION(X509_REQ)
74