Coverage Report

Created: 2026-02-14 06:34

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