- int iter, int wrap_nid, int pbe_nid,
- unsigned char *pass,
- ossl_ssize_t passlen,
- const EVP_CIPHER *kekciph)
- {
- CMS_RecipientInfo *ri = NULL;
- CMS_EnvelopedData *env;
- CMS_PasswordRecipientInfo *pwri;
- EVP_CIPHER_CTX ctx;
- X509_ALGOR *encalg = NULL;
- unsigned char iv[EVP_MAX_IV_LENGTH];
- int ivlen;
-
- env = cms_get0_enveloped(cms);
- if (!env)
- return NULL;
-
- if (wrap_nid <= 0)
- wrap_nid = NID_id_alg_PWRI_KEK;
-
- if (pbe_nid <= 0)
- pbe_nid = NID_id_pbkdf2;
-
- /* Get from enveloped data */
- if (kekciph == NULL)
- kekciph = env->encryptedContentInfo->cipher;
-
- if (kekciph == NULL)
- {
- CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, CMS_R_NO_CIPHER);
- return NULL;
- }
- if (wrap_nid != NID_id_alg_PWRI_KEK)
- {
- CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
- CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
- return NULL;
- }
-
- /* Setup algorithm identifier for cipher */
- encalg = X509_ALGOR_new();
- EVP_CIPHER_CTX_init(&ctx);
-
- if (EVP_EncryptInit_ex(&ctx, kekciph, NULL, NULL, NULL) <= 0)
- {
- CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB);
- goto err;
- }
-
- ivlen = EVP_CIPHER_CTX_iv_length(&ctx);
-
- if (ivlen > 0)
- {
- if (RAND_pseudo_bytes(iv, ivlen) <= 0)
- goto err;
- if (EVP_EncryptInit_ex(&ctx, NULL, NULL, NULL, iv) <= 0)
- {
- CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
- ERR_R_EVP_LIB);
- goto err;
- }
- encalg->parameter = ASN1_TYPE_new();
- if (!encalg->parameter)
- {
- CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
- ERR_R_MALLOC_FAILURE);
- goto err;
- }
- if (EVP_CIPHER_param_to_asn1(&ctx, encalg->parameter) <= 0)
- {
- CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
- CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
- goto err;
- }
- }
-
-
- encalg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(&ctx));
-
- EVP_CIPHER_CTX_cleanup(&ctx);
-
- /* Initialize recipient info */
- ri = M_ASN1_new_of(CMS_RecipientInfo);
- if (!ri)
- goto merr;
-
- ri->d.pwri = M_ASN1_new_of(CMS_PasswordRecipientInfo);
- if (!ri->d.pwri)
- goto merr;
- ri->type = CMS_RECIPINFO_PASS;
-
- pwri = ri->d.pwri;
- /* Since this is overwritten, free up empty structure already there */
- X509_ALGOR_free(pwri->keyEncryptionAlgorithm);
- pwri->keyEncryptionAlgorithm = X509_ALGOR_new();
- if (!pwri->keyEncryptionAlgorithm)
- goto merr;
- pwri->keyEncryptionAlgorithm->algorithm = OBJ_nid2obj(wrap_nid);
- pwri->keyEncryptionAlgorithm->parameter = ASN1_TYPE_new();
- if (!pwri->keyEncryptionAlgorithm->parameter)
- goto merr;
-
- if(!ASN1_item_pack(encalg, ASN1_ITEM_rptr(X509_ALGOR),
- &pwri->keyEncryptionAlgorithm->parameter->value.sequence))
- goto merr;
- pwri->keyEncryptionAlgorithm->parameter->type = V_ASN1_SEQUENCE;
-
- X509_ALGOR_free(encalg);
- encalg = NULL;
-
- /* Setup PBE algorithm */
-
- pwri->keyDerivationAlgorithm = PKCS5_pbkdf2_set(iter, NULL, 0, -1, -1);
-
- if (!pwri->keyDerivationAlgorithm)
- goto err;
-
- CMS_RecipientInfo_set0_password(ri, pass, passlen);
- pwri->version = 0;
-
- if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
- goto merr;
-
- return ri;
-
- merr:
- CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE);
- err:
- EVP_CIPHER_CTX_cleanup(&ctx);
- if (ri)
- M_ASN1_free_of(ri, CMS_RecipientInfo);
- if (encalg)
- X509_ALGOR_free(encalg);
- return NULL;
-
- }
-
-/* This is an implementation of the key wrapping mechanism in RFC3211,
- * at some point this should go into EVP.
+ int iter, int wrap_nid,
+ int pbe_nid,
+ unsigned char *pass,
+ ossl_ssize_t passlen,
+ const EVP_CIPHER *kekciph)
+{
+ CMS_RecipientInfo *ri = NULL;
+ CMS_EnvelopedData *env;
+ CMS_PasswordRecipientInfo *pwri;
+ EVP_CIPHER_CTX *ctx = NULL;
+ X509_ALGOR *encalg = NULL;
+ unsigned char iv[EVP_MAX_IV_LENGTH];
+ int ivlen;
+
+ env = cms_get0_enveloped(cms);
+ if (!env)
+ return NULL;
+
+ if (wrap_nid <= 0)
+ wrap_nid = NID_id_alg_PWRI_KEK;
+
+ if (pbe_nid <= 0)
+ pbe_nid = NID_id_pbkdf2;
+
+ /* Get from enveloped data */
+ if (kekciph == NULL)
+ kekciph = env->encryptedContentInfo->cipher;
+
+ if (kekciph == NULL) {
+ CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, CMS_R_NO_CIPHER);
+ return NULL;
+ }
+ if (wrap_nid != NID_id_alg_PWRI_KEK) {
+ CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
+ CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
+ return NULL;
+ }
+
+ /* Setup algorithm identifier for cipher */
+ encalg = X509_ALGOR_new();
+ if (encalg == NULL) {
+ goto merr;
+ }
+ ctx = EVP_CIPHER_CTX_new();
+
+ if (EVP_EncryptInit_ex(ctx, kekciph, NULL, NULL, NULL) <= 0) {
+ CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB);
+ goto err;
+ }
+
+ ivlen = EVP_CIPHER_CTX_iv_length(ctx);
+
+ if (ivlen > 0) {
+ if (RAND_bytes(iv, ivlen) <= 0)
+ goto err;
+ if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0) {
+ CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB);
+ goto err;
+ }
+ encalg->parameter = ASN1_TYPE_new();
+ if (!encalg->parameter) {
+ CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE);
+ goto err;
+ }
+ if (EVP_CIPHER_param_to_asn1(ctx, encalg->parameter) <= 0) {
+ CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
+ CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
+ goto err;
+ }
+ }
+
+ encalg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
+
+ EVP_CIPHER_CTX_free(ctx);
+ ctx = NULL;
+
+ /* Initialize recipient info */
+ ri = M_ASN1_new_of(CMS_RecipientInfo);
+ if (ri == NULL)
+ goto merr;
+
+ ri->d.pwri = M_ASN1_new_of(CMS_PasswordRecipientInfo);
+ if (ri->d.pwri == NULL)
+ goto merr;
+ ri->type = CMS_RECIPINFO_PASS;
+
+ pwri = ri->d.pwri;
+ /* Since this is overwritten, free up empty structure already there */
+ X509_ALGOR_free(pwri->keyEncryptionAlgorithm);
+ pwri->keyEncryptionAlgorithm = X509_ALGOR_new();
+ if (pwri->keyEncryptionAlgorithm == NULL)
+ goto merr;
+ pwri->keyEncryptionAlgorithm->algorithm = OBJ_nid2obj(wrap_nid);
+ pwri->keyEncryptionAlgorithm->parameter = ASN1_TYPE_new();
+ if (pwri->keyEncryptionAlgorithm->parameter == NULL)
+ goto merr;
+
+ if (!ASN1_item_pack(encalg, ASN1_ITEM_rptr(X509_ALGOR),
+ &pwri->keyEncryptionAlgorithm->parameter->
+ value.sequence))
+ goto merr;
+ pwri->keyEncryptionAlgorithm->parameter->type = V_ASN1_SEQUENCE;
+
+ X509_ALGOR_free(encalg);
+ encalg = NULL;
+
+ /* Setup PBE algorithm */
+
+ pwri->keyDerivationAlgorithm = PKCS5_pbkdf2_set(iter, NULL, 0, -1, -1);
+
+ if (!pwri->keyDerivationAlgorithm)
+ goto err;
+
+ CMS_RecipientInfo_set0_password(ri, pass, passlen);
+ pwri->version = 0;
+
+ if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
+ goto merr;
+
+ return ri;
+
+ merr:
+ CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE);
+ err:
+ EVP_CIPHER_CTX_free(ctx);
+ if (ri)
+ M_ASN1_free_of(ri, CMS_RecipientInfo);
+ X509_ALGOR_free(encalg);
+ return NULL;
+
+}
+
+/*
+ * This is an implementation of the key wrapping mechanism in RFC3211, at
+ * some point this should go into EVP.