Coverage Report

Created: 2026-08-08 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/x509/x_req.cc
Line
Count
Source
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 "../asn1/internal.h"
21
#include "internal.h"
22
23
24
using namespace bssl;
25
26
// X509_REQ_INFO is handled in an unusual way to get round invalid encodings.
27
// Some broken certificate requests don't encode the attributes field if it
28
// is empty. This is in violation of PKCS#10 but we need to tolerate it. We
29
// do this by making the attributes field OPTIONAL then using the callback to
30
// initialise it to an empty STACK. This means that the field will be
31
// correctly encoded unless we NULL out the field.
32
33
static int rinf_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
34
0
                   void *exarg) {
35
0
  X509_REQ_INFO *rinf = asn1_load_ptr_as<X509_REQ_INFO>(pval);
36
37
0
  if (operation == ASN1_OP_NEW_POST) {
38
0
    rinf->attributes = sk_X509_ATTRIBUTE_new_null();
39
0
    if (!rinf->attributes) {
40
0
      return 0;
41
0
    }
42
0
  }
43
44
0
  if (operation == ASN1_OP_D2I_POST) {
45
    // The only defined CSR version is v1(0). For compatibility, we also accept
46
    // a hypothetical v3(2). Although not defined, older versions of certbot
47
    // use it. See https://github.com/certbot/certbot/pull/9334.
48
0
    long version = ASN1_INTEGER_get(rinf->version);
49
0
    if (version != X509_REQ_VERSION_1 && version != 2) {
50
0
      OPENSSL_PUT_ERROR(X509, X509_R_INVALID_VERSION);
51
0
      return 0;
52
0
    }
53
0
  }
54
55
0
  return 1;
56
0
}
57
58
BSSL_NAMESPACE_BEGIN
59
60
ASN1_SEQUENCE_enc(X509_REQ_INFO, enc, rinf_cb) = {
61
    ASN1_SIMPLE(X509_REQ_INFO, version, ASN1_INTEGER),
62
    ASN1_SIMPLE(X509_REQ_INFO, subject, X509_NAME),
63
    ASN1_SIMPLE(X509_REQ_INFO, pubkey, X509_PUBKEY),
64
    // This isn't really OPTIONAL but it gets around invalid encodings.
65
    ASN1_IMP_SET_OF_OPT(X509_REQ_INFO, attributes, X509_ATTRIBUTE, 0),
66
} ASN1_SEQUENCE_END_enc(X509_REQ_INFO, X509_REQ_INFO)
67
68
IMPLEMENT_ASN1_FUNCTIONS_const(X509_REQ_INFO)
69
70
ASN1_SEQUENCE(X509_REQ) = {
71
    ASN1_SIMPLE(X509_REQ, req_info, bssl::X509_REQ_INFO),
72
    ASN1_SIMPLE(X509_REQ, sig_alg, X509_ALGOR),
73
    ASN1_SIMPLE(X509_REQ, signature, ASN1_BIT_STRING),
74
} ASN1_SEQUENCE_END(X509_REQ)
75
76
BSSL_NAMESPACE_END
77
78
IMPLEMENT_ASN1_FUNCTIONS_const(X509_REQ)
79
80
IMPLEMENT_ASN1_DUP_FUNCTION_const(X509_REQ)