| 1 |
// Copyright (c) 2017, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/pkcs7.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <limits.h> |
| 8 |
|
| 9 |
#include <openssl/bytestring.h> |
| 10 |
#include <openssl/err.h> |
| 11 |
#include <openssl/mem.h> |
| 12 |
#include <openssl/obj.h> |
| 13 |
#include <openssl/pem.h> |
| 14 |
#include <openssl/pool.h> |
| 15 |
#include <openssl/stack.h> |
| 16 |
#include <openssl/x509.h> |
| 17 |
|
| 18 |
#include "../internal.h" |
| 19 |
#include "internal.h" |
| 20 |
|
| 21 |
OPENSSL_BEGIN_ALLOW_DEPRECATED |
| 22 |
|
| 23 |
int PKCS7_get_certificates(STACK_OF(X509) *out_certs, CBS *cbs) { |
| 24 |
int ret = 0; |
| 25 |
const size_t initial_certs_len = sk_X509_num(out_certs); |
| 26 |
STACK_OF(CRYPTO_BUFFER) *raw = sk_CRYPTO_BUFFER_new_null(); |
| 27 |
if (raw == NULL || !PKCS7_get_raw_certificates(raw, cbs, NULL)) { |
| 28 |
goto err; |
| 29 |
} |
| 30 |
|
| 31 |
for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(raw); i++) { |
| 32 |
CRYPTO_BUFFER *buf = sk_CRYPTO_BUFFER_value(raw, i); |
| 33 |
X509 *x509 = X509_parse_from_buffer(buf); |
| 34 |
if (x509 == NULL || !sk_X509_push(out_certs, x509)) { |
| 35 |
X509_free(x509); |
| 36 |
goto err; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
ret = 1; |
| 41 |
|
| 42 |
err: |
| 43 |
sk_CRYPTO_BUFFER_pop_free(raw, CRYPTO_BUFFER_free); |
| 44 |
if (!ret) { |
| 45 |
while (sk_X509_num(out_certs) != initial_certs_len) { |
| 46 |
X509 *x509 = sk_X509_pop(out_certs); |
| 47 |
X509_free(x509); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return ret; |
| 52 |
} |
| 53 |
|
| 54 |
int PKCS7_get_CRLs(STACK_OF(X509_CRL) *out_crls, CBS *cbs) { |
| 55 |
CBS signed_data, crls; |
| 56 |
uint8_t *der_bytes = NULL; |
| 57 |
int ret = 0, has_crls; |
| 58 |
const size_t initial_crls_len = sk_X509_CRL_num(out_crls); |
| 59 |
|
| 60 |
// See https://tools.ietf.org/html/rfc2315#section-9.1 |
| 61 |
if (!pkcs7_parse_header(&der_bytes, &signed_data, cbs) || |
| 62 |
// Even if only CRLs are included, there may be an empty certificates |
| 63 |
// block. OpenSSL does this, for example. |
| 64 |
!CBS_get_optional_asn1( |
| 65 |
&signed_data, NULL, NULL, |
| 66 |
CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) || |
| 67 |
!CBS_get_optional_asn1( |
| 68 |
&signed_data, &crls, &has_crls, |
| 69 |
CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1)) { |
| 70 |
goto err; |
| 71 |
} |
| 72 |
|
| 73 |
if (!has_crls) { |
| 74 |
CBS_init(&crls, NULL, 0); |
| 75 |
} |
| 76 |
|
| 77 |
while (CBS_len(&crls) > 0) { |
| 78 |
CBS crl_data; |
| 79 |
X509_CRL *crl; |
| 80 |
const uint8_t *inp; |
| 81 |
|
| 82 |
if (!CBS_get_asn1_element(&crls, &crl_data, CBS_ASN1_SEQUENCE)) { |
| 83 |
goto err; |
| 84 |
} |
| 85 |
|
| 86 |
if (CBS_len(&crl_data) > LONG_MAX) { |
| 87 |
goto err; |
| 88 |
} |
| 89 |
inp = CBS_data(&crl_data); |
| 90 |
crl = d2i_X509_CRL(NULL, &inp, (long)CBS_len(&crl_data)); |
| 91 |
if (!crl) { |
| 92 |
goto err; |
| 93 |
} |
| 94 |
|
| 95 |
assert(inp == CBS_data(&crl_data) + CBS_len(&crl_data)); |
| 96 |
|
| 97 |
if (sk_X509_CRL_push(out_crls, crl) == 0) { |
| 98 |
X509_CRL_free(crl); |
| 99 |
goto err; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
ret = 1; |
| 104 |
|
| 105 |
err: |
| 106 |
OPENSSL_free(der_bytes); |
| 107 |
|
| 108 |
if (!ret) { |
| 109 |
while (sk_X509_CRL_num(out_crls) != initial_crls_len) { |
| 110 |
X509_CRL_free(sk_X509_CRL_pop(out_crls)); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return ret; |
| 115 |
} |
| 116 |
|
| 117 |
int PKCS7_get_PEM_certificates(STACK_OF(X509) *out_certs, BIO *pem_bio) { |
| 118 |
uint8_t *data; |
| 119 |
long len; |
| 120 |
int ret; |
| 121 |
|
| 122 |
// Even though we pass PEM_STRING_PKCS7 as the expected PEM type here, PEM |
| 123 |
// internally will actually allow several other values too, including |
| 124 |
// "CERTIFICATE". |
| 125 |
if (!PEM_bytes_read_bio(&data, &len, NULL /* PEM type output */, |
| 126 |
PEM_STRING_PKCS7, pem_bio, |
| 127 |
NULL /* password callback */, |
| 128 |
NULL /* password callback argument */)) { |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
CBS cbs; |
| 133 |
CBS_init(&cbs, data, len); |
| 134 |
ret = PKCS7_get_certificates(out_certs, &cbs); |
| 135 |
OPENSSL_free(data); |
| 136 |
return ret; |
| 137 |
} |
| 138 |
|
| 139 |
int PKCS7_get_PEM_CRLs(STACK_OF(X509_CRL) *out_crls, BIO *pem_bio) { |
| 140 |
uint8_t *data; |
| 141 |
long len; |
| 142 |
int ret; |
| 143 |
|
| 144 |
// Even though we pass PEM_STRING_PKCS7 as the expected PEM type here, PEM |
| 145 |
// internally will actually allow several other values too, including |
| 146 |
// "CERTIFICATE". |
| 147 |
if (!PEM_bytes_read_bio(&data, &len, NULL /* PEM type output */, |
| 148 |
PEM_STRING_PKCS7, pem_bio, |
| 149 |
NULL /* password callback */, |
| 150 |
NULL /* password callback argument */)) { |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
CBS cbs; |
| 155 |
CBS_init(&cbs, data, len); |
| 156 |
ret = PKCS7_get_CRLs(out_crls, &cbs); |
| 157 |
OPENSSL_free(data); |
| 158 |
return ret; |
| 159 |
} |
| 160 |
|
| 161 |
static int pkcs7_bundle_certificates_cb(CBB *out, const void *arg) { |
| 162 |
const STACK_OF(X509) *certs = arg; |
| 163 |
size_t i; |
| 164 |
CBB certificates; |
| 165 |
|
| 166 |
// See https://tools.ietf.org/html/rfc2315#section-9.1 |
| 167 |
if (!CBB_add_asn1(out, &certificates, |
| 168 |
CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) { |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
for (i = 0; i < sk_X509_num(certs); i++) { |
| 173 |
X509 *x509 = sk_X509_value(certs, i); |
| 174 |
uint8_t *buf; |
| 175 |
int len = i2d_X509(x509, NULL); |
| 176 |
|
| 177 |
if (len < 0 || !CBB_add_space(&certificates, &buf, len) || |
| 178 |
i2d_X509(x509, &buf) < 0) { |
| 179 |
return 0; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
// |certificates| is a implicitly-tagged SET OF. |
| 184 |
return CBB_flush_asn1_set_of(&certificates) && CBB_flush(out); |
| 185 |
} |
| 186 |
|
| 187 |
int PKCS7_bundle_certificates(CBB *out, const STACK_OF(X509) *certs) { |
| 188 |
return pkcs7_add_signed_data(out, /*digest_algos_cb=*/NULL, |
| 189 |
pkcs7_bundle_certificates_cb, |
| 190 |
/*signer_infos_cb=*/NULL, certs); |
| 191 |
} |
| 192 |
|
| 193 |
static int pkcs7_bundle_crls_cb(CBB *out, const void *arg) { |
| 194 |
const STACK_OF(X509_CRL) *crls = arg; |
| 195 |
size_t i; |
| 196 |
CBB crl_data; |
| 197 |
|
| 198 |
// See https://tools.ietf.org/html/rfc2315#section-9.1 |
| 199 |
if (!CBB_add_asn1(out, &crl_data, |
| 200 |
CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1)) { |
| 201 |
return 0; |
| 202 |
} |
| 203 |
|
| 204 |
for (i = 0; i < sk_X509_CRL_num(crls); i++) { |
| 205 |
X509_CRL *crl = sk_X509_CRL_value(crls, i); |
| 206 |
uint8_t *buf; |
| 207 |
int len = i2d_X509_CRL(crl, NULL); |
| 208 |
|
| 209 |
if (len < 0 || !CBB_add_space(&crl_data, &buf, len) || |
| 210 |
i2d_X509_CRL(crl, &buf) < 0) { |
| 211 |
return 0; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
// |crl_data| is a implicitly-tagged SET OF. |
| 216 |
return CBB_flush_asn1_set_of(&crl_data) && CBB_flush(out); |
| 217 |
} |
| 218 |
|
| 219 |
int PKCS7_bundle_CRLs(CBB *out, const STACK_OF(X509_CRL) *crls) { |
| 220 |
return pkcs7_add_signed_data(out, /*digest_algos_cb=*/NULL, |
| 221 |
pkcs7_bundle_crls_cb, |
| 222 |
/*signer_infos_cb=*/NULL, crls); |
| 223 |
} |
| 224 |
|
| 225 |
PKCS7 *d2i_PKCS7_bio(BIO *bio, PKCS7 **out) { |
| 226 |
GUARD_PTR(bio); |
| 227 |
uint8_t *data = NULL; |
| 228 |
size_t len; |
| 229 |
// Read BIO contents into newly allocated buffer |
| 230 |
if (!BIO_read_asn1(bio, &data, &len, INT_MAX)) { |
| 231 |
return NULL; |
| 232 |
} |
| 233 |
const uint8_t *ptr = data; |
| 234 |
// d2i_PKCS7 handles indefinite-length BER properly, so use it instead of |
| 235 |
// ASN1_item_d2i_bio |
| 236 |
PKCS7 *ret = d2i_PKCS7(out, &ptr, len); |
| 237 |
OPENSSL_free(data); |
| 238 |
return ret; |
| 239 |
} |
| 240 |
|
| 241 |
int i2d_PKCS7_bio(BIO *bio, const PKCS7 *p7) { |
| 242 |
return ASN1_item_i2d_bio(ASN1_ITEM_rptr(PKCS7), bio, (void *)p7); |
| 243 |
} |
| 244 |
|
| 245 |
int PKCS7_type_is_data(const PKCS7 *p7) { |
| 246 |
return OBJ_obj2nid(p7->type) == NID_pkcs7_data; |
| 247 |
} |
| 248 |
|
| 249 |
int PKCS7_type_is_digest(const PKCS7 *p7) { |
| 250 |
return OBJ_obj2nid(p7->type) == NID_pkcs7_digest; |
| 251 |
} |
| 252 |
|
| 253 |
int PKCS7_type_is_encrypted(const PKCS7 *p7) { |
| 254 |
return OBJ_obj2nid(p7->type) == NID_pkcs7_encrypted; |
| 255 |
} |
| 256 |
|
| 257 |
int PKCS7_type_is_enveloped(const PKCS7 *p7) { |
| 258 |
return OBJ_obj2nid(p7->type) == NID_pkcs7_enveloped; |
| 259 |
} |
| 260 |
|
| 261 |
int PKCS7_type_is_signed(const PKCS7 *p7) { |
| 262 |
return OBJ_obj2nid(p7->type) == NID_pkcs7_signed; |
| 263 |
} |
| 264 |
|
| 265 |
int PKCS7_type_is_signedAndEnveloped(const PKCS7 *p7) { |
| 266 |
return OBJ_obj2nid(p7->type) == NID_pkcs7_signedAndEnveloped; |
| 267 |
} |
| 268 |
|
| 269 |
// write_sha256_ai writes an AlgorithmIdentifier for SHA-256 to |
| 270 |
// |digest_algos_set|. |
| 271 |
static int write_sha256_ai(CBB *digest_algos_set, const void *arg) { |
| 272 |
CBB seq; |
| 273 |
return CBB_add_asn1(digest_algos_set, &seq, CBS_ASN1_SEQUENCE) && |
| 274 |
OBJ_nid2cbb(&seq, NID_sha256) && // |
| 275 |
// https://datatracker.ietf.org/doc/html/rfc5754#section-2 |
| 276 |
// "Implementations MUST generate SHA2 AlgorithmIdentifiers with absent |
| 277 |
// parameters." |
| 278 |
CBB_flush(digest_algos_set); |
| 279 |
} |
| 280 |
|
| 281 |
// sign_sha256 writes at most |max_out_sig| bytes of the signature of |data| by |
| 282 |
// |pkey| to |out_sig| and sets |*out_sig_len| to the number of bytes written. |
| 283 |
// It returns one on success or zero on error. |
| 284 |
static int sign_sha256(uint8_t *out_sig, size_t *out_sig_len, |
| 285 |
size_t max_out_sig, EVP_PKEY *pkey, BIO *data) { |
| 286 |
static const size_t kBufSize = 4096; |
| 287 |
uint8_t *buffer = OPENSSL_malloc(kBufSize); |
| 288 |
if (!buffer) { |
| 289 |
return 0; |
| 290 |
} |
| 291 |
|
| 292 |
EVP_MD_CTX ctx; |
| 293 |
EVP_MD_CTX_init(&ctx); |
| 294 |
|
| 295 |
int ret = 0; |
| 296 |
if (!EVP_DigestSignInit(&ctx, NULL, EVP_sha256(), NULL, pkey)) { |
| 297 |
goto out; |
| 298 |
} |
| 299 |
|
| 300 |
for (;;) { |
| 301 |
const int n = BIO_read(data, buffer, kBufSize); |
| 302 |
if (n == 0) { |
| 303 |
break; |
| 304 |
} else if (n < 0 || !EVP_DigestSignUpdate(&ctx, buffer, n)) { |
| 305 |
goto out; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
*out_sig_len = max_out_sig; |
| 310 |
if (!EVP_DigestSignFinal(&ctx, out_sig, out_sig_len)) { |
| 311 |
goto out; |
| 312 |
} |
| 313 |
|
| 314 |
ret = 1; |
| 315 |
|
| 316 |
out: |
| 317 |
EVP_MD_CTX_cleanup(&ctx); |
| 318 |
OPENSSL_free(buffer); |
| 319 |
return ret; |
| 320 |
} |
| 321 |
|
| 322 |
struct signer_info_data { |
| 323 |
const X509 *sign_cert; |
| 324 |
uint8_t *signature; |
| 325 |
size_t signature_len; |
| 326 |
}; |
| 327 |
|
| 328 |
// write_signer_info writes the SignerInfo structure from |
| 329 |
// https://datatracker.ietf.org/doc/html/rfc2315#section-9.2 to |out|. It |
| 330 |
// returns one on success or zero on error. |
| 331 |
static int write_signer_info(CBB *out, const void *arg) { |
| 332 |
const struct signer_info_data *const si_data = arg; |
| 333 |
|
| 334 |
int ret = 0; |
| 335 |
uint8_t *subject_bytes = NULL; |
| 336 |
uint8_t *serial_bytes = NULL; |
| 337 |
|
| 338 |
const int subject_len = |
| 339 |
i2d_X509_NAME(X509_get_subject_name(si_data->sign_cert), &subject_bytes); |
| 340 |
const int serial_len = i2d_ASN1_INTEGER( |
| 341 |
(ASN1_INTEGER *)X509_get0_serialNumber(si_data->sign_cert), |
| 342 |
&serial_bytes); |
| 343 |
|
| 344 |
CBB seq, issuer_and_serial, signing_algo, null, signature; |
| 345 |
if (subject_len < 0 || serial_len < 0 || |
| 346 |
!CBB_add_asn1(out, &seq, CBS_ASN1_SEQUENCE) || |
| 347 |
// version |
| 348 |
!CBB_add_asn1_uint64(&seq, 1) || |
| 349 |
!CBB_add_asn1(&seq, &issuer_and_serial, CBS_ASN1_SEQUENCE) || |
| 350 |
!CBB_add_bytes(&issuer_and_serial, subject_bytes, subject_len) || |
| 351 |
!CBB_add_bytes(&issuer_and_serial, serial_bytes, serial_len) || |
| 352 |
!write_sha256_ai(&seq, NULL) || |
| 353 |
!CBB_add_asn1(&seq, &signing_algo, CBS_ASN1_SEQUENCE) || |
| 354 |
!OBJ_nid2cbb(&signing_algo, NID_rsaEncryption) || |
| 355 |
!CBB_add_asn1(&signing_algo, &null, CBS_ASN1_NULL) || |
| 356 |
!CBB_add_asn1(&seq, &signature, CBS_ASN1_OCTETSTRING) || |
| 357 |
!CBB_add_bytes(&signature, si_data->signature, si_data->signature_len) || |
| 358 |
!CBB_flush(out)) { |
| 359 |
goto out; |
| 360 |
} |
| 361 |
|
| 362 |
ret = 1; |
| 363 |
|
| 364 |
out: |
| 365 |
OPENSSL_free(subject_bytes); |
| 366 |
OPENSSL_free(serial_bytes); |
| 367 |
return ret; |
| 368 |
} |
| 369 |
|
| 370 |
static int pkcs7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey) { |
| 371 |
// OpenSSL's docs say that this defaults to SHA1, but appears to actually |
| 372 |
// default to SHA256 in 1.1.x and 3.x for RSA, DSA, and EC(DSA). |
| 373 |
// https://linux.die.net/man/3/pkcs7_sign |
| 374 |
// https://github.com/openssl/openssl/blob/79c98fc6ccab49f02528e06cc046ac61f841a753/crypto/rsa/rsa_ameth.c#L438 |
| 375 |
const EVP_MD *digest = EVP_sha256(); |
| 376 |
PKCS7_SIGNER_INFO *si = NULL; |
| 377 |
|
| 378 |
switch (EVP_PKEY_id(pkey)) { |
| 379 |
case EVP_PKEY_RSA: |
| 380 |
case EVP_PKEY_DSA: |
| 381 |
case EVP_PKEY_EC: |
| 382 |
break; |
| 383 |
default: |
| 384 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_NO_DEFAULT_DIGEST); |
| 385 |
goto err; |
| 386 |
} |
| 387 |
|
| 388 |
// We add the signer's info below, including the static |digest|. We delegate |
| 389 |
// initialization of the |digest| into an |EVP_MD_CTX| to |BIO_f_md|. |
| 390 |
if ((si = PKCS7_SIGNER_INFO_new()) == NULL || |
| 391 |
!PKCS7_SIGNER_INFO_set(si, x509, pkey, digest) || |
| 392 |
!PKCS7_add_signer(p7, si)) { // |p7| takes ownership of |si| here |
| 393 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_PKCS7_DATASIGN); |
| 394 |
goto err; |
| 395 |
} |
| 396 |
return 1; |
| 397 |
err: |
| 398 |
PKCS7_SIGNER_INFO_free(si); |
| 399 |
return 0; |
| 400 |
} |
| 401 |
|
| 402 |
static int pkcs7_sign_add_signer(PKCS7 *p7, X509 *signcert, EVP_PKEY *pkey) { |
| 403 |
if (!X509_check_private_key(signcert, pkey)) { |
| 404 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); |
| 405 |
return 0; |
| 406 |
} |
| 407 |
|
| 408 |
if (!pkcs7_add_signature(p7, signcert, pkey)) { |
| 409 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_PKCS7_ADD_SIGNATURE_ERROR); |
| 410 |
return 0; |
| 411 |
} |
| 412 |
|
| 413 |
if (!PKCS7_add_certificate(p7, signcert)) { |
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
return 1; |
| 418 |
} |
| 419 |
|
| 420 |
static PKCS7 *pkcs7_do_general_sign(X509 *sign_cert, EVP_PKEY *pkey, |
| 421 |
struct stack_st_X509 *certs, BIO *data, |
| 422 |
int flags) { |
| 423 |
PKCS7 *ret = NULL; |
| 424 |
if ((ret = PKCS7_new()) == NULL || !PKCS7_set_type(ret, NID_pkcs7_signed) || |
| 425 |
!PKCS7_content_new(ret, NID_pkcs7_data)) { |
| 426 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PKCS7_LIB); |
| 427 |
goto err; |
| 428 |
} |
| 429 |
|
| 430 |
if (!pkcs7_sign_add_signer(ret, sign_cert, pkey)) { |
| 431 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_PKCS7_ADD_SIGNER_ERROR); |
| 432 |
goto err; |
| 433 |
} |
| 434 |
|
| 435 |
for (size_t i = 0; i < sk_X509_num(certs); i++) { |
| 436 |
if (!PKCS7_add_certificate(ret, sk_X509_value(certs, i))) { |
| 437 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_PKCS7_ADD_SIGNER_ERROR); |
| 438 |
goto err; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
if ((flags & PKCS7_DETACHED) && PKCS7_type_is_data(ret->d.sign->contents)) { |
| 443 |
ASN1_OCTET_STRING_free(ret->d.sign->contents->d.data); |
| 444 |
ret->d.sign->contents->d.data = NULL; |
| 445 |
} |
| 446 |
|
| 447 |
if (!pkcs7_final(ret, data)) { |
| 448 |
goto err; |
| 449 |
} |
| 450 |
|
| 451 |
return ret; |
| 452 |
err: |
| 453 |
PKCS7_free(ret); |
| 454 |
return NULL; |
| 455 |
} |
| 456 |
|
| 457 |
PKCS7 *PKCS7_sign(X509 *sign_cert, EVP_PKEY *pkey, STACK_OF(X509) *certs, |
| 458 |
BIO *data, int flags) { |
| 459 |
CBB cbb; |
| 460 |
if (!CBB_init(&cbb, 2048)) { |
| 461 |
return NULL; |
| 462 |
} |
| 463 |
|
| 464 |
uint8_t *der = NULL; |
| 465 |
size_t len; |
| 466 |
PKCS7 *ret = NULL; |
| 467 |
|
| 468 |
if (sign_cert == NULL && pkey == NULL && flags == PKCS7_DETACHED) { |
| 469 |
// Caller just wants to bundle certificates. |
| 470 |
if (!PKCS7_bundle_certificates(&cbb, certs)) { |
| 471 |
goto out; |
| 472 |
} |
| 473 |
} else if (sign_cert != NULL && pkey != NULL && certs == NULL && |
| 474 |
data != NULL && |
| 475 |
flags == (PKCS7_NOATTR | PKCS7_BINARY | PKCS7_NOCERTS | |
| 476 |
PKCS7_DETACHED) && |
| 477 |
EVP_PKEY_id(pkey) == NID_rsaEncryption) { |
| 478 |
// sign-file.c from the Linux kernel. |
| 479 |
const size_t signature_max_len = EVP_PKEY_size(pkey); |
| 480 |
struct signer_info_data si_data = { |
| 481 |
.sign_cert = sign_cert, |
| 482 |
.signature = OPENSSL_malloc(signature_max_len), |
| 483 |
}; |
| 484 |
|
| 485 |
if (!si_data.signature || |
| 486 |
!sign_sha256(si_data.signature, &si_data.signature_len, |
| 487 |
signature_max_len, pkey, data) || |
| 488 |
!pkcs7_add_signed_data(&cbb, write_sha256_ai, /*cert_crl_cb=*/NULL, |
| 489 |
write_signer_info, &si_data)) { |
| 490 |
OPENSSL_free(si_data.signature); |
| 491 |
goto out; |
| 492 |
} |
| 493 |
OPENSSL_free(si_data.signature); |
| 494 |
} else if (sign_cert != NULL && pkey != NULL && data != NULL && |
| 495 |
!(flags & PKCS7_NOCERTS)) { |
| 496 |
ret = pkcs7_do_general_sign(sign_cert, pkey, certs, data, flags); |
| 497 |
goto out; |
| 498 |
} else { |
| 499 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 500 |
goto out; |
| 501 |
} |
| 502 |
|
| 503 |
if (!CBB_finish(&cbb, &der, &len)) { |
| 504 |
goto out; |
| 505 |
} |
| 506 |
|
| 507 |
const uint8_t *const_der = der; |
| 508 |
ret = d2i_PKCS7(NULL, &const_der, len); |
| 509 |
|
| 510 |
out: |
| 511 |
CBB_cleanup(&cbb); |
| 512 |
OPENSSL_free(der); |
| 513 |
return ret; |
| 514 |
} |
| 515 |
|
| 516 |
int PKCS7_add_certificate(PKCS7 *p7, X509 *x509) { |
| 517 |
STACK_OF(X509) **sk; |
| 518 |
|
| 519 |
if (p7 == NULL || x509 == NULL) { |
| 520 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); |
| 521 |
return 0; |
| 522 |
} |
| 523 |
|
| 524 |
switch (OBJ_obj2nid(p7->type)) { |
| 525 |
case NID_pkcs7_signed: |
| 526 |
sk = &(p7->d.sign->cert); |
| 527 |
break; |
| 528 |
case NID_pkcs7_signedAndEnveloped: |
| 529 |
sk = &(p7->d.signed_and_enveloped->cert); |
| 530 |
break; |
| 531 |
default: |
| 532 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_WRONG_CONTENT_TYPE); |
| 533 |
return 0; |
| 534 |
} |
| 535 |
|
| 536 |
if (*sk == NULL) { |
| 537 |
*sk = sk_X509_new_null(); |
| 538 |
} |
| 539 |
if (*sk == NULL) { |
| 540 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_CRYPTO_LIB); |
| 541 |
return 0; |
| 542 |
} |
| 543 |
|
| 544 |
if (!sk_X509_push(*sk, x509)) { |
| 545 |
return 0; |
| 546 |
} |
| 547 |
X509_up_ref(x509); |
| 548 |
return 1; |
| 549 |
} |
| 550 |
|
| 551 |
int PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl) { |
| 552 |
STACK_OF(X509_CRL) **sk; |
| 553 |
|
| 554 |
if (p7 == NULL || crl == NULL) { |
| 555 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_PASSED_NULL_PARAMETER); |
| 556 |
return 0; |
| 557 |
} |
| 558 |
|
| 559 |
switch (OBJ_obj2nid(p7->type)) { |
| 560 |
case NID_pkcs7_signed: |
| 561 |
sk = &(p7->d.sign->crl); |
| 562 |
break; |
| 563 |
case NID_pkcs7_signedAndEnveloped: |
| 564 |
sk = &(p7->d.signed_and_enveloped->crl); |
| 565 |
break; |
| 566 |
default: |
| 567 |
OPENSSL_PUT_ERROR(PKCS7, PKCS7_R_WRONG_CONTENT_TYPE); |
| 568 |
return 0; |
| 569 |
} |
| 570 |
|
| 571 |
if (*sk == NULL) { |
| 572 |
*sk = sk_X509_CRL_new_null(); |
| 573 |
} |
| 574 |
if (*sk == NULL) { |
| 575 |
OPENSSL_PUT_ERROR(PKCS7, ERR_R_CRYPTO_LIB); |
| 576 |
return 0; |
| 577 |
} |
| 578 |
|
| 579 |
if (!sk_X509_CRL_push(*sk, crl)) { |
| 580 |
return 0; |
| 581 |
} |
| 582 |
X509_CRL_up_ref(crl); |
| 583 |
return 1; |
| 584 |
} |
| 585 |
|
| 586 |
OPENSSL_END_ALLOW_DEPRECATED |