| 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/pkcs7.h> |
| 16 |
| 17 | #include <assert.h> |
| 18 | #include <limits.h> |
| 19 |
| 20 | #include <openssl/bytestring.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/mem.h> |
| 23 | #include <openssl/obj.h> |
| 24 | #include <openssl/pem.h> |
| 25 | #include <openssl/pool.h> |
| 26 | #include <openssl/stack.h> |
| 27 | #include <openssl/x509.h> |
| 28 |
| 29 | #include "../internal.h" |
| 30 | #include "internal.h" |
| 31 |
| 32 |
| 33 | int PKCS7_get_certificates(STACK_OF(X509) *out_certs, CBS *cbs) { |
| 34 | int ret = 0; |
| 35 | const size_t initial_certs_len = sk_X509_num(out_certs); |
| 36 | STACK_OF(CRYPTO_BUFFER) *raw = sk_CRYPTO_BUFFER_new_null(); |
| 37 | if (raw == NULL || !PKCS7_get_raw_certificates(raw, cbs, NULL)) { |
| 38 | goto err; |
| 39 | } |
| 40 |
| 41 | for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(raw); i++) { |
| 42 | CRYPTO_BUFFER *buf = sk_CRYPTO_BUFFER_value(raw, i); |
| 43 | X509 *x509 = X509_parse_from_buffer(buf); |
| 44 | if (x509 == NULL || !sk_X509_push(out_certs, x509)) { |
| 45 | X509_free(x509); |
| 46 | goto err; |
| 47 | } |
| 48 | } |
| 49 |
| 50 | ret = 1; |
| 51 |
| 52 | err: |
| 53 | sk_CRYPTO_BUFFER_pop_free(raw, CRYPTO_BUFFER_free); |
| 54 | if (!ret) { |
| 55 | while (sk_X509_num(out_certs) != initial_certs_len) { |
| 56 | X509 *x509 = sk_X509_pop(out_certs); |
| 57 | X509_free(x509); |
| 58 | } |
| 59 | } |
| 60 |
| 61 | return ret; |
| 62 | } |
| 63 |
| 64 | int PKCS7_get_CRLs(STACK_OF(X509_CRL) *out_crls, CBS *cbs) { |
| 65 | CBS signed_data, crls; |
| 66 | uint8_t *der_bytes = NULL; |
| 67 | int ret = 0, has_crls; |
| 68 | const size_t initial_crls_len = sk_X509_CRL_num(out_crls); |
| 69 |
| 70 | // See https://tools.ietf.org/html/rfc2315#section-9.1 |
| 71 | if (!pkcs7_parse_header(&der_bytes, &signed_data, cbs) || |
| 72 | // Even if only CRLs are included, there may be an empty certificates |
| 73 | // block. OpenSSL does this, for example. |
| 74 | !CBS_get_optional_asn1( |
| 75 | &signed_data, NULL, NULL, |
| 76 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) || |
| 77 | !CBS_get_optional_asn1( |
| 78 | &signed_data, &crls, &has_crls, |
| 79 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1)) { |
| 80 | goto err; |
| 81 | } |
| 82 |
| 83 | if (!has_crls) { |
| 84 | CBS_init(&crls, NULL, 0); |
| 85 | } |
| 86 |
| 87 | while (CBS_len(&crls) > 0) { |
| 88 | CBS crl_data; |
| 89 | X509_CRL *crl; |
| 90 | const uint8_t *inp; |
| 91 |
| 92 | if (!CBS_get_asn1_element(&crls, &crl_data, CBS_ASN1_SEQUENCE)) { |
| 93 | goto err; |
| 94 | } |
| 95 |
| 96 | if (CBS_len(&crl_data) > LONG_MAX) { |
| 97 | goto err; |
| 98 | } |
| 99 | inp = CBS_data(&crl_data); |
| 100 | crl = d2i_X509_CRL(NULL, &inp, (long)CBS_len(&crl_data)); |
| 101 | if (!crl) { |
| 102 | goto err; |
| 103 | } |
| 104 |
| 105 | assert(inp == CBS_data(&crl_data) + CBS_len(&crl_data)); |
| 106 |
| 107 | if (sk_X509_CRL_push(out_crls, crl) == 0) { |
| 108 | X509_CRL_free(crl); |
| 109 | goto err; |
| 110 | } |
| 111 | } |
| 112 |
| 113 | ret = 1; |
| 114 |
| 115 | err: |
| 116 | OPENSSL_free(der_bytes); |
| 117 |
| 118 | if (!ret) { |
| 119 | while (sk_X509_CRL_num(out_crls) != initial_crls_len) { |
| 120 | X509_CRL_free(sk_X509_CRL_pop(out_crls)); |
| 121 | } |
| 122 | } |
| 123 |
| 124 | return ret; |
| 125 | } |
| 126 |
| 127 | int PKCS7_get_PEM_certificates(STACK_OF(X509) *out_certs, BIO *pem_bio) { |
| 128 | uint8_t *data; |
| 129 | long len; |
| 130 | int ret; |
| 131 |
| 132 | // Even though we pass PEM_STRING_PKCS7 as the expected PEM type here, PEM |
| 133 | // internally will actually allow several other values too, including |
| 134 | // "CERTIFICATE". |
| 135 | if (!PEM_bytes_read_bio(&data, &len, NULL /* PEM type output */, |
| 136 | PEM_STRING_PKCS7, pem_bio, |
| 137 | NULL /* password callback */, |
| 138 | NULL /* password callback argument */)) { |
| 139 | return 0; |
| 140 | } |
| 141 |
| 142 | CBS cbs; |
| 143 | CBS_init(&cbs, data, len); |
| 144 | ret = PKCS7_get_certificates(out_certs, &cbs); |
| 145 | OPENSSL_free(data); |
| 146 | return ret; |
| 147 | } |
| 148 |
| 149 | int PKCS7_get_PEM_CRLs(STACK_OF(X509_CRL) *out_crls, BIO *pem_bio) { |
| 150 | uint8_t *data; |
| 151 | long len; |
| 152 | int ret; |
| 153 |
| 154 | // Even though we pass PEM_STRING_PKCS7 as the expected PEM type here, PEM |
| 155 | // internally will actually allow several other values too, including |
| 156 | // "CERTIFICATE". |
| 157 | if (!PEM_bytes_read_bio(&data, &len, NULL /* PEM type output */, |
| 158 | PEM_STRING_PKCS7, pem_bio, |
| 159 | NULL /* password callback */, |
| 160 | NULL /* password callback argument */)) { |
| 161 | return 0; |
| 162 | } |
| 163 |
| 164 | CBS cbs; |
| 165 | CBS_init(&cbs, data, len); |
| 166 | ret = PKCS7_get_CRLs(out_crls, &cbs); |
| 167 | OPENSSL_free(data); |
| 168 | return ret; |
| 169 | } |
| 170 |
| 171 | static int pkcs7_bundle_certificates_cb(CBB *out, const void *arg) { |
| 172 | const STACK_OF(X509) *certs = reinterpret_cast<const STACK_OF(X509) *>(arg); |
| 173 | size_t i; |
| 174 | CBB certificates; |
| 175 |
| 176 | // See https://tools.ietf.org/html/rfc2315#section-9.1 |
| 177 | if (!CBB_add_asn1(out, &certificates, |
| 178 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) { |
| 179 | return 0; |
| 180 | } |
| 181 |
| 182 | for (i = 0; i < sk_X509_num(certs); i++) { |
| 183 | X509 *x509 = sk_X509_value(certs, i); |
| 184 | uint8_t *buf; |
| 185 | int len = i2d_X509(x509, NULL); |
| 186 |
| 187 | if (len < 0 || !CBB_add_space(&certificates, &buf, len) || |
| 188 | i2d_X509(x509, &buf) < 0) { |
| 189 | return 0; |
| 190 | } |
| 191 | } |
| 192 |
| 193 | // |certificates| is a implicitly-tagged SET OF. |
| 194 | return CBB_flush_asn1_set_of(&certificates) && CBB_flush(out); |
| 195 | } |
| 196 |
| 197 | int PKCS7_bundle_certificates(CBB *out, const STACK_OF(X509) *certs) { |
| 198 | return pkcs7_add_signed_data(out, /*digest_algos_cb=*/NULL, |
| 199 | pkcs7_bundle_certificates_cb, |
| 200 | /*signer_infos_cb=*/NULL, certs); |
| 201 | } |
| 202 |
| 203 | static int pkcs7_bundle_crls_cb(CBB *out, const void *arg) { |
| 204 | const STACK_OF(X509_CRL) *crls = |
| 205 | reinterpret_cast<const STACK_OF(X509_CRL) *>(arg); |
| 206 | size_t i; |
| 207 | CBB crl_data; |
| 208 |
| 209 | // See https://tools.ietf.org/html/rfc2315#section-9.1 |
| 210 | if (!CBB_add_asn1(out, &crl_data, |
| 211 | CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 1)) { |
| 212 | return 0; |
| 213 | } |
| 214 |
| 215 | for (i = 0; i < sk_X509_CRL_num(crls); i++) { |
| 216 | X509_CRL *crl = sk_X509_CRL_value(crls, i); |
| 217 | uint8_t *buf; |
| 218 | int len = i2d_X509_CRL(crl, NULL); |
| 219 |
| 220 | if (len < 0 || !CBB_add_space(&crl_data, &buf, len) || |
| 221 | i2d_X509_CRL(crl, &buf) < 0) { |
| 222 | return 0; |
| 223 | } |
| 224 | } |
| 225 |
| 226 | // |crl_data| is a implicitly-tagged SET OF. |
| 227 | return CBB_flush_asn1_set_of(&crl_data) && CBB_flush(out); |
| 228 | } |
| 229 |
| 230 | int PKCS7_bundle_CRLs(CBB *out, const STACK_OF(X509_CRL) *crls) { |
| 231 | return pkcs7_add_signed_data(out, /*digest_algos_cb=*/NULL, |
| 232 | pkcs7_bundle_crls_cb, |
| 233 | /*signer_infos_cb=*/NULL, crls); |
| 234 | } |
| 235 |
| 236 | static PKCS7 *pkcs7_new(CBS *cbs) { |
| 237 | CBS copy = *cbs, copy2 = *cbs; |
| 238 | PKCS7 *ret = reinterpret_cast<PKCS7 *>(OPENSSL_zalloc(sizeof(PKCS7))); |
| 239 | if (ret == NULL) { |
| 240 | return NULL; |
| 241 | } |
| 242 | ret->type = OBJ_nid2obj(NID_pkcs7_signed); |
| 243 | ret->d.sign = |
| 244 | reinterpret_cast<PKCS7_SIGNED *>(OPENSSL_malloc(sizeof(PKCS7_SIGNED))); |
| 245 | if (ret->d.sign == NULL) { |
| 246 | goto err; |
| 247 | } |
| 248 | ret->d.sign->cert = sk_X509_new_null(); |
| 249 | ret->d.sign->crl = sk_X509_CRL_new_null(); |
| 250 | if (ret->d.sign->cert == NULL || ret->d.sign->crl == NULL || |
| 251 | !PKCS7_get_certificates(ret->d.sign->cert, ©) || |
| 252 | !PKCS7_get_CRLs(ret->d.sign->crl, cbs)) { |
| 253 | goto err; |
| 254 | } |
| 255 |
| 256 | if (sk_X509_num(ret->d.sign->cert) == 0) { |
| 257 | sk_X509_free(ret->d.sign->cert); |
| 258 | ret->d.sign->cert = NULL; |
| 259 | } |
| 260 |
| 261 | if (sk_X509_CRL_num(ret->d.sign->crl) == 0) { |
| 262 | sk_X509_CRL_free(ret->d.sign->crl); |
| 263 | ret->d.sign->crl = NULL; |
| 264 | } |
| 265 |
| 266 | ret->ber_len = CBS_len(©2) - CBS_len(cbs); |
| 267 | ret->ber_bytes = reinterpret_cast<uint8_t *>( |
| 268 | OPENSSL_memdup(CBS_data(©2), ret->ber_len)); |
| 269 | if (ret->ber_bytes == NULL) { |
| 270 | goto err; |
| 271 | } |
| 272 |
| 273 | return ret; |
| 274 |
| 275 | err: |
| 276 | PKCS7_free(ret); |
| 277 | return NULL; |
| 278 | } |
| 279 |
| 280 | PKCS7 *d2i_PKCS7(PKCS7 **out, const uint8_t **inp, size_t len) { |
| 281 | CBS cbs; |
| 282 | CBS_init(&cbs, *inp, len); |
| 283 | PKCS7 *ret = pkcs7_new(&cbs); |
| 284 | if (ret == NULL) { |
| 285 | return NULL; |
| 286 | } |
| 287 | *inp = CBS_data(&cbs); |
| 288 | if (out != NULL) { |
| 289 | PKCS7_free(*out); |
| 290 | *out = ret; |
| 291 | } |
| 292 | return ret; |
| 293 | } |
| 294 |
| 295 | PKCS7 *d2i_PKCS7_bio(BIO *bio, PKCS7 **out) { |
| 296 | // Use a generous bound, to allow for PKCS#7 files containing large root sets. |
| 297 | static const size_t kMaxSize = 4 * 1024 * 1024; |
| 298 | uint8_t *data; |
| 299 | size_t len; |
| 300 | if (!BIO_read_asn1(bio, &data, &len, kMaxSize)) { |
| 301 | return NULL; |
| 302 | } |
| 303 |
| 304 | CBS cbs; |
| 305 | CBS_init(&cbs, data, len); |
| 306 | PKCS7 *ret = pkcs7_new(&cbs); |
| 307 | OPENSSL_free(data); |
| 308 | if (out != NULL && ret != NULL) { |
| 309 | PKCS7_free(*out); |
| 310 | *out = ret; |
| 311 | } |
| 312 | return ret; |
| 313 | } |
| 314 |
| 315 | int i2d_PKCS7(const PKCS7 *p7, uint8_t **out) { |
| 316 | if (p7->ber_len > INT_MAX) { |
| 317 | OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW); |
| 318 | return -1; |
| 319 | } |
| 320 |
| 321 | if (out == NULL) { |
| 322 | return (int)p7->ber_len; |
| 323 | } |
| 324 |
| 325 | if (*out == NULL) { |
| 326 | *out = |
| 327 | reinterpret_cast<uint8_t *>(OPENSSL_memdup(p7->ber_bytes, p7->ber_len)); |
| 328 | if (*out == NULL) { |
| 329 | return -1; |
| 330 | } |
| 331 | } else { |
| 332 | OPENSSL_memcpy(*out, p7->ber_bytes, p7->ber_len); |
| 333 | *out += p7->ber_len; |
| 334 | } |
| 335 | return (int)p7->ber_len; |
| 336 | } |
| 337 |
| 338 | int i2d_PKCS7_bio(BIO *bio, const PKCS7 *p7) { |
| 339 | return BIO_write_all(bio, p7->ber_bytes, p7->ber_len); |
| 340 | } |
| 341 |
| 342 | void PKCS7_free(PKCS7 *p7) { |
| 343 | if (p7 == NULL) { |
| 344 | return; |
| 345 | } |
| 346 |
| 347 | OPENSSL_free(p7->ber_bytes); |
| 348 | ASN1_OBJECT_free(p7->type); |
| 349 | // We only supported signed data. |
| 350 | if (p7->d.sign != NULL) { |
| 351 | sk_X509_pop_free(p7->d.sign->cert, X509_free); |
| 352 | sk_X509_CRL_pop_free(p7->d.sign->crl, X509_CRL_free); |
| 353 | OPENSSL_free(p7->d.sign); |
| 354 | } |
| 355 | OPENSSL_free(p7); |
| 356 | } |
| 357 |
| 358 | // We only support signed data, so these getters are no-ops. |
| 359 | int PKCS7_type_is_data(const PKCS7 *p7) { return 0; } |
| 360 | int PKCS7_type_is_digest(const PKCS7 *p7) { return 0; } |
| 361 | int PKCS7_type_is_encrypted(const PKCS7 *p7) { return 0; } |
| 362 | int PKCS7_type_is_enveloped(const PKCS7 *p7) { return 0; } |
| 363 | int PKCS7_type_is_signed(const PKCS7 *p7) { return 1; } |
| 364 | int PKCS7_type_is_signedAndEnveloped(const PKCS7 *p7) { return 0; } |
| 365 |
| 366 | // write_sha256_ai writes an AlgorithmIdentifier for SHA-256 to |
| 367 | // |digest_algos_set|. |
| 368 | static int write_sha256_ai(CBB *digest_algos_set, const void *arg) { |
| 369 | CBB seq; |
| 370 | return CBB_add_asn1(digest_algos_set, &seq, CBS_ASN1_SEQUENCE) && |
| 371 | OBJ_nid2cbb(&seq, NID_sha256) && // |
| 372 | // https://datatracker.ietf.org/doc/html/rfc5754#section-2 |
| 373 | // "Implementations MUST generate SHA2 AlgorithmIdentifiers with absent |
| 374 | // parameters." |
| 375 | CBB_flush(digest_algos_set); |
| 376 | } |
| 377 |
| 378 | // sign_sha256 writes at most |max_out_sig| bytes of the signature of |data| by |
| 379 | // |pkey| to |out_sig| and sets |*out_sig_len| to the number of bytes written. |
| 380 | // It returns one on success or zero on error. |
| 381 | static int sign_sha256(uint8_t *out_sig, size_t *out_sig_len, |
| 382 | size_t max_out_sig, EVP_PKEY *pkey, BIO *data) { |
| 383 | static const size_t kBufSize = 4096; |
| 384 | uint8_t *buffer = reinterpret_cast<uint8_t *>(OPENSSL_malloc(kBufSize)); |
| 385 | if (!buffer) { |
| 386 | return 0; |
| 387 | } |
| 388 |
| 389 | EVP_MD_CTX ctx; |
| 390 | EVP_MD_CTX_init(&ctx); |
| 391 |
| 392 | int ret = 0; |
| 393 | if (!EVP_DigestSignInit(&ctx, NULL, EVP_sha256(), NULL, pkey)) { |
| 394 | goto out; |
| 395 | } |
| 396 |
| 397 | for (;;) { |
| 398 | const int n = BIO_read(data, buffer, kBufSize); |
| 399 | if (n == 0) { |
| 400 | break; |
| 401 | } else if (n < 0 || !EVP_DigestSignUpdate(&ctx, buffer, n)) { |
| 402 | goto out; |
| 403 | } |
| 404 | } |
| 405 |
| 406 | *out_sig_len = max_out_sig; |
| 407 | if (!EVP_DigestSignFinal(&ctx, out_sig, out_sig_len)) { |
| 408 | goto out; |
| 409 | } |
| 410 |
| 411 | ret = 1; |
| 412 |
| 413 | out: |
| 414 | EVP_MD_CTX_cleanup(&ctx); |
| 415 | OPENSSL_free(buffer); |
| 416 | return ret; |
| 417 | } |
| 418 |
| 419 | namespace { |
| 420 | struct signer_info_data { |
| 421 | const X509 *sign_cert; |
| 422 | uint8_t *signature; |
| 423 | size_t signature_len; |
| 424 | }; |
| 425 | } // namespace |
| 426 |
| 427 | // write_signer_info writes the SignerInfo structure from |
| 428 | // https://datatracker.ietf.org/doc/html/rfc2315#section-9.2 to |out|. It |
| 429 | // returns one on success or zero on error. |
| 430 | static int write_signer_info(CBB *out, const void *arg) { |
| 431 | const struct signer_info_data *const si_data = |
| 432 | reinterpret_cast<const struct signer_info_data *>(arg); |
| 433 |
| 434 | int ret = 0; |
| 435 | uint8_t *subject_bytes = NULL; |
| 436 | uint8_t *serial_bytes = NULL; |
| 437 |
| 438 | const int subject_len = |
| 439 | i2d_X509_NAME(X509_get_subject_name(si_data->sign_cert), &subject_bytes); |
| 440 | const int serial_len = i2d_ASN1_INTEGER( |
| 441 | (ASN1_INTEGER *)X509_get0_serialNumber(si_data->sign_cert), |
| 442 | &serial_bytes); |
| 443 |
| 444 | CBB seq, issuer_and_serial, signing_algo, null, signature; |
| 445 | if (subject_len < 0 || serial_len < 0 || |
| 446 | !CBB_add_asn1(out, &seq, CBS_ASN1_SEQUENCE) || |
| 447 | // version |
| 448 | !CBB_add_asn1_uint64(&seq, 1) || |
| 449 | !CBB_add_asn1(&seq, &issuer_and_serial, CBS_ASN1_SEQUENCE) || |
| 450 | !CBB_add_bytes(&issuer_and_serial, subject_bytes, subject_len) || |
| 451 | !CBB_add_bytes(&issuer_and_serial, serial_bytes, serial_len) || |
| 452 | !write_sha256_ai(&seq, NULL) || |
| 453 | !CBB_add_asn1(&seq, &signing_algo, CBS_ASN1_SEQUENCE) || |
| 454 | !OBJ_nid2cbb(&signing_algo, NID_rsaEncryption) || |
| 455 | !CBB_add_asn1(&signing_algo, &null, CBS_ASN1_NULL) || |
| 456 | !CBB_add_asn1(&seq, &signature, CBS_ASN1_OCTETSTRING) || |
| 457 | !CBB_add_bytes(&signature, si_data->signature, si_data->signature_len) || |
| 458 | !CBB_flush(out)) { |
| 459 | goto out; |
| 460 | } |
| 461 |
| 462 | ret = 1; |
| 463 |
| 464 | out: |
| 465 | OPENSSL_free(subject_bytes); |
| 466 | OPENSSL_free(serial_bytes); |
| 467 | return ret; |
| 468 | } |
| 469 |
| 470 | PKCS7 *PKCS7_sign(X509 *sign_cert, EVP_PKEY *pkey, STACK_OF(X509) *certs, |
| 471 | BIO *data, int flags) { |
| 472 | CBB cbb; |
| 473 | if (!CBB_init(&cbb, 2048)) { |
| 474 | return NULL; |
| 475 | } |
| 476 |
| 477 | uint8_t *der = NULL; |
| 478 | size_t len; |
| 479 | PKCS7 *ret = NULL; |
| 480 |
| 481 | if (sign_cert == NULL && pkey == NULL && flags == PKCS7_DETACHED) { |
| 482 | // Caller just wants to bundle certificates. |
| 483 | if (!PKCS7_bundle_certificates(&cbb, certs)) { |
| 484 | goto out; |
| 485 | } |
| 486 | } else if (sign_cert != NULL && pkey != NULL && certs == NULL && |
| 487 | data != NULL && |
| 488 | flags == (PKCS7_NOATTR | PKCS7_BINARY | PKCS7_NOCERTS | |
| 489 | PKCS7_DETACHED) && |
| 490 | EVP_PKEY_id(pkey) == NID_rsaEncryption) { |
| 491 | // sign-file.c from the Linux kernel. |
| 492 | const size_t signature_max_len = EVP_PKEY_size(pkey); |
| 493 | struct signer_info_data si_data = { |
| 494 | /*sign_cert=*/sign_cert, |
| 495 | /*signature=*/ |
| 496 | reinterpret_cast<uint8_t *>(OPENSSL_malloc(signature_max_len)), |
| 497 | /*signature_len=*/0, |
| 498 | }; |
| 499 |
| 500 | if (!si_data.signature || |
| 501 | !sign_sha256(si_data.signature, &si_data.signature_len, |
| 502 | signature_max_len, pkey, data) || |
| 503 | !pkcs7_add_signed_data(&cbb, write_sha256_ai, /*cert_crl_cb=*/NULL, |
| 504 | write_signer_info, &si_data)) { |
| 505 | OPENSSL_free(si_data.signature); |
| 506 | goto out; |
| 507 | } |
| 508 | OPENSSL_free(si_data.signature); |
| 509 | } else { |
| 510 | OPENSSL_PUT_ERROR(PKCS7, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 511 | goto out; |
| 512 | } |
| 513 |
| 514 | if (!CBB_finish(&cbb, &der, &len)) { |
| 515 | goto out; |
| 516 | } |
| 517 |
| 518 | CBS cbs; |
| 519 | CBS_init(&cbs, der, len); |
| 520 | ret = pkcs7_new(&cbs); |
| 521 |
| 522 | out: |
| 523 | CBB_cleanup(&cbb); |
| 524 | OPENSSL_free(der); |
| 525 | return ret; |
| 526 | } |
| 527 |