Coverage Report

Created: 2026-10-27 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/x509/x509_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 <openssl/asn1.h>
16
#include <openssl/asn1t.h>
17
#include <openssl/bn.h>
18
#include <openssl/err.h>
19
#include <openssl/evp.h>
20
#include <openssl/mem.h>
21
#include <openssl/obj.h>
22
#include <openssl/pem.h>
23
#include <openssl/x509.h>
24
25
#include "../asn1/internal.h"
26
#include "internal.h"
27
28
29
using namespace bssl;
30
31
namespace {
32
33
0
int X509_REQ_add0_attr(X509_REQ *req, X509_ATTRIBUTE *attr) {
34
0
  if (req->req_info->attributes == nullptr) {
35
0
    req->req_info->attributes = sk_X509_ATTRIBUTE_new_null();
36
0
  }
37
0
  if (req->req_info->attributes == nullptr ||
38
0
      !sk_X509_ATTRIBUTE_push(req->req_info->attributes, attr)) {
39
0
    return 0;
40
0
  }
41
42
0
  return 1;
43
0
}
44
45
}  // namespace
46
47
0
long X509_REQ_get_version(const X509_REQ *req) {
48
0
  return ASN1_INTEGER_get(req->req_info->version);
49
0
}
50
51
0
X509_NAME *X509_REQ_get_subject_name(const X509_REQ *req) {
52
0
  return req->req_info->subject;
53
0
}
54
55
0
EVP_PKEY *X509_REQ_get_pubkey(const X509_REQ *req) {
56
0
  if (req == nullptr) {
57
0
    return nullptr;
58
0
  }
59
0
  return X509_PUBKEY_get(req->req_info->pubkey);
60
0
}
61
62
0
EVP_PKEY *X509_REQ_get0_pubkey(const X509_REQ *req) {
63
0
  if (req == nullptr) {
64
0
    return nullptr;
65
0
  }
66
0
  return X509_PUBKEY_get0(req->req_info->pubkey);
67
0
}
68
69
0
int X509_REQ_check_private_key(const X509_REQ *x, const EVP_PKEY *k) {
70
0
  const EVP_PKEY *xk = X509_REQ_get0_pubkey(x);
71
0
  if (xk == nullptr) {
72
0
    return 0;
73
0
  }
74
75
0
  if (EVP_PKEY_eq(xk, k) == 1) {
76
0
    return 1;
77
0
  }
78
79
0
  if (EVP_PKEY_id(xk) != EVP_PKEY_id(k)) {
80
0
    OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH);
81
0
  } else {
82
0
    OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH);
83
0
  }
84
0
  return 0;
85
0
}
86
87
0
int X509_REQ_extension_nid(int req_nid) {
88
0
  return req_nid == NID_ext_req || req_nid == NID_ms_ext_req;
89
0
}
90
91
0
STACK_OF(X509_EXTENSION) *X509_REQ_get_extensions(const X509_REQ *req) {
92
0
  if (req == nullptr || req->req_info == nullptr) {
93
0
    return nullptr;
94
0
  }
95
96
0
  int idx = X509_REQ_get_attr_by_NID(req, NID_ext_req, -1);
97
0
  if (idx == -1) {
98
0
    idx = X509_REQ_get_attr_by_NID(req, NID_ms_ext_req, -1);
99
0
  }
100
0
  if (idx == -1) {
101
0
    return nullptr;
102
0
  }
103
104
0
  const X509_ATTRIBUTE *attr = X509_REQ_get_attr(req, idx);
105
  // TODO(davidben): `X509_ATTRIBUTE_get0_type` is not const-correct. It should
106
  // take and return a const pointer.
107
0
  const ASN1_TYPE *ext = X509_ATTRIBUTE_get0_type((X509_ATTRIBUTE *)attr, 0);
108
0
  if (!ext || ext->type != V_ASN1_SEQUENCE) {
109
0
    return nullptr;
110
0
  }
111
0
  const unsigned char *p = ext->value.sequence->data;
112
0
  return (STACK_OF(X509_EXTENSION) *)ASN1_item_d2i(
113
0
      nullptr, &p, ext->value.sequence->length,
114
0
      ASN1_ITEM_rptr(X509_EXTENSIONS));
115
0
}
116
117
// Add a STACK_OF extensions to a certificate request: allow alternative OIDs
118
// in case we want to create a non standard one.
119
120
int X509_REQ_add_extensions_nid(X509_REQ *req,
121
0
                                const STACK_OF(X509_EXTENSION) *exts, int nid) {
122
  // Generate encoding of extensions
123
0
  unsigned char *ext = nullptr;
124
0
  int ext_len =
125
0
      ASN1_item_i2d((ASN1_VALUE *)exts, &ext, ASN1_ITEM_rptr(X509_EXTENSIONS));
126
0
  if (ext_len <= 0) {
127
0
    return 0;
128
0
  }
129
0
  int ret = X509_REQ_add1_attr_by_NID(req, nid, V_ASN1_SEQUENCE, ext, ext_len);
130
0
  OPENSSL_free(ext);
131
0
  return ret;
132
0
}
133
134
// This is the normal usage: use the "official" OID
135
int X509_REQ_add_extensions(X509_REQ *req,
136
0
                            const STACK_OF(X509_EXTENSION) *exts) {
137
0
  return X509_REQ_add_extensions_nid(req, exts, NID_ext_req);
138
0
}
139
140
0
int X509_REQ_get_attr_count(const X509_REQ *req) {
141
0
  return (int)sk_X509_ATTRIBUTE_num(req->req_info->attributes);
142
0
}
143
144
0
int X509_REQ_get_attr_by_NID(const X509_REQ *req, int nid, int lastpos) {
145
0
  const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
146
0
  if (obj == nullptr) {
147
0
    return -1;
148
0
  }
149
0
  return X509_REQ_get_attr_by_OBJ(req, obj, lastpos);
150
0
}
151
152
int X509_REQ_get_attr_by_OBJ(const X509_REQ *req, const ASN1_OBJECT *obj,
153
0
                             int lastpos) {
154
0
  if (req->req_info->attributes == nullptr) {
155
0
    return -1;
156
0
  }
157
0
  lastpos++;
158
0
  if (lastpos < 0) {
159
0
    lastpos = 0;
160
0
  }
161
0
  int n = (int)sk_X509_ATTRIBUTE_num(req->req_info->attributes);
162
0
  for (; lastpos < n; lastpos++) {
163
0
    const X509_ATTRIBUTE *attr =
164
0
        sk_X509_ATTRIBUTE_value(req->req_info->attributes, lastpos);
165
0
    if (OBJ_cmp(attr->object, obj) == 0) {
166
0
      return lastpos;
167
0
    }
168
0
  }
169
0
  return -1;
170
0
}
171
172
0
X509_ATTRIBUTE *X509_REQ_get_attr(const X509_REQ *req, int loc) {
173
0
  if (req->req_info->attributes == nullptr || loc < 0 ||
174
0
      sk_X509_ATTRIBUTE_num(req->req_info->attributes) <= (size_t)loc) {
175
0
    return nullptr;
176
0
  }
177
0
  return sk_X509_ATTRIBUTE_value(req->req_info->attributes, loc);
178
0
}
179
180
0
X509_ATTRIBUTE *X509_REQ_delete_attr(X509_REQ *req, int loc) {
181
0
  if (req->req_info->attributes == nullptr || loc < 0 ||
182
0
      sk_X509_ATTRIBUTE_num(req->req_info->attributes) <= (size_t)loc) {
183
0
    return nullptr;
184
0
  }
185
0
  return sk_X509_ATTRIBUTE_delete(req->req_info->attributes, loc);
186
0
}
187
188
0
int X509_REQ_add1_attr(X509_REQ *req, const X509_ATTRIBUTE *attr) {
189
0
  X509_ATTRIBUTE *new_attr = X509_ATTRIBUTE_dup(attr);
190
0
  if (new_attr == nullptr || !X509_REQ_add0_attr(req, new_attr)) {
191
0
    X509_ATTRIBUTE_free(new_attr);
192
0
    return 0;
193
0
  }
194
195
0
  return 1;
196
0
}
197
198
int X509_REQ_add1_attr_by_OBJ(X509_REQ *req, const ASN1_OBJECT *obj,
199
                              int attrtype, const unsigned char *data,
200
0
                              int len) {
201
0
  X509_ATTRIBUTE *attr =
202
0
      X509_ATTRIBUTE_create_by_OBJ(nullptr, obj, attrtype, data, len);
203
0
  if (attr == nullptr || !X509_REQ_add0_attr(req, attr)) {
204
0
    X509_ATTRIBUTE_free(attr);
205
0
    return 0;
206
0
  }
207
208
0
  return 1;
209
0
}
210
211
int X509_REQ_add1_attr_by_NID(X509_REQ *req, int nid, int attrtype,
212
0
                              const unsigned char *data, int len) {
213
0
  X509_ATTRIBUTE *attr =
214
0
      X509_ATTRIBUTE_create_by_NID(nullptr, nid, attrtype, data, len);
215
0
  if (attr == nullptr || !X509_REQ_add0_attr(req, attr)) {
216
0
    X509_ATTRIBUTE_free(attr);
217
0
    return 0;
218
0
  }
219
220
0
  return 1;
221
0
}
222
223
int X509_REQ_add1_attr_by_txt(X509_REQ *req, const char *attrname, int attrtype,
224
0
                              const unsigned char *data, int len) {
225
0
  X509_ATTRIBUTE *attr =
226
0
      X509_ATTRIBUTE_create_by_txt(nullptr, attrname, attrtype, data, len);
227
0
  if (attr == nullptr || !X509_REQ_add0_attr(req, attr)) {
228
0
    X509_ATTRIBUTE_free(attr);
229
0
    return 0;
230
0
  }
231
232
0
  return 1;
233
0
}
234
235
void X509_REQ_get0_signature(const X509_REQ *req, const ASN1_BIT_STRING **psig,
236
0
                             const X509_ALGOR **palg) {
237
0
  if (psig != nullptr) {
238
0
    *psig = req->signature;
239
0
  }
240
0
  if (palg != nullptr) {
241
0
    *palg = req->sig_alg;
242
0
  }
243
0
}
244
245
0
int X509_REQ_get_signature_nid(const X509_REQ *req) {
246
0
  return OBJ_obj2nid(req->sig_alg->algorithm);
247
0
}
248
249
0
int i2d_re_X509_REQ_tbs(X509_REQ *req, unsigned char **pp) {
250
0
  asn1_encoding_clear(&req->req_info->enc);
251
0
  return i2d_X509_REQ_INFO(req->req_info, pp);
252
0
}