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