155714Skris/* crypto/pkcs7/pk7_lib.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296341Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296341Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296341Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296341Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296341Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include "cryptlib.h" 6155714Skris#include <openssl/objects.h> 6255714Skris#include <openssl/x509.h> 63238405Sjkim#include "asn1_locl.h" 6455714Skris 6555714Skrislong PKCS7_ctrl(PKCS7 *p7, int cmd, long larg, char *parg) 66296341Sdelphij{ 67296341Sdelphij int nid; 68296341Sdelphij long ret; 6955714Skris 70296341Sdelphij nid = OBJ_obj2nid(p7->type); 7155714Skris 72296341Sdelphij switch (cmd) { 73296341Sdelphij /* NOTE(emilia): does not support detached digested data. */ 74296341Sdelphij case PKCS7_OP_SET_DETACHED_SIGNATURE: 75296341Sdelphij if (nid == NID_pkcs7_signed) { 76296341Sdelphij ret = p7->detached = (int)larg; 77296341Sdelphij if (ret && PKCS7_type_is_data(p7->d.sign->contents)) { 78296341Sdelphij ASN1_OCTET_STRING *os; 79296341Sdelphij os = p7->d.sign->contents->d.data; 80296341Sdelphij ASN1_OCTET_STRING_free(os); 81296341Sdelphij p7->d.sign->contents->d.data = NULL; 82296341Sdelphij } 83296341Sdelphij } else { 84296341Sdelphij PKCS7err(PKCS7_F_PKCS7_CTRL, 85296341Sdelphij PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE); 86296341Sdelphij ret = 0; 87296341Sdelphij } 88296341Sdelphij break; 89296341Sdelphij case PKCS7_OP_GET_DETACHED_SIGNATURE: 90296341Sdelphij if (nid == NID_pkcs7_signed) { 91296341Sdelphij if (!p7->d.sign || !p7->d.sign->contents->d.ptr) 92296341Sdelphij ret = 1; 93296341Sdelphij else 94296341Sdelphij ret = 0; 9555714Skris 96296341Sdelphij p7->detached = ret; 97296341Sdelphij } else { 98296341Sdelphij PKCS7err(PKCS7_F_PKCS7_CTRL, 99296341Sdelphij PKCS7_R_OPERATION_NOT_SUPPORTED_ON_THIS_TYPE); 100296341Sdelphij ret = 0; 101296341Sdelphij } 102296341Sdelphij 103296341Sdelphij break; 104296341Sdelphij default: 105296341Sdelphij PKCS7err(PKCS7_F_PKCS7_CTRL, PKCS7_R_UNKNOWN_OPERATION); 106296341Sdelphij ret = 0; 107296341Sdelphij } 108296341Sdelphij return (ret); 109296341Sdelphij} 110296341Sdelphij 11155714Skrisint PKCS7_content_new(PKCS7 *p7, int type) 112296341Sdelphij{ 113296341Sdelphij PKCS7 *ret = NULL; 11455714Skris 115296341Sdelphij if ((ret = PKCS7_new()) == NULL) 116296341Sdelphij goto err; 117296341Sdelphij if (!PKCS7_set_type(ret, type)) 118296341Sdelphij goto err; 119296341Sdelphij if (!PKCS7_set_content(p7, ret)) 120296341Sdelphij goto err; 12155714Skris 122296341Sdelphij return (1); 123296341Sdelphij err: 124296341Sdelphij if (ret != NULL) 125296341Sdelphij PKCS7_free(ret); 126296341Sdelphij return (0); 127296341Sdelphij} 12855714Skris 12955714Skrisint PKCS7_set_content(PKCS7 *p7, PKCS7 *p7_data) 130296341Sdelphij{ 131296341Sdelphij int i; 13255714Skris 133296341Sdelphij i = OBJ_obj2nid(p7->type); 134296341Sdelphij switch (i) { 135296341Sdelphij case NID_pkcs7_signed: 136296341Sdelphij if (p7->d.sign->contents != NULL) 137296341Sdelphij PKCS7_free(p7->d.sign->contents); 138296341Sdelphij p7->d.sign->contents = p7_data; 139296341Sdelphij break; 140296341Sdelphij case NID_pkcs7_digest: 141296341Sdelphij if (p7->d.digest->contents != NULL) 142296341Sdelphij PKCS7_free(p7->d.digest->contents); 143296341Sdelphij p7->d.digest->contents = p7_data; 144296341Sdelphij break; 145296341Sdelphij case NID_pkcs7_data: 146296341Sdelphij case NID_pkcs7_enveloped: 147296341Sdelphij case NID_pkcs7_signedAndEnveloped: 148296341Sdelphij case NID_pkcs7_encrypted: 149296341Sdelphij default: 150296341Sdelphij PKCS7err(PKCS7_F_PKCS7_SET_CONTENT, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); 151296341Sdelphij goto err; 152296341Sdelphij } 153296341Sdelphij return (1); 154296341Sdelphij err: 155296341Sdelphij return (0); 156296341Sdelphij} 15755714Skris 15855714Skrisint PKCS7_set_type(PKCS7 *p7, int type) 159296341Sdelphij{ 160296341Sdelphij ASN1_OBJECT *obj; 16155714Skris 162296341Sdelphij /* 163296341Sdelphij * PKCS7_content_free(p7); 164296341Sdelphij */ 165296341Sdelphij obj = OBJ_nid2obj(type); /* will not fail */ 16655714Skris 167296341Sdelphij switch (type) { 168296341Sdelphij case NID_pkcs7_signed: 169296341Sdelphij p7->type = obj; 170296341Sdelphij if ((p7->d.sign = PKCS7_SIGNED_new()) == NULL) 171296341Sdelphij goto err; 172296341Sdelphij if (!ASN1_INTEGER_set(p7->d.sign->version, 1)) { 173296341Sdelphij PKCS7_SIGNED_free(p7->d.sign); 174296341Sdelphij p7->d.sign = NULL; 175296341Sdelphij goto err; 176296341Sdelphij } 177296341Sdelphij break; 178296341Sdelphij case NID_pkcs7_data: 179296341Sdelphij p7->type = obj; 180296341Sdelphij if ((p7->d.data = M_ASN1_OCTET_STRING_new()) == NULL) 181296341Sdelphij goto err; 182296341Sdelphij break; 183296341Sdelphij case NID_pkcs7_signedAndEnveloped: 184296341Sdelphij p7->type = obj; 185296341Sdelphij if ((p7->d.signed_and_enveloped = PKCS7_SIGN_ENVELOPE_new()) 186296341Sdelphij == NULL) 187296341Sdelphij goto err; 188296341Sdelphij ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1); 189296341Sdelphij if (!ASN1_INTEGER_set(p7->d.signed_and_enveloped->version, 1)) 190296341Sdelphij goto err; 191296341Sdelphij p7->d.signed_and_enveloped->enc_data->content_type 192296341Sdelphij = OBJ_nid2obj(NID_pkcs7_data); 193296341Sdelphij break; 194296341Sdelphij case NID_pkcs7_enveloped: 195296341Sdelphij p7->type = obj; 196296341Sdelphij if ((p7->d.enveloped = PKCS7_ENVELOPE_new()) 197296341Sdelphij == NULL) 198296341Sdelphij goto err; 199296341Sdelphij if (!ASN1_INTEGER_set(p7->d.enveloped->version, 0)) 200296341Sdelphij goto err; 201296341Sdelphij p7->d.enveloped->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data); 202296341Sdelphij break; 203296341Sdelphij case NID_pkcs7_encrypted: 204296341Sdelphij p7->type = obj; 205296341Sdelphij if ((p7->d.encrypted = PKCS7_ENCRYPT_new()) 206296341Sdelphij == NULL) 207296341Sdelphij goto err; 208296341Sdelphij if (!ASN1_INTEGER_set(p7->d.encrypted->version, 0)) 209296341Sdelphij goto err; 210296341Sdelphij p7->d.encrypted->enc_data->content_type = OBJ_nid2obj(NID_pkcs7_data); 211296341Sdelphij break; 21259191Skris 213296341Sdelphij case NID_pkcs7_digest: 214296341Sdelphij p7->type = obj; 215296341Sdelphij if ((p7->d.digest = PKCS7_DIGEST_new()) 216296341Sdelphij == NULL) 217296341Sdelphij goto err; 218296341Sdelphij if (!ASN1_INTEGER_set(p7->d.digest->version, 0)) 219296341Sdelphij goto err; 220296341Sdelphij break; 221296341Sdelphij default: 222296341Sdelphij PKCS7err(PKCS7_F_PKCS7_SET_TYPE, PKCS7_R_UNSUPPORTED_CONTENT_TYPE); 223296341Sdelphij goto err; 224296341Sdelphij } 225296341Sdelphij return (1); 226296341Sdelphij err: 227296341Sdelphij return (0); 228296341Sdelphij} 22955714Skris 230160814Ssimonint PKCS7_set0_type_other(PKCS7 *p7, int type, ASN1_TYPE *other) 231296341Sdelphij{ 232296341Sdelphij p7->type = OBJ_nid2obj(type); 233296341Sdelphij p7->d.other = other; 234296341Sdelphij return 1; 235296341Sdelphij} 236160814Ssimon 23755714Skrisint PKCS7_add_signer(PKCS7 *p7, PKCS7_SIGNER_INFO *psi) 238296341Sdelphij{ 239296341Sdelphij int i, j, nid; 240296341Sdelphij X509_ALGOR *alg; 241296341Sdelphij STACK_OF(PKCS7_SIGNER_INFO) *signer_sk; 242296341Sdelphij STACK_OF(X509_ALGOR) *md_sk; 24355714Skris 244296341Sdelphij i = OBJ_obj2nid(p7->type); 245296341Sdelphij switch (i) { 246296341Sdelphij case NID_pkcs7_signed: 247296341Sdelphij signer_sk = p7->d.sign->signer_info; 248296341Sdelphij md_sk = p7->d.sign->md_algs; 249296341Sdelphij break; 250296341Sdelphij case NID_pkcs7_signedAndEnveloped: 251296341Sdelphij signer_sk = p7->d.signed_and_enveloped->signer_info; 252296341Sdelphij md_sk = p7->d.signed_and_enveloped->md_algs; 253296341Sdelphij break; 254296341Sdelphij default: 255296341Sdelphij PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, PKCS7_R_WRONG_CONTENT_TYPE); 256296341Sdelphij return (0); 257296341Sdelphij } 25855714Skris 259296341Sdelphij nid = OBJ_obj2nid(psi->digest_alg->algorithm); 26055714Skris 261296341Sdelphij /* If the digest is not currently listed, add it */ 262296341Sdelphij j = 0; 263296341Sdelphij for (i = 0; i < sk_X509_ALGOR_num(md_sk); i++) { 264296341Sdelphij alg = sk_X509_ALGOR_value(md_sk, i); 265296341Sdelphij if (OBJ_obj2nid(alg->algorithm) == nid) { 266296341Sdelphij j = 1; 267296341Sdelphij break; 268296341Sdelphij } 269296341Sdelphij } 270296341Sdelphij if (!j) { /* we need to add another algorithm */ 271296341Sdelphij if (!(alg = X509_ALGOR_new()) 272296341Sdelphij || !(alg->parameter = ASN1_TYPE_new())) { 273296341Sdelphij X509_ALGOR_free(alg); 274296341Sdelphij PKCS7err(PKCS7_F_PKCS7_ADD_SIGNER, ERR_R_MALLOC_FAILURE); 275296341Sdelphij return (0); 276296341Sdelphij } 277296341Sdelphij alg->algorithm = OBJ_nid2obj(nid); 278296341Sdelphij alg->parameter->type = V_ASN1_NULL; 279296341Sdelphij if (!sk_X509_ALGOR_push(md_sk, alg)) { 280296341Sdelphij X509_ALGOR_free(alg); 281296341Sdelphij return 0; 282296341Sdelphij } 283296341Sdelphij } 28455714Skris 285296341Sdelphij if (!sk_PKCS7_SIGNER_INFO_push(signer_sk, psi)) 286296341Sdelphij return 0; 287296341Sdelphij return (1); 288296341Sdelphij} 28955714Skris 29055714Skrisint PKCS7_add_certificate(PKCS7 *p7, X509 *x509) 291296341Sdelphij{ 292296341Sdelphij int i; 293296341Sdelphij STACK_OF(X509) **sk; 29455714Skris 295296341Sdelphij i = OBJ_obj2nid(p7->type); 296296341Sdelphij switch (i) { 297296341Sdelphij case NID_pkcs7_signed: 298296341Sdelphij sk = &(p7->d.sign->cert); 299296341Sdelphij break; 300296341Sdelphij case NID_pkcs7_signedAndEnveloped: 301296341Sdelphij sk = &(p7->d.signed_and_enveloped->cert); 302296341Sdelphij break; 303296341Sdelphij default: 304296341Sdelphij PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, PKCS7_R_WRONG_CONTENT_TYPE); 305296341Sdelphij return (0); 306296341Sdelphij } 30755714Skris 308296341Sdelphij if (*sk == NULL) 309296341Sdelphij *sk = sk_X509_new_null(); 310296341Sdelphij if (*sk == NULL) { 311296341Sdelphij PKCS7err(PKCS7_F_PKCS7_ADD_CERTIFICATE, ERR_R_MALLOC_FAILURE); 312296341Sdelphij return 0; 313296341Sdelphij } 314296341Sdelphij CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509); 315296341Sdelphij if (!sk_X509_push(*sk, x509)) { 316296341Sdelphij X509_free(x509); 317296341Sdelphij return 0; 318296341Sdelphij } 319296341Sdelphij return (1); 320296341Sdelphij} 32155714Skris 32255714Skrisint PKCS7_add_crl(PKCS7 *p7, X509_CRL *crl) 323296341Sdelphij{ 324296341Sdelphij int i; 325296341Sdelphij STACK_OF(X509_CRL) **sk; 32655714Skris 327296341Sdelphij i = OBJ_obj2nid(p7->type); 328296341Sdelphij switch (i) { 329296341Sdelphij case NID_pkcs7_signed: 330296341Sdelphij sk = &(p7->d.sign->crl); 331296341Sdelphij break; 332296341Sdelphij case NID_pkcs7_signedAndEnveloped: 333296341Sdelphij sk = &(p7->d.signed_and_enveloped->crl); 334296341Sdelphij break; 335296341Sdelphij default: 336296341Sdelphij PKCS7err(PKCS7_F_PKCS7_ADD_CRL, PKCS7_R_WRONG_CONTENT_TYPE); 337296341Sdelphij return (0); 338296341Sdelphij } 33955714Skris 340296341Sdelphij if (*sk == NULL) 341296341Sdelphij *sk = sk_X509_CRL_new_null(); 342296341Sdelphij if (*sk == NULL) { 343296341Sdelphij PKCS7err(PKCS7_F_PKCS7_ADD_CRL, ERR_R_MALLOC_FAILURE); 344296341Sdelphij return 0; 345296341Sdelphij } 34655714Skris 347296341Sdelphij CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509_CRL); 348296341Sdelphij if (!sk_X509_CRL_push(*sk, crl)) { 349296341Sdelphij X509_CRL_free(crl); 350296341Sdelphij return 0; 351296341Sdelphij } 352296341Sdelphij return (1); 353296341Sdelphij} 35455714Skris 35555714Skrisint PKCS7_SIGNER_INFO_set(PKCS7_SIGNER_INFO *p7i, X509 *x509, EVP_PKEY *pkey, 356296341Sdelphij const EVP_MD *dgst) 357296341Sdelphij{ 358296341Sdelphij int ret; 359160814Ssimon 360296341Sdelphij /* We now need to add another PKCS7_SIGNER_INFO entry */ 361296341Sdelphij if (!ASN1_INTEGER_set(p7i->version, 1)) 362296341Sdelphij goto err; 363296341Sdelphij if (!X509_NAME_set(&p7i->issuer_and_serial->issuer, 364296341Sdelphij X509_get_issuer_name(x509))) 365296341Sdelphij goto err; 36655714Skris 367296341Sdelphij /* 368296341Sdelphij * because ASN1_INTEGER_set is used to set a 'long' we will do things the 369296341Sdelphij * ugly way. 370296341Sdelphij */ 371296341Sdelphij M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial); 372296341Sdelphij if (!(p7i->issuer_and_serial->serial = 373296341Sdelphij M_ASN1_INTEGER_dup(X509_get_serialNumber(x509)))) 374296341Sdelphij goto err; 37555714Skris 376296341Sdelphij /* lets keep the pkey around for a while */ 377296341Sdelphij CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY); 378296341Sdelphij p7i->pkey = pkey; 37955714Skris 380296341Sdelphij /* Set the algorithms */ 38155714Skris 382296341Sdelphij X509_ALGOR_set0(p7i->digest_alg, OBJ_nid2obj(EVP_MD_type(dgst)), 383296341Sdelphij V_ASN1_NULL, NULL); 38455714Skris 385296341Sdelphij if (pkey->ameth && pkey->ameth->pkey_ctrl) { 386296341Sdelphij ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_SIGN, 0, p7i); 387296341Sdelphij if (ret > 0) 388296341Sdelphij return 1; 389296341Sdelphij if (ret != -2) { 390296341Sdelphij PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET, 391296341Sdelphij PKCS7_R_SIGNING_CTRL_FAILURE); 392296341Sdelphij return 0; 393296341Sdelphij } 394296341Sdelphij } 395296341Sdelphij PKCS7err(PKCS7_F_PKCS7_SIGNER_INFO_SET, 396296341Sdelphij PKCS7_R_SIGNING_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); 397296341Sdelphij err: 398296341Sdelphij return 0; 399296341Sdelphij} 40055714Skris 40155714SkrisPKCS7_SIGNER_INFO *PKCS7_add_signature(PKCS7 *p7, X509 *x509, EVP_PKEY *pkey, 402296341Sdelphij const EVP_MD *dgst) 403296341Sdelphij{ 404296341Sdelphij PKCS7_SIGNER_INFO *si = NULL; 40555714Skris 406296341Sdelphij if (dgst == NULL) { 407296341Sdelphij int def_nid; 408296341Sdelphij if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) <= 0) 409296341Sdelphij goto err; 410296341Sdelphij dgst = EVP_get_digestbynid(def_nid); 411296341Sdelphij if (dgst == NULL) { 412296341Sdelphij PKCS7err(PKCS7_F_PKCS7_ADD_SIGNATURE, PKCS7_R_NO_DEFAULT_DIGEST); 413296341Sdelphij goto err; 414296341Sdelphij } 415296341Sdelphij } 416238405Sjkim 417296341Sdelphij if ((si = PKCS7_SIGNER_INFO_new()) == NULL) 418296341Sdelphij goto err; 419296341Sdelphij if (!PKCS7_SIGNER_INFO_set(si, x509, pkey, dgst)) 420296341Sdelphij goto err; 421296341Sdelphij if (!PKCS7_add_signer(p7, si)) 422296341Sdelphij goto err; 423296341Sdelphij return (si); 424296341Sdelphij err: 425296341Sdelphij if (si) 426296341Sdelphij PKCS7_SIGNER_INFO_free(si); 427296341Sdelphij return (NULL); 428296341Sdelphij} 42955714Skris 430160814Ssimonint PKCS7_set_digest(PKCS7 *p7, const EVP_MD *md) 431296341Sdelphij{ 432296341Sdelphij if (PKCS7_type_is_digest(p7)) { 433296341Sdelphij if (!(p7->d.digest->md->parameter = ASN1_TYPE_new())) { 434296341Sdelphij PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, ERR_R_MALLOC_FAILURE); 435296341Sdelphij return 0; 436296341Sdelphij } 437296341Sdelphij p7->d.digest->md->parameter->type = V_ASN1_NULL; 438296341Sdelphij p7->d.digest->md->algorithm = OBJ_nid2obj(EVP_MD_nid(md)); 439296341Sdelphij return 1; 440296341Sdelphij } 441160814Ssimon 442296341Sdelphij PKCS7err(PKCS7_F_PKCS7_SET_DIGEST, PKCS7_R_WRONG_CONTENT_TYPE); 443296341Sdelphij return 1; 444296341Sdelphij} 445296341Sdelphij 44655714SkrisSTACK_OF(PKCS7_SIGNER_INFO) *PKCS7_get_signer_info(PKCS7 *p7) 447296341Sdelphij{ 448296341Sdelphij if (p7 == NULL || p7->d.ptr == NULL) 449296341Sdelphij return NULL; 450296341Sdelphij if (PKCS7_type_is_signed(p7)) { 451296341Sdelphij return (p7->d.sign->signer_info); 452296341Sdelphij } else if (PKCS7_type_is_signedAndEnveloped(p7)) { 453296341Sdelphij return (p7->d.signed_and_enveloped->signer_info); 454296341Sdelphij } else 455296341Sdelphij return (NULL); 456296341Sdelphij} 45755714Skris 458238405Sjkimvoid PKCS7_SIGNER_INFO_get0_algs(PKCS7_SIGNER_INFO *si, EVP_PKEY **pk, 459296341Sdelphij X509_ALGOR **pdig, X509_ALGOR **psig) 460296341Sdelphij{ 461296341Sdelphij if (pk) 462296341Sdelphij *pk = si->pkey; 463296341Sdelphij if (pdig) 464296341Sdelphij *pdig = si->digest_alg; 465296341Sdelphij if (psig) 466296341Sdelphij *psig = si->digest_enc_alg; 467296341Sdelphij} 468238405Sjkim 469238405Sjkimvoid PKCS7_RECIP_INFO_get0_alg(PKCS7_RECIP_INFO *ri, X509_ALGOR **penc) 470296341Sdelphij{ 471296341Sdelphij if (penc) 472296341Sdelphij *penc = ri->key_enc_algor; 473296341Sdelphij} 474238405Sjkim 47555714SkrisPKCS7_RECIP_INFO *PKCS7_add_recipient(PKCS7 *p7, X509 *x509) 476296341Sdelphij{ 477296341Sdelphij PKCS7_RECIP_INFO *ri; 47855714Skris 479296341Sdelphij if ((ri = PKCS7_RECIP_INFO_new()) == NULL) 480296341Sdelphij goto err; 481296341Sdelphij if (!PKCS7_RECIP_INFO_set(ri, x509)) 482296341Sdelphij goto err; 483296341Sdelphij if (!PKCS7_add_recipient_info(p7, ri)) 484296341Sdelphij goto err; 485296341Sdelphij return ri; 486296341Sdelphij err: 487296341Sdelphij if (ri) 488296341Sdelphij PKCS7_RECIP_INFO_free(ri); 489296341Sdelphij return NULL; 490296341Sdelphij} 49155714Skris 49255714Skrisint PKCS7_add_recipient_info(PKCS7 *p7, PKCS7_RECIP_INFO *ri) 493296341Sdelphij{ 494296341Sdelphij int i; 495296341Sdelphij STACK_OF(PKCS7_RECIP_INFO) *sk; 49655714Skris 497296341Sdelphij i = OBJ_obj2nid(p7->type); 498296341Sdelphij switch (i) { 499296341Sdelphij case NID_pkcs7_signedAndEnveloped: 500296341Sdelphij sk = p7->d.signed_and_enveloped->recipientinfo; 501296341Sdelphij break; 502296341Sdelphij case NID_pkcs7_enveloped: 503296341Sdelphij sk = p7->d.enveloped->recipientinfo; 504296341Sdelphij break; 505296341Sdelphij default: 506296341Sdelphij PKCS7err(PKCS7_F_PKCS7_ADD_RECIPIENT_INFO, 507296341Sdelphij PKCS7_R_WRONG_CONTENT_TYPE); 508296341Sdelphij return (0); 509296341Sdelphij } 51055714Skris 511296341Sdelphij if (!sk_PKCS7_RECIP_INFO_push(sk, ri)) 512296341Sdelphij return 0; 513296341Sdelphij return (1); 514296341Sdelphij} 51555714Skris 51655714Skrisint PKCS7_RECIP_INFO_set(PKCS7_RECIP_INFO *p7i, X509 *x509) 517296341Sdelphij{ 518296341Sdelphij int ret; 519296341Sdelphij EVP_PKEY *pkey = NULL; 520296341Sdelphij if (!ASN1_INTEGER_set(p7i->version, 0)) 521296341Sdelphij return 0; 522296341Sdelphij if (!X509_NAME_set(&p7i->issuer_and_serial->issuer, 523296341Sdelphij X509_get_issuer_name(x509))) 524296341Sdelphij return 0; 52555714Skris 526296341Sdelphij M_ASN1_INTEGER_free(p7i->issuer_and_serial->serial); 527296341Sdelphij if (!(p7i->issuer_and_serial->serial = 528296341Sdelphij M_ASN1_INTEGER_dup(X509_get_serialNumber(x509)))) 529296341Sdelphij return 0; 53055714Skris 531296341Sdelphij pkey = X509_get_pubkey(x509); 53255714Skris 533296341Sdelphij if (!pkey || !pkey->ameth || !pkey->ameth->pkey_ctrl) { 534296341Sdelphij PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET, 535296341Sdelphij PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); 536296341Sdelphij goto err; 537296341Sdelphij } 538238405Sjkim 539296341Sdelphij ret = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_PKCS7_ENCRYPT, 0, p7i); 540296341Sdelphij if (ret == -2) { 541296341Sdelphij PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET, 542296341Sdelphij PKCS7_R_ENCRYPTION_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); 543296341Sdelphij goto err; 544296341Sdelphij } 545296341Sdelphij if (ret <= 0) { 546296341Sdelphij PKCS7err(PKCS7_F_PKCS7_RECIP_INFO_SET, 547296341Sdelphij PKCS7_R_ENCRYPTION_CTRL_FAILURE); 548296341Sdelphij goto err; 549296341Sdelphij } 550238405Sjkim 551296341Sdelphij EVP_PKEY_free(pkey); 552238405Sjkim 553296341Sdelphij CRYPTO_add(&x509->references, 1, CRYPTO_LOCK_X509); 554296341Sdelphij p7i->cert = x509; 55555714Skris 556296341Sdelphij return 1; 557238405Sjkim 558296341Sdelphij err: 559296341Sdelphij if (pkey) 560296341Sdelphij EVP_PKEY_free(pkey); 561296341Sdelphij return 0; 562296341Sdelphij} 56355714Skris 56455714SkrisX509 *PKCS7_cert_from_signer_info(PKCS7 *p7, PKCS7_SIGNER_INFO *si) 565296341Sdelphij{ 566296341Sdelphij if (PKCS7_type_is_signed(p7)) 567296341Sdelphij return (X509_find_by_issuer_and_serial(p7->d.sign->cert, 568296341Sdelphij si->issuer_and_serial->issuer, 569296341Sdelphij si-> 570296341Sdelphij issuer_and_serial->serial)); 571296341Sdelphij else 572296341Sdelphij return (NULL); 573296341Sdelphij} 57455714Skris 57555714Skrisint PKCS7_set_cipher(PKCS7 *p7, const EVP_CIPHER *cipher) 576296341Sdelphij{ 577296341Sdelphij int i; 578296341Sdelphij PKCS7_ENC_CONTENT *ec; 57955714Skris 580296341Sdelphij i = OBJ_obj2nid(p7->type); 581296341Sdelphij switch (i) { 582296341Sdelphij case NID_pkcs7_signedAndEnveloped: 583296341Sdelphij ec = p7->d.signed_and_enveloped->enc_data; 584296341Sdelphij break; 585296341Sdelphij case NID_pkcs7_enveloped: 586296341Sdelphij ec = p7->d.enveloped->enc_data; 587296341Sdelphij break; 588296341Sdelphij default: 589296341Sdelphij PKCS7err(PKCS7_F_PKCS7_SET_CIPHER, PKCS7_R_WRONG_CONTENT_TYPE); 590296341Sdelphij return (0); 591296341Sdelphij } 59255714Skris 593296341Sdelphij /* Check cipher OID exists and has data in it */ 594296341Sdelphij i = EVP_CIPHER_type(cipher); 595296341Sdelphij if (i == NID_undef) { 596296341Sdelphij PKCS7err(PKCS7_F_PKCS7_SET_CIPHER, 597296341Sdelphij PKCS7_R_CIPHER_HAS_NO_OBJECT_IDENTIFIER); 598296341Sdelphij return (0); 599296341Sdelphij } 60055714Skris 601296341Sdelphij ec->cipher = cipher; 602296341Sdelphij return 1; 603296341Sdelphij} 60455714Skris 605238405Sjkimint PKCS7_stream(unsigned char ***boundary, PKCS7 *p7) 606296341Sdelphij{ 607296341Sdelphij ASN1_OCTET_STRING *os = NULL; 608238405Sjkim 609296341Sdelphij switch (OBJ_obj2nid(p7->type)) { 610296341Sdelphij case NID_pkcs7_data: 611296341Sdelphij os = p7->d.data; 612296341Sdelphij break; 613238405Sjkim 614296341Sdelphij case NID_pkcs7_signedAndEnveloped: 615296341Sdelphij os = p7->d.signed_and_enveloped->enc_data->enc_data; 616296341Sdelphij if (os == NULL) { 617296341Sdelphij os = M_ASN1_OCTET_STRING_new(); 618296341Sdelphij p7->d.signed_and_enveloped->enc_data->enc_data = os; 619296341Sdelphij } 620296341Sdelphij break; 621238405Sjkim 622296341Sdelphij case NID_pkcs7_enveloped: 623296341Sdelphij os = p7->d.enveloped->enc_data->enc_data; 624296341Sdelphij if (os == NULL) { 625296341Sdelphij os = M_ASN1_OCTET_STRING_new(); 626296341Sdelphij p7->d.enveloped->enc_data->enc_data = os; 627296341Sdelphij } 628296341Sdelphij break; 629238405Sjkim 630296341Sdelphij case NID_pkcs7_signed: 631296341Sdelphij os = p7->d.sign->contents->d.data; 632296341Sdelphij break; 633238405Sjkim 634296341Sdelphij default: 635296341Sdelphij os = NULL; 636296341Sdelphij break; 637296341Sdelphij } 638238405Sjkim 639296341Sdelphij if (os == NULL) 640296341Sdelphij return 0; 641238405Sjkim 642296341Sdelphij os->flags |= ASN1_STRING_FLAG_NDEF; 643296341Sdelphij *boundary = &os->data; 644296341Sdelphij 645296341Sdelphij return 1; 646296341Sdelphij} 647