summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2019-08-10 16:42:20 +0000
committerjsing <>2019-08-10 16:42:20 +0000
commit348e8055f6b4ea773466a117767c16e615a549ab (patch)
treefd2cdff41f524a9488d8863f2cd9058c06fe6058 /src/lib
parentd2294fe6ec6b67e094cc8b699125f5d1027c17e3 (diff)
downloadopenbsd-348e8055f6b4ea773466a117767c16e615a549ab.tar.gz
openbsd-348e8055f6b4ea773466a117767c16e615a549ab.tar.bz2
openbsd-348e8055f6b4ea773466a117767c16e615a549ab.zip
First pass at style(9).
Whitespace only and no change according to diff -w.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/cms/cms_asn1.c408
-rw-r--r--src/lib/libcrypto/cms/cms_att.c108
-rw-r--r--src/lib/libcrypto/cms/cms_cd.c84
-rw-r--r--src/lib/libcrypto/cms/cms_dd.c116
-rw-r--r--src/lib/libcrypto/cms/cms_enc.c324
-rw-r--r--src/lib/libcrypto/cms/cms_env.c1264
-rw-r--r--src/lib/libcrypto/cms/cms_err.c534
-rw-r--r--src/lib/libcrypto/cms/cms_ess.c498
-rw-r--r--src/lib/libcrypto/cms/cms_io.c74
-rw-r--r--src/lib/libcrypto/cms/cms_kari.c636
-rw-r--r--src/lib/libcrypto/cms/cms_lib.c814
-rw-r--r--src/lib/libcrypto/cms/cms_pwri.c674
-rw-r--r--src/lib/libcrypto/cms/cms_sd.c1488
-rw-r--r--src/lib/libcrypto/cms/cms_smime.c1326
14 files changed, 4174 insertions, 4174 deletions
diff --git a/src/lib/libcrypto/cms/cms_asn1.c b/src/lib/libcrypto/cms/cms_asn1.c
index 70f245ea40..fbe27892dc 100644
--- a/src/lib/libcrypto/cms/cms_asn1.c
+++ b/src/lib/libcrypto/cms/cms_asn1.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_asn1.c,v 1.12 2019/08/10 16:03:53 jsing Exp $ */ 1/* $OpenBSD: cms_asn1.c,v 1.13 2019/08/10 16:42:20 jsing Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -60,102 +60,102 @@
60 60
61 61
62ASN1_SEQUENCE(CMS_IssuerAndSerialNumber) = { 62ASN1_SEQUENCE(CMS_IssuerAndSerialNumber) = {
63 ASN1_SIMPLE(CMS_IssuerAndSerialNumber, issuer, X509_NAME), 63 ASN1_SIMPLE(CMS_IssuerAndSerialNumber, issuer, X509_NAME),
64 ASN1_SIMPLE(CMS_IssuerAndSerialNumber, serialNumber, ASN1_INTEGER) 64 ASN1_SIMPLE(CMS_IssuerAndSerialNumber, serialNumber, ASN1_INTEGER)
65} ASN1_SEQUENCE_END(CMS_IssuerAndSerialNumber) 65} ASN1_SEQUENCE_END(CMS_IssuerAndSerialNumber)
66 66
67ASN1_SEQUENCE(CMS_OtherCertificateFormat) = { 67ASN1_SEQUENCE(CMS_OtherCertificateFormat) = {
68 ASN1_SIMPLE(CMS_OtherCertificateFormat, otherCertFormat, ASN1_OBJECT), 68 ASN1_SIMPLE(CMS_OtherCertificateFormat, otherCertFormat, ASN1_OBJECT),
69 ASN1_OPT(CMS_OtherCertificateFormat, otherCert, ASN1_ANY) 69 ASN1_OPT(CMS_OtherCertificateFormat, otherCert, ASN1_ANY)
70} static_ASN1_SEQUENCE_END(CMS_OtherCertificateFormat) 70} static_ASN1_SEQUENCE_END(CMS_OtherCertificateFormat)
71 71
72ASN1_CHOICE(CMS_CertificateChoices) = { 72ASN1_CHOICE(CMS_CertificateChoices) = {
73 ASN1_SIMPLE(CMS_CertificateChoices, d.certificate, X509), 73 ASN1_SIMPLE(CMS_CertificateChoices, d.certificate, X509),
74 ASN1_IMP(CMS_CertificateChoices, d.extendedCertificate, ASN1_SEQUENCE, 0), 74 ASN1_IMP(CMS_CertificateChoices, d.extendedCertificate, ASN1_SEQUENCE, 0),
75 ASN1_IMP(CMS_CertificateChoices, d.v1AttrCert, ASN1_SEQUENCE, 1), 75 ASN1_IMP(CMS_CertificateChoices, d.v1AttrCert, ASN1_SEQUENCE, 1),
76 ASN1_IMP(CMS_CertificateChoices, d.v2AttrCert, ASN1_SEQUENCE, 2), 76 ASN1_IMP(CMS_CertificateChoices, d.v2AttrCert, ASN1_SEQUENCE, 2),
77 ASN1_IMP(CMS_CertificateChoices, d.other, CMS_OtherCertificateFormat, 3) 77 ASN1_IMP(CMS_CertificateChoices, d.other, CMS_OtherCertificateFormat, 3)
78} ASN1_CHOICE_END(CMS_CertificateChoices) 78} ASN1_CHOICE_END(CMS_CertificateChoices)
79 79
80ASN1_CHOICE(CMS_SignerIdentifier) = { 80ASN1_CHOICE(CMS_SignerIdentifier) = {
81 ASN1_SIMPLE(CMS_SignerIdentifier, d.issuerAndSerialNumber, CMS_IssuerAndSerialNumber), 81 ASN1_SIMPLE(CMS_SignerIdentifier, d.issuerAndSerialNumber, CMS_IssuerAndSerialNumber),
82 ASN1_IMP(CMS_SignerIdentifier, d.subjectKeyIdentifier, ASN1_OCTET_STRING, 0) 82 ASN1_IMP(CMS_SignerIdentifier, d.subjectKeyIdentifier, ASN1_OCTET_STRING, 0)
83} static_ASN1_CHOICE_END(CMS_SignerIdentifier) 83} static_ASN1_CHOICE_END(CMS_SignerIdentifier)
84 84
85ASN1_NDEF_SEQUENCE(CMS_EncapsulatedContentInfo) = { 85ASN1_NDEF_SEQUENCE(CMS_EncapsulatedContentInfo) = {
86 ASN1_SIMPLE(CMS_EncapsulatedContentInfo, eContentType, ASN1_OBJECT), 86 ASN1_SIMPLE(CMS_EncapsulatedContentInfo, eContentType, ASN1_OBJECT),
87 ASN1_NDEF_EXP_OPT(CMS_EncapsulatedContentInfo, eContent, ASN1_OCTET_STRING_NDEF, 0) 87 ASN1_NDEF_EXP_OPT(CMS_EncapsulatedContentInfo, eContent, ASN1_OCTET_STRING_NDEF, 0)
88} static_ASN1_NDEF_SEQUENCE_END(CMS_EncapsulatedContentInfo) 88} static_ASN1_NDEF_SEQUENCE_END(CMS_EncapsulatedContentInfo)
89 89
90/* Minor tweak to operation: free up signer key, cert */ 90/* Minor tweak to operation: free up signer key, cert */
91static int cms_si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 91static int cms_si_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
92 void *exarg) 92 void *exarg)
93{ 93{
94 if (operation == ASN1_OP_FREE_POST) { 94 if (operation == ASN1_OP_FREE_POST) {
95 CMS_SignerInfo *si = (CMS_SignerInfo *)*pval; 95 CMS_SignerInfo *si = (CMS_SignerInfo *)*pval;
96 EVP_PKEY_free(si->pkey); 96 EVP_PKEY_free(si->pkey);
97 X509_free(si->signer); 97 X509_free(si->signer);
98 EVP_MD_CTX_free(si->mctx); 98 EVP_MD_CTX_free(si->mctx);
99 } 99 }
100 return 1; 100 return 1;
101} 101}
102 102
103ASN1_SEQUENCE_cb(CMS_SignerInfo, cms_si_cb) = { 103ASN1_SEQUENCE_cb(CMS_SignerInfo, cms_si_cb) = {
104 ASN1_EMBED(CMS_SignerInfo, version, INT32), 104 ASN1_EMBED(CMS_SignerInfo, version, INT32),
105 ASN1_SIMPLE(CMS_SignerInfo, sid, CMS_SignerIdentifier), 105 ASN1_SIMPLE(CMS_SignerInfo, sid, CMS_SignerIdentifier),
106 ASN1_SIMPLE(CMS_SignerInfo, digestAlgorithm, X509_ALGOR), 106 ASN1_SIMPLE(CMS_SignerInfo, digestAlgorithm, X509_ALGOR),
107 ASN1_IMP_SET_OF_OPT(CMS_SignerInfo, signedAttrs, X509_ATTRIBUTE, 0), 107 ASN1_IMP_SET_OF_OPT(CMS_SignerInfo, signedAttrs, X509_ATTRIBUTE, 0),
108 ASN1_SIMPLE(CMS_SignerInfo, signatureAlgorithm, X509_ALGOR), 108 ASN1_SIMPLE(CMS_SignerInfo, signatureAlgorithm, X509_ALGOR),
109 ASN1_SIMPLE(CMS_SignerInfo, signature, ASN1_OCTET_STRING), 109 ASN1_SIMPLE(CMS_SignerInfo, signature, ASN1_OCTET_STRING),
110 ASN1_IMP_SET_OF_OPT(CMS_SignerInfo, unsignedAttrs, X509_ATTRIBUTE, 1) 110 ASN1_IMP_SET_OF_OPT(CMS_SignerInfo, unsignedAttrs, X509_ATTRIBUTE, 1)
111} ASN1_SEQUENCE_END_cb(CMS_SignerInfo, CMS_SignerInfo) 111} ASN1_SEQUENCE_END_cb(CMS_SignerInfo, CMS_SignerInfo)
112 112
113ASN1_SEQUENCE(CMS_OtherRevocationInfoFormat) = { 113ASN1_SEQUENCE(CMS_OtherRevocationInfoFormat) = {
114 ASN1_SIMPLE(CMS_OtherRevocationInfoFormat, otherRevInfoFormat, ASN1_OBJECT), 114 ASN1_SIMPLE(CMS_OtherRevocationInfoFormat, otherRevInfoFormat, ASN1_OBJECT),
115 ASN1_OPT(CMS_OtherRevocationInfoFormat, otherRevInfo, ASN1_ANY) 115 ASN1_OPT(CMS_OtherRevocationInfoFormat, otherRevInfo, ASN1_ANY)
116} static_ASN1_SEQUENCE_END(CMS_OtherRevocationInfoFormat) 116} static_ASN1_SEQUENCE_END(CMS_OtherRevocationInfoFormat)
117 117
118ASN1_CHOICE(CMS_RevocationInfoChoice) = { 118ASN1_CHOICE(CMS_RevocationInfoChoice) = {
119 ASN1_SIMPLE(CMS_RevocationInfoChoice, d.crl, X509_CRL), 119 ASN1_SIMPLE(CMS_RevocationInfoChoice, d.crl, X509_CRL),
120 ASN1_IMP(CMS_RevocationInfoChoice, d.other, CMS_OtherRevocationInfoFormat, 1) 120 ASN1_IMP(CMS_RevocationInfoChoice, d.other, CMS_OtherRevocationInfoFormat, 1)
121} ASN1_CHOICE_END(CMS_RevocationInfoChoice) 121} ASN1_CHOICE_END(CMS_RevocationInfoChoice)
122 122
123ASN1_NDEF_SEQUENCE(CMS_SignedData) = { 123ASN1_NDEF_SEQUENCE(CMS_SignedData) = {
124 ASN1_EMBED(CMS_SignedData, version, INT32), 124 ASN1_EMBED(CMS_SignedData, version, INT32),
125 ASN1_SET_OF(CMS_SignedData, digestAlgorithms, X509_ALGOR), 125 ASN1_SET_OF(CMS_SignedData, digestAlgorithms, X509_ALGOR),
126 ASN1_SIMPLE(CMS_SignedData, encapContentInfo, CMS_EncapsulatedContentInfo), 126 ASN1_SIMPLE(CMS_SignedData, encapContentInfo, CMS_EncapsulatedContentInfo),
127 ASN1_IMP_SET_OF_OPT(CMS_SignedData, certificates, CMS_CertificateChoices, 0), 127 ASN1_IMP_SET_OF_OPT(CMS_SignedData, certificates, CMS_CertificateChoices, 0),
128 ASN1_IMP_SET_OF_OPT(CMS_SignedData, crls, CMS_RevocationInfoChoice, 1), 128 ASN1_IMP_SET_OF_OPT(CMS_SignedData, crls, CMS_RevocationInfoChoice, 1),
129 ASN1_SET_OF(CMS_SignedData, signerInfos, CMS_SignerInfo) 129 ASN1_SET_OF(CMS_SignedData, signerInfos, CMS_SignerInfo)
130} ASN1_NDEF_SEQUENCE_END(CMS_SignedData) 130} ASN1_NDEF_SEQUENCE_END(CMS_SignedData)
131 131
132ASN1_SEQUENCE(CMS_OriginatorInfo) = { 132ASN1_SEQUENCE(CMS_OriginatorInfo) = {
133 ASN1_IMP_SET_OF_OPT(CMS_OriginatorInfo, certificates, CMS_CertificateChoices, 0), 133 ASN1_IMP_SET_OF_OPT(CMS_OriginatorInfo, certificates, CMS_CertificateChoices, 0),
134 ASN1_IMP_SET_OF_OPT(CMS_OriginatorInfo, crls, CMS_RevocationInfoChoice, 1) 134 ASN1_IMP_SET_OF_OPT(CMS_OriginatorInfo, crls, CMS_RevocationInfoChoice, 1)
135} static_ASN1_SEQUENCE_END(CMS_OriginatorInfo) 135} static_ASN1_SEQUENCE_END(CMS_OriginatorInfo)
136 136
137ASN1_NDEF_SEQUENCE(CMS_EncryptedContentInfo) = { 137ASN1_NDEF_SEQUENCE(CMS_EncryptedContentInfo) = {
138 ASN1_SIMPLE(CMS_EncryptedContentInfo, contentType, ASN1_OBJECT), 138 ASN1_SIMPLE(CMS_EncryptedContentInfo, contentType, ASN1_OBJECT),
139 ASN1_SIMPLE(CMS_EncryptedContentInfo, contentEncryptionAlgorithm, X509_ALGOR), 139 ASN1_SIMPLE(CMS_EncryptedContentInfo, contentEncryptionAlgorithm, X509_ALGOR),
140 ASN1_IMP_OPT(CMS_EncryptedContentInfo, encryptedContent, ASN1_OCTET_STRING_NDEF, 0) 140 ASN1_IMP_OPT(CMS_EncryptedContentInfo, encryptedContent, ASN1_OCTET_STRING_NDEF, 0)
141} static_ASN1_NDEF_SEQUENCE_END(CMS_EncryptedContentInfo) 141} static_ASN1_NDEF_SEQUENCE_END(CMS_EncryptedContentInfo)
142 142
143ASN1_SEQUENCE(CMS_KeyTransRecipientInfo) = { 143ASN1_SEQUENCE(CMS_KeyTransRecipientInfo) = {
144 ASN1_EMBED(CMS_KeyTransRecipientInfo, version, INT32), 144 ASN1_EMBED(CMS_KeyTransRecipientInfo, version, INT32),
145 ASN1_SIMPLE(CMS_KeyTransRecipientInfo, rid, CMS_SignerIdentifier), 145 ASN1_SIMPLE(CMS_KeyTransRecipientInfo, rid, CMS_SignerIdentifier),
146 ASN1_SIMPLE(CMS_KeyTransRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR), 146 ASN1_SIMPLE(CMS_KeyTransRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR),
147 ASN1_SIMPLE(CMS_KeyTransRecipientInfo, encryptedKey, ASN1_OCTET_STRING) 147 ASN1_SIMPLE(CMS_KeyTransRecipientInfo, encryptedKey, ASN1_OCTET_STRING)
148} ASN1_SEQUENCE_END(CMS_KeyTransRecipientInfo) 148} ASN1_SEQUENCE_END(CMS_KeyTransRecipientInfo)
149 149
150ASN1_SEQUENCE(CMS_OtherKeyAttribute) = { 150ASN1_SEQUENCE(CMS_OtherKeyAttribute) = {
151 ASN1_SIMPLE(CMS_OtherKeyAttribute, keyAttrId, ASN1_OBJECT), 151 ASN1_SIMPLE(CMS_OtherKeyAttribute, keyAttrId, ASN1_OBJECT),
152 ASN1_OPT(CMS_OtherKeyAttribute, keyAttr, ASN1_ANY) 152 ASN1_OPT(CMS_OtherKeyAttribute, keyAttr, ASN1_ANY)
153} ASN1_SEQUENCE_END(CMS_OtherKeyAttribute) 153} ASN1_SEQUENCE_END(CMS_OtherKeyAttribute)
154 154
155ASN1_SEQUENCE(CMS_RecipientKeyIdentifier) = { 155ASN1_SEQUENCE(CMS_RecipientKeyIdentifier) = {
156 ASN1_SIMPLE(CMS_RecipientKeyIdentifier, subjectKeyIdentifier, ASN1_OCTET_STRING), 156 ASN1_SIMPLE(CMS_RecipientKeyIdentifier, subjectKeyIdentifier, ASN1_OCTET_STRING),
157 ASN1_OPT(CMS_RecipientKeyIdentifier, date, ASN1_GENERALIZEDTIME), 157 ASN1_OPT(CMS_RecipientKeyIdentifier, date, ASN1_GENERALIZEDTIME),
158 ASN1_OPT(CMS_RecipientKeyIdentifier, other, CMS_OtherKeyAttribute) 158 ASN1_OPT(CMS_RecipientKeyIdentifier, other, CMS_OtherKeyAttribute)
159} ASN1_SEQUENCE_END(CMS_RecipientKeyIdentifier) 159} ASN1_SEQUENCE_END(CMS_RecipientKeyIdentifier)
160 160
161ASN1_CHOICE(CMS_KeyAgreeRecipientIdentifier) = { 161ASN1_CHOICE(CMS_KeyAgreeRecipientIdentifier) = {
@@ -164,18 +164,18 @@ ASN1_CHOICE(CMS_KeyAgreeRecipientIdentifier) = {
164} static_ASN1_CHOICE_END(CMS_KeyAgreeRecipientIdentifier) 164} static_ASN1_CHOICE_END(CMS_KeyAgreeRecipientIdentifier)
165 165
166static int cms_rek_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 166static int cms_rek_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
167 void *exarg) 167 void *exarg)
168{ 168{
169 CMS_RecipientEncryptedKey *rek = (CMS_RecipientEncryptedKey *)*pval; 169 CMS_RecipientEncryptedKey *rek = (CMS_RecipientEncryptedKey *)*pval;
170 if (operation == ASN1_OP_FREE_POST) { 170 if (operation == ASN1_OP_FREE_POST) {
171 EVP_PKEY_free(rek->pkey); 171 EVP_PKEY_free(rek->pkey);
172 } 172 }
173 return 1; 173 return 1;
174} 174}
175 175
176ASN1_SEQUENCE_cb(CMS_RecipientEncryptedKey, cms_rek_cb) = { 176ASN1_SEQUENCE_cb(CMS_RecipientEncryptedKey, cms_rek_cb) = {
177 ASN1_SIMPLE(CMS_RecipientEncryptedKey, rid, CMS_KeyAgreeRecipientIdentifier), 177 ASN1_SIMPLE(CMS_RecipientEncryptedKey, rid, CMS_KeyAgreeRecipientIdentifier),
178 ASN1_SIMPLE(CMS_RecipientEncryptedKey, encryptedKey, ASN1_OCTET_STRING) 178 ASN1_SIMPLE(CMS_RecipientEncryptedKey, encryptedKey, ASN1_OCTET_STRING)
179} ASN1_SEQUENCE_END_cb(CMS_RecipientEncryptedKey, CMS_RecipientEncryptedKey) 179} ASN1_SEQUENCE_END_cb(CMS_RecipientEncryptedKey, CMS_RecipientEncryptedKey)
180 180
181ASN1_SEQUENCE(CMS_OriginatorPublicKey) = { 181ASN1_SEQUENCE(CMS_OriginatorPublicKey) = {
@@ -190,48 +190,48 @@ ASN1_CHOICE(CMS_OriginatorIdentifierOrKey) = {
190} static_ASN1_CHOICE_END(CMS_OriginatorIdentifierOrKey) 190} static_ASN1_CHOICE_END(CMS_OriginatorIdentifierOrKey)
191 191
192static int cms_kari_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 192static int cms_kari_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
193 void *exarg) 193 void *exarg)
194{ 194{
195 CMS_KeyAgreeRecipientInfo *kari = (CMS_KeyAgreeRecipientInfo *)*pval; 195 CMS_KeyAgreeRecipientInfo *kari = (CMS_KeyAgreeRecipientInfo *)*pval;
196 if (operation == ASN1_OP_NEW_POST) { 196 if (operation == ASN1_OP_NEW_POST) {
197 kari->ctx = EVP_CIPHER_CTX_new(); 197 kari->ctx = EVP_CIPHER_CTX_new();
198 if (kari->ctx == NULL) 198 if (kari->ctx == NULL)
199 return 0; 199 return 0;
200 EVP_CIPHER_CTX_set_flags(kari->ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); 200 EVP_CIPHER_CTX_set_flags(kari->ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
201 kari->pctx = NULL; 201 kari->pctx = NULL;
202 } else if (operation == ASN1_OP_FREE_POST) { 202 } else if (operation == ASN1_OP_FREE_POST) {
203 EVP_PKEY_CTX_free(kari->pctx); 203 EVP_PKEY_CTX_free(kari->pctx);
204 EVP_CIPHER_CTX_free(kari->ctx); 204 EVP_CIPHER_CTX_free(kari->ctx);
205 } 205 }
206 return 1; 206 return 1;
207} 207}
208 208
209ASN1_SEQUENCE_cb(CMS_KeyAgreeRecipientInfo, cms_kari_cb) = { 209ASN1_SEQUENCE_cb(CMS_KeyAgreeRecipientInfo, cms_kari_cb) = {
210 ASN1_EMBED(CMS_KeyAgreeRecipientInfo, version, INT32), 210 ASN1_EMBED(CMS_KeyAgreeRecipientInfo, version, INT32),
211 ASN1_EXP(CMS_KeyAgreeRecipientInfo, originator, CMS_OriginatorIdentifierOrKey, 0), 211 ASN1_EXP(CMS_KeyAgreeRecipientInfo, originator, CMS_OriginatorIdentifierOrKey, 0),
212 ASN1_EXP_OPT(CMS_KeyAgreeRecipientInfo, ukm, ASN1_OCTET_STRING, 1), 212 ASN1_EXP_OPT(CMS_KeyAgreeRecipientInfo, ukm, ASN1_OCTET_STRING, 1),
213 ASN1_SIMPLE(CMS_KeyAgreeRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR), 213 ASN1_SIMPLE(CMS_KeyAgreeRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR),
214 ASN1_SEQUENCE_OF(CMS_KeyAgreeRecipientInfo, recipientEncryptedKeys, CMS_RecipientEncryptedKey) 214 ASN1_SEQUENCE_OF(CMS_KeyAgreeRecipientInfo, recipientEncryptedKeys, CMS_RecipientEncryptedKey)
215} ASN1_SEQUENCE_END_cb(CMS_KeyAgreeRecipientInfo, CMS_KeyAgreeRecipientInfo) 215} ASN1_SEQUENCE_END_cb(CMS_KeyAgreeRecipientInfo, CMS_KeyAgreeRecipientInfo)
216 216
217ASN1_SEQUENCE(CMS_KEKIdentifier) = { 217ASN1_SEQUENCE(CMS_KEKIdentifier) = {
218 ASN1_SIMPLE(CMS_KEKIdentifier, keyIdentifier, ASN1_OCTET_STRING), 218 ASN1_SIMPLE(CMS_KEKIdentifier, keyIdentifier, ASN1_OCTET_STRING),
219 ASN1_OPT(CMS_KEKIdentifier, date, ASN1_GENERALIZEDTIME), 219 ASN1_OPT(CMS_KEKIdentifier, date, ASN1_GENERALIZEDTIME),
220 ASN1_OPT(CMS_KEKIdentifier, other, CMS_OtherKeyAttribute) 220 ASN1_OPT(CMS_KEKIdentifier, other, CMS_OtherKeyAttribute)
221} static_ASN1_SEQUENCE_END(CMS_KEKIdentifier) 221} static_ASN1_SEQUENCE_END(CMS_KEKIdentifier)
222 222
223ASN1_SEQUENCE(CMS_KEKRecipientInfo) = { 223ASN1_SEQUENCE(CMS_KEKRecipientInfo) = {
224 ASN1_EMBED(CMS_KEKRecipientInfo, version, INT32), 224 ASN1_EMBED(CMS_KEKRecipientInfo, version, INT32),
225 ASN1_SIMPLE(CMS_KEKRecipientInfo, kekid, CMS_KEKIdentifier), 225 ASN1_SIMPLE(CMS_KEKRecipientInfo, kekid, CMS_KEKIdentifier),
226 ASN1_SIMPLE(CMS_KEKRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR), 226 ASN1_SIMPLE(CMS_KEKRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR),
227 ASN1_SIMPLE(CMS_KEKRecipientInfo, encryptedKey, ASN1_OCTET_STRING) 227 ASN1_SIMPLE(CMS_KEKRecipientInfo, encryptedKey, ASN1_OCTET_STRING)
228} ASN1_SEQUENCE_END(CMS_KEKRecipientInfo) 228} ASN1_SEQUENCE_END(CMS_KEKRecipientInfo)
229 229
230ASN1_SEQUENCE(CMS_PasswordRecipientInfo) = { 230ASN1_SEQUENCE(CMS_PasswordRecipientInfo) = {
231 ASN1_EMBED(CMS_PasswordRecipientInfo, version, INT32), 231 ASN1_EMBED(CMS_PasswordRecipientInfo, version, INT32),
232 ASN1_IMP_OPT(CMS_PasswordRecipientInfo, keyDerivationAlgorithm, X509_ALGOR, 0), 232 ASN1_IMP_OPT(CMS_PasswordRecipientInfo, keyDerivationAlgorithm, X509_ALGOR, 0),
233 ASN1_SIMPLE(CMS_PasswordRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR), 233 ASN1_SIMPLE(CMS_PasswordRecipientInfo, keyEncryptionAlgorithm, X509_ALGOR),
234 ASN1_SIMPLE(CMS_PasswordRecipientInfo, encryptedKey, ASN1_OCTET_STRING) 234 ASN1_SIMPLE(CMS_PasswordRecipientInfo, encryptedKey, ASN1_OCTET_STRING)
235} ASN1_SEQUENCE_END(CMS_PasswordRecipientInfo) 235} ASN1_SEQUENCE_END(CMS_PasswordRecipientInfo)
236 236
237ASN1_SEQUENCE(CMS_OtherRecipientInfo) = { 237ASN1_SEQUENCE(CMS_OtherRecipientInfo) = {
@@ -241,71 +241,71 @@ ASN1_SEQUENCE(CMS_OtherRecipientInfo) = {
241 241
242/* Free up RecipientInfo additional data */ 242/* Free up RecipientInfo additional data */
243static int cms_ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 243static int cms_ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
244 void *exarg) 244 void *exarg)
245{ 245{
246 if (operation == ASN1_OP_FREE_PRE) { 246 if (operation == ASN1_OP_FREE_PRE) {
247 CMS_RecipientInfo *ri = (CMS_RecipientInfo *)*pval; 247 CMS_RecipientInfo *ri = (CMS_RecipientInfo *)*pval;
248 if (ri->type == CMS_RECIPINFO_TRANS) { 248 if (ri->type == CMS_RECIPINFO_TRANS) {
249 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri; 249 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
250 EVP_PKEY_free(ktri->pkey); 250 EVP_PKEY_free(ktri->pkey);
251 X509_free(ktri->recip); 251 X509_free(ktri->recip);
252 EVP_PKEY_CTX_free(ktri->pctx); 252 EVP_PKEY_CTX_free(ktri->pctx);
253 } else if (ri->type == CMS_RECIPINFO_KEK) { 253 } else if (ri->type == CMS_RECIPINFO_KEK) {
254 CMS_KEKRecipientInfo *kekri = ri->d.kekri; 254 CMS_KEKRecipientInfo *kekri = ri->d.kekri;
255 OPENSSL_clear_free(kekri->key, kekri->keylen); 255 OPENSSL_clear_free(kekri->key, kekri->keylen);
256 } else if (ri->type == CMS_RECIPINFO_PASS) { 256 } else if (ri->type == CMS_RECIPINFO_PASS) {
257 CMS_PasswordRecipientInfo *pwri = ri->d.pwri; 257 CMS_PasswordRecipientInfo *pwri = ri->d.pwri;
258 OPENSSL_clear_free(pwri->pass, pwri->passlen); 258 OPENSSL_clear_free(pwri->pass, pwri->passlen);
259 } 259 }
260 } 260 }
261 return 1; 261 return 1;
262} 262}
263 263
264ASN1_CHOICE_cb(CMS_RecipientInfo, cms_ri_cb) = { 264ASN1_CHOICE_cb(CMS_RecipientInfo, cms_ri_cb) = {
265 ASN1_SIMPLE(CMS_RecipientInfo, d.ktri, CMS_KeyTransRecipientInfo), 265 ASN1_SIMPLE(CMS_RecipientInfo, d.ktri, CMS_KeyTransRecipientInfo),
266 ASN1_IMP(CMS_RecipientInfo, d.kari, CMS_KeyAgreeRecipientInfo, 1), 266 ASN1_IMP(CMS_RecipientInfo, d.kari, CMS_KeyAgreeRecipientInfo, 1),
267 ASN1_IMP(CMS_RecipientInfo, d.kekri, CMS_KEKRecipientInfo, 2), 267 ASN1_IMP(CMS_RecipientInfo, d.kekri, CMS_KEKRecipientInfo, 2),
268 ASN1_IMP(CMS_RecipientInfo, d.pwri, CMS_PasswordRecipientInfo, 3), 268 ASN1_IMP(CMS_RecipientInfo, d.pwri, CMS_PasswordRecipientInfo, 3),
269 ASN1_IMP(CMS_RecipientInfo, d.ori, CMS_OtherRecipientInfo, 4) 269 ASN1_IMP(CMS_RecipientInfo, d.ori, CMS_OtherRecipientInfo, 4)
270} ASN1_CHOICE_END_cb(CMS_RecipientInfo, CMS_RecipientInfo, type) 270} ASN1_CHOICE_END_cb(CMS_RecipientInfo, CMS_RecipientInfo, type)
271 271
272ASN1_NDEF_SEQUENCE(CMS_EnvelopedData) = { 272ASN1_NDEF_SEQUENCE(CMS_EnvelopedData) = {
273 ASN1_EMBED(CMS_EnvelopedData, version, INT32), 273 ASN1_EMBED(CMS_EnvelopedData, version, INT32),
274 ASN1_IMP_OPT(CMS_EnvelopedData, originatorInfo, CMS_OriginatorInfo, 0), 274 ASN1_IMP_OPT(CMS_EnvelopedData, originatorInfo, CMS_OriginatorInfo, 0),
275 ASN1_SET_OF(CMS_EnvelopedData, recipientInfos, CMS_RecipientInfo), 275 ASN1_SET_OF(CMS_EnvelopedData, recipientInfos, CMS_RecipientInfo),
276 ASN1_SIMPLE(CMS_EnvelopedData, encryptedContentInfo, CMS_EncryptedContentInfo), 276 ASN1_SIMPLE(CMS_EnvelopedData, encryptedContentInfo, CMS_EncryptedContentInfo),
277 ASN1_IMP_SET_OF_OPT(CMS_EnvelopedData, unprotectedAttrs, X509_ATTRIBUTE, 1) 277 ASN1_IMP_SET_OF_OPT(CMS_EnvelopedData, unprotectedAttrs, X509_ATTRIBUTE, 1)
278} ASN1_NDEF_SEQUENCE_END(CMS_EnvelopedData) 278} ASN1_NDEF_SEQUENCE_END(CMS_EnvelopedData)
279 279
280ASN1_NDEF_SEQUENCE(CMS_DigestedData) = { 280ASN1_NDEF_SEQUENCE(CMS_DigestedData) = {
281 ASN1_EMBED(CMS_DigestedData, version, INT32), 281 ASN1_EMBED(CMS_DigestedData, version, INT32),
282 ASN1_SIMPLE(CMS_DigestedData, digestAlgorithm, X509_ALGOR), 282 ASN1_SIMPLE(CMS_DigestedData, digestAlgorithm, X509_ALGOR),
283 ASN1_SIMPLE(CMS_DigestedData, encapContentInfo, CMS_EncapsulatedContentInfo), 283 ASN1_SIMPLE(CMS_DigestedData, encapContentInfo, CMS_EncapsulatedContentInfo),
284 ASN1_SIMPLE(CMS_DigestedData, digest, ASN1_OCTET_STRING) 284 ASN1_SIMPLE(CMS_DigestedData, digest, ASN1_OCTET_STRING)
285} ASN1_NDEF_SEQUENCE_END(CMS_DigestedData) 285} ASN1_NDEF_SEQUENCE_END(CMS_DigestedData)
286 286
287ASN1_NDEF_SEQUENCE(CMS_EncryptedData) = { 287ASN1_NDEF_SEQUENCE(CMS_EncryptedData) = {
288 ASN1_EMBED(CMS_EncryptedData, version, INT32), 288 ASN1_EMBED(CMS_EncryptedData, version, INT32),
289 ASN1_SIMPLE(CMS_EncryptedData, encryptedContentInfo, CMS_EncryptedContentInfo), 289 ASN1_SIMPLE(CMS_EncryptedData, encryptedContentInfo, CMS_EncryptedContentInfo),
290 ASN1_IMP_SET_OF_OPT(CMS_EncryptedData, unprotectedAttrs, X509_ATTRIBUTE, 1) 290 ASN1_IMP_SET_OF_OPT(CMS_EncryptedData, unprotectedAttrs, X509_ATTRIBUTE, 1)
291} ASN1_NDEF_SEQUENCE_END(CMS_EncryptedData) 291} ASN1_NDEF_SEQUENCE_END(CMS_EncryptedData)
292 292
293ASN1_NDEF_SEQUENCE(CMS_AuthenticatedData) = { 293ASN1_NDEF_SEQUENCE(CMS_AuthenticatedData) = {
294 ASN1_EMBED(CMS_AuthenticatedData, version, INT32), 294 ASN1_EMBED(CMS_AuthenticatedData, version, INT32),
295 ASN1_IMP_OPT(CMS_AuthenticatedData, originatorInfo, CMS_OriginatorInfo, 0), 295 ASN1_IMP_OPT(CMS_AuthenticatedData, originatorInfo, CMS_OriginatorInfo, 0),
296 ASN1_SET_OF(CMS_AuthenticatedData, recipientInfos, CMS_RecipientInfo), 296 ASN1_SET_OF(CMS_AuthenticatedData, recipientInfos, CMS_RecipientInfo),
297 ASN1_SIMPLE(CMS_AuthenticatedData, macAlgorithm, X509_ALGOR), 297 ASN1_SIMPLE(CMS_AuthenticatedData, macAlgorithm, X509_ALGOR),
298 ASN1_IMP(CMS_AuthenticatedData, digestAlgorithm, X509_ALGOR, 1), 298 ASN1_IMP(CMS_AuthenticatedData, digestAlgorithm, X509_ALGOR, 1),
299 ASN1_SIMPLE(CMS_AuthenticatedData, encapContentInfo, CMS_EncapsulatedContentInfo), 299 ASN1_SIMPLE(CMS_AuthenticatedData, encapContentInfo, CMS_EncapsulatedContentInfo),
300 ASN1_IMP_SET_OF_OPT(CMS_AuthenticatedData, authAttrs, X509_ALGOR, 2), 300 ASN1_IMP_SET_OF_OPT(CMS_AuthenticatedData, authAttrs, X509_ALGOR, 2),
301 ASN1_SIMPLE(CMS_AuthenticatedData, mac, ASN1_OCTET_STRING), 301 ASN1_SIMPLE(CMS_AuthenticatedData, mac, ASN1_OCTET_STRING),
302 ASN1_IMP_SET_OF_OPT(CMS_AuthenticatedData, unauthAttrs, X509_ALGOR, 3) 302 ASN1_IMP_SET_OF_OPT(CMS_AuthenticatedData, unauthAttrs, X509_ALGOR, 3)
303} static_ASN1_NDEF_SEQUENCE_END(CMS_AuthenticatedData) 303} static_ASN1_NDEF_SEQUENCE_END(CMS_AuthenticatedData)
304 304
305ASN1_NDEF_SEQUENCE(CMS_CompressedData) = { 305ASN1_NDEF_SEQUENCE(CMS_CompressedData) = {
306 ASN1_EMBED(CMS_CompressedData, version, INT32), 306 ASN1_EMBED(CMS_CompressedData, version, INT32),
307 ASN1_SIMPLE(CMS_CompressedData, compressionAlgorithm, X509_ALGOR), 307 ASN1_SIMPLE(CMS_CompressedData, compressionAlgorithm, X509_ALGOR),
308 ASN1_SIMPLE(CMS_CompressedData, encapContentInfo, CMS_EncapsulatedContentInfo), 308 ASN1_SIMPLE(CMS_CompressedData, encapContentInfo, CMS_EncapsulatedContentInfo),
309} ASN1_NDEF_SEQUENCE_END(CMS_CompressedData) 309} ASN1_NDEF_SEQUENCE_END(CMS_CompressedData)
310 310
311/* This is the ANY DEFINED BY table for the top level ContentInfo structure */ 311/* This is the ANY DEFINED BY table for the top level ContentInfo structure */
@@ -313,50 +313,50 @@ ASN1_NDEF_SEQUENCE(CMS_CompressedData) = {
313ASN1_ADB_TEMPLATE(cms_default) = ASN1_EXP(CMS_ContentInfo, d.other, ASN1_ANY, 0); 313ASN1_ADB_TEMPLATE(cms_default) = ASN1_EXP(CMS_ContentInfo, d.other, ASN1_ANY, 0);
314 314
315ASN1_ADB(CMS_ContentInfo) = { 315ASN1_ADB(CMS_ContentInfo) = {
316 ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP(CMS_ContentInfo, d.data, ASN1_OCTET_STRING_NDEF, 0)), 316 ADB_ENTRY(NID_pkcs7_data, ASN1_NDEF_EXP(CMS_ContentInfo, d.data, ASN1_OCTET_STRING_NDEF, 0)),
317 ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP(CMS_ContentInfo, d.signedData, CMS_SignedData, 0)), 317 ADB_ENTRY(NID_pkcs7_signed, ASN1_NDEF_EXP(CMS_ContentInfo, d.signedData, CMS_SignedData, 0)),
318 ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP(CMS_ContentInfo, d.envelopedData, CMS_EnvelopedData, 0)), 318 ADB_ENTRY(NID_pkcs7_enveloped, ASN1_NDEF_EXP(CMS_ContentInfo, d.envelopedData, CMS_EnvelopedData, 0)),
319 ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP(CMS_ContentInfo, d.digestedData, CMS_DigestedData, 0)), 319 ADB_ENTRY(NID_pkcs7_digest, ASN1_NDEF_EXP(CMS_ContentInfo, d.digestedData, CMS_DigestedData, 0)),
320 ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP(CMS_ContentInfo, d.encryptedData, CMS_EncryptedData, 0)), 320 ADB_ENTRY(NID_pkcs7_encrypted, ASN1_NDEF_EXP(CMS_ContentInfo, d.encryptedData, CMS_EncryptedData, 0)),
321 ADB_ENTRY(NID_id_smime_ct_authData, ASN1_NDEF_EXP(CMS_ContentInfo, d.authenticatedData, CMS_AuthenticatedData, 0)), 321 ADB_ENTRY(NID_id_smime_ct_authData, ASN1_NDEF_EXP(CMS_ContentInfo, d.authenticatedData, CMS_AuthenticatedData, 0)),
322 ADB_ENTRY(NID_id_smime_ct_compressedData, ASN1_NDEF_EXP(CMS_ContentInfo, d.compressedData, CMS_CompressedData, 0)), 322 ADB_ENTRY(NID_id_smime_ct_compressedData, ASN1_NDEF_EXP(CMS_ContentInfo, d.compressedData, CMS_CompressedData, 0)),
323} ASN1_ADB_END(CMS_ContentInfo, 0, contentType, 0, &cms_default_tt, NULL); 323} ASN1_ADB_END(CMS_ContentInfo, 0, contentType, 0, &cms_default_tt, NULL);
324 324
325/* CMS streaming support */ 325/* CMS streaming support */
326static int cms_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it, 326static int cms_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
327 void *exarg) 327 void *exarg)
328{ 328{
329 ASN1_STREAM_ARG *sarg = exarg; 329 ASN1_STREAM_ARG *sarg = exarg;
330 CMS_ContentInfo *cms = NULL; 330 CMS_ContentInfo *cms = NULL;
331 if (pval) 331 if (pval)
332 cms = (CMS_ContentInfo *)*pval; 332 cms = (CMS_ContentInfo *)*pval;
333 else 333 else
334 return 1; 334 return 1;
335 switch (operation) { 335 switch (operation) {
336 336
337 case ASN1_OP_STREAM_PRE: 337 case ASN1_OP_STREAM_PRE:
338 if (CMS_stream(&sarg->boundary, cms) <= 0) 338 if (CMS_stream(&sarg->boundary, cms) <= 0)
339 return 0; 339 return 0;
340 /* fall thru */ 340 /* fall thru */
341 case ASN1_OP_DETACHED_PRE: 341 case ASN1_OP_DETACHED_PRE:
342 sarg->ndef_bio = CMS_dataInit(cms, sarg->out); 342 sarg->ndef_bio = CMS_dataInit(cms, sarg->out);
343 if (!sarg->ndef_bio) 343 if (!sarg->ndef_bio)
344 return 0; 344 return 0;
345 break; 345 break;
346 346
347 case ASN1_OP_STREAM_POST: 347 case ASN1_OP_STREAM_POST:
348 case ASN1_OP_DETACHED_POST: 348 case ASN1_OP_DETACHED_POST:
349 if (CMS_dataFinal(cms, sarg->ndef_bio) <= 0) 349 if (CMS_dataFinal(cms, sarg->ndef_bio) <= 0)
350 return 0; 350 return 0;
351 break; 351 break;
352 352
353 } 353 }
354 return 1; 354 return 1;
355} 355}
356 356
357ASN1_NDEF_SEQUENCE_cb(CMS_ContentInfo, cms_cb) = { 357ASN1_NDEF_SEQUENCE_cb(CMS_ContentInfo, cms_cb) = {
358 ASN1_SIMPLE(CMS_ContentInfo, contentType, ASN1_OBJECT), 358 ASN1_SIMPLE(CMS_ContentInfo, contentType, ASN1_OBJECT),
359 ASN1_ADB_OBJECT(CMS_ContentInfo) 359 ASN1_ADB_OBJECT(CMS_ContentInfo)
360} ASN1_NDEF_SEQUENCE_END_cb(CMS_ContentInfo, CMS_ContentInfo) 360} ASN1_NDEF_SEQUENCE_END_cb(CMS_ContentInfo, CMS_ContentInfo)
361 361
362/* Specials for signed attributes */ 362/* Specials for signed attributes */
@@ -367,7 +367,7 @@ ASN1_NDEF_SEQUENCE_cb(CMS_ContentInfo, cms_cb) = {
367 */ 367 */
368 368
369ASN1_ITEM_TEMPLATE(CMS_Attributes_Sign) = 369ASN1_ITEM_TEMPLATE(CMS_Attributes_Sign) =
370 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, CMS_ATTRIBUTES, X509_ATTRIBUTE) 370 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_ORDER, 0, CMS_ATTRIBUTES, X509_ATTRIBUTE)
371ASN1_ITEM_TEMPLATE_END(CMS_Attributes_Sign) 371ASN1_ITEM_TEMPLATE_END(CMS_Attributes_Sign)
372 372
373/* 373/*
@@ -376,8 +376,8 @@ ASN1_ITEM_TEMPLATE_END(CMS_Attributes_Sign)
376 */ 376 */
377 377
378ASN1_ITEM_TEMPLATE(CMS_Attributes_Verify) = 378ASN1_ITEM_TEMPLATE(CMS_Attributes_Verify) =
379 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL, 379 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF | ASN1_TFLG_IMPTAG | ASN1_TFLG_UNIVERSAL,
380 V_ASN1_SET, CMS_ATTRIBUTES, X509_ATTRIBUTE) 380 V_ASN1_SET, CMS_ATTRIBUTES, X509_ATTRIBUTE)
381ASN1_ITEM_TEMPLATE_END(CMS_Attributes_Verify) 381ASN1_ITEM_TEMPLATE_END(CMS_Attributes_Verify)
382 382
383 383
@@ -406,9 +406,9 @@ ASN1_SEQUENCE(CMS_Receipt) = {
406 */ 406 */
407 407
408typedef struct { 408typedef struct {
409 X509_ALGOR *keyInfo; 409 X509_ALGOR *keyInfo;
410 ASN1_OCTET_STRING *entityUInfo; 410 ASN1_OCTET_STRING *entityUInfo;
411 ASN1_OCTET_STRING *suppPubInfo; 411 ASN1_OCTET_STRING *suppPubInfo;
412} CMS_SharedInfo; 412} CMS_SharedInfo;
413 413
414ASN1_SEQUENCE(CMS_SharedInfo) = { 414ASN1_SEQUENCE(CMS_SharedInfo) = {
@@ -418,31 +418,31 @@ ASN1_SEQUENCE(CMS_SharedInfo) = {
418} static_ASN1_SEQUENCE_END(CMS_SharedInfo) 418} static_ASN1_SEQUENCE_END(CMS_SharedInfo)
419 419
420int CMS_SharedInfo_encode(unsigned char **pder, X509_ALGOR *kekalg, 420int CMS_SharedInfo_encode(unsigned char **pder, X509_ALGOR *kekalg,
421 ASN1_OCTET_STRING *ukm, int keylen) 421 ASN1_OCTET_STRING *ukm, int keylen)
422{ 422{
423 union { 423 union {
424 CMS_SharedInfo *pecsi; 424 CMS_SharedInfo *pecsi;
425 ASN1_VALUE *a; 425 ASN1_VALUE *a;
426 } intsi = { 426 } intsi = {
427 NULL 427 NULL
428 }; 428 };
429 429
430 ASN1_OCTET_STRING oklen; 430 ASN1_OCTET_STRING oklen;
431 unsigned char kl[4]; 431 unsigned char kl[4];
432 CMS_SharedInfo ecsi; 432 CMS_SharedInfo ecsi;
433 433
434 keylen <<= 3; 434 keylen <<= 3;
435 kl[0] = (keylen >> 24) & 0xff; 435 kl[0] = (keylen >> 24) & 0xff;
436 kl[1] = (keylen >> 16) & 0xff; 436 kl[1] = (keylen >> 16) & 0xff;
437 kl[2] = (keylen >> 8) & 0xff; 437 kl[2] = (keylen >> 8) & 0xff;
438 kl[3] = keylen & 0xff; 438 kl[3] = keylen & 0xff;
439 oklen.length = 4; 439 oklen.length = 4;
440 oklen.data = kl; 440 oklen.data = kl;
441 oklen.type = V_ASN1_OCTET_STRING; 441 oklen.type = V_ASN1_OCTET_STRING;
442 oklen.flags = 0; 442 oklen.flags = 0;
443 ecsi.keyInfo = kekalg; 443 ecsi.keyInfo = kekalg;
444 ecsi.entityUInfo = ukm; 444 ecsi.entityUInfo = ukm;
445 ecsi.suppPubInfo = &oklen; 445 ecsi.suppPubInfo = &oklen;
446 intsi.pecsi = &ecsi; 446 intsi.pecsi = &ecsi;
447 return ASN1_item_i2d(intsi.a, pder, ASN1_ITEM_rptr(CMS_SharedInfo)); 447 return ASN1_item_i2d(intsi.a, pder, ASN1_ITEM_rptr(CMS_SharedInfo));
448} 448}
diff --git a/src/lib/libcrypto/cms/cms_att.c b/src/lib/libcrypto/cms/cms_att.c
index db896b10c3..b288262b81 100644
--- a/src/lib/libcrypto/cms/cms_att.c
+++ b/src/lib/libcrypto/cms/cms_att.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_att.c,v 1.7 2019/08/10 16:03:53 jsing Exp $ */ 1/* $OpenBSD: cms_att.c,v 1.8 2019/08/10 16:42:20 jsing Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -63,135 +63,135 @@
63 63
64int CMS_signed_get_attr_count(const CMS_SignerInfo *si) 64int CMS_signed_get_attr_count(const CMS_SignerInfo *si)
65{ 65{
66 return X509at_get_attr_count(si->signedAttrs); 66 return X509at_get_attr_count(si->signedAttrs);
67} 67}
68 68
69int CMS_signed_get_attr_by_NID(const CMS_SignerInfo *si, int nid, int lastpos) 69int CMS_signed_get_attr_by_NID(const CMS_SignerInfo *si, int nid, int lastpos)
70{ 70{
71 return X509at_get_attr_by_NID(si->signedAttrs, nid, lastpos); 71 return X509at_get_attr_by_NID(si->signedAttrs, nid, lastpos);
72} 72}
73 73
74int CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo *si, const ASN1_OBJECT *obj, 74int CMS_signed_get_attr_by_OBJ(const CMS_SignerInfo *si, const ASN1_OBJECT *obj,
75 int lastpos) 75 int lastpos)
76{ 76{
77 return X509at_get_attr_by_OBJ(si->signedAttrs, obj, lastpos); 77 return X509at_get_attr_by_OBJ(si->signedAttrs, obj, lastpos);
78} 78}
79 79
80X509_ATTRIBUTE *CMS_signed_get_attr(const CMS_SignerInfo *si, int loc) 80X509_ATTRIBUTE *CMS_signed_get_attr(const CMS_SignerInfo *si, int loc)
81{ 81{
82 return X509at_get_attr(si->signedAttrs, loc); 82 return X509at_get_attr(si->signedAttrs, loc);
83} 83}
84 84
85X509_ATTRIBUTE *CMS_signed_delete_attr(CMS_SignerInfo *si, int loc) 85X509_ATTRIBUTE *CMS_signed_delete_attr(CMS_SignerInfo *si, int loc)
86{ 86{
87 return X509at_delete_attr(si->signedAttrs, loc); 87 return X509at_delete_attr(si->signedAttrs, loc);
88} 88}
89 89
90int CMS_signed_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr) 90int CMS_signed_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
91{ 91{
92 if (X509at_add1_attr(&si->signedAttrs, attr)) 92 if (X509at_add1_attr(&si->signedAttrs, attr))
93 return 1; 93 return 1;
94 return 0; 94 return 0;
95} 95}
96 96
97int CMS_signed_add1_attr_by_OBJ(CMS_SignerInfo *si, 97int CMS_signed_add1_attr_by_OBJ(CMS_SignerInfo *si,
98 const ASN1_OBJECT *obj, int type, 98 const ASN1_OBJECT *obj, int type,
99 const void *bytes, int len) 99 const void *bytes, int len)
100{ 100{
101 if (X509at_add1_attr_by_OBJ(&si->signedAttrs, obj, type, bytes, len)) 101 if (X509at_add1_attr_by_OBJ(&si->signedAttrs, obj, type, bytes, len))
102 return 1; 102 return 1;
103 return 0; 103 return 0;
104} 104}
105 105
106int CMS_signed_add1_attr_by_NID(CMS_SignerInfo *si, 106int CMS_signed_add1_attr_by_NID(CMS_SignerInfo *si,
107 int nid, int type, const void *bytes, int len) 107 int nid, int type, const void *bytes, int len)
108{ 108{
109 if (X509at_add1_attr_by_NID(&si->signedAttrs, nid, type, bytes, len)) 109 if (X509at_add1_attr_by_NID(&si->signedAttrs, nid, type, bytes, len))
110 return 1; 110 return 1;
111 return 0; 111 return 0;
112} 112}
113 113
114int CMS_signed_add1_attr_by_txt(CMS_SignerInfo *si, 114int CMS_signed_add1_attr_by_txt(CMS_SignerInfo *si,
115 const char *attrname, int type, 115 const char *attrname, int type,
116 const void *bytes, int len) 116 const void *bytes, int len)
117{ 117{
118 if (X509at_add1_attr_by_txt(&si->signedAttrs, attrname, type, bytes, len)) 118 if (X509at_add1_attr_by_txt(&si->signedAttrs, attrname, type, bytes, len))
119 return 1; 119 return 1;
120 return 0; 120 return 0;
121} 121}
122 122
123void *CMS_signed_get0_data_by_OBJ(CMS_SignerInfo *si, const ASN1_OBJECT *oid, 123void *CMS_signed_get0_data_by_OBJ(CMS_SignerInfo *si, const ASN1_OBJECT *oid,
124 int lastpos, int type) 124 int lastpos, int type)
125{ 125{
126 return X509at_get0_data_by_OBJ(si->signedAttrs, oid, lastpos, type); 126 return X509at_get0_data_by_OBJ(si->signedAttrs, oid, lastpos, type);
127} 127}
128 128
129int CMS_unsigned_get_attr_count(const CMS_SignerInfo *si) 129int CMS_unsigned_get_attr_count(const CMS_SignerInfo *si)
130{ 130{
131 return X509at_get_attr_count(si->unsignedAttrs); 131 return X509at_get_attr_count(si->unsignedAttrs);
132} 132}
133 133
134int CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo *si, int nid, 134int CMS_unsigned_get_attr_by_NID(const CMS_SignerInfo *si, int nid,
135 int lastpos) 135 int lastpos)
136{ 136{
137 return X509at_get_attr_by_NID(si->unsignedAttrs, nid, lastpos); 137 return X509at_get_attr_by_NID(si->unsignedAttrs, nid, lastpos);
138} 138}
139 139
140int CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo *si, 140int CMS_unsigned_get_attr_by_OBJ(const CMS_SignerInfo *si,
141 const ASN1_OBJECT *obj, int lastpos) 141 const ASN1_OBJECT *obj, int lastpos)
142{ 142{
143 return X509at_get_attr_by_OBJ(si->unsignedAttrs, obj, lastpos); 143 return X509at_get_attr_by_OBJ(si->unsignedAttrs, obj, lastpos);
144} 144}
145 145
146X509_ATTRIBUTE *CMS_unsigned_get_attr(const CMS_SignerInfo *si, int loc) 146X509_ATTRIBUTE *CMS_unsigned_get_attr(const CMS_SignerInfo *si, int loc)
147{ 147{
148 return X509at_get_attr(si->unsignedAttrs, loc); 148 return X509at_get_attr(si->unsignedAttrs, loc);
149} 149}
150 150
151X509_ATTRIBUTE *CMS_unsigned_delete_attr(CMS_SignerInfo *si, int loc) 151X509_ATTRIBUTE *CMS_unsigned_delete_attr(CMS_SignerInfo *si, int loc)
152{ 152{
153 return X509at_delete_attr(si->unsignedAttrs, loc); 153 return X509at_delete_attr(si->unsignedAttrs, loc);
154} 154}
155 155
156int CMS_unsigned_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr) 156int CMS_unsigned_add1_attr(CMS_SignerInfo *si, X509_ATTRIBUTE *attr)
157{ 157{
158 if (X509at_add1_attr(&si->unsignedAttrs, attr)) 158 if (X509at_add1_attr(&si->unsignedAttrs, attr))
159 return 1; 159 return 1;
160 return 0; 160 return 0;
161} 161}
162 162
163int CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo *si, 163int CMS_unsigned_add1_attr_by_OBJ(CMS_SignerInfo *si,
164 const ASN1_OBJECT *obj, int type, 164 const ASN1_OBJECT *obj, int type,
165 const void *bytes, int len) 165 const void *bytes, int len)
166{ 166{
167 if (X509at_add1_attr_by_OBJ(&si->unsignedAttrs, obj, type, bytes, len)) 167 if (X509at_add1_attr_by_OBJ(&si->unsignedAttrs, obj, type, bytes, len))
168 return 1; 168 return 1;
169 return 0; 169 return 0;
170} 170}
171 171
172int CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo *si, 172int CMS_unsigned_add1_attr_by_NID(CMS_SignerInfo *si,
173 int nid, int type, 173 int nid, int type,
174 const void *bytes, int len) 174 const void *bytes, int len)
175{ 175{
176 if (X509at_add1_attr_by_NID(&si->unsignedAttrs, nid, type, bytes, len)) 176 if (X509at_add1_attr_by_NID(&si->unsignedAttrs, nid, type, bytes, len))
177 return 1; 177 return 1;
178 return 0; 178 return 0;
179} 179}
180 180
181int CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo *si, 181int CMS_unsigned_add1_attr_by_txt(CMS_SignerInfo *si,
182 const char *attrname, int type, 182 const char *attrname, int type,
183 const void *bytes, int len) 183 const void *bytes, int len)
184{ 184{
185 if (X509at_add1_attr_by_txt(&si->unsignedAttrs, attrname, 185 if (X509at_add1_attr_by_txt(&si->unsignedAttrs, attrname,
186 type, bytes, len)) 186 type, bytes, len))
187 return 1; 187 return 1;
188 return 0; 188 return 0;
189} 189}
190 190
191void *CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid, 191void *CMS_unsigned_get0_data_by_OBJ(CMS_SignerInfo *si, ASN1_OBJECT *oid,
192 int lastpos, int type) 192 int lastpos, int type)
193{ 193{
194 return X509at_get0_data_by_OBJ(si->unsignedAttrs, oid, lastpos, type); 194 return X509at_get0_data_by_OBJ(si->unsignedAttrs, oid, lastpos, type);
195} 195}
196 196
197/* Specific attribute cases */ 197/* Specific attribute cases */
diff --git a/src/lib/libcrypto/cms/cms_cd.c b/src/lib/libcrypto/cms/cms_cd.c
index 8882f1c898..55fff110d1 100644
--- a/src/lib/libcrypto/cms/cms_cd.c
+++ b/src/lib/libcrypto/cms/cms_cd.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_cd.c,v 1.11 2019/08/10 16:39:17 jsing Exp $ */ 1/* $OpenBSD: cms_cd.c,v 1.12 2019/08/10 16:42:20 jsing Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -68,60 +68,60 @@
68 68
69CMS_ContentInfo *cms_CompressedData_create(int comp_nid) 69CMS_ContentInfo *cms_CompressedData_create(int comp_nid)
70{ 70{
71 CMS_ContentInfo *cms; 71 CMS_ContentInfo *cms;
72 CMS_CompressedData *cd; 72 CMS_CompressedData *cd;
73 /* 73 /*
74 * Will need something cleverer if there is ever more than one 74 * Will need something cleverer if there is ever more than one
75 * compression algorithm or parameters have some meaning... 75 * compression algorithm or parameters have some meaning...
76 */ 76 */
77 if (comp_nid != NID_zlib_compression) { 77 if (comp_nid != NID_zlib_compression) {
78 CMSerr(CMS_F_CMS_COMPRESSEDDATA_CREATE, 78 CMSerr(CMS_F_CMS_COMPRESSEDDATA_CREATE,
79 CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM); 79 CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
80 return NULL; 80 return NULL;
81 } 81 }
82 cms = CMS_ContentInfo_new(); 82 cms = CMS_ContentInfo_new();
83 if (cms == NULL) 83 if (cms == NULL)
84 return NULL; 84 return NULL;
85 85
86 cd = M_ASN1_new_of(CMS_CompressedData); 86 cd = M_ASN1_new_of(CMS_CompressedData);
87 87
88 if (cd == NULL) 88 if (cd == NULL)
89 goto err; 89 goto err;
90 90
91 cms->contentType = OBJ_nid2obj(NID_id_smime_ct_compressedData); 91 cms->contentType = OBJ_nid2obj(NID_id_smime_ct_compressedData);
92 cms->d.compressedData = cd; 92 cms->d.compressedData = cd;
93 93
94 cd->version = 0; 94 cd->version = 0;
95 95
96 X509_ALGOR_set0(cd->compressionAlgorithm, 96 X509_ALGOR_set0(cd->compressionAlgorithm,
97 OBJ_nid2obj(NID_zlib_compression), V_ASN1_UNDEF, NULL); 97 OBJ_nid2obj(NID_zlib_compression), V_ASN1_UNDEF, NULL);
98 98
99 cd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data); 99 cd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data);
100 100
101 return cms; 101 return cms;
102 102
103 err: 103 err:
104 CMS_ContentInfo_free(cms); 104 CMS_ContentInfo_free(cms);
105 return NULL; 105 return NULL;
106} 106}
107 107
108BIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms) 108BIO *cms_CompressedData_init_bio(CMS_ContentInfo *cms)
109{ 109{
110 CMS_CompressedData *cd; 110 CMS_CompressedData *cd;
111 const ASN1_OBJECT *compoid; 111 const ASN1_OBJECT *compoid;
112 if (OBJ_obj2nid(cms->contentType) != NID_id_smime_ct_compressedData) { 112 if (OBJ_obj2nid(cms->contentType) != NID_id_smime_ct_compressedData) {
113 CMSerr(CMS_F_CMS_COMPRESSEDDATA_INIT_BIO, 113 CMSerr(CMS_F_CMS_COMPRESSEDDATA_INIT_BIO,
114 CMS_R_CONTENT_TYPE_NOT_COMPRESSED_DATA); 114 CMS_R_CONTENT_TYPE_NOT_COMPRESSED_DATA);
115 return NULL; 115 return NULL;
116 } 116 }
117 cd = cms->d.compressedData; 117 cd = cms->d.compressedData;
118 X509_ALGOR_get0(&compoid, NULL, NULL, cd->compressionAlgorithm); 118 X509_ALGOR_get0(&compoid, NULL, NULL, cd->compressionAlgorithm);
119 if (OBJ_obj2nid(compoid) != NID_zlib_compression) { 119 if (OBJ_obj2nid(compoid) != NID_zlib_compression) {
120 CMSerr(CMS_F_CMS_COMPRESSEDDATA_INIT_BIO, 120 CMSerr(CMS_F_CMS_COMPRESSEDDATA_INIT_BIO,
121 CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM); 121 CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
122 return NULL; 122 return NULL;
123 } 123 }
124 return BIO_new(BIO_f_zlib()); 124 return BIO_new(BIO_f_zlib());
125} 125}
126 126
127#endif 127#endif
diff --git a/src/lib/libcrypto/cms/cms_dd.c b/src/lib/libcrypto/cms/cms_dd.c
index 1f834b6efb..fcc446fa2e 100644
--- a/src/lib/libcrypto/cms/cms_dd.c
+++ b/src/lib/libcrypto/cms/cms_dd.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_dd.c,v 1.9 2019/08/10 16:39:17 jsing Exp $ */ 1/* $OpenBSD: cms_dd.c,v 1.10 2019/08/10 16:42:20 jsing Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -64,81 +64,81 @@
64 64
65CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md) 65CMS_ContentInfo *cms_DigestedData_create(const EVP_MD *md)
66{ 66{
67 CMS_ContentInfo *cms; 67 CMS_ContentInfo *cms;
68 CMS_DigestedData *dd; 68 CMS_DigestedData *dd;
69 cms = CMS_ContentInfo_new(); 69 cms = CMS_ContentInfo_new();
70 if (cms == NULL) 70 if (cms == NULL)
71 return NULL; 71 return NULL;
72 72
73 dd = M_ASN1_new_of(CMS_DigestedData); 73 dd = M_ASN1_new_of(CMS_DigestedData);
74 74
75 if (dd == NULL) 75 if (dd == NULL)
76 goto err; 76 goto err;
77 77
78 cms->contentType = OBJ_nid2obj(NID_pkcs7_digest); 78 cms->contentType = OBJ_nid2obj(NID_pkcs7_digest);
79 cms->d.digestedData = dd; 79 cms->d.digestedData = dd;
80 80
81 dd->version = 0; 81 dd->version = 0;
82 dd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data); 82 dd->encapContentInfo->eContentType = OBJ_nid2obj(NID_pkcs7_data);
83 83
84 X509_ALGOR_set_md(dd->digestAlgorithm, md); 84 X509_ALGOR_set_md(dd->digestAlgorithm, md);
85 85
86 return cms; 86 return cms;
87 87
88 err: 88 err:
89 CMS_ContentInfo_free(cms); 89 CMS_ContentInfo_free(cms);
90 return NULL; 90 return NULL;
91} 91}
92 92
93BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms) 93BIO *cms_DigestedData_init_bio(CMS_ContentInfo *cms)
94{ 94{
95 CMS_DigestedData *dd; 95 CMS_DigestedData *dd;
96 dd = cms->d.digestedData; 96 dd = cms->d.digestedData;
97 return cms_DigestAlgorithm_init_bio(dd->digestAlgorithm); 97 return cms_DigestAlgorithm_init_bio(dd->digestAlgorithm);
98} 98}
99 99
100int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify) 100int cms_DigestedData_do_final(CMS_ContentInfo *cms, BIO *chain, int verify)
101{ 101{
102 EVP_MD_CTX *mctx = EVP_MD_CTX_new(); 102 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
103 unsigned char md[EVP_MAX_MD_SIZE]; 103 unsigned char md[EVP_MAX_MD_SIZE];
104 unsigned int mdlen; 104 unsigned int mdlen;
105 int r = 0; 105 int r = 0;
106 CMS_DigestedData *dd; 106 CMS_DigestedData *dd;
107 107
108 if (mctx == NULL) { 108 if (mctx == NULL) {
109 CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL, ERR_R_MALLOC_FAILURE); 109 CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL, ERR_R_MALLOC_FAILURE);
110 goto err; 110 goto err;
111 } 111 }
112 112
113 dd = cms->d.digestedData; 113 dd = cms->d.digestedData;
114 114
115 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, dd->digestAlgorithm)) 115 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, dd->digestAlgorithm))
116 goto err; 116 goto err;
117 117
118 if (EVP_DigestFinal_ex(mctx, md, &mdlen) <= 0) 118 if (EVP_DigestFinal_ex(mctx, md, &mdlen) <= 0)
119 goto err; 119 goto err;
120 120
121 if (verify) { 121 if (verify) {
122 if (mdlen != (unsigned int)dd->digest->length) { 122 if (mdlen != (unsigned int)dd->digest->length) {
123 CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL, 123 CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL,
124 CMS_R_MESSAGEDIGEST_WRONG_LENGTH); 124 CMS_R_MESSAGEDIGEST_WRONG_LENGTH);
125 goto err; 125 goto err;
126 } 126 }
127 127
128 if (memcmp(md, dd->digest->data, mdlen)) 128 if (memcmp(md, dd->digest->data, mdlen))
129 CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL, 129 CMSerr(CMS_F_CMS_DIGESTEDDATA_DO_FINAL,
130 CMS_R_VERIFICATION_FAILURE); 130 CMS_R_VERIFICATION_FAILURE);
131 else 131 else
132 r = 1; 132 r = 1;
133 } else { 133 } else {
134 if (!ASN1_STRING_set(dd->digest, md, mdlen)) 134 if (!ASN1_STRING_set(dd->digest, md, mdlen))
135 goto err; 135 goto err;
136 r = 1; 136 r = 1;
137 } 137 }
138 138
139 err: 139 err:
140 EVP_MD_CTX_free(mctx); 140 EVP_MD_CTX_free(mctx);
141 141
142 return r; 142 return r;
143 143
144} 144}
diff --git a/src/lib/libcrypto/cms/cms_enc.c b/src/lib/libcrypto/cms/cms_enc.c
index c8a3d705b5..b019d8e8e0 100644
--- a/src/lib/libcrypto/cms/cms_enc.c
+++ b/src/lib/libcrypto/cms/cms_enc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_enc.c,v 1.12 2019/08/10 16:39:17 jsing Exp $ */ 1/* $OpenBSD: cms_enc.c,v 1.13 2019/08/10 16:42:20 jsing Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -67,192 +67,192 @@
67 67
68BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec) 68BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec)
69{ 69{
70 BIO *b; 70 BIO *b;
71 EVP_CIPHER_CTX *ctx; 71 EVP_CIPHER_CTX *ctx;
72 const EVP_CIPHER *ciph; 72 const EVP_CIPHER *ciph;
73 X509_ALGOR *calg = ec->contentEncryptionAlgorithm; 73 X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
74 unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL; 74 unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL;
75 unsigned char *tkey = NULL; 75 unsigned char *tkey = NULL;
76 size_t tkeylen = 0; 76 size_t tkeylen = 0;
77 77
78 int ok = 0; 78 int ok = 0;
79 79
80 int enc, keep_key = 0; 80 int enc, keep_key = 0;
81 81
82 enc = ec->cipher ? 1 : 0; 82 enc = ec->cipher ? 1 : 0;
83 83
84 b = BIO_new(BIO_f_cipher()); 84 b = BIO_new(BIO_f_cipher());
85 if (b == NULL) { 85 if (b == NULL) {
86 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE); 86 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
87 return NULL; 87 return NULL;
88 } 88 }
89 89
90 BIO_get_cipher_ctx(b, &ctx); 90 BIO_get_cipher_ctx(b, &ctx);
91 91
92 if (enc) { 92 if (enc) {
93 ciph = ec->cipher; 93 ciph = ec->cipher;
94 /* 94 /*
95 * If not keeping key set cipher to NULL so subsequent calls decrypt. 95 * If not keeping key set cipher to NULL so subsequent calls decrypt.
96 */ 96 */
97 if (ec->key) 97 if (ec->key)
98 ec->cipher = NULL; 98 ec->cipher = NULL;
99 } else { 99 } else {
100 ciph = EVP_get_cipherbyobj(calg->algorithm); 100 ciph = EVP_get_cipherbyobj(calg->algorithm);
101 101
102 if (!ciph) { 102 if (!ciph) {
103 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, CMS_R_UNKNOWN_CIPHER); 103 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, CMS_R_UNKNOWN_CIPHER);
104 goto err; 104 goto err;
105 } 105 }
106 } 106 }
107 107
108 if (EVP_CipherInit_ex(ctx, ciph, NULL, NULL, NULL, enc) <= 0) { 108 if (EVP_CipherInit_ex(ctx, ciph, NULL, NULL, NULL, enc) <= 0) {
109 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, 109 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
110 CMS_R_CIPHER_INITIALISATION_ERROR); 110 CMS_R_CIPHER_INITIALISATION_ERROR);
111 goto err; 111 goto err;
112 } 112 }
113 113
114 if (enc) { 114 if (enc) {
115 int ivlen; 115 int ivlen;
116 calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx)); 116 calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
117 /* Generate a random IV if we need one */ 117 /* Generate a random IV if we need one */
118 ivlen = EVP_CIPHER_CTX_iv_length(ctx); 118 ivlen = EVP_CIPHER_CTX_iv_length(ctx);
119 if (ivlen > 0) { 119 if (ivlen > 0) {
120 if (RAND_bytes(iv, ivlen) <= 0) 120 if (RAND_bytes(iv, ivlen) <= 0)
121 goto err; 121 goto err;
122 piv = iv; 122 piv = iv;
123 } 123 }
124 } else if (EVP_CIPHER_asn1_to_param(ctx, calg->parameter) <= 0) { 124 } else if (EVP_CIPHER_asn1_to_param(ctx, calg->parameter) <= 0) {
125 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, 125 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
126 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR); 126 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
127 goto err; 127 goto err;
128 } 128 }
129 tkeylen = EVP_CIPHER_CTX_key_length(ctx); 129 tkeylen = EVP_CIPHER_CTX_key_length(ctx);
130 /* Generate random session key */ 130 /* Generate random session key */
131 if (!enc || !ec->key) { 131 if (!enc || !ec->key) {
132 tkey = OPENSSL_malloc(tkeylen); 132 tkey = OPENSSL_malloc(tkeylen);
133 if (tkey == NULL) { 133 if (tkey == NULL) {
134 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE); 134 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
135 goto err; 135 goto err;
136 } 136 }
137 if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0) 137 if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
138 goto err; 138 goto err;
139 } 139 }
140 140
141 if (!ec->key) { 141 if (!ec->key) {
142 ec->key = tkey; 142 ec->key = tkey;
143 ec->keylen = tkeylen; 143 ec->keylen = tkeylen;
144 tkey = NULL; 144 tkey = NULL;
145 if (enc) 145 if (enc)
146 keep_key = 1; 146 keep_key = 1;
147 else 147 else
148 ERR_clear_error(); 148 ERR_clear_error();
149 149
150 } 150 }
151 151
152 if (ec->keylen != tkeylen) { 152 if (ec->keylen != tkeylen) {
153 /* If necessary set key length */ 153 /* If necessary set key length */
154 if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) { 154 if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) {
155 /* 155 /*
156 * Only reveal failure if debugging so we don't leak information 156 * Only reveal failure if debugging so we don't leak information
157 * which may be useful in MMA. 157 * which may be useful in MMA.
158 */ 158 */
159 if (enc || ec->debug) { 159 if (enc || ec->debug) {
160 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, 160 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
161 CMS_R_INVALID_KEY_LENGTH); 161 CMS_R_INVALID_KEY_LENGTH);
162 goto err; 162 goto err;
163 } else { 163 } else {
164 /* Use random key */ 164 /* Use random key */
165 OPENSSL_clear_free(ec->key, ec->keylen); 165 OPENSSL_clear_free(ec->key, ec->keylen);
166 ec->key = tkey; 166 ec->key = tkey;
167 ec->keylen = tkeylen; 167 ec->keylen = tkeylen;
168 tkey = NULL; 168 tkey = NULL;
169 ERR_clear_error(); 169 ERR_clear_error();
170 } 170 }
171 } 171 }
172 } 172 }
173 173
174 if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) { 174 if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) {
175 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, 175 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
176 CMS_R_CIPHER_INITIALISATION_ERROR); 176 CMS_R_CIPHER_INITIALISATION_ERROR);
177 goto err; 177 goto err;
178 } 178 }
179 if (enc) { 179 if (enc) {
180 calg->parameter = ASN1_TYPE_new(); 180 calg->parameter = ASN1_TYPE_new();
181 if (calg->parameter == NULL) { 181 if (calg->parameter == NULL) {
182 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE); 182 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
183 goto err; 183 goto err;
184 } 184 }
185 if (EVP_CIPHER_param_to_asn1(ctx, calg->parameter) <= 0) { 185 if (EVP_CIPHER_param_to_asn1(ctx, calg->parameter) <= 0) {
186 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, 186 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
187 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR); 187 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
188 goto err; 188 goto err;
189 } 189 }
190 /* If parameter type not set omit parameter */ 190 /* If parameter type not set omit parameter */
191 if (calg->parameter->type == V_ASN1_UNDEF) { 191 if (calg->parameter->type == V_ASN1_UNDEF) {
192 ASN1_TYPE_free(calg->parameter); 192 ASN1_TYPE_free(calg->parameter);
193 calg->parameter = NULL; 193 calg->parameter = NULL;
194 } 194 }
195 } 195 }
196 ok = 1; 196 ok = 1;
197 197
198 err: 198 err:
199 if (!keep_key || !ok) { 199 if (!keep_key || !ok) {
200 OPENSSL_clear_free(ec->key, ec->keylen); 200 OPENSSL_clear_free(ec->key, ec->keylen);
201 ec->key = NULL; 201 ec->key = NULL;
202 } 202 }
203 OPENSSL_clear_free(tkey, tkeylen); 203 OPENSSL_clear_free(tkey, tkeylen);
204 if (ok) 204 if (ok)
205 return b; 205 return b;
206 BIO_free(b); 206 BIO_free(b);
207 return NULL; 207 return NULL;
208} 208}
209 209
210int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec, 210int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
211 const EVP_CIPHER *cipher, 211 const EVP_CIPHER *cipher,
212 const unsigned char *key, size_t keylen) 212 const unsigned char *key, size_t keylen)
213{ 213{
214 ec->cipher = cipher; 214 ec->cipher = cipher;
215 if (key) { 215 if (key) {
216 if ((ec->key = OPENSSL_malloc(keylen)) == NULL) { 216 if ((ec->key = OPENSSL_malloc(keylen)) == NULL) {
217 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT, ERR_R_MALLOC_FAILURE); 217 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT, ERR_R_MALLOC_FAILURE);
218 return 0; 218 return 0;
219 } 219 }
220 memcpy(ec->key, key, keylen); 220 memcpy(ec->key, key, keylen);
221 } 221 }
222 ec->keylen = keylen; 222 ec->keylen = keylen;
223 if (cipher) 223 if (cipher)
224 ec->contentType = OBJ_nid2obj(NID_pkcs7_data); 224 ec->contentType = OBJ_nid2obj(NID_pkcs7_data);
225 return 1; 225 return 1;
226} 226}
227 227
228int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph, 228int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
229 const unsigned char *key, size_t keylen) 229 const unsigned char *key, size_t keylen)
230{ 230{
231 CMS_EncryptedContentInfo *ec; 231 CMS_EncryptedContentInfo *ec;
232 if (!key || !keylen) { 232 if (!key || !keylen) {
233 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NO_KEY); 233 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NO_KEY);
234 return 0; 234 return 0;
235 } 235 }
236 if (ciph) { 236 if (ciph) {
237 cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData); 237 cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
238 if (!cms->d.encryptedData) { 238 if (!cms->d.encryptedData) {
239 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, ERR_R_MALLOC_FAILURE); 239 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, ERR_R_MALLOC_FAILURE);
240 return 0; 240 return 0;
241 } 241 }
242 cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted); 242 cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);
243 cms->d.encryptedData->version = 0; 243 cms->d.encryptedData->version = 0;
244 } else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted) { 244 } else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted) {
245 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NOT_ENCRYPTED_DATA); 245 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NOT_ENCRYPTED_DATA);
246 return 0; 246 return 0;
247 } 247 }
248 ec = cms->d.encryptedData->encryptedContentInfo; 248 ec = cms->d.encryptedData->encryptedContentInfo;
249 return cms_EncryptedContent_init(ec, ciph, key, keylen); 249 return cms_EncryptedContent_init(ec, ciph, key, keylen);
250} 250}
251 251
252BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms) 252BIO *cms_EncryptedData_init_bio(CMS_ContentInfo *cms)
253{ 253{
254 CMS_EncryptedData *enc = cms->d.encryptedData; 254 CMS_EncryptedData *enc = cms->d.encryptedData;
255 if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs) 255 if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
256 enc->version = 2; 256 enc->version = 2;
257 return cms_EncryptedContent_init_bio(enc->encryptedContentInfo); 257 return cms_EncryptedContent_init_bio(enc->encryptedContentInfo);
258} 258}
diff --git a/src/lib/libcrypto/cms/cms_env.c b/src/lib/libcrypto/cms/cms_env.c
index ee92fc39ee..e137cadf34 100644
--- a/src/lib/libcrypto/cms/cms_env.c
+++ b/src/lib/libcrypto/cms/cms_env.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_env.c,v 1.14 2019/08/10 16:39:17 jsing Exp $ */ 1/* $OpenBSD: cms_env.c,v 1.15 2019/08/10 16:42:20 jsing Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -67,103 +67,103 @@
67 67
68CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms) 68CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
69{ 69{
70 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) { 70 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) {
71 CMSerr(CMS_F_CMS_GET0_ENVELOPED, 71 CMSerr(CMS_F_CMS_GET0_ENVELOPED,
72 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA); 72 CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA);
73 return NULL; 73 return NULL;
74 } 74 }
75 return cms->d.envelopedData; 75 return cms->d.envelopedData;
76} 76}
77 77
78static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms) 78static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms)
79{ 79{
80 if (cms->d.other == NULL) { 80 if (cms->d.other == NULL) {
81 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData); 81 cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData);
82 if (!cms->d.envelopedData) { 82 if (!cms->d.envelopedData) {
83 CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT, ERR_R_MALLOC_FAILURE); 83 CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT, ERR_R_MALLOC_FAILURE);
84 return NULL; 84 return NULL;
85 } 85 }
86 cms->d.envelopedData->version = 0; 86 cms->d.envelopedData->version = 0;
87 cms->d.envelopedData->encryptedContentInfo->contentType = 87 cms->d.envelopedData->encryptedContentInfo->contentType =
88 OBJ_nid2obj(NID_pkcs7_data); 88 OBJ_nid2obj(NID_pkcs7_data);
89 ASN1_OBJECT_free(cms->contentType); 89 ASN1_OBJECT_free(cms->contentType);
90 cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped); 90 cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped);
91 return cms->d.envelopedData; 91 return cms->d.envelopedData;
92 } 92 }
93 return cms_get0_enveloped(cms); 93 return cms_get0_enveloped(cms);
94} 94}
95 95
96int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd) 96int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd)
97{ 97{
98 EVP_PKEY *pkey; 98 EVP_PKEY *pkey;
99 int i; 99 int i;
100 if (ri->type == CMS_RECIPINFO_TRANS) 100 if (ri->type == CMS_RECIPINFO_TRANS)
101 pkey = ri->d.ktri->pkey; 101 pkey = ri->d.ktri->pkey;
102 else if (ri->type == CMS_RECIPINFO_AGREE) { 102 else if (ri->type == CMS_RECIPINFO_AGREE) {
103 EVP_PKEY_CTX *pctx = ri->d.kari->pctx; 103 EVP_PKEY_CTX *pctx = ri->d.kari->pctx;
104 if (!pctx) 104 if (!pctx)
105 return 0; 105 return 0;
106 pkey = EVP_PKEY_CTX_get0_pkey(pctx); 106 pkey = EVP_PKEY_CTX_get0_pkey(pctx);
107 if (!pkey) 107 if (!pkey)
108 return 0; 108 return 0;
109 } else 109 } else
110 return 0; 110 return 0;
111 if (!pkey->ameth || !pkey->ameth->pkey_ctrl) 111 if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
112 return 1; 112 return 1;
113 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri); 113 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri);
114 if (i == -2) { 114 if (i == -2) {
115 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, 115 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL,
116 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); 116 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
117 return 0; 117 return 0;
118 } 118 }
119 if (i <= 0) { 119 if (i <= 0) {
120 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE); 120 CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE);
121 return 0; 121 return 0;
122 } 122 }
123 return 1; 123 return 1;
124} 124}
125 125
126STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms) 126STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms)
127{ 127{
128 CMS_EnvelopedData *env; 128 CMS_EnvelopedData *env;
129 env = cms_get0_enveloped(cms); 129 env = cms_get0_enveloped(cms);
130 if (!env) 130 if (!env)
131 return NULL; 131 return NULL;
132 return env->recipientInfos; 132 return env->recipientInfos;
133} 133}
134 134
135int CMS_RecipientInfo_type(CMS_RecipientInfo *ri) 135int CMS_RecipientInfo_type(CMS_RecipientInfo *ri)
136{ 136{
137 return ri->type; 137 return ri->type;
138} 138}
139 139
140EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri) 140EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri)
141{ 141{
142 if (ri->type == CMS_RECIPINFO_TRANS) 142 if (ri->type == CMS_RECIPINFO_TRANS)
143 return ri->d.ktri->pctx; 143 return ri->d.ktri->pctx;
144 else if (ri->type == CMS_RECIPINFO_AGREE) 144 else if (ri->type == CMS_RECIPINFO_AGREE)
145 return ri->d.kari->pctx; 145 return ri->d.kari->pctx;
146 return NULL; 146 return NULL;
147} 147}
148 148
149CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher) 149CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
150{ 150{
151 CMS_ContentInfo *cms; 151 CMS_ContentInfo *cms;
152 CMS_EnvelopedData *env; 152 CMS_EnvelopedData *env;
153 cms = CMS_ContentInfo_new(); 153 cms = CMS_ContentInfo_new();
154 if (cms == NULL) 154 if (cms == NULL)
155 goto merr; 155 goto merr;
156 env = cms_enveloped_data_init(cms); 156 env = cms_enveloped_data_init(cms);
157 if (env == NULL) 157 if (env == NULL)
158 goto merr; 158 goto merr;
159 if (!cms_EncryptedContent_init(env->encryptedContentInfo, 159 if (!cms_EncryptedContent_init(env->encryptedContentInfo,
160 cipher, NULL, 0)) 160 cipher, NULL, 0))
161 goto merr; 161 goto merr;
162 return cms; 162 return cms;
163 merr: 163 merr:
164 CMS_ContentInfo_free(cms); 164 CMS_ContentInfo_free(cms);
165 CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE); 165 CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE);
166 return NULL; 166 return NULL;
167} 167}
168 168
169/* Key Transport Recipient Info (KTRI) routines */ 169/* Key Transport Recipient Info (KTRI) routines */
@@ -171,49 +171,49 @@ CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher)
171/* Initialise a ktri based on passed certificate and key */ 171/* Initialise a ktri based on passed certificate and key */
172 172
173static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip, 173static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
174 EVP_PKEY *pk, unsigned int flags) 174 EVP_PKEY *pk, unsigned int flags)
175{ 175{
176 CMS_KeyTransRecipientInfo *ktri; 176 CMS_KeyTransRecipientInfo *ktri;
177 int idtype; 177 int idtype;
178 178
179 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo); 179 ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo);
180 if (!ri->d.ktri) 180 if (!ri->d.ktri)
181 return 0; 181 return 0;
182 ri->type = CMS_RECIPINFO_TRANS; 182 ri->type = CMS_RECIPINFO_TRANS;
183 183
184 ktri = ri->d.ktri; 184 ktri = ri->d.ktri;
185 185
186 if (flags & CMS_USE_KEYID) { 186 if (flags & CMS_USE_KEYID) {
187 ktri->version = 2; 187 ktri->version = 2;
188 idtype = CMS_RECIPINFO_KEYIDENTIFIER; 188 idtype = CMS_RECIPINFO_KEYIDENTIFIER;
189 } else { 189 } else {
190 ktri->version = 0; 190 ktri->version = 0;
191 idtype = CMS_RECIPINFO_ISSUER_SERIAL; 191 idtype = CMS_RECIPINFO_ISSUER_SERIAL;
192 } 192 }
193 193
194 /* 194 /*
195 * Not a typo: RecipientIdentifier and SignerIdentifier are the same 195 * Not a typo: RecipientIdentifier and SignerIdentifier are the same
196 * structure. 196 * structure.
197 */ 197 */
198 198
199 if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype)) 199 if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype))
200 return 0; 200 return 0;
201 201
202 X509_up_ref(recip); 202 X509_up_ref(recip);
203 EVP_PKEY_up_ref(pk); 203 EVP_PKEY_up_ref(pk);
204 204
205 ktri->pkey = pk; 205 ktri->pkey = pk;
206 ktri->recip = recip; 206 ktri->recip = recip;
207 207
208 if (flags & CMS_KEY_PARAM) { 208 if (flags & CMS_KEY_PARAM) {
209 ktri->pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL); 209 ktri->pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
210 if (ktri->pctx == NULL) 210 if (ktri->pctx == NULL)
211 return 0; 211 return 0;
212 if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0) 212 if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0)
213 return 0; 213 return 0;
214 } else if (!cms_env_asn1_ctrl(ri, 0)) 214 } else if (!cms_env_asn1_ctrl(ri, 0))
215 return 0; 215 return 0;
216 return 1; 216 return 1;
217} 217}
218 218
219/* 219/*
@@ -221,713 +221,713 @@ static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip,
221 */ 221 */
222 222
223CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, 223CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms,
224 X509 *recip, unsigned int flags) 224 X509 *recip, unsigned int flags)
225{ 225{
226 CMS_RecipientInfo *ri = NULL; 226 CMS_RecipientInfo *ri = NULL;
227 CMS_EnvelopedData *env; 227 CMS_EnvelopedData *env;
228 EVP_PKEY *pk = NULL; 228 EVP_PKEY *pk = NULL;
229 env = cms_get0_enveloped(cms); 229 env = cms_get0_enveloped(cms);
230 if (!env) 230 if (!env)
231 goto err; 231 goto err;
232 232
233 /* Initialize recipient info */ 233 /* Initialize recipient info */
234 ri = M_ASN1_new_of(CMS_RecipientInfo); 234 ri = M_ASN1_new_of(CMS_RecipientInfo);
235 if (!ri) 235 if (!ri)
236 goto merr; 236 goto merr;
237 237
238 pk = X509_get0_pubkey(recip); 238 pk = X509_get0_pubkey(recip);
239 if (!pk) { 239 if (!pk) {
240 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, CMS_R_ERROR_GETTING_PUBLIC_KEY); 240 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, CMS_R_ERROR_GETTING_PUBLIC_KEY);
241 goto err; 241 goto err;
242 } 242 }
243 243
244 switch (cms_pkey_get_ri_type(pk)) { 244 switch (cms_pkey_get_ri_type(pk)) {
245 245
246 case CMS_RECIPINFO_TRANS: 246 case CMS_RECIPINFO_TRANS:
247 if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags)) 247 if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags))
248 goto err; 248 goto err;
249 break; 249 break;
250 250
251 case CMS_RECIPINFO_AGREE: 251 case CMS_RECIPINFO_AGREE:
252 if (!cms_RecipientInfo_kari_init(ri, recip, pk, flags)) 252 if (!cms_RecipientInfo_kari_init(ri, recip, pk, flags))
253 goto err; 253 goto err;
254 break; 254 break;
255 255
256 default: 256 default:
257 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, 257 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT,
258 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); 258 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
259 goto err; 259 goto err;
260 260
261 } 261 }
262 262
263 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri)) 263 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
264 goto merr; 264 goto merr;
265 265
266 return ri; 266 return ri;
267 267
268 merr: 268 merr:
269 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE); 269 CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE);
270 err: 270 err:
271 M_ASN1_free_of(ri, CMS_RecipientInfo); 271 M_ASN1_free_of(ri, CMS_RecipientInfo);
272 return NULL; 272 return NULL;
273 273
274} 274}
275 275
276int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri, 276int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri,
277 EVP_PKEY **pk, X509 **recip, 277 EVP_PKEY **pk, X509 **recip,
278 X509_ALGOR **palg) 278 X509_ALGOR **palg)
279{ 279{
280 CMS_KeyTransRecipientInfo *ktri; 280 CMS_KeyTransRecipientInfo *ktri;
281 if (ri->type != CMS_RECIPINFO_TRANS) { 281 if (ri->type != CMS_RECIPINFO_TRANS) {
282 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS, 282 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS,
283 CMS_R_NOT_KEY_TRANSPORT); 283 CMS_R_NOT_KEY_TRANSPORT);
284 return 0; 284 return 0;
285 } 285 }
286 286
287 ktri = ri->d.ktri; 287 ktri = ri->d.ktri;
288 288
289 if (pk) 289 if (pk)
290 *pk = ktri->pkey; 290 *pk = ktri->pkey;
291 if (recip) 291 if (recip)
292 *recip = ktri->recip; 292 *recip = ktri->recip;
293 if (palg) 293 if (palg)
294 *palg = ktri->keyEncryptionAlgorithm; 294 *palg = ktri->keyEncryptionAlgorithm;
295 return 1; 295 return 1;
296} 296}
297 297
298int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri, 298int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri,
299 ASN1_OCTET_STRING **keyid, 299 ASN1_OCTET_STRING **keyid,
300 X509_NAME **issuer, 300 X509_NAME **issuer,
301 ASN1_INTEGER **sno) 301 ASN1_INTEGER **sno)
302{ 302{
303 CMS_KeyTransRecipientInfo *ktri; 303 CMS_KeyTransRecipientInfo *ktri;
304 if (ri->type != CMS_RECIPINFO_TRANS) { 304 if (ri->type != CMS_RECIPINFO_TRANS) {
305 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID, 305 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID,
306 CMS_R_NOT_KEY_TRANSPORT); 306 CMS_R_NOT_KEY_TRANSPORT);
307 return 0; 307 return 0;
308 } 308 }
309 ktri = ri->d.ktri; 309 ktri = ri->d.ktri;
310 310
311 return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno); 311 return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno);
312} 312}
313 313
314int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert) 314int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert)
315{ 315{
316 if (ri->type != CMS_RECIPINFO_TRANS) { 316 if (ri->type != CMS_RECIPINFO_TRANS) {
317 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP, 317 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP,
318 CMS_R_NOT_KEY_TRANSPORT); 318 CMS_R_NOT_KEY_TRANSPORT);
319 return -2; 319 return -2;
320 } 320 }
321 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert); 321 return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert);
322} 322}
323 323
324int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey) 324int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey)
325{ 325{
326 if (ri->type != CMS_RECIPINFO_TRANS) { 326 if (ri->type != CMS_RECIPINFO_TRANS) {
327 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT); 327 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT);
328 return 0; 328 return 0;
329 } 329 }
330 EVP_PKEY_free(ri->d.ktri->pkey); 330 EVP_PKEY_free(ri->d.ktri->pkey);
331 ri->d.ktri->pkey = pkey; 331 ri->d.ktri->pkey = pkey;
332 return 1; 332 return 1;
333} 333}
334 334
335/* Encrypt content key in key transport recipient info */ 335/* Encrypt content key in key transport recipient info */
336 336
337static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms, 337static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms,
338 CMS_RecipientInfo *ri) 338 CMS_RecipientInfo *ri)
339{ 339{
340 CMS_KeyTransRecipientInfo *ktri; 340 CMS_KeyTransRecipientInfo *ktri;
341 CMS_EncryptedContentInfo *ec; 341 CMS_EncryptedContentInfo *ec;
342 EVP_PKEY_CTX *pctx; 342 EVP_PKEY_CTX *pctx;
343 unsigned char *ek = NULL; 343 unsigned char *ek = NULL;
344 size_t eklen; 344 size_t eklen;
345 345
346 int ret = 0; 346 int ret = 0;
347 347
348 if (ri->type != CMS_RECIPINFO_TRANS) { 348 if (ri->type != CMS_RECIPINFO_TRANS) {
349 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_NOT_KEY_TRANSPORT); 349 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_NOT_KEY_TRANSPORT);
350 return 0; 350 return 0;
351 } 351 }
352 ktri = ri->d.ktri; 352 ktri = ri->d.ktri;
353 ec = cms->d.envelopedData->encryptedContentInfo; 353 ec = cms->d.envelopedData->encryptedContentInfo;
354 354
355 pctx = ktri->pctx; 355 pctx = ktri->pctx;
356 356
357 if (pctx) { 357 if (pctx) {
358 if (!cms_env_asn1_ctrl(ri, 0)) 358 if (!cms_env_asn1_ctrl(ri, 0))
359 goto err; 359 goto err;
360 } else { 360 } else {
361 pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL); 361 pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL);
362 if (pctx == NULL) 362 if (pctx == NULL)
363 return 0; 363 return 0;
364 364
365 if (EVP_PKEY_encrypt_init(pctx) <= 0) 365 if (EVP_PKEY_encrypt_init(pctx) <= 0)
366 goto err; 366 goto err;
367 } 367 }
368 368
369 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT, 369 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT,
370 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0) { 370 EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0) {
371 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR); 371 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR);
372 goto err; 372 goto err;
373 } 373 }
374 374
375 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0) 375 if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0)
376 goto err; 376 goto err;
377 377
378 ek = OPENSSL_malloc(eklen); 378 ek = OPENSSL_malloc(eklen);
379 379
380 if (ek == NULL) { 380 if (ek == NULL) {
381 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, ERR_R_MALLOC_FAILURE); 381 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
382 goto err; 382 goto err;
383 } 383 }
384 384
385 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0) 385 if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0)
386 goto err; 386 goto err;
387 387
388 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen); 388 ASN1_STRING_set0(ktri->encryptedKey, ek, eklen);
389 ek = NULL; 389 ek = NULL;
390 390
391 ret = 1; 391 ret = 1;
392 392
393 err: 393 err:
394 EVP_PKEY_CTX_free(pctx); 394 EVP_PKEY_CTX_free(pctx);
395 ktri->pctx = NULL; 395 ktri->pctx = NULL;
396 OPENSSL_free(ek); 396 OPENSSL_free(ek);
397 return ret; 397 return ret;
398 398
399} 399}
400 400
401/* Decrypt content key from KTRI */ 401/* Decrypt content key from KTRI */
402 402
403static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms, 403static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
404 CMS_RecipientInfo *ri) 404 CMS_RecipientInfo *ri)
405{ 405{
406 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri; 406 CMS_KeyTransRecipientInfo *ktri = ri->d.ktri;
407 EVP_PKEY *pkey = ktri->pkey; 407 EVP_PKEY *pkey = ktri->pkey;
408 unsigned char *ek = NULL; 408 unsigned char *ek = NULL;
409 size_t eklen; 409 size_t eklen;
410 int ret = 0; 410 int ret = 0;
411 CMS_EncryptedContentInfo *ec; 411 CMS_EncryptedContentInfo *ec;
412 ec = cms->d.envelopedData->encryptedContentInfo; 412 ec = cms->d.envelopedData->encryptedContentInfo;
413 413
414 if (ktri->pkey == NULL) { 414 if (ktri->pkey == NULL) {
415 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_NO_PRIVATE_KEY); 415 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_NO_PRIVATE_KEY);
416 return 0; 416 return 0;
417 } 417 }
418 418
419 ktri->pctx = EVP_PKEY_CTX_new(pkey, NULL); 419 ktri->pctx = EVP_PKEY_CTX_new(pkey, NULL);
420 if (ktri->pctx == NULL) 420 if (ktri->pctx == NULL)
421 return 0; 421 return 0;
422 422
423 if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0) 423 if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0)
424 goto err; 424 goto err;
425 425
426 if (!cms_env_asn1_ctrl(ri, 1)) 426 if (!cms_env_asn1_ctrl(ri, 1))
427 goto err; 427 goto err;
428 428
429 if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT, 429 if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT,
430 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) { 430 EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) {
431 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR); 431 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR);
432 goto err; 432 goto err;
433 } 433 }
434 434
435 if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen, 435 if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen,
436 ktri->encryptedKey->data, 436 ktri->encryptedKey->data,
437 ktri->encryptedKey->length) <= 0) 437 ktri->encryptedKey->length) <= 0)
438 goto err; 438 goto err;
439 439
440 ek = OPENSSL_malloc(eklen); 440 ek = OPENSSL_malloc(eklen);
441 441
442 if (ek == NULL) { 442 if (ek == NULL) {
443 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, ERR_R_MALLOC_FAILURE); 443 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, ERR_R_MALLOC_FAILURE);
444 goto err; 444 goto err;
445 } 445 }
446 446
447 if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen, 447 if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen,
448 ktri->encryptedKey->data, 448 ktri->encryptedKey->data,
449 ktri->encryptedKey->length) <= 0) { 449 ktri->encryptedKey->length) <= 0) {
450 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB); 450 CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB);
451 goto err; 451 goto err;
452 } 452 }
453 453
454 ret = 1; 454 ret = 1;
455 455
456 OPENSSL_clear_free(ec->key, ec->keylen); 456 OPENSSL_clear_free(ec->key, ec->keylen);
457 ec->key = ek; 457 ec->key = ek;
458 ec->keylen = eklen; 458 ec->keylen = eklen;
459 459
460 err: 460 err:
461 EVP_PKEY_CTX_free(ktri->pctx); 461 EVP_PKEY_CTX_free(ktri->pctx);
462 ktri->pctx = NULL; 462 ktri->pctx = NULL;
463 if (!ret) 463 if (!ret)
464 OPENSSL_free(ek); 464 OPENSSL_free(ek);
465 465
466 return ret; 466 return ret;
467} 467}
468 468
469/* Key Encrypted Key (KEK) RecipientInfo routines */ 469/* Key Encrypted Key (KEK) RecipientInfo routines */
470 470
471int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri, 471int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
472 const unsigned char *id, size_t idlen) 472 const unsigned char *id, size_t idlen)
473{ 473{
474 ASN1_OCTET_STRING tmp_os; 474 ASN1_OCTET_STRING tmp_os;
475 CMS_KEKRecipientInfo *kekri; 475 CMS_KEKRecipientInfo *kekri;
476 if (ri->type != CMS_RECIPINFO_KEK) { 476 if (ri->type != CMS_RECIPINFO_KEK) {
477 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK); 477 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK);
478 return -2; 478 return -2;
479 } 479 }
480 kekri = ri->d.kekri; 480 kekri = ri->d.kekri;
481 tmp_os.type = V_ASN1_OCTET_STRING; 481 tmp_os.type = V_ASN1_OCTET_STRING;
482 tmp_os.flags = 0; 482 tmp_os.flags = 0;
483 tmp_os.data = (unsigned char *)id; 483 tmp_os.data = (unsigned char *)id;
484 tmp_os.length = (int)idlen; 484 tmp_os.length = (int)idlen;
485 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier); 485 return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier);
486} 486}
487 487
488/* For now hard code AES key wrap info */ 488/* For now hard code AES key wrap info */
489 489
490static size_t aes_wrap_keylen(int nid) 490static size_t aes_wrap_keylen(int nid)
491{ 491{
492 switch (nid) { 492 switch (nid) {
493 case NID_id_aes128_wrap: 493 case NID_id_aes128_wrap:
494 return 16; 494 return 16;
495 495
496 case NID_id_aes192_wrap: 496 case NID_id_aes192_wrap:
497 return 24; 497 return 24;
498 498
499 case NID_id_aes256_wrap: 499 case NID_id_aes256_wrap:
500 return 32; 500 return 32;
501 501
502 default: 502 default:
503 return 0; 503 return 0;
504 } 504 }
505} 505}
506 506
507CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid, 507CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid,
508 unsigned char *key, size_t keylen, 508 unsigned char *key, size_t keylen,
509 unsigned char *id, size_t idlen, 509 unsigned char *id, size_t idlen,
510 ASN1_GENERALIZEDTIME *date, 510 ASN1_GENERALIZEDTIME *date,
511 ASN1_OBJECT *otherTypeId, 511 ASN1_OBJECT *otherTypeId,
512 ASN1_TYPE *otherType) 512 ASN1_TYPE *otherType)
513{ 513{
514 CMS_RecipientInfo *ri = NULL; 514 CMS_RecipientInfo *ri = NULL;
515 CMS_EnvelopedData *env; 515 CMS_EnvelopedData *env;
516 CMS_KEKRecipientInfo *kekri; 516 CMS_KEKRecipientInfo *kekri;
517 env = cms_get0_enveloped(cms); 517 env = cms_get0_enveloped(cms);
518 if (!env) 518 if (!env)
519 goto err; 519 goto err;
520 520
521 if (nid == NID_undef) { 521 if (nid == NID_undef) {
522 switch (keylen) { 522 switch (keylen) {
523 case 16: 523 case 16:
524 nid = NID_id_aes128_wrap; 524 nid = NID_id_aes128_wrap;
525 break; 525 break;
526 526
527 case 24: 527 case 24:
528 nid = NID_id_aes192_wrap; 528 nid = NID_id_aes192_wrap;
529 break; 529 break;
530 530
531 case 32: 531 case 32:
532 nid = NID_id_aes256_wrap; 532 nid = NID_id_aes256_wrap;
533 break; 533 break;
534 534
535 default: 535 default:
536 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH); 536 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
537 goto err; 537 goto err;
538 } 538 }
539 539
540 } else { 540 } else {
541 541
542 size_t exp_keylen = aes_wrap_keylen(nid); 542 size_t exp_keylen = aes_wrap_keylen(nid);
543 543
544 if (!exp_keylen) { 544 if (!exp_keylen) {
545 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, 545 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY,
546 CMS_R_UNSUPPORTED_KEK_ALGORITHM); 546 CMS_R_UNSUPPORTED_KEK_ALGORITHM);
547 goto err; 547 goto err;
548 } 548 }
549 549
550 if (keylen != exp_keylen) { 550 if (keylen != exp_keylen) {
551 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH); 551 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH);
552 goto err; 552 goto err;
553 } 553 }
554 554
555 } 555 }
556 556
557 /* Initialize recipient info */ 557 /* Initialize recipient info */
558 ri = M_ASN1_new_of(CMS_RecipientInfo); 558 ri = M_ASN1_new_of(CMS_RecipientInfo);
559 if (!ri) 559 if (!ri)
560 goto merr; 560 goto merr;
561 561
562 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo); 562 ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo);
563 if (!ri->d.kekri) 563 if (!ri->d.kekri)
564 goto merr; 564 goto merr;
565 ri->type = CMS_RECIPINFO_KEK; 565 ri->type = CMS_RECIPINFO_KEK;
566 566
567 kekri = ri->d.kekri; 567 kekri = ri->d.kekri;
568 568
569 if (otherTypeId) { 569 if (otherTypeId) {
570 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute); 570 kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute);
571 if (kekri->kekid->other == NULL) 571 if (kekri->kekid->other == NULL)
572 goto merr; 572 goto merr;
573 } 573 }
574 574
575 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri)) 575 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
576 goto merr; 576 goto merr;
577 577
578 /* After this point no calls can fail */ 578 /* After this point no calls can fail */
579 579
580 kekri->version = 4; 580 kekri->version = 4;
581 581
582 kekri->key = key; 582 kekri->key = key;
583 kekri->keylen = keylen; 583 kekri->keylen = keylen;
584 584
585 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen); 585 ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen);
586 586
587 kekri->kekid->date = date; 587 kekri->kekid->date = date;
588 588
589 if (kekri->kekid->other) { 589 if (kekri->kekid->other) {
590 kekri->kekid->other->keyAttrId = otherTypeId; 590 kekri->kekid->other->keyAttrId = otherTypeId;
591 kekri->kekid->other->keyAttr = otherType; 591 kekri->kekid->other->keyAttr = otherType;
592 } 592 }
593 593
594 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm, 594 X509_ALGOR_set0(kekri->keyEncryptionAlgorithm,
595 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL); 595 OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL);
596 596
597 return ri; 597 return ri;
598 598
599 merr: 599 merr:
600 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE); 600 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE);
601 err: 601 err:
602 M_ASN1_free_of(ri, CMS_RecipientInfo); 602 M_ASN1_free_of(ri, CMS_RecipientInfo);
603 return NULL; 603 return NULL;
604 604
605} 605}
606 606
607int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri, 607int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri,
608 X509_ALGOR **palg, 608 X509_ALGOR **palg,
609 ASN1_OCTET_STRING **pid, 609 ASN1_OCTET_STRING **pid,
610 ASN1_GENERALIZEDTIME **pdate, 610 ASN1_GENERALIZEDTIME **pdate,
611 ASN1_OBJECT **potherid, 611 ASN1_OBJECT **potherid,
612 ASN1_TYPE **pothertype) 612 ASN1_TYPE **pothertype)
613{ 613{
614 CMS_KEKIdentifier *rkid; 614 CMS_KEKIdentifier *rkid;
615 if (ri->type != CMS_RECIPINFO_KEK) { 615 if (ri->type != CMS_RECIPINFO_KEK) {
616 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK); 616 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK);
617 return 0; 617 return 0;
618 } 618 }
619 rkid = ri->d.kekri->kekid; 619 rkid = ri->d.kekri->kekid;
620 if (palg) 620 if (palg)
621 *palg = ri->d.kekri->keyEncryptionAlgorithm; 621 *palg = ri->d.kekri->keyEncryptionAlgorithm;
622 if (pid) 622 if (pid)
623 *pid = rkid->keyIdentifier; 623 *pid = rkid->keyIdentifier;
624 if (pdate) 624 if (pdate)
625 *pdate = rkid->date; 625 *pdate = rkid->date;
626 if (potherid) { 626 if (potherid) {
627 if (rkid->other) 627 if (rkid->other)
628 *potherid = rkid->other->keyAttrId; 628 *potherid = rkid->other->keyAttrId;
629 else 629 else
630 *potherid = NULL; 630 *potherid = NULL;
631 } 631 }
632 if (pothertype) { 632 if (pothertype) {
633 if (rkid->other) 633 if (rkid->other)
634 *pothertype = rkid->other->keyAttr; 634 *pothertype = rkid->other->keyAttr;
635 else 635 else
636 *pothertype = NULL; 636 *pothertype = NULL;
637 } 637 }
638 return 1; 638 return 1;
639} 639}
640 640
641int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri, 641int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
642 unsigned char *key, size_t keylen) 642 unsigned char *key, size_t keylen)
643{ 643{
644 CMS_KEKRecipientInfo *kekri; 644 CMS_KEKRecipientInfo *kekri;
645 if (ri->type != CMS_RECIPINFO_KEK) { 645 if (ri->type != CMS_RECIPINFO_KEK) {
646 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK); 646 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK);
647 return 0; 647 return 0;
648 } 648 }
649 649
650 kekri = ri->d.kekri; 650 kekri = ri->d.kekri;
651 kekri->key = key; 651 kekri->key = key;
652 kekri->keylen = keylen; 652 kekri->keylen = keylen;
653 return 1; 653 return 1;
654} 654}
655 655
656/* Encrypt content key in KEK recipient info */ 656/* Encrypt content key in KEK recipient info */
657 657
658static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms, 658static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
659 CMS_RecipientInfo *ri) 659 CMS_RecipientInfo *ri)
660{ 660{
661 CMS_EncryptedContentInfo *ec; 661 CMS_EncryptedContentInfo *ec;
662 CMS_KEKRecipientInfo *kekri; 662 CMS_KEKRecipientInfo *kekri;
663 AES_KEY actx; 663 AES_KEY actx;
664 unsigned char *wkey = NULL; 664 unsigned char *wkey = NULL;
665 int wkeylen; 665 int wkeylen;
666 int r = 0; 666 int r = 0;
667 667
668 ec = cms->d.envelopedData->encryptedContentInfo; 668 ec = cms->d.envelopedData->encryptedContentInfo;
669 669
670 kekri = ri->d.kekri; 670 kekri = ri->d.kekri;
671 671
672 if (!kekri->key) { 672 if (!kekri->key) {
673 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY); 673 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY);
674 return 0; 674 return 0;
675 } 675 }
676 676
677 if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx)) { 677 if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
678 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, 678 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
679 CMS_R_ERROR_SETTING_KEY); 679 CMS_R_ERROR_SETTING_KEY);
680 goto err; 680 goto err;
681 } 681 }
682 682
683 wkey = OPENSSL_malloc(ec->keylen + 8); 683 wkey = OPENSSL_malloc(ec->keylen + 8);
684 684
685 if (wkey == NULL) { 685 if (wkey == NULL) {
686 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE); 686 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE);
687 goto err; 687 goto err;
688 } 688 }
689 689
690 wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen); 690 wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
691 691
692 if (wkeylen <= 0) { 692 if (wkeylen <= 0) {
693 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR); 693 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
694 goto err; 694 goto err;
695 } 695 }
696 696
697 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen); 697 ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen);
698 698
699 r = 1; 699 r = 1;
700 700
701 err: 701 err:
702 702
703 if (!r) 703 if (!r)
704 OPENSSL_free(wkey); 704 OPENSSL_free(wkey);
705 OPENSSL_cleanse(&actx, sizeof(actx)); 705 OPENSSL_cleanse(&actx, sizeof(actx));
706 706
707 return r; 707 return r;
708 708
709} 709}
710 710
711/* Decrypt content key in KEK recipient info */ 711/* Decrypt content key in KEK recipient info */
712 712
713static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms, 713static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
714 CMS_RecipientInfo *ri) 714 CMS_RecipientInfo *ri)
715{ 715{
716 CMS_EncryptedContentInfo *ec; 716 CMS_EncryptedContentInfo *ec;
717 CMS_KEKRecipientInfo *kekri; 717 CMS_KEKRecipientInfo *kekri;
718 AES_KEY actx; 718 AES_KEY actx;
719 unsigned char *ukey = NULL; 719 unsigned char *ukey = NULL;
720 int ukeylen; 720 int ukeylen;
721 int r = 0, wrap_nid; 721 int r = 0, wrap_nid;
722 722
723 ec = cms->d.envelopedData->encryptedContentInfo; 723 ec = cms->d.envelopedData->encryptedContentInfo;
724 724
725 kekri = ri->d.kekri; 725 kekri = ri->d.kekri;
726 726
727 if (!kekri->key) { 727 if (!kekri->key) {
728 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY); 728 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY);
729 return 0; 729 return 0;
730 } 730 }
731 731
732 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm); 732 wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
733 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) { 733 if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
734 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, 734 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
735 CMS_R_INVALID_KEY_LENGTH); 735 CMS_R_INVALID_KEY_LENGTH);
736 return 0; 736 return 0;
737 } 737 }
738 738
739 /* If encrypted key length is invalid don't bother */ 739 /* If encrypted key length is invalid don't bother */
740 740
741 if (kekri->encryptedKey->length < 16) { 741 if (kekri->encryptedKey->length < 16) {
742 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, 742 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
743 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH); 743 CMS_R_INVALID_ENCRYPTED_KEY_LENGTH);
744 goto err; 744 goto err;
745 } 745 }
746 746
747 if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx)) { 747 if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
748 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, 748 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
749 CMS_R_ERROR_SETTING_KEY); 749 CMS_R_ERROR_SETTING_KEY);
750 goto err; 750 goto err;
751 } 751 }
752 752
753 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8); 753 ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
754 754
755 if (ukey == NULL) { 755 if (ukey == NULL) {
756 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE); 756 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE);
757 goto err; 757 goto err;
758 } 758 }
759 759
760 ukeylen = AES_unwrap_key(&actx, NULL, ukey, 760 ukeylen = AES_unwrap_key(&actx, NULL, ukey,
761 kekri->encryptedKey->data, 761 kekri->encryptedKey->data,
762 kekri->encryptedKey->length); 762 kekri->encryptedKey->length);
763 763
764 if (ukeylen <= 0) { 764 if (ukeylen <= 0) {
765 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR); 765 CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR);
766 goto err; 766 goto err;
767 } 767 }
768 768
769 ec->key = ukey; 769 ec->key = ukey;
770 ec->keylen = ukeylen; 770 ec->keylen = ukeylen;
771 771
772 r = 1; 772 r = 1;
773 773
774 err: 774 err:
775 775
776 if (!r) 776 if (!r)
777 OPENSSL_free(ukey); 777 OPENSSL_free(ukey);
778 OPENSSL_cleanse(&actx, sizeof(actx)); 778 OPENSSL_cleanse(&actx, sizeof(actx));
779 779
780 return r; 780 return r;
781 781
782} 782}
783 783
784int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri) 784int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
785{ 785{
786 switch (ri->type) { 786 switch (ri->type) {
787 case CMS_RECIPINFO_TRANS: 787 case CMS_RECIPINFO_TRANS:
788 return cms_RecipientInfo_ktri_decrypt(cms, ri); 788 return cms_RecipientInfo_ktri_decrypt(cms, ri);
789 789
790 case CMS_RECIPINFO_KEK: 790 case CMS_RECIPINFO_KEK:
791 return cms_RecipientInfo_kekri_decrypt(cms, ri); 791 return cms_RecipientInfo_kekri_decrypt(cms, ri);
792 792
793 case CMS_RECIPINFO_PASS: 793 case CMS_RECIPINFO_PASS:
794 return cms_RecipientInfo_pwri_crypt(cms, ri, 0); 794 return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
795 795
796 default: 796 default:
797 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT, 797 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
798 CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE); 798 CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE);
799 return 0; 799 return 0;
800 } 800 }
801} 801}
802 802
803int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri) 803int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
804{ 804{
805 switch (ri->type) { 805 switch (ri->type) {
806 case CMS_RECIPINFO_TRANS: 806 case CMS_RECIPINFO_TRANS:
807 return cms_RecipientInfo_ktri_encrypt(cms, ri); 807 return cms_RecipientInfo_ktri_encrypt(cms, ri);
808 808
809 case CMS_RECIPINFO_AGREE: 809 case CMS_RECIPINFO_AGREE:
810 return cms_RecipientInfo_kari_encrypt(cms, ri); 810 return cms_RecipientInfo_kari_encrypt(cms, ri);
811 811
812 case CMS_RECIPINFO_KEK: 812 case CMS_RECIPINFO_KEK:
813 return cms_RecipientInfo_kekri_encrypt(cms, ri); 813 return cms_RecipientInfo_kekri_encrypt(cms, ri);
814 814
815 case CMS_RECIPINFO_PASS: 815 case CMS_RECIPINFO_PASS:
816 return cms_RecipientInfo_pwri_crypt(cms, ri, 1); 816 return cms_RecipientInfo_pwri_crypt(cms, ri, 1);
817 817
818 default: 818 default:
819 CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT, 819 CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT,
820 CMS_R_UNSUPPORTED_RECIPIENT_TYPE); 820 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
821 return 0; 821 return 0;
822 } 822 }
823} 823}
824 824
825/* Check structures and fixup version numbers (if necessary) */ 825/* Check structures and fixup version numbers (if necessary) */
826 826
827static void cms_env_set_originfo_version(CMS_EnvelopedData *env) 827static void cms_env_set_originfo_version(CMS_EnvelopedData *env)
828{ 828{
829 CMS_OriginatorInfo *org = env->originatorInfo; 829 CMS_OriginatorInfo *org = env->originatorInfo;
830 int i; 830 int i;
831 if (org == NULL) 831 if (org == NULL)
832 return; 832 return;
833 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) { 833 for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) {
834 CMS_CertificateChoices *cch; 834 CMS_CertificateChoices *cch;
835 cch = sk_CMS_CertificateChoices_value(org->certificates, i); 835 cch = sk_CMS_CertificateChoices_value(org->certificates, i);
836 if (cch->type == CMS_CERTCHOICE_OTHER) { 836 if (cch->type == CMS_CERTCHOICE_OTHER) {
837 env->version = 4; 837 env->version = 4;
838 return; 838 return;
839 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) { 839 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
840 if (env->version < 3) 840 if (env->version < 3)
841 env->version = 3; 841 env->version = 3;
842 } 842 }
843 } 843 }
844 844
845 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) { 845 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) {
846 CMS_RevocationInfoChoice *rch; 846 CMS_RevocationInfoChoice *rch;
847 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i); 847 rch = sk_CMS_RevocationInfoChoice_value(org->crls, i);
848 if (rch->type == CMS_REVCHOICE_OTHER) { 848 if (rch->type == CMS_REVCHOICE_OTHER) {
849 env->version = 4; 849 env->version = 4;
850 return; 850 return;
851 } 851 }
852 } 852 }
853} 853}
854 854
855static void cms_env_set_version(CMS_EnvelopedData *env) 855static void cms_env_set_version(CMS_EnvelopedData *env)
856{ 856{
857 int i; 857 int i;
858 CMS_RecipientInfo *ri; 858 CMS_RecipientInfo *ri;
859 859
860 /* 860 /*
861 * Can't set version higher than 4 so if 4 or more already nothing to do. 861 * Can't set version higher than 4 so if 4 or more already nothing to do.
862 */ 862 */
863 if (env->version >= 4) 863 if (env->version >= 4)
864 return; 864 return;
865 865
866 cms_env_set_originfo_version(env); 866 cms_env_set_originfo_version(env);
867 867
868 if (env->version >= 3) 868 if (env->version >= 3)
869 return; 869 return;
870 870
871 for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) { 871 for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) {
872 ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i); 872 ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i);
873 if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) { 873 if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) {
874 env->version = 3; 874 env->version = 3;
875 return; 875 return;
876 } else if (ri->type != CMS_RECIPINFO_TRANS 876 } else if (ri->type != CMS_RECIPINFO_TRANS
877 || ri->d.ktri->version != 0) { 877 || ri->d.ktri->version != 0) {
878 env->version = 2; 878 env->version = 2;
879 } 879 }
880 } 880 }
881 if (env->originatorInfo || env->unprotectedAttrs) 881 if (env->originatorInfo || env->unprotectedAttrs)
882 env->version = 2; 882 env->version = 2;
883 if (env->version == 2) 883 if (env->version == 2)
884 return; 884 return;
885 env->version = 0; 885 env->version = 0;
886} 886}
887 887
888BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms) 888BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
889{ 889{
890 CMS_EncryptedContentInfo *ec; 890 CMS_EncryptedContentInfo *ec;
891 STACK_OF(CMS_RecipientInfo) *rinfos; 891 STACK_OF(CMS_RecipientInfo) *rinfos;
892 CMS_RecipientInfo *ri; 892 CMS_RecipientInfo *ri;
893 int i, ok = 0; 893 int i, ok = 0;
894 BIO *ret; 894 BIO *ret;
895 895
896 /* Get BIO first to set up key */ 896 /* Get BIO first to set up key */
897 897
898 ec = cms->d.envelopedData->encryptedContentInfo; 898 ec = cms->d.envelopedData->encryptedContentInfo;
899 ret = cms_EncryptedContent_init_bio(ec); 899 ret = cms_EncryptedContent_init_bio(ec);
900 900
901 /* If error or no cipher end of processing */ 901 /* If error or no cipher end of processing */
902 902
903 if (!ret || !ec->cipher) 903 if (!ret || !ec->cipher)
904 return ret; 904 return ret;
905 905
906 /* Now encrypt content key according to each RecipientInfo type */ 906 /* Now encrypt content key according to each RecipientInfo type */
907 907
908 rinfos = cms->d.envelopedData->recipientInfos; 908 rinfos = cms->d.envelopedData->recipientInfos;
909 909
910 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) { 910 for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) {
911 ri = sk_CMS_RecipientInfo_value(rinfos, i); 911 ri = sk_CMS_RecipientInfo_value(rinfos, i);
912 if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) { 912 if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) {
913 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO, 913 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
914 CMS_R_ERROR_SETTING_RECIPIENTINFO); 914 CMS_R_ERROR_SETTING_RECIPIENTINFO);
915 goto err; 915 goto err;
916 } 916 }
917 } 917 }
918 cms_env_set_version(cms->d.envelopedData); 918 cms_env_set_version(cms->d.envelopedData);
919 919
920 ok = 1; 920 ok = 1;
921 921
922 err: 922 err:
923 ec->cipher = NULL; 923 ec->cipher = NULL;
924 OPENSSL_clear_free(ec->key, ec->keylen); 924 OPENSSL_clear_free(ec->key, ec->keylen);
925 ec->key = NULL; 925 ec->key = NULL;
926 ec->keylen = 0; 926 ec->keylen = 0;
927 if (ok) 927 if (ok)
928 return ret; 928 return ret;
929 BIO_free(ret); 929 BIO_free(ret);
930 return NULL; 930 return NULL;
931 931
932} 932}
933 933
@@ -938,11 +938,11 @@ BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
938 */ 938 */
939int cms_pkey_get_ri_type(EVP_PKEY *pk) 939int cms_pkey_get_ri_type(EVP_PKEY *pk)
940{ 940{
941 if (pk->ameth && pk->ameth->pkey_ctrl) { 941 if (pk->ameth && pk->ameth->pkey_ctrl) {
942 int i, r; 942 int i, r;
943 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r); 943 i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r);
944 if (i > 0) 944 if (i > 0)
945 return r; 945 return r;
946 } 946 }
947 return CMS_RECIPINFO_TRANS; 947 return CMS_RECIPINFO_TRANS;
948} 948}
diff --git a/src/lib/libcrypto/cms/cms_err.c b/src/lib/libcrypto/cms/cms_err.c
index 7af0bb8514..af0ef8c38a 100644
--- a/src/lib/libcrypto/cms/cms_err.c
+++ b/src/lib/libcrypto/cms/cms_err.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_err.c,v 1.8 2019/08/10 16:38:47 jsing Exp $ */ 1/* $OpenBSD: cms_err.c,v 1.9 2019/08/10 16:42:20 jsing Exp $ */
2/* 2/*
3 * Generated by util/mkerr.pl DO NOT EDIT 3 * Generated by util/mkerr.pl DO NOT EDIT
4 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. 4 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
@@ -15,270 +15,270 @@
15#ifndef OPENSSL_NO_ERR 15#ifndef OPENSSL_NO_ERR
16 16
17static const ERR_STRING_DATA CMS_str_functs[] = { 17static const ERR_STRING_DATA CMS_str_functs[] = {
18 {ERR_PACK(ERR_LIB_CMS, CMS_F_CHECK_CONTENT, 0), "check_content"}, 18 {ERR_PACK(ERR_LIB_CMS, CMS_F_CHECK_CONTENT, 0), "check_content"},
19 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD0_CERT, 0), "CMS_add0_cert"}, 19 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD0_CERT, 0), "CMS_add0_cert"},
20 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD0_RECIPIENT_KEY, 0), 20 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD0_RECIPIENT_KEY, 0),
21 "CMS_add0_recipient_key"}, 21 "CMS_add0_recipient_key"},
22 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, 0), 22 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, 0),
23 "CMS_add0_recipient_password"}, 23 "CMS_add0_recipient_password"},
24 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD1_RECEIPTREQUEST, 0), 24 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD1_RECEIPTREQUEST, 0),
25 "CMS_add1_ReceiptRequest"}, 25 "CMS_add1_ReceiptRequest"},
26 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD1_RECIPIENT_CERT, 0), 26 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD1_RECIPIENT_CERT, 0),
27 "CMS_add1_recipient_cert"}, 27 "CMS_add1_recipient_cert"},
28 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD1_SIGNER, 0), "CMS_add1_signer"}, 28 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD1_SIGNER, 0), "CMS_add1_signer"},
29 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD1_SIGNINGTIME, 0), 29 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ADD1_SIGNINGTIME, 0),
30 "cms_add1_signingTime"}, 30 "cms_add1_signingTime"},
31 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_COMPRESS, 0), "CMS_compress"}, 31 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_COMPRESS, 0), "CMS_compress"},
32 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_COMPRESSEDDATA_CREATE, 0), 32 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_COMPRESSEDDATA_CREATE, 0),
33 "cms_CompressedData_create"}, 33 "cms_CompressedData_create"},
34 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_COMPRESSEDDATA_INIT_BIO, 0), 34 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_COMPRESSEDDATA_INIT_BIO, 0),
35 "cms_CompressedData_init_bio"}, 35 "cms_CompressedData_init_bio"},
36 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_COPY_CONTENT, 0), "cms_copy_content"}, 36 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_COPY_CONTENT, 0), "cms_copy_content"},
37 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_COPY_MESSAGEDIGEST, 0), 37 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_COPY_MESSAGEDIGEST, 0),
38 "cms_copy_messageDigest"}, 38 "cms_copy_messageDigest"},
39 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DATA, 0), "CMS_data"}, 39 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DATA, 0), "CMS_data"},
40 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DATAFINAL, 0), "CMS_dataFinal"}, 40 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DATAFINAL, 0), "CMS_dataFinal"},
41 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DATAINIT, 0), "CMS_dataInit"}, 41 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DATAINIT, 0), "CMS_dataInit"},
42 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DECRYPT, 0), "CMS_decrypt"}, 42 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DECRYPT, 0), "CMS_decrypt"},
43 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DECRYPT_SET1_KEY, 0), 43 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DECRYPT_SET1_KEY, 0),
44 "CMS_decrypt_set1_key"}, 44 "CMS_decrypt_set1_key"},
45 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DECRYPT_SET1_PASSWORD, 0), 45 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DECRYPT_SET1_PASSWORD, 0),
46 "CMS_decrypt_set1_password"}, 46 "CMS_decrypt_set1_password"},
47 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DECRYPT_SET1_PKEY, 0), 47 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DECRYPT_SET1_PKEY, 0),
48 "CMS_decrypt_set1_pkey"}, 48 "CMS_decrypt_set1_pkey"},
49 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DIGESTALGORITHM_FIND_CTX, 0), 49 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DIGESTALGORITHM_FIND_CTX, 0),
50 "cms_DigestAlgorithm_find_ctx"}, 50 "cms_DigestAlgorithm_find_ctx"},
51 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, 0), 51 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, 0),
52 "cms_DigestAlgorithm_init_bio"}, 52 "cms_DigestAlgorithm_init_bio"},
53 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DIGESTEDDATA_DO_FINAL, 0), 53 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DIGESTEDDATA_DO_FINAL, 0),
54 "cms_DigestedData_do_final"}, 54 "cms_DigestedData_do_final"},
55 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DIGEST_VERIFY, 0), "CMS_digest_verify"}, 55 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_DIGEST_VERIFY, 0), "CMS_digest_verify"},
56 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENCODE_RECEIPT, 0), "cms_encode_Receipt"}, 56 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENCODE_RECEIPT, 0), "cms_encode_Receipt"},
57 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENCRYPT, 0), "CMS_encrypt"}, 57 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENCRYPT, 0), "CMS_encrypt"},
58 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENCRYPTEDCONTENT_INIT, 0), 58 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENCRYPTEDCONTENT_INIT, 0),
59 "cms_EncryptedContent_init"}, 59 "cms_EncryptedContent_init"},
60 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, 0), 60 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, 0),
61 "cms_EncryptedContent_init_bio"}, 61 "cms_EncryptedContent_init_bio"},
62 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENCRYPTEDDATA_DECRYPT, 0), 62 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENCRYPTEDDATA_DECRYPT, 0),
63 "CMS_EncryptedData_decrypt"}, 63 "CMS_EncryptedData_decrypt"},
64 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, 0), 64 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, 0),
65 "CMS_EncryptedData_encrypt"}, 65 "CMS_EncryptedData_encrypt"},
66 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, 0), 66 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, 0),
67 "CMS_EncryptedData_set1_key"}, 67 "CMS_EncryptedData_set1_key"},
68 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENVELOPEDDATA_CREATE, 0), 68 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENVELOPEDDATA_CREATE, 0),
69 "CMS_EnvelopedData_create"}, 69 "CMS_EnvelopedData_create"},
70 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENVELOPEDDATA_INIT_BIO, 0), 70 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENVELOPEDDATA_INIT_BIO, 0),
71 "cms_EnvelopedData_init_bio"}, 71 "cms_EnvelopedData_init_bio"},
72 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENVELOPED_DATA_INIT, 0), 72 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENVELOPED_DATA_INIT, 0),
73 "cms_enveloped_data_init"}, 73 "cms_enveloped_data_init"},
74 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENV_ASN1_CTRL, 0), "cms_env_asn1_ctrl"}, 74 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_ENV_ASN1_CTRL, 0), "cms_env_asn1_ctrl"},
75 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_FINAL, 0), "CMS_final"}, 75 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_FINAL, 0), "CMS_final"},
76 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_GET0_CERTIFICATE_CHOICES, 0), 76 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_GET0_CERTIFICATE_CHOICES, 0),
77 "cms_get0_certificate_choices"}, 77 "cms_get0_certificate_choices"},
78 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_GET0_CONTENT, 0), "CMS_get0_content"}, 78 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_GET0_CONTENT, 0), "CMS_get0_content"},
79 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_GET0_ECONTENT_TYPE, 0), 79 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_GET0_ECONTENT_TYPE, 0),
80 "cms_get0_econtent_type"}, 80 "cms_get0_econtent_type"},
81 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_GET0_ENVELOPED, 0), "cms_get0_enveloped"}, 81 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_GET0_ENVELOPED, 0), "cms_get0_enveloped"},
82 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_GET0_REVOCATION_CHOICES, 0), 82 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_GET0_REVOCATION_CHOICES, 0),
83 "cms_get0_revocation_choices"}, 83 "cms_get0_revocation_choices"},
84 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_GET0_SIGNED, 0), "cms_get0_signed"}, 84 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_GET0_SIGNED, 0), "cms_get0_signed"},
85 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_MSGSIGDIGEST_ADD1, 0), 85 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_MSGSIGDIGEST_ADD1, 0),
86 "cms_msgSigDigest_add1"}, 86 "cms_msgSigDigest_add1"},
87 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECEIPTREQUEST_CREATE0, 0), 87 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECEIPTREQUEST_CREATE0, 0),
88 "CMS_ReceiptRequest_create0"}, 88 "CMS_ReceiptRequest_create0"},
89 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECEIPT_VERIFY, 0), "cms_Receipt_verify"}, 89 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECEIPT_VERIFY, 0), "cms_Receipt_verify"},
90 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_DECRYPT, 0), 90 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_DECRYPT, 0),
91 "CMS_RecipientInfo_decrypt"}, 91 "CMS_RecipientInfo_decrypt"},
92 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_ENCRYPT, 0), 92 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_ENCRYPT, 0),
93 "CMS_RecipientInfo_encrypt"}, 93 "CMS_RecipientInfo_encrypt"},
94 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT, 0), 94 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT, 0),
95 "cms_RecipientInfo_kari_encrypt"}, 95 "cms_RecipientInfo_kari_encrypt"},
96 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ALG, 0), 96 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ALG, 0),
97 "CMS_RecipientInfo_kari_get0_alg"}, 97 "CMS_RecipientInfo_kari_get0_alg"},
98 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ORIG_ID, 0), 98 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ORIG_ID, 0),
99 "CMS_RecipientInfo_kari_get0_orig_id"}, 99 "CMS_RecipientInfo_kari_get0_orig_id"},
100 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KARI_GET0_REKS, 0), 100 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KARI_GET0_REKS, 0),
101 "CMS_RecipientInfo_kari_get0_reks"}, 101 "CMS_RecipientInfo_kari_get0_reks"},
102 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KARI_ORIG_ID_CMP, 0), 102 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KARI_ORIG_ID_CMP, 0),
103 "CMS_RecipientInfo_kari_orig_id_cmp"}, 103 "CMS_RecipientInfo_kari_orig_id_cmp"},
104 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, 0), 104 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, 0),
105 "cms_RecipientInfo_kekri_decrypt"}, 105 "cms_RecipientInfo_kekri_decrypt"},
106 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, 0), 106 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, 0),
107 "cms_RecipientInfo_kekri_encrypt"}, 107 "cms_RecipientInfo_kekri_encrypt"},
108 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, 0), 108 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, 0),
109 "CMS_RecipientInfo_kekri_get0_id"}, 109 "CMS_RecipientInfo_kekri_get0_id"},
110 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, 0), 110 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, 0),
111 "CMS_RecipientInfo_kekri_id_cmp"}, 111 "CMS_RecipientInfo_kekri_id_cmp"},
112 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP, 0), 112 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP, 0),
113 "CMS_RecipientInfo_ktri_cert_cmp"}, 113 "CMS_RecipientInfo_ktri_cert_cmp"},
114 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, 0), 114 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, 0),
115 "cms_RecipientInfo_ktri_decrypt"}, 115 "cms_RecipientInfo_ktri_decrypt"},
116 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, 0), 116 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, 0),
117 "cms_RecipientInfo_ktri_encrypt"}, 117 "cms_RecipientInfo_ktri_encrypt"},
118 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS, 0), 118 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS, 0),
119 "CMS_RecipientInfo_ktri_get0_algs"}, 119 "CMS_RecipientInfo_ktri_get0_algs"},
120 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID, 0), 120 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID, 0),
121 "CMS_RecipientInfo_ktri_get0_signer_id"}, 121 "CMS_RecipientInfo_ktri_get0_signer_id"},
122 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, 0), 122 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, 0),
123 "cms_RecipientInfo_pwri_crypt"}, 123 "cms_RecipientInfo_pwri_crypt"},
124 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_SET0_KEY, 0), 124 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_SET0_KEY, 0),
125 "CMS_RecipientInfo_set0_key"}, 125 "CMS_RecipientInfo_set0_key"},
126 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_SET0_PASSWORD, 0), 126 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_SET0_PASSWORD, 0),
127 "CMS_RecipientInfo_set0_password"}, 127 "CMS_RecipientInfo_set0_password"},
128 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, 0), 128 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, 0),
129 "CMS_RecipientInfo_set0_pkey"}, 129 "CMS_RecipientInfo_set0_pkey"},
130 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SD_ASN1_CTRL, 0), "cms_sd_asn1_ctrl"}, 130 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SD_ASN1_CTRL, 0), "cms_sd_asn1_ctrl"},
131 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SET1_IAS, 0), "cms_set1_ias"}, 131 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SET1_IAS, 0), "cms_set1_ias"},
132 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SET1_KEYID, 0), "cms_set1_keyid"}, 132 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SET1_KEYID, 0), "cms_set1_keyid"},
133 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SET1_SIGNERIDENTIFIER, 0), 133 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SET1_SIGNERIDENTIFIER, 0),
134 "cms_set1_SignerIdentifier"}, 134 "cms_set1_SignerIdentifier"},
135 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SET_DETACHED, 0), "CMS_set_detached"}, 135 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SET_DETACHED, 0), "CMS_set_detached"},
136 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGN, 0), "CMS_sign"}, 136 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGN, 0), "CMS_sign"},
137 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGNED_DATA_INIT, 0), 137 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGNED_DATA_INIT, 0),
138 "cms_signed_data_init"}, 138 "cms_signed_data_init"},
139 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, 0), 139 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, 0),
140 "cms_SignerInfo_content_sign"}, 140 "cms_SignerInfo_content_sign"},
141 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGNERINFO_SIGN, 0), 141 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGNERINFO_SIGN, 0),
142 "CMS_SignerInfo_sign"}, 142 "CMS_SignerInfo_sign"},
143 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGNERINFO_VERIFY, 0), 143 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGNERINFO_VERIFY, 0),
144 "CMS_SignerInfo_verify"}, 144 "CMS_SignerInfo_verify"},
145 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGNERINFO_VERIFY_CERT, 0), 145 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGNERINFO_VERIFY_CERT, 0),
146 "cms_signerinfo_verify_cert"}, 146 "cms_signerinfo_verify_cert"},
147 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, 0), 147 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, 0),
148 "CMS_SignerInfo_verify_content"}, 148 "CMS_SignerInfo_verify_content"},
149 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGN_RECEIPT, 0), "CMS_sign_receipt"}, 149 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_SIGN_RECEIPT, 0), "CMS_sign_receipt"},
150 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_STREAM, 0), "CMS_stream"}, 150 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_STREAM, 0), "CMS_stream"},
151 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_UNCOMPRESS, 0), "CMS_uncompress"}, 151 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_UNCOMPRESS, 0), "CMS_uncompress"},
152 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_VERIFY, 0), "CMS_verify"}, 152 {ERR_PACK(ERR_LIB_CMS, CMS_F_CMS_VERIFY, 0), "CMS_verify"},
153 {ERR_PACK(ERR_LIB_CMS, CMS_F_KEK_UNWRAP_KEY, 0), "kek_unwrap_key"}, 153 {ERR_PACK(ERR_LIB_CMS, CMS_F_KEK_UNWRAP_KEY, 0), "kek_unwrap_key"},
154 {0, NULL} 154 {0, NULL}
155}; 155};
156 156
157static const ERR_STRING_DATA CMS_str_reasons[] = { 157static const ERR_STRING_DATA CMS_str_reasons[] = {
158 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_ADD_SIGNER_ERROR), "add signer error"}, 158 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_ADD_SIGNER_ERROR), "add signer error"},
159 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CERTIFICATE_ALREADY_PRESENT), 159 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CERTIFICATE_ALREADY_PRESENT),
160 "certificate already present"}, 160 "certificate already present"},
161 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CERTIFICATE_HAS_NO_KEYID), 161 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CERTIFICATE_HAS_NO_KEYID),
162 "certificate has no keyid"}, 162 "certificate has no keyid"},
163 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CERTIFICATE_VERIFY_ERROR), 163 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CERTIFICATE_VERIFY_ERROR),
164 "certificate verify error"}, 164 "certificate verify error"},
165 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CIPHER_INITIALISATION_ERROR), 165 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CIPHER_INITIALISATION_ERROR),
166 "cipher initialisation error"}, 166 "cipher initialisation error"},
167 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR), 167 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR),
168 "cipher parameter initialisation error"}, 168 "cipher parameter initialisation error"},
169 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CMS_DATAFINAL_ERROR), 169 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CMS_DATAFINAL_ERROR),
170 "cms datafinal error"}, 170 "cms datafinal error"},
171 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CMS_LIB), "cms lib"}, 171 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CMS_LIB), "cms lib"},
172 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CONTENTIDENTIFIER_MISMATCH), 172 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CONTENTIDENTIFIER_MISMATCH),
173 "contentidentifier mismatch"}, 173 "contentidentifier mismatch"},
174 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CONTENT_NOT_FOUND), "content not found"}, 174 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CONTENT_NOT_FOUND), "content not found"},
175 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CONTENT_TYPE_MISMATCH), 175 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CONTENT_TYPE_MISMATCH),
176 "content type mismatch"}, 176 "content type mismatch"},
177 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CONTENT_TYPE_NOT_COMPRESSED_DATA), 177 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CONTENT_TYPE_NOT_COMPRESSED_DATA),
178 "content type not compressed data"}, 178 "content type not compressed data"},
179 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA), 179 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA),
180 "content type not enveloped data"}, 180 "content type not enveloped data"},
181 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA), 181 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA),
182 "content type not signed data"}, 182 "content type not signed data"},
183 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CONTENT_VERIFY_ERROR), 183 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CONTENT_VERIFY_ERROR),
184 "content verify error"}, 184 "content verify error"},
185 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CTRL_ERROR), "ctrl error"}, 185 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CTRL_ERROR), "ctrl error"},
186 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CTRL_FAILURE), "ctrl failure"}, 186 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_CTRL_FAILURE), "ctrl failure"},
187 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_DECRYPT_ERROR), "decrypt error"}, 187 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_DECRYPT_ERROR), "decrypt error"},
188 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_ERROR_GETTING_PUBLIC_KEY), 188 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_ERROR_GETTING_PUBLIC_KEY),
189 "error getting public key"}, 189 "error getting public key"},
190 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE), 190 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE),
191 "error reading messagedigest attribute"}, 191 "error reading messagedigest attribute"},
192 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_ERROR_SETTING_KEY), "error setting key"}, 192 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_ERROR_SETTING_KEY), "error setting key"},
193 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_ERROR_SETTING_RECIPIENTINFO), 193 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_ERROR_SETTING_RECIPIENTINFO),
194 "error setting recipientinfo"}, 194 "error setting recipientinfo"},
195 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_INVALID_ENCRYPTED_KEY_LENGTH), 195 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_INVALID_ENCRYPTED_KEY_LENGTH),
196 "invalid encrypted key length"}, 196 "invalid encrypted key length"},
197 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER), 197 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER),
198 "invalid key encryption parameter"}, 198 "invalid key encryption parameter"},
199 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_INVALID_KEY_LENGTH), "invalid key length"}, 199 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_INVALID_KEY_LENGTH), "invalid key length"},
200 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_MD_BIO_INIT_ERROR), "md bio init error"}, 200 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_MD_BIO_INIT_ERROR), "md bio init error"},
201 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH), 201 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH),
202 "messagedigest attribute wrong length"}, 202 "messagedigest attribute wrong length"},
203 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_MESSAGEDIGEST_WRONG_LENGTH), 203 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_MESSAGEDIGEST_WRONG_LENGTH),
204 "messagedigest wrong length"}, 204 "messagedigest wrong length"},
205 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_MSGSIGDIGEST_ERROR), "msgsigdigest error"}, 205 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_MSGSIGDIGEST_ERROR), "msgsigdigest error"},
206 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE), 206 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE),
207 "msgsigdigest verification failure"}, 207 "msgsigdigest verification failure"},
208 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_MSGSIGDIGEST_WRONG_LENGTH), 208 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_MSGSIGDIGEST_WRONG_LENGTH),
209 "msgsigdigest wrong length"}, 209 "msgsigdigest wrong length"},
210 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NEED_ONE_SIGNER), "need one signer"}, 210 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NEED_ONE_SIGNER), "need one signer"},
211 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NOT_A_SIGNED_RECEIPT), 211 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NOT_A_SIGNED_RECEIPT),
212 "not a signed receipt"}, 212 "not a signed receipt"},
213 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NOT_ENCRYPTED_DATA), "not encrypted data"}, 213 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NOT_ENCRYPTED_DATA), "not encrypted data"},
214 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NOT_KEK), "not kek"}, 214 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NOT_KEK), "not kek"},
215 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NOT_KEY_AGREEMENT), "not key agreement"}, 215 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NOT_KEY_AGREEMENT), "not key agreement"},
216 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NOT_KEY_TRANSPORT), "not key transport"}, 216 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NOT_KEY_TRANSPORT), "not key transport"},
217 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NOT_PWRI), "not pwri"}, 217 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NOT_PWRI), "not pwri"},
218 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE), 218 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE),
219 "not supported for this key type"}, 219 "not supported for this key type"},
220 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_CIPHER), "no cipher"}, 220 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_CIPHER), "no cipher"},
221 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_CONTENT), "no content"}, 221 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_CONTENT), "no content"},
222 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_CONTENT_TYPE), "no content type"}, 222 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_CONTENT_TYPE), "no content type"},
223 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_DEFAULT_DIGEST), "no default digest"}, 223 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_DEFAULT_DIGEST), "no default digest"},
224 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_DIGEST_SET), "no digest set"}, 224 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_DIGEST_SET), "no digest set"},
225 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_KEY), "no key"}, 225 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_KEY), "no key"},
226 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_KEY_OR_CERT), "no key or cert"}, 226 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_KEY_OR_CERT), "no key or cert"},
227 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_MATCHING_DIGEST), "no matching digest"}, 227 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_MATCHING_DIGEST), "no matching digest"},
228 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_MATCHING_RECIPIENT), 228 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_MATCHING_RECIPIENT),
229 "no matching recipient"}, 229 "no matching recipient"},
230 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_MATCHING_SIGNATURE), 230 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_MATCHING_SIGNATURE),
231 "no matching signature"}, 231 "no matching signature"},
232 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_MSGSIGDIGEST), "no msgsigdigest"}, 232 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_MSGSIGDIGEST), "no msgsigdigest"},
233 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_PASSWORD), "no password"}, 233 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_PASSWORD), "no password"},
234 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_PRIVATE_KEY), "no private key"}, 234 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_PRIVATE_KEY), "no private key"},
235 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_PUBLIC_KEY), "no public key"}, 235 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_PUBLIC_KEY), "no public key"},
236 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_RECEIPT_REQUEST), "no receipt request"}, 236 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_RECEIPT_REQUEST), "no receipt request"},
237 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_SIGNERS), "no signers"}, 237 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_NO_SIGNERS), "no signers"},
238 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE), 238 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE),
239 "private key does not match certificate"}, 239 "private key does not match certificate"},
240 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_RECEIPT_DECODE_ERROR), 240 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_RECEIPT_DECODE_ERROR),
241 "receipt decode error"}, 241 "receipt decode error"},
242 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_RECIPIENT_ERROR), "recipient error"}, 242 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_RECIPIENT_ERROR), "recipient error"},
243 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND), 243 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND),
244 "signer certificate not found"}, 244 "signer certificate not found"},
245 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_SIGNFINAL_ERROR), "signfinal error"}, 245 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_SIGNFINAL_ERROR), "signfinal error"},
246 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_SMIME_TEXT_ERROR), "smime text error"}, 246 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_SMIME_TEXT_ERROR), "smime text error"},
247 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_STORE_INIT_ERROR), "store init error"}, 247 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_STORE_INIT_ERROR), "store init error"},
248 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_TYPE_NOT_COMPRESSED_DATA), 248 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_TYPE_NOT_COMPRESSED_DATA),
249 "type not compressed data"}, 249 "type not compressed data"},
250 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_TYPE_NOT_DATA), "type not data"}, 250 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_TYPE_NOT_DATA), "type not data"},
251 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_TYPE_NOT_DIGESTED_DATA), 251 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_TYPE_NOT_DIGESTED_DATA),
252 "type not digested data"}, 252 "type not digested data"},
253 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_TYPE_NOT_ENCRYPTED_DATA), 253 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_TYPE_NOT_ENCRYPTED_DATA),
254 "type not encrypted data"}, 254 "type not encrypted data"},
255 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_TYPE_NOT_ENVELOPED_DATA), 255 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_TYPE_NOT_ENVELOPED_DATA),
256 "type not enveloped data"}, 256 "type not enveloped data"},
257 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNABLE_TO_FINALIZE_CONTEXT), 257 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNABLE_TO_FINALIZE_CONTEXT),
258 "unable to finalize context"}, 258 "unable to finalize context"},
259 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNKNOWN_CIPHER), "unknown cipher"}, 259 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNKNOWN_CIPHER), "unknown cipher"},
260 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNKNOWN_DIGEST_ALGORITHM), 260 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNKNOWN_DIGEST_ALGORITHM),
261 "unknown digest algorithm"}, 261 "unknown digest algorithm"},
262 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNKNOWN_ID), "unknown id"}, 262 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNKNOWN_ID), "unknown id"},
263 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM), 263 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM),
264 "unsupported compression algorithm"}, 264 "unsupported compression algorithm"},
265 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNSUPPORTED_CONTENT_TYPE), 265 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNSUPPORTED_CONTENT_TYPE),
266 "unsupported content type"}, 266 "unsupported content type"},
267 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNSUPPORTED_KEK_ALGORITHM), 267 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNSUPPORTED_KEK_ALGORITHM),
268 "unsupported kek algorithm"}, 268 "unsupported kek algorithm"},
269 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM), 269 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM),
270 "unsupported key encryption algorithm"}, 270 "unsupported key encryption algorithm"},
271 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE), 271 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE),
272 "unsupported recipientinfo type"}, 272 "unsupported recipientinfo type"},
273 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNSUPPORTED_RECIPIENT_TYPE), 273 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNSUPPORTED_RECIPIENT_TYPE),
274 "unsupported recipient type"}, 274 "unsupported recipient type"},
275 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNSUPPORTED_TYPE), "unsupported type"}, 275 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNSUPPORTED_TYPE), "unsupported type"},
276 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNWRAP_ERROR), "unwrap error"}, 276 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNWRAP_ERROR), "unwrap error"},
277 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNWRAP_FAILURE), "unwrap failure"}, 277 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_UNWRAP_FAILURE), "unwrap failure"},
278 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_VERIFICATION_FAILURE), 278 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_VERIFICATION_FAILURE),
279 "verification failure"}, 279 "verification failure"},
280 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_WRAP_ERROR), "wrap error"}, 280 {ERR_PACK(ERR_LIB_CMS, 0, CMS_R_WRAP_ERROR), "wrap error"},
281 {0, NULL} 281 {0, NULL}
282}; 282};
283 283
284#endif 284#endif
@@ -286,10 +286,10 @@ static const ERR_STRING_DATA CMS_str_reasons[] = {
286int ERR_load_CMS_strings(void) 286int ERR_load_CMS_strings(void)
287{ 287{
288#ifndef OPENSSL_NO_ERR 288#ifndef OPENSSL_NO_ERR
289 if (ERR_func_error_string(CMS_str_functs[0].error) == NULL) { 289 if (ERR_func_error_string(CMS_str_functs[0].error) == NULL) {
290 ERR_load_strings_const(CMS_str_functs); 290 ERR_load_strings_const(CMS_str_functs);
291 ERR_load_strings_const(CMS_str_reasons); 291 ERR_load_strings_const(CMS_str_reasons);
292 } 292 }
293#endif 293#endif
294 return 1; 294 return 1;
295} 295}
diff --git a/src/lib/libcrypto/cms/cms_ess.c b/src/lib/libcrypto/cms/cms_ess.c
index d1483c4d49..bcc3e58368 100644
--- a/src/lib/libcrypto/cms/cms_ess.c
+++ b/src/lib/libcrypto/cms/cms_ess.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_ess.c,v 1.12 2019/08/10 16:39:17 jsing Exp $ */ 1/* $OpenBSD: cms_ess.c,v 1.13 2019/08/10 16:42:20 jsing Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -67,274 +67,274 @@ IMPLEMENT_ASN1_FUNCTIONS(CMS_ReceiptRequest)
67 67
68int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr) 68int CMS_get1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest **prr)
69{ 69{
70 ASN1_STRING *str; 70 ASN1_STRING *str;
71 CMS_ReceiptRequest *rr = NULL; 71 CMS_ReceiptRequest *rr = NULL;
72 if (prr) 72 if (prr)
73 *prr = NULL; 73 *prr = NULL;
74 str = CMS_signed_get0_data_by_OBJ(si, 74 str = CMS_signed_get0_data_by_OBJ(si,
75 OBJ_nid2obj 75 OBJ_nid2obj
76 (NID_id_smime_aa_receiptRequest), -3, 76 (NID_id_smime_aa_receiptRequest), -3,
77 V_ASN1_SEQUENCE); 77 V_ASN1_SEQUENCE);
78 if (!str) 78 if (!str)
79 return 0; 79 return 0;
80 80
81 rr = ASN1_item_unpack(str, ASN1_ITEM_rptr(CMS_ReceiptRequest)); 81 rr = ASN1_item_unpack(str, ASN1_ITEM_rptr(CMS_ReceiptRequest));
82 if (!rr) 82 if (!rr)
83 return -1; 83 return -1;
84 if (prr) 84 if (prr)
85 *prr = rr; 85 *prr = rr;
86 else 86 else
87 CMS_ReceiptRequest_free(rr); 87 CMS_ReceiptRequest_free(rr);
88 return 1; 88 return 1;
89} 89}
90 90
91CMS_ReceiptRequest *CMS_ReceiptRequest_create0(unsigned char *id, int idlen, 91CMS_ReceiptRequest *CMS_ReceiptRequest_create0(unsigned char *id, int idlen,
92 int allorfirst, 92 int allorfirst,
93 STACK_OF(GENERAL_NAMES) 93 STACK_OF(GENERAL_NAMES)
94 *receiptList, STACK_OF(GENERAL_NAMES) 94 *receiptList, STACK_OF(GENERAL_NAMES)
95 *receiptsTo) 95 *receiptsTo)
96{ 96{
97 CMS_ReceiptRequest *rr = NULL; 97 CMS_ReceiptRequest *rr = NULL;
98 98
99 rr = CMS_ReceiptRequest_new(); 99 rr = CMS_ReceiptRequest_new();
100 if (rr == NULL) 100 if (rr == NULL)
101 goto merr; 101 goto merr;
102 if (id) 102 if (id)
103 ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen); 103 ASN1_STRING_set0(rr->signedContentIdentifier, id, idlen);
104 else { 104 else {
105 if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32)) 105 if (!ASN1_STRING_set(rr->signedContentIdentifier, NULL, 32))
106 goto merr; 106 goto merr;
107 if (RAND_bytes(rr->signedContentIdentifier->data, 32) <= 0) 107 if (RAND_bytes(rr->signedContentIdentifier->data, 32) <= 0)
108 goto err; 108 goto err;
109 } 109 }
110 110
111 sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free); 111 sk_GENERAL_NAMES_pop_free(rr->receiptsTo, GENERAL_NAMES_free);
112 rr->receiptsTo = receiptsTo; 112 rr->receiptsTo = receiptsTo;
113 113
114 if (receiptList) { 114 if (receiptList) {
115 rr->receiptsFrom->type = 1; 115 rr->receiptsFrom->type = 1;
116 rr->receiptsFrom->d.receiptList = receiptList; 116 rr->receiptsFrom->d.receiptList = receiptList;
117 } else { 117 } else {
118 rr->receiptsFrom->type = 0; 118 rr->receiptsFrom->type = 0;
119 rr->receiptsFrom->d.allOrFirstTier = allorfirst; 119 rr->receiptsFrom->d.allOrFirstTier = allorfirst;
120 } 120 }
121 121
122 return rr; 122 return rr;
123 123
124 merr: 124 merr:
125 CMSerr(CMS_F_CMS_RECEIPTREQUEST_CREATE0, ERR_R_MALLOC_FAILURE); 125 CMSerr(CMS_F_CMS_RECEIPTREQUEST_CREATE0, ERR_R_MALLOC_FAILURE);
126 126
127 err: 127 err:
128 CMS_ReceiptRequest_free(rr); 128 CMS_ReceiptRequest_free(rr);
129 return NULL; 129 return NULL;
130 130
131} 131}
132 132
133int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr) 133int CMS_add1_ReceiptRequest(CMS_SignerInfo *si, CMS_ReceiptRequest *rr)
134{ 134{
135 unsigned char *rrder = NULL; 135 unsigned char *rrder = NULL;
136 int rrderlen, r = 0; 136 int rrderlen, r = 0;
137 137
138 rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder); 138 rrderlen = i2d_CMS_ReceiptRequest(rr, &rrder);
139 if (rrderlen < 0) 139 if (rrderlen < 0)
140 goto merr; 140 goto merr;
141 141
142 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest, 142 if (!CMS_signed_add1_attr_by_NID(si, NID_id_smime_aa_receiptRequest,
143 V_ASN1_SEQUENCE, rrder, rrderlen)) 143 V_ASN1_SEQUENCE, rrder, rrderlen))
144 goto merr; 144 goto merr;
145 145
146 r = 1; 146 r = 1;
147 147
148 merr: 148 merr:
149 if (!r) 149 if (!r)
150 CMSerr(CMS_F_CMS_ADD1_RECEIPTREQUEST, ERR_R_MALLOC_FAILURE); 150 CMSerr(CMS_F_CMS_ADD1_RECEIPTREQUEST, ERR_R_MALLOC_FAILURE);
151 151
152 OPENSSL_free(rrder); 152 OPENSSL_free(rrder);
153 153
154 return r; 154 return r;
155 155
156} 156}
157 157
158void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr, 158void CMS_ReceiptRequest_get0_values(CMS_ReceiptRequest *rr,
159 ASN1_STRING **pcid, 159 ASN1_STRING **pcid,
160 int *pallorfirst, 160 int *pallorfirst,
161 STACK_OF(GENERAL_NAMES) **plist, 161 STACK_OF(GENERAL_NAMES) **plist,
162 STACK_OF(GENERAL_NAMES) **prto) 162 STACK_OF(GENERAL_NAMES) **prto)
163{ 163{
164 if (pcid) 164 if (pcid)
165 *pcid = rr->signedContentIdentifier; 165 *pcid = rr->signedContentIdentifier;
166 if (rr->receiptsFrom->type == 0) { 166 if (rr->receiptsFrom->type == 0) {
167 if (pallorfirst) 167 if (pallorfirst)
168 *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier; 168 *pallorfirst = (int)rr->receiptsFrom->d.allOrFirstTier;
169 if (plist) 169 if (plist)
170 *plist = NULL; 170 *plist = NULL;
171 } else { 171 } else {
172 if (pallorfirst) 172 if (pallorfirst)
173 *pallorfirst = -1; 173 *pallorfirst = -1;
174 if (plist) 174 if (plist)
175 *plist = rr->receiptsFrom->d.receiptList; 175 *plist = rr->receiptsFrom->d.receiptList;
176 } 176 }
177 if (prto) 177 if (prto)
178 *prto = rr->receiptsTo; 178 *prto = rr->receiptsTo;
179} 179}
180 180
181/* Digest a SignerInfo structure for msgSigDigest attribute processing */ 181/* Digest a SignerInfo structure for msgSigDigest attribute processing */
182 182
183static int cms_msgSigDigest(CMS_SignerInfo *si, 183static int cms_msgSigDigest(CMS_SignerInfo *si,
184 unsigned char *dig, unsigned int *diglen) 184 unsigned char *dig, unsigned int *diglen)
185{ 185{
186 const EVP_MD *md; 186 const EVP_MD *md;
187 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm); 187 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
188 if (md == NULL) 188 if (md == NULL)
189 return 0; 189 return 0;
190 if (!ASN1_item_digest(ASN1_ITEM_rptr(CMS_Attributes_Verify), md, 190 if (!ASN1_item_digest(ASN1_ITEM_rptr(CMS_Attributes_Verify), md,
191 si->signedAttrs, dig, diglen)) 191 si->signedAttrs, dig, diglen))
192 return 0; 192 return 0;
193 return 1; 193 return 1;
194} 194}
195 195
196/* Add a msgSigDigest attribute to a SignerInfo */ 196/* Add a msgSigDigest attribute to a SignerInfo */
197 197
198int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src) 198int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src)
199{ 199{
200 unsigned char dig[EVP_MAX_MD_SIZE]; 200 unsigned char dig[EVP_MAX_MD_SIZE];
201 unsigned int diglen; 201 unsigned int diglen;
202 if (!cms_msgSigDigest(src, dig, &diglen)) { 202 if (!cms_msgSigDigest(src, dig, &diglen)) {
203 CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, CMS_R_MSGSIGDIGEST_ERROR); 203 CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, CMS_R_MSGSIGDIGEST_ERROR);
204 return 0; 204 return 0;
205 } 205 }
206 if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest, 206 if (!CMS_signed_add1_attr_by_NID(dest, NID_id_smime_aa_msgSigDigest,
207 V_ASN1_OCTET_STRING, dig, diglen)) { 207 V_ASN1_OCTET_STRING, dig, diglen)) {
208 CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, ERR_R_MALLOC_FAILURE); 208 CMSerr(CMS_F_CMS_MSGSIGDIGEST_ADD1, ERR_R_MALLOC_FAILURE);
209 return 0; 209 return 0;
210 } 210 }
211 return 1; 211 return 1;
212} 212}
213 213
214/* Verify signed receipt after it has already passed normal CMS verify */ 214/* Verify signed receipt after it has already passed normal CMS verify */
215 215
216int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms) 216int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
217{ 217{
218 int r = 0, i; 218 int r = 0, i;
219 CMS_ReceiptRequest *rr = NULL; 219 CMS_ReceiptRequest *rr = NULL;
220 CMS_Receipt *rct = NULL; 220 CMS_Receipt *rct = NULL;
221 STACK_OF(CMS_SignerInfo) *sis, *osis; 221 STACK_OF(CMS_SignerInfo) *sis, *osis;
222 CMS_SignerInfo *si, *osi = NULL; 222 CMS_SignerInfo *si, *osi = NULL;
223 ASN1_OCTET_STRING *msig, **pcont; 223 ASN1_OCTET_STRING *msig, **pcont;
224 ASN1_OBJECT *octype; 224 ASN1_OBJECT *octype;
225 unsigned char dig[EVP_MAX_MD_SIZE]; 225 unsigned char dig[EVP_MAX_MD_SIZE];
226 unsigned int diglen; 226 unsigned int diglen;
227 227
228 /* Get SignerInfos, also checks SignedData content type */ 228 /* Get SignerInfos, also checks SignedData content type */
229 osis = CMS_get0_SignerInfos(req_cms); 229 osis = CMS_get0_SignerInfos(req_cms);
230 sis = CMS_get0_SignerInfos(cms); 230 sis = CMS_get0_SignerInfos(cms);
231 if (!osis || !sis) 231 if (!osis || !sis)
232 goto err; 232 goto err;
233 233
234 if (sk_CMS_SignerInfo_num(sis) != 1) { 234 if (sk_CMS_SignerInfo_num(sis) != 1) {
235 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NEED_ONE_SIGNER); 235 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NEED_ONE_SIGNER);
236 goto err; 236 goto err;
237 } 237 }
238 238
239 /* Check receipt content type */ 239 /* Check receipt content type */
240 if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) { 240 if (OBJ_obj2nid(CMS_get0_eContentType(cms)) != NID_id_smime_ct_receipt) {
241 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NOT_A_SIGNED_RECEIPT); 241 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NOT_A_SIGNED_RECEIPT);
242 goto err; 242 goto err;
243 } 243 }
244 244
245 /* Extract and decode receipt content */ 245 /* Extract and decode receipt content */
246 pcont = CMS_get0_content(cms); 246 pcont = CMS_get0_content(cms);
247 if (!pcont || !*pcont) { 247 if (!pcont || !*pcont) {
248 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT); 248 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT);
249 goto err; 249 goto err;
250 } 250 }
251 251
252 rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt)); 252 rct = ASN1_item_unpack(*pcont, ASN1_ITEM_rptr(CMS_Receipt));
253 253
254 if (!rct) { 254 if (!rct) {
255 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_RECEIPT_DECODE_ERROR); 255 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_RECEIPT_DECODE_ERROR);
256 goto err; 256 goto err;
257 } 257 }
258 258
259 /* Locate original request */ 259 /* Locate original request */
260 260
261 for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) { 261 for (i = 0; i < sk_CMS_SignerInfo_num(osis); i++) {
262 osi = sk_CMS_SignerInfo_value(osis, i); 262 osi = sk_CMS_SignerInfo_value(osis, i);
263 if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue)) 263 if (!ASN1_STRING_cmp(osi->signature, rct->originatorSignatureValue))
264 break; 264 break;
265 } 265 }
266 266
267 if (i == sk_CMS_SignerInfo_num(osis)) { 267 if (i == sk_CMS_SignerInfo_num(osis)) {
268 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MATCHING_SIGNATURE); 268 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MATCHING_SIGNATURE);
269 goto err; 269 goto err;
270 } 270 }
271 271
272 si = sk_CMS_SignerInfo_value(sis, 0); 272 si = sk_CMS_SignerInfo_value(sis, 0);
273 273
274 /* Get msgSigDigest value and compare */ 274 /* Get msgSigDigest value and compare */
275 275
276 msig = CMS_signed_get0_data_by_OBJ(si, 276 msig = CMS_signed_get0_data_by_OBJ(si,
277 OBJ_nid2obj 277 OBJ_nid2obj
278 (NID_id_smime_aa_msgSigDigest), -3, 278 (NID_id_smime_aa_msgSigDigest), -3,
279 V_ASN1_OCTET_STRING); 279 V_ASN1_OCTET_STRING);
280 280
281 if (!msig) { 281 if (!msig) {
282 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MSGSIGDIGEST); 282 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_MSGSIGDIGEST);
283 goto err; 283 goto err;
284 } 284 }
285 285
286 if (!cms_msgSigDigest(osi, dig, &diglen)) { 286 if (!cms_msgSigDigest(osi, dig, &diglen)) {
287 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_ERROR); 287 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_ERROR);
288 goto err; 288 goto err;
289 } 289 }
290 290
291 if (diglen != (unsigned int)msig->length) { 291 if (diglen != (unsigned int)msig->length) {
292 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_WRONG_LENGTH); 292 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_MSGSIGDIGEST_WRONG_LENGTH);
293 goto err; 293 goto err;
294 } 294 }
295 295
296 if (memcmp(dig, msig->data, diglen)) { 296 if (memcmp(dig, msig->data, diglen)) {
297 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, 297 CMSerr(CMS_F_CMS_RECEIPT_VERIFY,
298 CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE); 298 CMS_R_MSGSIGDIGEST_VERIFICATION_FAILURE);
299 goto err; 299 goto err;
300 } 300 }
301 301
302 /* Compare content types */ 302 /* Compare content types */
303 303
304 octype = CMS_signed_get0_data_by_OBJ(osi, 304 octype = CMS_signed_get0_data_by_OBJ(osi,
305 OBJ_nid2obj(NID_pkcs9_contentType), 305 OBJ_nid2obj(NID_pkcs9_contentType),
306 -3, V_ASN1_OBJECT); 306 -3, V_ASN1_OBJECT);
307 if (!octype) { 307 if (!octype) {
308 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT_TYPE); 308 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_CONTENT_TYPE);
309 goto err; 309 goto err;
310 } 310 }
311 311
312 /* Compare details in receipt request */ 312 /* Compare details in receipt request */
313 313
314 if (OBJ_cmp(octype, rct->contentType)) { 314 if (OBJ_cmp(octype, rct->contentType)) {
315 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENT_TYPE_MISMATCH); 315 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENT_TYPE_MISMATCH);
316 goto err; 316 goto err;
317 } 317 }
318 318
319 /* Get original receipt request details */ 319 /* Get original receipt request details */
320 320
321 if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) { 321 if (CMS_get1_ReceiptRequest(osi, &rr) <= 0) {
322 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_RECEIPT_REQUEST); 322 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_NO_RECEIPT_REQUEST);
323 goto err; 323 goto err;
324 } 324 }
325 325
326 if (ASN1_STRING_cmp(rr->signedContentIdentifier, 326 if (ASN1_STRING_cmp(rr->signedContentIdentifier,
327 rct->signedContentIdentifier)) { 327 rct->signedContentIdentifier)) {
328 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENTIDENTIFIER_MISMATCH); 328 CMSerr(CMS_F_CMS_RECEIPT_VERIFY, CMS_R_CONTENTIDENTIFIER_MISMATCH);
329 goto err; 329 goto err;
330 } 330 }
331 331
332 r = 1; 332 r = 1;
333 333
334 err: 334 err:
335 CMS_ReceiptRequest_free(rr); 335 CMS_ReceiptRequest_free(rr);
336 M_ASN1_free_of(rct, CMS_Receipt); 336 M_ASN1_free_of(rct, CMS_Receipt);
337 return r; 337 return r;
338 338
339} 339}
340 340
@@ -345,38 +345,38 @@ int cms_Receipt_verify(CMS_ContentInfo *cms, CMS_ContentInfo *req_cms)
345 345
346ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si) 346ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si)
347{ 347{
348 CMS_Receipt rct; 348 CMS_Receipt rct;
349 CMS_ReceiptRequest *rr = NULL; 349 CMS_ReceiptRequest *rr = NULL;
350 ASN1_OBJECT *ctype; 350 ASN1_OBJECT *ctype;
351 ASN1_OCTET_STRING *os = NULL; 351 ASN1_OCTET_STRING *os = NULL;
352 352
353 /* Get original receipt request */ 353 /* Get original receipt request */
354 354
355 /* Get original receipt request details */ 355 /* Get original receipt request details */
356 356
357 if (CMS_get1_ReceiptRequest(si, &rr) <= 0) { 357 if (CMS_get1_ReceiptRequest(si, &rr) <= 0) {
358 CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_RECEIPT_REQUEST); 358 CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_RECEIPT_REQUEST);
359 goto err; 359 goto err;
360 } 360 }
361 361
362 /* Get original content type */ 362 /* Get original content type */
363 363
364 ctype = CMS_signed_get0_data_by_OBJ(si, 364 ctype = CMS_signed_get0_data_by_OBJ(si,
365 OBJ_nid2obj(NID_pkcs9_contentType), 365 OBJ_nid2obj(NID_pkcs9_contentType),
366 -3, V_ASN1_OBJECT); 366 -3, V_ASN1_OBJECT);
367 if (!ctype) { 367 if (!ctype) {
368 CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_CONTENT_TYPE); 368 CMSerr(CMS_F_CMS_ENCODE_RECEIPT, CMS_R_NO_CONTENT_TYPE);
369 goto err; 369 goto err;
370 } 370 }
371 371
372 rct.version = 1; 372 rct.version = 1;
373 rct.contentType = ctype; 373 rct.contentType = ctype;
374 rct.signedContentIdentifier = rr->signedContentIdentifier; 374 rct.signedContentIdentifier = rr->signedContentIdentifier;
375 rct.originatorSignatureValue = si->signature; 375 rct.originatorSignatureValue = si->signature;
376 376
377 os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL); 377 os = ASN1_item_pack(&rct, ASN1_ITEM_rptr(CMS_Receipt), NULL);
378 378
379 err: 379 err:
380 CMS_ReceiptRequest_free(rr); 380 CMS_ReceiptRequest_free(rr);
381 return os; 381 return os;
382} 382}
diff --git a/src/lib/libcrypto/cms/cms_io.c b/src/lib/libcrypto/cms/cms_io.c
index 9af30424e5..56d7cd68e1 100644
--- a/src/lib/libcrypto/cms/cms_io.c
+++ b/src/lib/libcrypto/cms/cms_io.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_io.c,v 1.7 2019/08/10 16:03:54 jsing Exp $ */ 1/* $OpenBSD: cms_io.c,v 1.8 2019/08/10 16:42:20 jsing Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -61,73 +61,73 @@
61 61
62int CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms) 62int CMS_stream(unsigned char ***boundary, CMS_ContentInfo *cms)
63{ 63{
64 ASN1_OCTET_STRING **pos; 64 ASN1_OCTET_STRING **pos;
65 pos = CMS_get0_content(cms); 65 pos = CMS_get0_content(cms);
66 if (pos == NULL) 66 if (pos == NULL)
67 return 0; 67 return 0;
68 if (*pos == NULL) 68 if (*pos == NULL)
69 *pos = ASN1_OCTET_STRING_new(); 69 *pos = ASN1_OCTET_STRING_new();
70 if (*pos != NULL) { 70 if (*pos != NULL) {
71 (*pos)->flags |= ASN1_STRING_FLAG_NDEF; 71 (*pos)->flags |= ASN1_STRING_FLAG_NDEF;
72 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT; 72 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
73 *boundary = &(*pos)->data; 73 *boundary = &(*pos)->data;
74 return 1; 74 return 1;
75 } 75 }
76 CMSerr(CMS_F_CMS_STREAM, ERR_R_MALLOC_FAILURE); 76 CMSerr(CMS_F_CMS_STREAM, ERR_R_MALLOC_FAILURE);
77 return 0; 77 return 0;
78} 78}
79 79
80CMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms) 80CMS_ContentInfo *d2i_CMS_bio(BIO *bp, CMS_ContentInfo **cms)
81{ 81{
82 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms); 82 return ASN1_item_d2i_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
83} 83}
84 84
85int i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms) 85int i2d_CMS_bio(BIO *bp, CMS_ContentInfo *cms)
86{ 86{
87 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms); 87 return ASN1_item_i2d_bio(ASN1_ITEM_rptr(CMS_ContentInfo), bp, cms);
88} 88}
89 89
90IMPLEMENT_PEM_rw_const(CMS, CMS_ContentInfo, PEM_STRING_CMS, CMS_ContentInfo) 90IMPLEMENT_PEM_rw_const(CMS, CMS_ContentInfo, PEM_STRING_CMS, CMS_ContentInfo)
91 91
92BIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms) 92BIO *BIO_new_CMS(BIO *out, CMS_ContentInfo *cms)
93{ 93{
94 return BIO_new_NDEF(out, (ASN1_VALUE *)cms, 94 return BIO_new_NDEF(out, (ASN1_VALUE *)cms,
95 ASN1_ITEM_rptr(CMS_ContentInfo)); 95 ASN1_ITEM_rptr(CMS_ContentInfo));
96} 96}
97 97
98/* CMS wrappers round generalised stream and MIME routines */ 98/* CMS wrappers round generalised stream and MIME routines */
99 99
100int i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags) 100int i2d_CMS_bio_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, int flags)
101{ 101{
102 return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)cms, in, flags, 102 return i2d_ASN1_bio_stream(out, (ASN1_VALUE *)cms, in, flags,
103 ASN1_ITEM_rptr(CMS_ContentInfo)); 103 ASN1_ITEM_rptr(CMS_ContentInfo));
104} 104}
105 105
106int PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in, 106int PEM_write_bio_CMS_stream(BIO *out, CMS_ContentInfo *cms, BIO *in,
107 int flags) 107 int flags)
108{ 108{
109 return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *)cms, in, flags, 109 return PEM_write_bio_ASN1_stream(out, (ASN1_VALUE *)cms, in, flags,
110 "CMS", ASN1_ITEM_rptr(CMS_ContentInfo)); 110 "CMS", ASN1_ITEM_rptr(CMS_ContentInfo));
111} 111}
112 112
113int SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags) 113int SMIME_write_CMS(BIO *bio, CMS_ContentInfo *cms, BIO *data, int flags)
114{ 114{
115 STACK_OF(X509_ALGOR) *mdalgs; 115 STACK_OF(X509_ALGOR) *mdalgs;
116 int ctype_nid = OBJ_obj2nid(cms->contentType); 116 int ctype_nid = OBJ_obj2nid(cms->contentType);
117 int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms)); 117 int econt_nid = OBJ_obj2nid(CMS_get0_eContentType(cms));
118 if (ctype_nid == NID_pkcs7_signed) 118 if (ctype_nid == NID_pkcs7_signed)
119 mdalgs = cms->d.signedData->digestAlgorithms; 119 mdalgs = cms->d.signedData->digestAlgorithms;
120 else 120 else
121 mdalgs = NULL; 121 mdalgs = NULL;
122 122
123 return SMIME_write_ASN1(bio, (ASN1_VALUE *)cms, data, flags, 123 return SMIME_write_ASN1(bio, (ASN1_VALUE *)cms, data, flags,
124 ctype_nid, econt_nid, mdalgs, 124 ctype_nid, econt_nid, mdalgs,
125 ASN1_ITEM_rptr(CMS_ContentInfo)); 125 ASN1_ITEM_rptr(CMS_ContentInfo));
126} 126}
127 127
128CMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont) 128CMS_ContentInfo *SMIME_read_CMS(BIO *bio, BIO **bcont)
129{ 129{
130 return (CMS_ContentInfo *)SMIME_read_ASN1(bio, bcont, 130 return (CMS_ContentInfo *)SMIME_read_ASN1(bio, bcont,
131 ASN1_ITEM_rptr 131 ASN1_ITEM_rptr
132 (CMS_ContentInfo)); 132 (CMS_ContentInfo));
133} 133}
diff --git a/src/lib/libcrypto/cms/cms_kari.c b/src/lib/libcrypto/cms/cms_kari.c
index 8a52d8401c..7aad3c755b 100644
--- a/src/lib/libcrypto/cms/cms_kari.c
+++ b/src/lib/libcrypto/cms/cms_kari.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_kari.c,v 1.4 2019/08/10 16:39:17 jsing Exp $ */ 1/* $OpenBSD: cms_kari.c,v 1.5 2019/08/10 16:42:20 jsing Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -65,19 +65,19 @@
65/* Key Agreement Recipient Info (KARI) routines */ 65/* Key Agreement Recipient Info (KARI) routines */
66 66
67int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri, 67int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri,
68 X509_ALGOR **palg, 68 X509_ALGOR **palg,
69 ASN1_OCTET_STRING **pukm) 69 ASN1_OCTET_STRING **pukm)
70{ 70{
71 if (ri->type != CMS_RECIPINFO_AGREE) { 71 if (ri->type != CMS_RECIPINFO_AGREE) {
72 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ALG, 72 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ALG,
73 CMS_R_NOT_KEY_AGREEMENT); 73 CMS_R_NOT_KEY_AGREEMENT);
74 return 0; 74 return 0;
75 } 75 }
76 if (palg) 76 if (palg)
77 *palg = ri->d.kari->keyEncryptionAlgorithm; 77 *palg = ri->d.kari->keyEncryptionAlgorithm;
78 if (pukm) 78 if (pukm)
79 *pukm = ri->d.kari->ukm; 79 *pukm = ri->d.kari->ukm;
80 return 1; 80 return 1;
81} 81}
82 82
83/* Retrieve recipient encrypted keys from a kari */ 83/* Retrieve recipient encrypted keys from a kari */
@@ -85,142 +85,142 @@ int CMS_RecipientInfo_kari_get0_alg(CMS_RecipientInfo *ri,
85STACK_OF(CMS_RecipientEncryptedKey) 85STACK_OF(CMS_RecipientEncryptedKey)
86*CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo *ri) 86*CMS_RecipientInfo_kari_get0_reks(CMS_RecipientInfo *ri)
87{ 87{
88 if (ri->type != CMS_RECIPINFO_AGREE) { 88 if (ri->type != CMS_RECIPINFO_AGREE) {
89 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_REKS, 89 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_REKS,
90 CMS_R_NOT_KEY_AGREEMENT); 90 CMS_R_NOT_KEY_AGREEMENT);
91 return NULL; 91 return NULL;
92 } 92 }
93 return ri->d.kari->recipientEncryptedKeys; 93 return ri->d.kari->recipientEncryptedKeys;
94} 94}
95 95
96int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo *ri, 96int CMS_RecipientInfo_kari_get0_orig_id(CMS_RecipientInfo *ri,
97 X509_ALGOR **pubalg, 97 X509_ALGOR **pubalg,
98 ASN1_BIT_STRING **pubkey, 98 ASN1_BIT_STRING **pubkey,
99 ASN1_OCTET_STRING **keyid, 99 ASN1_OCTET_STRING **keyid,
100 X509_NAME **issuer, 100 X509_NAME **issuer,
101 ASN1_INTEGER **sno) 101 ASN1_INTEGER **sno)
102{ 102{
103 CMS_OriginatorIdentifierOrKey *oik; 103 CMS_OriginatorIdentifierOrKey *oik;
104 if (ri->type != CMS_RECIPINFO_AGREE) { 104 if (ri->type != CMS_RECIPINFO_AGREE) {
105 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ORIG_ID, 105 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_GET0_ORIG_ID,
106 CMS_R_NOT_KEY_AGREEMENT); 106 CMS_R_NOT_KEY_AGREEMENT);
107 return 0; 107 return 0;
108 } 108 }
109 oik = ri->d.kari->originator; 109 oik = ri->d.kari->originator;
110 if (issuer) 110 if (issuer)
111 *issuer = NULL; 111 *issuer = NULL;
112 if (sno) 112 if (sno)
113 *sno = NULL; 113 *sno = NULL;
114 if (keyid) 114 if (keyid)
115 *keyid = NULL; 115 *keyid = NULL;
116 if (pubalg) 116 if (pubalg)
117 *pubalg = NULL; 117 *pubalg = NULL;
118 if (pubkey) 118 if (pubkey)
119 *pubkey = NULL; 119 *pubkey = NULL;
120 if (oik->type == CMS_OIK_ISSUER_SERIAL) { 120 if (oik->type == CMS_OIK_ISSUER_SERIAL) {
121 if (issuer) 121 if (issuer)
122 *issuer = oik->d.issuerAndSerialNumber->issuer; 122 *issuer = oik->d.issuerAndSerialNumber->issuer;
123 if (sno) 123 if (sno)
124 *sno = oik->d.issuerAndSerialNumber->serialNumber; 124 *sno = oik->d.issuerAndSerialNumber->serialNumber;
125 } else if (oik->type == CMS_OIK_KEYIDENTIFIER) { 125 } else if (oik->type == CMS_OIK_KEYIDENTIFIER) {
126 if (keyid) 126 if (keyid)
127 *keyid = oik->d.subjectKeyIdentifier; 127 *keyid = oik->d.subjectKeyIdentifier;
128 } else if (oik->type == CMS_OIK_PUBKEY) { 128 } else if (oik->type == CMS_OIK_PUBKEY) {
129 if (pubalg) 129 if (pubalg)
130 *pubalg = oik->d.originatorKey->algorithm; 130 *pubalg = oik->d.originatorKey->algorithm;
131 if (pubkey) 131 if (pubkey)
132 *pubkey = oik->d.originatorKey->publicKey; 132 *pubkey = oik->d.originatorKey->publicKey;
133 } else 133 } else
134 return 0; 134 return 0;
135 return 1; 135 return 1;
136} 136}
137 137
138int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo *ri, X509 *cert) 138int CMS_RecipientInfo_kari_orig_id_cmp(CMS_RecipientInfo *ri, X509 *cert)
139{ 139{
140 CMS_OriginatorIdentifierOrKey *oik; 140 CMS_OriginatorIdentifierOrKey *oik;
141 if (ri->type != CMS_RECIPINFO_AGREE) { 141 if (ri->type != CMS_RECIPINFO_AGREE) {
142 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ORIG_ID_CMP, 142 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ORIG_ID_CMP,
143 CMS_R_NOT_KEY_AGREEMENT); 143 CMS_R_NOT_KEY_AGREEMENT);
144 return -2; 144 return -2;
145 } 145 }
146 oik = ri->d.kari->originator; 146 oik = ri->d.kari->originator;
147 if (oik->type == CMS_OIK_ISSUER_SERIAL) 147 if (oik->type == CMS_OIK_ISSUER_SERIAL)
148 return cms_ias_cert_cmp(oik->d.issuerAndSerialNumber, cert); 148 return cms_ias_cert_cmp(oik->d.issuerAndSerialNumber, cert);
149 else if (oik->type == CMS_OIK_KEYIDENTIFIER) 149 else if (oik->type == CMS_OIK_KEYIDENTIFIER)
150 return cms_keyid_cert_cmp(oik->d.subjectKeyIdentifier, cert); 150 return cms_keyid_cert_cmp(oik->d.subjectKeyIdentifier, cert);
151 return -1; 151 return -1;
152} 152}
153 153
154int CMS_RecipientEncryptedKey_get0_id(CMS_RecipientEncryptedKey *rek, 154int CMS_RecipientEncryptedKey_get0_id(CMS_RecipientEncryptedKey *rek,
155 ASN1_OCTET_STRING **keyid, 155 ASN1_OCTET_STRING **keyid,
156 ASN1_GENERALIZEDTIME **tm, 156 ASN1_GENERALIZEDTIME **tm,
157 CMS_OtherKeyAttribute **other, 157 CMS_OtherKeyAttribute **other,
158 X509_NAME **issuer, ASN1_INTEGER **sno) 158 X509_NAME **issuer, ASN1_INTEGER **sno)
159{ 159{
160 CMS_KeyAgreeRecipientIdentifier *rid = rek->rid; 160 CMS_KeyAgreeRecipientIdentifier *rid = rek->rid;
161 if (rid->type == CMS_REK_ISSUER_SERIAL) { 161 if (rid->type == CMS_REK_ISSUER_SERIAL) {
162 if (issuer) 162 if (issuer)
163 *issuer = rid->d.issuerAndSerialNumber->issuer; 163 *issuer = rid->d.issuerAndSerialNumber->issuer;
164 if (sno) 164 if (sno)
165 *sno = rid->d.issuerAndSerialNumber->serialNumber; 165 *sno = rid->d.issuerAndSerialNumber->serialNumber;
166 if (keyid) 166 if (keyid)
167 *keyid = NULL; 167 *keyid = NULL;
168 if (tm) 168 if (tm)
169 *tm = NULL; 169 *tm = NULL;
170 if (other) 170 if (other)
171 *other = NULL; 171 *other = NULL;
172 } else if (rid->type == CMS_REK_KEYIDENTIFIER) { 172 } else if (rid->type == CMS_REK_KEYIDENTIFIER) {
173 if (keyid) 173 if (keyid)
174 *keyid = rid->d.rKeyId->subjectKeyIdentifier; 174 *keyid = rid->d.rKeyId->subjectKeyIdentifier;
175 if (tm) 175 if (tm)
176 *tm = rid->d.rKeyId->date; 176 *tm = rid->d.rKeyId->date;
177 if (other) 177 if (other)
178 *other = rid->d.rKeyId->other; 178 *other = rid->d.rKeyId->other;
179 if (issuer) 179 if (issuer)
180 *issuer = NULL; 180 *issuer = NULL;
181 if (sno) 181 if (sno)
182 *sno = NULL; 182 *sno = NULL;
183 } else 183 } else
184 return 0; 184 return 0;
185 return 1; 185 return 1;
186} 186}
187 187
188int CMS_RecipientEncryptedKey_cert_cmp(CMS_RecipientEncryptedKey *rek, 188int CMS_RecipientEncryptedKey_cert_cmp(CMS_RecipientEncryptedKey *rek,
189 X509 *cert) 189 X509 *cert)
190{ 190{
191 CMS_KeyAgreeRecipientIdentifier *rid = rek->rid; 191 CMS_KeyAgreeRecipientIdentifier *rid = rek->rid;
192 if (rid->type == CMS_REK_ISSUER_SERIAL) 192 if (rid->type == CMS_REK_ISSUER_SERIAL)
193 return cms_ias_cert_cmp(rid->d.issuerAndSerialNumber, cert); 193 return cms_ias_cert_cmp(rid->d.issuerAndSerialNumber, cert);
194 else if (rid->type == CMS_REK_KEYIDENTIFIER) 194 else if (rid->type == CMS_REK_KEYIDENTIFIER)
195 return cms_keyid_cert_cmp(rid->d.rKeyId->subjectKeyIdentifier, cert); 195 return cms_keyid_cert_cmp(rid->d.rKeyId->subjectKeyIdentifier, cert);
196 else 196 else
197 return -1; 197 return -1;
198} 198}
199 199
200int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pk) 200int CMS_RecipientInfo_kari_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pk)
201{ 201{
202 EVP_PKEY_CTX *pctx; 202 EVP_PKEY_CTX *pctx;
203 CMS_KeyAgreeRecipientInfo *kari = ri->d.kari; 203 CMS_KeyAgreeRecipientInfo *kari = ri->d.kari;
204 204
205 EVP_PKEY_CTX_free(kari->pctx); 205 EVP_PKEY_CTX_free(kari->pctx);
206 kari->pctx = NULL; 206 kari->pctx = NULL;
207 if (!pk) 207 if (!pk)
208 return 1; 208 return 1;
209 pctx = EVP_PKEY_CTX_new(pk, NULL); 209 pctx = EVP_PKEY_CTX_new(pk, NULL);
210 if (!pctx || !EVP_PKEY_derive_init(pctx)) 210 if (!pctx || !EVP_PKEY_derive_init(pctx))
211 goto err; 211 goto err;
212 kari->pctx = pctx; 212 kari->pctx = pctx;
213 return 1; 213 return 1;
214 err: 214 err:
215 EVP_PKEY_CTX_free(pctx); 215 EVP_PKEY_CTX_free(pctx);
216 return 0; 216 return 0;
217} 217}
218 218
219EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri) 219EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri)
220{ 220{
221 if (ri->type == CMS_RECIPINFO_AGREE) 221 if (ri->type == CMS_RECIPINFO_AGREE)
222 return ri->d.kari->ctx; 222 return ri->d.kari->ctx;
223 return NULL; 223 return NULL;
224} 224}
225 225
226/* 226/*
@@ -229,231 +229,231 @@ EVP_CIPHER_CTX *CMS_RecipientInfo_kari_get0_ctx(CMS_RecipientInfo *ri)
229 */ 229 */
230 230
231static int cms_kek_cipher(unsigned char **pout, size_t *poutlen, 231static int cms_kek_cipher(unsigned char **pout, size_t *poutlen,
232 const unsigned char *in, size_t inlen, 232 const unsigned char *in, size_t inlen,
233 CMS_KeyAgreeRecipientInfo *kari, int enc) 233 CMS_KeyAgreeRecipientInfo *kari, int enc)
234{ 234{
235 /* Key encryption key */ 235 /* Key encryption key */
236 unsigned char kek[EVP_MAX_KEY_LENGTH]; 236 unsigned char kek[EVP_MAX_KEY_LENGTH];
237 size_t keklen; 237 size_t keklen;
238 int rv = 0; 238 int rv = 0;
239 unsigned char *out = NULL; 239 unsigned char *out = NULL;
240 int outlen; 240 int outlen;
241 keklen = EVP_CIPHER_CTX_key_length(kari->ctx); 241 keklen = EVP_CIPHER_CTX_key_length(kari->ctx);
242 if (keklen > EVP_MAX_KEY_LENGTH) 242 if (keklen > EVP_MAX_KEY_LENGTH)
243 return 0; 243 return 0;
244 /* Derive KEK */ 244 /* Derive KEK */
245 if (EVP_PKEY_derive(kari->pctx, kek, &keklen) <= 0) 245 if (EVP_PKEY_derive(kari->pctx, kek, &keklen) <= 0)
246 goto err; 246 goto err;
247 /* Set KEK in context */ 247 /* Set KEK in context */
248 if (!EVP_CipherInit_ex(kari->ctx, NULL, NULL, kek, NULL, enc)) 248 if (!EVP_CipherInit_ex(kari->ctx, NULL, NULL, kek, NULL, enc))
249 goto err; 249 goto err;
250 /* obtain output length of ciphered key */ 250 /* obtain output length of ciphered key */
251 if (!EVP_CipherUpdate(kari->ctx, NULL, &outlen, in, inlen)) 251 if (!EVP_CipherUpdate(kari->ctx, NULL, &outlen, in, inlen))
252 goto err; 252 goto err;
253 out = OPENSSL_malloc(outlen); 253 out = OPENSSL_malloc(outlen);
254 if (out == NULL) 254 if (out == NULL)
255 goto err; 255 goto err;
256 if (!EVP_CipherUpdate(kari->ctx, out, &outlen, in, inlen)) 256 if (!EVP_CipherUpdate(kari->ctx, out, &outlen, in, inlen))
257 goto err; 257 goto err;
258 *pout = out; 258 *pout = out;
259 *poutlen = (size_t)outlen; 259 *poutlen = (size_t)outlen;
260 rv = 1; 260 rv = 1;
261 261
262 err: 262 err:
263 OPENSSL_cleanse(kek, keklen); 263 OPENSSL_cleanse(kek, keklen);
264 if (!rv) 264 if (!rv)
265 OPENSSL_free(out); 265 OPENSSL_free(out);
266 EVP_CIPHER_CTX_reset(kari->ctx); 266 EVP_CIPHER_CTX_reset(kari->ctx);
267 /* FIXME: WHY IS kari->pctx freed here? /RL */ 267 /* FIXME: WHY IS kari->pctx freed here? /RL */
268 EVP_PKEY_CTX_free(kari->pctx); 268 EVP_PKEY_CTX_free(kari->pctx);
269 kari->pctx = NULL; 269 kari->pctx = NULL;
270 return rv; 270 return rv;
271} 271}
272 272
273int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo *cms, 273int CMS_RecipientInfo_kari_decrypt(CMS_ContentInfo *cms,
274 CMS_RecipientInfo *ri, 274 CMS_RecipientInfo *ri,
275 CMS_RecipientEncryptedKey *rek) 275 CMS_RecipientEncryptedKey *rek)
276{ 276{
277 int rv = 0; 277 int rv = 0;
278 unsigned char *enckey = NULL, *cek = NULL; 278 unsigned char *enckey = NULL, *cek = NULL;
279 size_t enckeylen; 279 size_t enckeylen;
280 size_t ceklen; 280 size_t ceklen;
281 CMS_EncryptedContentInfo *ec; 281 CMS_EncryptedContentInfo *ec;
282 enckeylen = rek->encryptedKey->length; 282 enckeylen = rek->encryptedKey->length;
283 enckey = rek->encryptedKey->data; 283 enckey = rek->encryptedKey->data;
284 /* Setup all parameters to derive KEK */ 284 /* Setup all parameters to derive KEK */
285 if (!cms_env_asn1_ctrl(ri, 1)) 285 if (!cms_env_asn1_ctrl(ri, 1))
286 goto err; 286 goto err;
287 /* Attempt to decrypt CEK */ 287 /* Attempt to decrypt CEK */
288 if (!cms_kek_cipher(&cek, &ceklen, enckey, enckeylen, ri->d.kari, 0)) 288 if (!cms_kek_cipher(&cek, &ceklen, enckey, enckeylen, ri->d.kari, 0))
289 goto err; 289 goto err;
290 ec = cms->d.envelopedData->encryptedContentInfo; 290 ec = cms->d.envelopedData->encryptedContentInfo;
291 OPENSSL_clear_free(ec->key, ec->keylen); 291 OPENSSL_clear_free(ec->key, ec->keylen);
292 ec->key = cek; 292 ec->key = cek;
293 ec->keylen = ceklen; 293 ec->keylen = ceklen;
294 cek = NULL; 294 cek = NULL;
295 rv = 1; 295 rv = 1;
296 err: 296 err:
297 OPENSSL_free(cek); 297 OPENSSL_free(cek);
298 return rv; 298 return rv;
299} 299}
300 300
301/* Create ephemeral key and initialise context based on it */ 301/* Create ephemeral key and initialise context based on it */
302static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo *kari, 302static int cms_kari_create_ephemeral_key(CMS_KeyAgreeRecipientInfo *kari,
303 EVP_PKEY *pk) 303 EVP_PKEY *pk)
304{ 304{
305 EVP_PKEY_CTX *pctx = NULL; 305 EVP_PKEY_CTX *pctx = NULL;
306 EVP_PKEY *ekey = NULL; 306 EVP_PKEY *ekey = NULL;
307 int rv = 0; 307 int rv = 0;
308 pctx = EVP_PKEY_CTX_new(pk, NULL); 308 pctx = EVP_PKEY_CTX_new(pk, NULL);
309 if (!pctx) 309 if (!pctx)
310 goto err; 310 goto err;
311 if (EVP_PKEY_keygen_init(pctx) <= 0) 311 if (EVP_PKEY_keygen_init(pctx) <= 0)
312 goto err; 312 goto err;
313 if (EVP_PKEY_keygen(pctx, &ekey) <= 0) 313 if (EVP_PKEY_keygen(pctx, &ekey) <= 0)
314 goto err; 314 goto err;
315 EVP_PKEY_CTX_free(pctx); 315 EVP_PKEY_CTX_free(pctx);
316 pctx = EVP_PKEY_CTX_new(ekey, NULL); 316 pctx = EVP_PKEY_CTX_new(ekey, NULL);
317 if (!pctx) 317 if (!pctx)
318 goto err; 318 goto err;
319 if (EVP_PKEY_derive_init(pctx) <= 0) 319 if (EVP_PKEY_derive_init(pctx) <= 0)
320 goto err; 320 goto err;
321 kari->pctx = pctx; 321 kari->pctx = pctx;
322 rv = 1; 322 rv = 1;
323 err: 323 err:
324 if (!rv) 324 if (!rv)
325 EVP_PKEY_CTX_free(pctx); 325 EVP_PKEY_CTX_free(pctx);
326 EVP_PKEY_free(ekey); 326 EVP_PKEY_free(ekey);
327 return rv; 327 return rv;
328} 328}
329 329
330/* Initialise a kari based on passed certificate and key */ 330/* Initialise a kari based on passed certificate and key */
331 331
332int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip, 332int cms_RecipientInfo_kari_init(CMS_RecipientInfo *ri, X509 *recip,
333 EVP_PKEY *pk, unsigned int flags) 333 EVP_PKEY *pk, unsigned int flags)
334{ 334{
335 CMS_KeyAgreeRecipientInfo *kari; 335 CMS_KeyAgreeRecipientInfo *kari;
336 CMS_RecipientEncryptedKey *rek = NULL; 336 CMS_RecipientEncryptedKey *rek = NULL;
337 337
338 ri->d.kari = M_ASN1_new_of(CMS_KeyAgreeRecipientInfo); 338 ri->d.kari = M_ASN1_new_of(CMS_KeyAgreeRecipientInfo);
339 if (!ri->d.kari) 339 if (!ri->d.kari)
340 return 0; 340 return 0;
341 ri->type = CMS_RECIPINFO_AGREE; 341 ri->type = CMS_RECIPINFO_AGREE;
342 342
343 kari = ri->d.kari; 343 kari = ri->d.kari;
344 kari->version = 3; 344 kari->version = 3;
345 345
346 rek = M_ASN1_new_of(CMS_RecipientEncryptedKey); 346 rek = M_ASN1_new_of(CMS_RecipientEncryptedKey);
347 if (rek == NULL) 347 if (rek == NULL)
348 return 0; 348 return 0;
349 349
350 if (!sk_CMS_RecipientEncryptedKey_push(kari->recipientEncryptedKeys, rek)) { 350 if (!sk_CMS_RecipientEncryptedKey_push(kari->recipientEncryptedKeys, rek)) {
351 M_ASN1_free_of(rek, CMS_RecipientEncryptedKey); 351 M_ASN1_free_of(rek, CMS_RecipientEncryptedKey);
352 return 0; 352 return 0;
353 } 353 }
354 354
355 if (flags & CMS_USE_KEYID) { 355 if (flags & CMS_USE_KEYID) {
356 rek->rid->type = CMS_REK_KEYIDENTIFIER; 356 rek->rid->type = CMS_REK_KEYIDENTIFIER;
357 rek->rid->d.rKeyId = M_ASN1_new_of(CMS_RecipientKeyIdentifier); 357 rek->rid->d.rKeyId = M_ASN1_new_of(CMS_RecipientKeyIdentifier);
358 if (rek->rid->d.rKeyId == NULL) 358 if (rek->rid->d.rKeyId == NULL)
359 return 0; 359 return 0;
360 if (!cms_set1_keyid(&rek->rid->d.rKeyId->subjectKeyIdentifier, recip)) 360 if (!cms_set1_keyid(&rek->rid->d.rKeyId->subjectKeyIdentifier, recip))
361 return 0; 361 return 0;
362 } else { 362 } else {
363 rek->rid->type = CMS_REK_ISSUER_SERIAL; 363 rek->rid->type = CMS_REK_ISSUER_SERIAL;
364 if (!cms_set1_ias(&rek->rid->d.issuerAndSerialNumber, recip)) 364 if (!cms_set1_ias(&rek->rid->d.issuerAndSerialNumber, recip))
365 return 0; 365 return 0;
366 } 366 }
367 367
368 /* Create ephemeral key */ 368 /* Create ephemeral key */
369 if (!cms_kari_create_ephemeral_key(kari, pk)) 369 if (!cms_kari_create_ephemeral_key(kari, pk))
370 return 0; 370 return 0;
371 371
372 EVP_PKEY_up_ref(pk); 372 EVP_PKEY_up_ref(pk);
373 rek->pkey = pk; 373 rek->pkey = pk;
374 return 1; 374 return 1;
375} 375}
376 376
377static int cms_wrap_init(CMS_KeyAgreeRecipientInfo *kari, 377static int cms_wrap_init(CMS_KeyAgreeRecipientInfo *kari,
378 const EVP_CIPHER *cipher) 378 const EVP_CIPHER *cipher)
379{ 379{
380 EVP_CIPHER_CTX *ctx = kari->ctx; 380 EVP_CIPHER_CTX *ctx = kari->ctx;
381 const EVP_CIPHER *kekcipher; 381 const EVP_CIPHER *kekcipher;
382 int keylen = EVP_CIPHER_key_length(cipher); 382 int keylen = EVP_CIPHER_key_length(cipher);
383 /* If a suitable wrap algorithm is already set nothing to do */ 383 /* If a suitable wrap algorithm is already set nothing to do */
384 kekcipher = EVP_CIPHER_CTX_cipher(ctx); 384 kekcipher = EVP_CIPHER_CTX_cipher(ctx);
385 385
386 if (kekcipher) { 386 if (kekcipher) {
387 if (EVP_CIPHER_CTX_mode(ctx) != EVP_CIPH_WRAP_MODE) 387 if (EVP_CIPHER_CTX_mode(ctx) != EVP_CIPH_WRAP_MODE)
388 return 0; 388 return 0;
389 return 1; 389 return 1;
390 } 390 }
391 /* 391 /*
392 * Pick a cipher based on content encryption cipher. If it is DES3 use 392 * Pick a cipher based on content encryption cipher. If it is DES3 use
393 * DES3 wrap otherwise use AES wrap similar to key size. 393 * DES3 wrap otherwise use AES wrap similar to key size.
394 */ 394 */
395#ifndef OPENSSL_NO_DES 395#ifndef OPENSSL_NO_DES
396 if (EVP_CIPHER_type(cipher) == NID_des_ede3_cbc) 396 if (EVP_CIPHER_type(cipher) == NID_des_ede3_cbc)
397 kekcipher = EVP_des_ede3_wrap(); 397 kekcipher = EVP_des_ede3_wrap();
398 else 398 else
399#endif 399#endif
400 if (keylen <= 16) 400 if (keylen <= 16)
401 kekcipher = EVP_aes_128_wrap(); 401 kekcipher = EVP_aes_128_wrap();
402 else if (keylen <= 24) 402 else if (keylen <= 24)
403 kekcipher = EVP_aes_192_wrap(); 403 kekcipher = EVP_aes_192_wrap();
404 else 404 else
405 kekcipher = EVP_aes_256_wrap(); 405 kekcipher = EVP_aes_256_wrap();
406 return EVP_EncryptInit_ex(ctx, kekcipher, NULL, NULL, NULL); 406 return EVP_EncryptInit_ex(ctx, kekcipher, NULL, NULL, NULL);
407} 407}
408 408
409/* Encrypt content key in key agreement recipient info */ 409/* Encrypt content key in key agreement recipient info */
410 410
411int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo *cms, 411int cms_RecipientInfo_kari_encrypt(CMS_ContentInfo *cms,
412 CMS_RecipientInfo *ri) 412 CMS_RecipientInfo *ri)
413{ 413{
414 CMS_KeyAgreeRecipientInfo *kari; 414 CMS_KeyAgreeRecipientInfo *kari;
415 CMS_EncryptedContentInfo *ec; 415 CMS_EncryptedContentInfo *ec;
416 CMS_RecipientEncryptedKey *rek; 416 CMS_RecipientEncryptedKey *rek;
417 STACK_OF(CMS_RecipientEncryptedKey) *reks; 417 STACK_OF(CMS_RecipientEncryptedKey) *reks;
418 int i; 418 int i;
419 419
420 if (ri->type != CMS_RECIPINFO_AGREE) { 420 if (ri->type != CMS_RECIPINFO_AGREE) {
421 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT, CMS_R_NOT_KEY_AGREEMENT); 421 CMSerr(CMS_F_CMS_RECIPIENTINFO_KARI_ENCRYPT, CMS_R_NOT_KEY_AGREEMENT);
422 return 0; 422 return 0;
423 } 423 }
424 kari = ri->d.kari; 424 kari = ri->d.kari;
425 reks = kari->recipientEncryptedKeys; 425 reks = kari->recipientEncryptedKeys;
426 ec = cms->d.envelopedData->encryptedContentInfo; 426 ec = cms->d.envelopedData->encryptedContentInfo;
427 /* Initialise wrap algorithm parameters */ 427 /* Initialise wrap algorithm parameters */
428 if (!cms_wrap_init(kari, ec->cipher)) 428 if (!cms_wrap_init(kari, ec->cipher))
429 return 0; 429 return 0;
430 /* 430 /*
431 * If no originator key set up initialise for ephemeral key the public key 431 * If no originator key set up initialise for ephemeral key the public key
432 * ASN1 structure will set the actual public key value. 432 * ASN1 structure will set the actual public key value.
433 */ 433 */
434 if (kari->originator->type == -1) { 434 if (kari->originator->type == -1) {
435 CMS_OriginatorIdentifierOrKey *oik = kari->originator; 435 CMS_OriginatorIdentifierOrKey *oik = kari->originator;
436 oik->type = CMS_OIK_PUBKEY; 436 oik->type = CMS_OIK_PUBKEY;
437 oik->d.originatorKey = M_ASN1_new_of(CMS_OriginatorPublicKey); 437 oik->d.originatorKey = M_ASN1_new_of(CMS_OriginatorPublicKey);
438 if (!oik->d.originatorKey) 438 if (!oik->d.originatorKey)
439 return 0; 439 return 0;
440 } 440 }
441 /* Initialise KDF algorithm */ 441 /* Initialise KDF algorithm */
442 if (!cms_env_asn1_ctrl(ri, 0)) 442 if (!cms_env_asn1_ctrl(ri, 0))
443 return 0; 443 return 0;
444 /* For each rek, derive KEK, encrypt CEK */ 444 /* For each rek, derive KEK, encrypt CEK */
445 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) { 445 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
446 unsigned char *enckey; 446 unsigned char *enckey;
447 size_t enckeylen; 447 size_t enckeylen;
448 rek = sk_CMS_RecipientEncryptedKey_value(reks, i); 448 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
449 if (EVP_PKEY_derive_set_peer(kari->pctx, rek->pkey) <= 0) 449 if (EVP_PKEY_derive_set_peer(kari->pctx, rek->pkey) <= 0)
450 return 0; 450 return 0;
451 if (!cms_kek_cipher(&enckey, &enckeylen, ec->key, ec->keylen, 451 if (!cms_kek_cipher(&enckey, &enckeylen, ec->key, ec->keylen,
452 kari, 1)) 452 kari, 1))
453 return 0; 453 return 0;
454 ASN1_STRING_set0(rek->encryptedKey, enckey, enckeylen); 454 ASN1_STRING_set0(rek->encryptedKey, enckey, enckeylen);
455 } 455 }
456 456
457 return 1; 457 return 1;
458 458
459} 459}
diff --git a/src/lib/libcrypto/cms/cms_lib.c b/src/lib/libcrypto/cms/cms_lib.c
index dd192f9775..baee32c398 100644
--- a/src/lib/libcrypto/cms/cms_lib.c
+++ b/src/lib/libcrypto/cms/cms_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_lib.c,v 1.8 2019/08/10 16:03:54 jsing Exp $ */ 1/* $OpenBSD: cms_lib.c,v 1.9 2019/08/10 16:42:20 jsing Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -66,131 +66,131 @@ IMPLEMENT_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
66 66
67const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms) 67const ASN1_OBJECT *CMS_get0_type(const CMS_ContentInfo *cms)
68{ 68{
69 return cms->contentType; 69 return cms->contentType;
70} 70}
71 71
72CMS_ContentInfo *cms_Data_create(void) 72CMS_ContentInfo *cms_Data_create(void)
73{ 73{
74 CMS_ContentInfo *cms; 74 CMS_ContentInfo *cms;
75 cms = CMS_ContentInfo_new(); 75 cms = CMS_ContentInfo_new();
76 if (cms != NULL) { 76 if (cms != NULL) {
77 cms->contentType = OBJ_nid2obj(NID_pkcs7_data); 77 cms->contentType = OBJ_nid2obj(NID_pkcs7_data);
78 /* Never detached */ 78 /* Never detached */
79 CMS_set_detached(cms, 0); 79 CMS_set_detached(cms, 0);
80 } 80 }
81 return cms; 81 return cms;
82} 82}
83 83
84BIO *cms_content_bio(CMS_ContentInfo *cms) 84BIO *cms_content_bio(CMS_ContentInfo *cms)
85{ 85{
86 ASN1_OCTET_STRING **pos = CMS_get0_content(cms); 86 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
87 if (!pos) 87 if (!pos)
88 return NULL; 88 return NULL;
89 /* If content detached data goes nowhere: create NULL BIO */ 89 /* If content detached data goes nowhere: create NULL BIO */
90 if (!*pos) 90 if (!*pos)
91 return BIO_new(BIO_s_null()); 91 return BIO_new(BIO_s_null());
92 /* 92 /*
93 * If content not detached and created return memory BIO 93 * If content not detached and created return memory BIO
94 */ 94 */
95 if (!*pos || ((*pos)->flags == ASN1_STRING_FLAG_CONT)) 95 if (!*pos || ((*pos)->flags == ASN1_STRING_FLAG_CONT))
96 return BIO_new(BIO_s_mem()); 96 return BIO_new(BIO_s_mem());
97 /* Else content was read in: return read only BIO for it */ 97 /* Else content was read in: return read only BIO for it */
98 return BIO_new_mem_buf((*pos)->data, (*pos)->length); 98 return BIO_new_mem_buf((*pos)->data, (*pos)->length);
99} 99}
100 100
101BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont) 101BIO *CMS_dataInit(CMS_ContentInfo *cms, BIO *icont)
102{ 102{
103 BIO *cmsbio, *cont; 103 BIO *cmsbio, *cont;
104 if (icont) 104 if (icont)
105 cont = icont; 105 cont = icont;
106 else 106 else
107 cont = cms_content_bio(cms); 107 cont = cms_content_bio(cms);
108 if (!cont) { 108 if (!cont) {
109 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT); 109 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_NO_CONTENT);
110 return NULL; 110 return NULL;
111 } 111 }
112 switch (OBJ_obj2nid(cms->contentType)) { 112 switch (OBJ_obj2nid(cms->contentType)) {
113 113
114 case NID_pkcs7_data: 114 case NID_pkcs7_data:
115 return cont; 115 return cont;
116 116
117 case NID_pkcs7_signed: 117 case NID_pkcs7_signed:
118 cmsbio = cms_SignedData_init_bio(cms); 118 cmsbio = cms_SignedData_init_bio(cms);
119 break; 119 break;
120 120
121 case NID_pkcs7_digest: 121 case NID_pkcs7_digest:
122 cmsbio = cms_DigestedData_init_bio(cms); 122 cmsbio = cms_DigestedData_init_bio(cms);
123 break; 123 break;
124#ifdef ZLIB 124#ifdef ZLIB
125 case NID_id_smime_ct_compressedData: 125 case NID_id_smime_ct_compressedData:
126 cmsbio = cms_CompressedData_init_bio(cms); 126 cmsbio = cms_CompressedData_init_bio(cms);
127 break; 127 break;
128#endif 128#endif
129 129
130 case NID_pkcs7_encrypted: 130 case NID_pkcs7_encrypted:
131 cmsbio = cms_EncryptedData_init_bio(cms); 131 cmsbio = cms_EncryptedData_init_bio(cms);
132 break; 132 break;
133 133
134 case NID_pkcs7_enveloped: 134 case NID_pkcs7_enveloped:
135 cmsbio = cms_EnvelopedData_init_bio(cms); 135 cmsbio = cms_EnvelopedData_init_bio(cms);
136 break; 136 break;
137 137
138 default: 138 default:
139 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE); 139 CMSerr(CMS_F_CMS_DATAINIT, CMS_R_UNSUPPORTED_TYPE);
140 return NULL; 140 return NULL;
141 } 141 }
142 142
143 if (cmsbio) 143 if (cmsbio)
144 return BIO_push(cmsbio, cont); 144 return BIO_push(cmsbio, cont);
145 145
146 if (!icont) 146 if (!icont)
147 BIO_free(cont); 147 BIO_free(cont);
148 return NULL; 148 return NULL;
149 149
150} 150}
151 151
152int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio) 152int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
153{ 153{
154 ASN1_OCTET_STRING **pos = CMS_get0_content(cms); 154 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
155 if (!pos) 155 if (!pos)
156 return 0; 156 return 0;
157 /* If embedded content find memory BIO and set content */ 157 /* If embedded content find memory BIO and set content */
158 if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) { 158 if (*pos && ((*pos)->flags & ASN1_STRING_FLAG_CONT)) {
159 BIO *mbio; 159 BIO *mbio;
160 unsigned char *cont; 160 unsigned char *cont;
161 long contlen; 161 long contlen;
162 mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM); 162 mbio = BIO_find_type(cmsbio, BIO_TYPE_MEM);
163 if (!mbio) { 163 if (!mbio) {
164 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND); 164 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_CONTENT_NOT_FOUND);
165 return 0; 165 return 0;
166 } 166 }
167 contlen = BIO_get_mem_data(mbio, &cont); 167 contlen = BIO_get_mem_data(mbio, &cont);
168 /* Set bio as read only so its content can't be clobbered */ 168 /* Set bio as read only so its content can't be clobbered */
169 BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY); 169 BIO_set_flags(mbio, BIO_FLAGS_MEM_RDONLY);
170 BIO_set_mem_eof_return(mbio, 0); 170 BIO_set_mem_eof_return(mbio, 0);
171 ASN1_STRING_set0(*pos, cont, contlen); 171 ASN1_STRING_set0(*pos, cont, contlen);
172 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT; 172 (*pos)->flags &= ~ASN1_STRING_FLAG_CONT;
173 } 173 }
174 174
175 switch (OBJ_obj2nid(cms->contentType)) { 175 switch (OBJ_obj2nid(cms->contentType)) {
176 176
177 case NID_pkcs7_data: 177 case NID_pkcs7_data:
178 case NID_pkcs7_enveloped: 178 case NID_pkcs7_enveloped:
179 case NID_pkcs7_encrypted: 179 case NID_pkcs7_encrypted:
180 case NID_id_smime_ct_compressedData: 180 case NID_id_smime_ct_compressedData:
181 /* Nothing to do */ 181 /* Nothing to do */
182 return 1; 182 return 1;
183 183
184 case NID_pkcs7_signed: 184 case NID_pkcs7_signed:
185 return cms_SignedData_final(cms, cmsbio); 185 return cms_SignedData_final(cms, cmsbio);
186 186
187 case NID_pkcs7_digest: 187 case NID_pkcs7_digest:
188 return cms_DigestedData_do_final(cms, cmsbio, 0); 188 return cms_DigestedData_do_final(cms, cmsbio, 0);
189 189
190 default: 190 default:
191 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE); 191 CMSerr(CMS_F_CMS_DATAFINAL, CMS_R_UNSUPPORTED_TYPE);
192 return 0; 192 return 0;
193 } 193 }
194} 194}
195 195
196/* 196/*
@@ -200,36 +200,36 @@ int CMS_dataFinal(CMS_ContentInfo *cms, BIO *cmsbio)
200 200
201ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms) 201ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
202{ 202{
203 switch (OBJ_obj2nid(cms->contentType)) { 203 switch (OBJ_obj2nid(cms->contentType)) {
204 204
205 case NID_pkcs7_data: 205 case NID_pkcs7_data:
206 return &cms->d.data; 206 return &cms->d.data;
207 207
208 case NID_pkcs7_signed: 208 case NID_pkcs7_signed:
209 return &cms->d.signedData->encapContentInfo->eContent; 209 return &cms->d.signedData->encapContentInfo->eContent;
210 210
211 case NID_pkcs7_enveloped: 211 case NID_pkcs7_enveloped:
212 return &cms->d.envelopedData->encryptedContentInfo->encryptedContent; 212 return &cms->d.envelopedData->encryptedContentInfo->encryptedContent;
213 213
214 case NID_pkcs7_digest: 214 case NID_pkcs7_digest:
215 return &cms->d.digestedData->encapContentInfo->eContent; 215 return &cms->d.digestedData->encapContentInfo->eContent;
216 216
217 case NID_pkcs7_encrypted: 217 case NID_pkcs7_encrypted:
218 return &cms->d.encryptedData->encryptedContentInfo->encryptedContent; 218 return &cms->d.encryptedData->encryptedContentInfo->encryptedContent;
219 219
220 case NID_id_smime_ct_authData: 220 case NID_id_smime_ct_authData:
221 return &cms->d.authenticatedData->encapContentInfo->eContent; 221 return &cms->d.authenticatedData->encapContentInfo->eContent;
222 222
223 case NID_id_smime_ct_compressedData: 223 case NID_id_smime_ct_compressedData:
224 return &cms->d.compressedData->encapContentInfo->eContent; 224 return &cms->d.compressedData->encapContentInfo->eContent;
225 225
226 default: 226 default:
227 if (cms->d.other->type == V_ASN1_OCTET_STRING) 227 if (cms->d.other->type == V_ASN1_OCTET_STRING)
228 return &cms->d.other->value.octet_string; 228 return &cms->d.other->value.octet_string;
229 CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE); 229 CMSerr(CMS_F_CMS_GET0_CONTENT, CMS_R_UNSUPPORTED_CONTENT_TYPE);
230 return NULL; 230 return NULL;
231 231
232 } 232 }
233} 233}
234 234
235/* 235/*
@@ -239,394 +239,394 @@ ASN1_OCTET_STRING **CMS_get0_content(CMS_ContentInfo *cms)
239 239
240static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms) 240static ASN1_OBJECT **cms_get0_econtent_type(CMS_ContentInfo *cms)
241{ 241{
242 switch (OBJ_obj2nid(cms->contentType)) { 242 switch (OBJ_obj2nid(cms->contentType)) {
243 243
244 case NID_pkcs7_signed: 244 case NID_pkcs7_signed:
245 return &cms->d.signedData->encapContentInfo->eContentType; 245 return &cms->d.signedData->encapContentInfo->eContentType;
246 246
247 case NID_pkcs7_enveloped: 247 case NID_pkcs7_enveloped:
248 return &cms->d.envelopedData->encryptedContentInfo->contentType; 248 return &cms->d.envelopedData->encryptedContentInfo->contentType;
249 249
250 case NID_pkcs7_digest: 250 case NID_pkcs7_digest:
251 return &cms->d.digestedData->encapContentInfo->eContentType; 251 return &cms->d.digestedData->encapContentInfo->eContentType;
252 252
253 case NID_pkcs7_encrypted: 253 case NID_pkcs7_encrypted:
254 return &cms->d.encryptedData->encryptedContentInfo->contentType; 254 return &cms->d.encryptedData->encryptedContentInfo->contentType;
255 255
256 case NID_id_smime_ct_authData: 256 case NID_id_smime_ct_authData:
257 return &cms->d.authenticatedData->encapContentInfo->eContentType; 257 return &cms->d.authenticatedData->encapContentInfo->eContentType;
258 258
259 case NID_id_smime_ct_compressedData: 259 case NID_id_smime_ct_compressedData:
260 return &cms->d.compressedData->encapContentInfo->eContentType; 260 return &cms->d.compressedData->encapContentInfo->eContentType;
261 261
262 default: 262 default:
263 CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE, CMS_R_UNSUPPORTED_CONTENT_TYPE); 263 CMSerr(CMS_F_CMS_GET0_ECONTENT_TYPE, CMS_R_UNSUPPORTED_CONTENT_TYPE);
264 return NULL; 264 return NULL;
265 265
266 } 266 }
267} 267}
268 268
269const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms) 269const ASN1_OBJECT *CMS_get0_eContentType(CMS_ContentInfo *cms)
270{ 270{
271 ASN1_OBJECT **petype; 271 ASN1_OBJECT **petype;
272 petype = cms_get0_econtent_type(cms); 272 petype = cms_get0_econtent_type(cms);
273 if (petype) 273 if (petype)
274 return *petype; 274 return *petype;
275 return NULL; 275 return NULL;
276} 276}
277 277
278int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid) 278int CMS_set1_eContentType(CMS_ContentInfo *cms, const ASN1_OBJECT *oid)
279{ 279{
280 ASN1_OBJECT **petype, *etype; 280 ASN1_OBJECT **petype, *etype;
281 petype = cms_get0_econtent_type(cms); 281 petype = cms_get0_econtent_type(cms);
282 if (!petype) 282 if (!petype)
283 return 0; 283 return 0;
284 if (!oid) 284 if (!oid)
285 return 1; 285 return 1;
286 etype = OBJ_dup(oid); 286 etype = OBJ_dup(oid);
287 if (!etype) 287 if (!etype)
288 return 0; 288 return 0;
289 ASN1_OBJECT_free(*petype); 289 ASN1_OBJECT_free(*petype);
290 *petype = etype; 290 *petype = etype;
291 return 1; 291 return 1;
292} 292}
293 293
294int CMS_is_detached(CMS_ContentInfo *cms) 294int CMS_is_detached(CMS_ContentInfo *cms)
295{ 295{
296 ASN1_OCTET_STRING **pos; 296 ASN1_OCTET_STRING **pos;
297 pos = CMS_get0_content(cms); 297 pos = CMS_get0_content(cms);
298 if (!pos) 298 if (!pos)
299 return -1; 299 return -1;
300 if (*pos) 300 if (*pos)
301 return 0; 301 return 0;
302 return 1; 302 return 1;
303} 303}
304 304
305int CMS_set_detached(CMS_ContentInfo *cms, int detached) 305int CMS_set_detached(CMS_ContentInfo *cms, int detached)
306{ 306{
307 ASN1_OCTET_STRING **pos; 307 ASN1_OCTET_STRING **pos;
308 pos = CMS_get0_content(cms); 308 pos = CMS_get0_content(cms);
309 if (!pos) 309 if (!pos)
310 return 0; 310 return 0;
311 if (detached) { 311 if (detached) {
312 ASN1_OCTET_STRING_free(*pos); 312 ASN1_OCTET_STRING_free(*pos);
313 *pos = NULL; 313 *pos = NULL;
314 return 1; 314 return 1;
315 } 315 }
316 if (*pos == NULL) 316 if (*pos == NULL)
317 *pos = ASN1_OCTET_STRING_new(); 317 *pos = ASN1_OCTET_STRING_new();
318 if (*pos != NULL) { 318 if (*pos != NULL) {
319 /* 319 /*
320 * NB: special flag to show content is created and not read in. 320 * NB: special flag to show content is created and not read in.
321 */ 321 */
322 (*pos)->flags |= ASN1_STRING_FLAG_CONT; 322 (*pos)->flags |= ASN1_STRING_FLAG_CONT;
323 return 1; 323 return 1;
324 } 324 }
325 CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE); 325 CMSerr(CMS_F_CMS_SET_DETACHED, ERR_R_MALLOC_FAILURE);
326 return 0; 326 return 0;
327} 327}
328 328
329/* Create a digest BIO from an X509_ALGOR structure */ 329/* Create a digest BIO from an X509_ALGOR structure */
330 330
331BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm) 331BIO *cms_DigestAlgorithm_init_bio(X509_ALGOR *digestAlgorithm)
332{ 332{
333 BIO *mdbio = NULL; 333 BIO *mdbio = NULL;
334 const ASN1_OBJECT *digestoid; 334 const ASN1_OBJECT *digestoid;
335 const EVP_MD *digest; 335 const EVP_MD *digest;
336 X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm); 336 X509_ALGOR_get0(&digestoid, NULL, NULL, digestAlgorithm);
337 digest = EVP_get_digestbyobj(digestoid); 337 digest = EVP_get_digestbyobj(digestoid);
338 if (!digest) { 338 if (!digest) {
339 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, 339 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO,
340 CMS_R_UNKNOWN_DIGEST_ALGORITHM); 340 CMS_R_UNKNOWN_DIGEST_ALGORITHM);
341 goto err; 341 goto err;
342 } 342 }
343 mdbio = BIO_new(BIO_f_md()); 343 mdbio = BIO_new(BIO_f_md());
344 if (mdbio == NULL || !BIO_set_md(mdbio, digest)) { 344 if (mdbio == NULL || !BIO_set_md(mdbio, digest)) {
345 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, CMS_R_MD_BIO_INIT_ERROR); 345 CMSerr(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO, CMS_R_MD_BIO_INIT_ERROR);
346 goto err; 346 goto err;
347 } 347 }
348 return mdbio; 348 return mdbio;
349 err: 349 err:
350 BIO_free(mdbio); 350 BIO_free(mdbio);
351 return NULL; 351 return NULL;
352} 352}
353 353
354/* Locate a message digest content from a BIO chain based on SignerInfo */ 354/* Locate a message digest content from a BIO chain based on SignerInfo */
355 355
356int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain, 356int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
357 X509_ALGOR *mdalg) 357 X509_ALGOR *mdalg)
358{ 358{
359 int nid; 359 int nid;
360 const ASN1_OBJECT *mdoid; 360 const ASN1_OBJECT *mdoid;
361 X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg); 361 X509_ALGOR_get0(&mdoid, NULL, NULL, mdalg);
362 nid = OBJ_obj2nid(mdoid); 362 nid = OBJ_obj2nid(mdoid);
363 /* Look for digest type to match signature */ 363 /* Look for digest type to match signature */
364 for (;;) { 364 for (;;) {
365 EVP_MD_CTX *mtmp; 365 EVP_MD_CTX *mtmp;
366 chain = BIO_find_type(chain, BIO_TYPE_MD); 366 chain = BIO_find_type(chain, BIO_TYPE_MD);
367 if (chain == NULL) { 367 if (chain == NULL) {
368 CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX, 368 CMSerr(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX,
369 CMS_R_NO_MATCHING_DIGEST); 369 CMS_R_NO_MATCHING_DIGEST);
370 return 0; 370 return 0;
371 } 371 }
372 BIO_get_md_ctx(chain, &mtmp); 372 BIO_get_md_ctx(chain, &mtmp);
373 if (EVP_MD_CTX_type(mtmp) == nid 373 if (EVP_MD_CTX_type(mtmp) == nid
374 /* 374 /*
375 * Workaround for broken implementations that use signature 375 * Workaround for broken implementations that use signature
376 * algorithm OID instead of digest. 376 * algorithm OID instead of digest.
377 */ 377 */
378 || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid) 378 || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
379 return EVP_MD_CTX_copy_ex(mctx, mtmp); 379 return EVP_MD_CTX_copy_ex(mctx, mtmp);
380 chain = BIO_next(chain); 380 chain = BIO_next(chain);
381 } 381 }
382} 382}
383 383
384static STACK_OF(CMS_CertificateChoices) 384static STACK_OF(CMS_CertificateChoices)
385**cms_get0_certificate_choices(CMS_ContentInfo *cms) 385**cms_get0_certificate_choices(CMS_ContentInfo *cms)
386{ 386{
387 switch (OBJ_obj2nid(cms->contentType)) { 387 switch (OBJ_obj2nid(cms->contentType)) {
388 388
389 case NID_pkcs7_signed: 389 case NID_pkcs7_signed:
390 return &cms->d.signedData->certificates; 390 return &cms->d.signedData->certificates;
391 391
392 case NID_pkcs7_enveloped: 392 case NID_pkcs7_enveloped:
393 if (cms->d.envelopedData->originatorInfo == NULL) 393 if (cms->d.envelopedData->originatorInfo == NULL)
394 return NULL; 394 return NULL;
395 return &cms->d.envelopedData->originatorInfo->certificates; 395 return &cms->d.envelopedData->originatorInfo->certificates;
396 396
397 default: 397 default:
398 CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES, 398 CMSerr(CMS_F_CMS_GET0_CERTIFICATE_CHOICES,
399 CMS_R_UNSUPPORTED_CONTENT_TYPE); 399 CMS_R_UNSUPPORTED_CONTENT_TYPE);
400 return NULL; 400 return NULL;
401 401
402 } 402 }
403} 403}
404 404
405CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms) 405CMS_CertificateChoices *CMS_add0_CertificateChoices(CMS_ContentInfo *cms)
406{ 406{
407 STACK_OF(CMS_CertificateChoices) **pcerts; 407 STACK_OF(CMS_CertificateChoices) **pcerts;
408 CMS_CertificateChoices *cch; 408 CMS_CertificateChoices *cch;
409 pcerts = cms_get0_certificate_choices(cms); 409 pcerts = cms_get0_certificate_choices(cms);
410 if (!pcerts) 410 if (!pcerts)
411 return NULL; 411 return NULL;
412 if (!*pcerts) 412 if (!*pcerts)
413 *pcerts = sk_CMS_CertificateChoices_new_null(); 413 *pcerts = sk_CMS_CertificateChoices_new_null();
414 if (!*pcerts) 414 if (!*pcerts)
415 return NULL; 415 return NULL;
416 cch = M_ASN1_new_of(CMS_CertificateChoices); 416 cch = M_ASN1_new_of(CMS_CertificateChoices);
417 if (!cch) 417 if (!cch)
418 return NULL; 418 return NULL;
419 if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) { 419 if (!sk_CMS_CertificateChoices_push(*pcerts, cch)) {
420 M_ASN1_free_of(cch, CMS_CertificateChoices); 420 M_ASN1_free_of(cch, CMS_CertificateChoices);
421 return NULL; 421 return NULL;
422 } 422 }
423 return cch; 423 return cch;
424} 424}
425 425
426int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert) 426int CMS_add0_cert(CMS_ContentInfo *cms, X509 *cert)
427{ 427{
428 CMS_CertificateChoices *cch; 428 CMS_CertificateChoices *cch;
429 STACK_OF(CMS_CertificateChoices) **pcerts; 429 STACK_OF(CMS_CertificateChoices) **pcerts;
430 int i; 430 int i;
431 pcerts = cms_get0_certificate_choices(cms); 431 pcerts = cms_get0_certificate_choices(cms);
432 if (!pcerts) 432 if (!pcerts)
433 return 0; 433 return 0;
434 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) { 434 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
435 cch = sk_CMS_CertificateChoices_value(*pcerts, i); 435 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
436 if (cch->type == CMS_CERTCHOICE_CERT) { 436 if (cch->type == CMS_CERTCHOICE_CERT) {
437 if (!X509_cmp(cch->d.certificate, cert)) { 437 if (!X509_cmp(cch->d.certificate, cert)) {
438 CMSerr(CMS_F_CMS_ADD0_CERT, 438 CMSerr(CMS_F_CMS_ADD0_CERT,
439 CMS_R_CERTIFICATE_ALREADY_PRESENT); 439 CMS_R_CERTIFICATE_ALREADY_PRESENT);
440 return 0; 440 return 0;
441 } 441 }
442 } 442 }
443 } 443 }
444 cch = CMS_add0_CertificateChoices(cms); 444 cch = CMS_add0_CertificateChoices(cms);
445 if (!cch) 445 if (!cch)
446 return 0; 446 return 0;
447 cch->type = CMS_CERTCHOICE_CERT; 447 cch->type = CMS_CERTCHOICE_CERT;
448 cch->d.certificate = cert; 448 cch->d.certificate = cert;
449 return 1; 449 return 1;
450} 450}
451 451
452int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert) 452int CMS_add1_cert(CMS_ContentInfo *cms, X509 *cert)
453{ 453{
454 int r; 454 int r;
455 r = CMS_add0_cert(cms, cert); 455 r = CMS_add0_cert(cms, cert);
456 if (r > 0) 456 if (r > 0)
457 X509_up_ref(cert); 457 X509_up_ref(cert);
458 return r; 458 return r;
459} 459}
460 460
461static STACK_OF(CMS_RevocationInfoChoice) 461static STACK_OF(CMS_RevocationInfoChoice)
462**cms_get0_revocation_choices(CMS_ContentInfo *cms) 462**cms_get0_revocation_choices(CMS_ContentInfo *cms)
463{ 463{
464 switch (OBJ_obj2nid(cms->contentType)) { 464 switch (OBJ_obj2nid(cms->contentType)) {
465 465
466 case NID_pkcs7_signed: 466 case NID_pkcs7_signed:
467 return &cms->d.signedData->crls; 467 return &cms->d.signedData->crls;
468 468
469 case NID_pkcs7_enveloped: 469 case NID_pkcs7_enveloped:
470 if (cms->d.envelopedData->originatorInfo == NULL) 470 if (cms->d.envelopedData->originatorInfo == NULL)
471 return NULL; 471 return NULL;
472 return &cms->d.envelopedData->originatorInfo->crls; 472 return &cms->d.envelopedData->originatorInfo->crls;
473 473
474 default: 474 default:
475 CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES, 475 CMSerr(CMS_F_CMS_GET0_REVOCATION_CHOICES,
476 CMS_R_UNSUPPORTED_CONTENT_TYPE); 476 CMS_R_UNSUPPORTED_CONTENT_TYPE);
477 return NULL; 477 return NULL;
478 478
479 } 479 }
480} 480}
481 481
482CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms) 482CMS_RevocationInfoChoice *CMS_add0_RevocationInfoChoice(CMS_ContentInfo *cms)
483{ 483{
484 STACK_OF(CMS_RevocationInfoChoice) **pcrls; 484 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
485 CMS_RevocationInfoChoice *rch; 485 CMS_RevocationInfoChoice *rch;
486 pcrls = cms_get0_revocation_choices(cms); 486 pcrls = cms_get0_revocation_choices(cms);
487 if (!pcrls) 487 if (!pcrls)
488 return NULL; 488 return NULL;
489 if (!*pcrls) 489 if (!*pcrls)
490 *pcrls = sk_CMS_RevocationInfoChoice_new_null(); 490 *pcrls = sk_CMS_RevocationInfoChoice_new_null();
491 if (!*pcrls) 491 if (!*pcrls)
492 return NULL; 492 return NULL;
493 rch = M_ASN1_new_of(CMS_RevocationInfoChoice); 493 rch = M_ASN1_new_of(CMS_RevocationInfoChoice);
494 if (!rch) 494 if (!rch)
495 return NULL; 495 return NULL;
496 if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) { 496 if (!sk_CMS_RevocationInfoChoice_push(*pcrls, rch)) {
497 M_ASN1_free_of(rch, CMS_RevocationInfoChoice); 497 M_ASN1_free_of(rch, CMS_RevocationInfoChoice);
498 return NULL; 498 return NULL;
499 } 499 }
500 return rch; 500 return rch;
501} 501}
502 502
503int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl) 503int CMS_add0_crl(CMS_ContentInfo *cms, X509_CRL *crl)
504{ 504{
505 CMS_RevocationInfoChoice *rch; 505 CMS_RevocationInfoChoice *rch;
506 rch = CMS_add0_RevocationInfoChoice(cms); 506 rch = CMS_add0_RevocationInfoChoice(cms);
507 if (!rch) 507 if (!rch)
508 return 0; 508 return 0;
509 rch->type = CMS_REVCHOICE_CRL; 509 rch->type = CMS_REVCHOICE_CRL;
510 rch->d.crl = crl; 510 rch->d.crl = crl;
511 return 1; 511 return 1;
512} 512}
513 513
514int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl) 514int CMS_add1_crl(CMS_ContentInfo *cms, X509_CRL *crl)
515{ 515{
516 int r; 516 int r;
517 r = CMS_add0_crl(cms, crl); 517 r = CMS_add0_crl(cms, crl);
518 if (r > 0) 518 if (r > 0)
519 X509_CRL_up_ref(crl); 519 X509_CRL_up_ref(crl);
520 return r; 520 return r;
521} 521}
522 522
523STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms) 523STACK_OF(X509) *CMS_get1_certs(CMS_ContentInfo *cms)
524{ 524{
525 STACK_OF(X509) *certs = NULL; 525 STACK_OF(X509) *certs = NULL;
526 CMS_CertificateChoices *cch; 526 CMS_CertificateChoices *cch;
527 STACK_OF(CMS_CertificateChoices) **pcerts; 527 STACK_OF(CMS_CertificateChoices) **pcerts;
528 int i; 528 int i;
529 pcerts = cms_get0_certificate_choices(cms); 529 pcerts = cms_get0_certificate_choices(cms);
530 if (!pcerts) 530 if (!pcerts)
531 return NULL; 531 return NULL;
532 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) { 532 for (i = 0; i < sk_CMS_CertificateChoices_num(*pcerts); i++) {
533 cch = sk_CMS_CertificateChoices_value(*pcerts, i); 533 cch = sk_CMS_CertificateChoices_value(*pcerts, i);
534 if (cch->type == 0) { 534 if (cch->type == 0) {
535 if (!certs) { 535 if (!certs) {
536 certs = sk_X509_new_null(); 536 certs = sk_X509_new_null();
537 if (!certs) 537 if (!certs)
538 return NULL; 538 return NULL;
539 } 539 }
540 if (!sk_X509_push(certs, cch->d.certificate)) { 540 if (!sk_X509_push(certs, cch->d.certificate)) {
541 sk_X509_pop_free(certs, X509_free); 541 sk_X509_pop_free(certs, X509_free);
542 return NULL; 542 return NULL;
543 } 543 }
544 X509_up_ref(cch->d.certificate); 544 X509_up_ref(cch->d.certificate);
545 } 545 }
546 } 546 }
547 return certs; 547 return certs;
548 548
549} 549}
550 550
551STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms) 551STACK_OF(X509_CRL) *CMS_get1_crls(CMS_ContentInfo *cms)
552{ 552{
553 STACK_OF(X509_CRL) *crls = NULL; 553 STACK_OF(X509_CRL) *crls = NULL;
554 STACK_OF(CMS_RevocationInfoChoice) **pcrls; 554 STACK_OF(CMS_RevocationInfoChoice) **pcrls;
555 CMS_RevocationInfoChoice *rch; 555 CMS_RevocationInfoChoice *rch;
556 int i; 556 int i;
557 pcrls = cms_get0_revocation_choices(cms); 557 pcrls = cms_get0_revocation_choices(cms);
558 if (!pcrls) 558 if (!pcrls)
559 return NULL; 559 return NULL;
560 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) { 560 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(*pcrls); i++) {
561 rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i); 561 rch = sk_CMS_RevocationInfoChoice_value(*pcrls, i);
562 if (rch->type == 0) { 562 if (rch->type == 0) {
563 if (!crls) { 563 if (!crls) {
564 crls = sk_X509_CRL_new_null(); 564 crls = sk_X509_CRL_new_null();
565 if (!crls) 565 if (!crls)
566 return NULL; 566 return NULL;
567 } 567 }
568 if (!sk_X509_CRL_push(crls, rch->d.crl)) { 568 if (!sk_X509_CRL_push(crls, rch->d.crl)) {
569 sk_X509_CRL_pop_free(crls, X509_CRL_free); 569 sk_X509_CRL_pop_free(crls, X509_CRL_free);
570 return NULL; 570 return NULL;
571 } 571 }
572 X509_CRL_up_ref(rch->d.crl); 572 X509_CRL_up_ref(rch->d.crl);
573 } 573 }
574 } 574 }
575 return crls; 575 return crls;
576} 576}
577 577
578int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert) 578int cms_ias_cert_cmp(CMS_IssuerAndSerialNumber *ias, X509 *cert)
579{ 579{
580 int ret; 580 int ret;
581 ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert)); 581 ret = X509_NAME_cmp(ias->issuer, X509_get_issuer_name(cert));
582 if (ret) 582 if (ret)
583 return ret; 583 return ret;
584 return ASN1_INTEGER_cmp(ias->serialNumber, X509_get_serialNumber(cert)); 584 return ASN1_INTEGER_cmp(ias->serialNumber, X509_get_serialNumber(cert));
585} 585}
586 586
587int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert) 587int cms_keyid_cert_cmp(ASN1_OCTET_STRING *keyid, X509 *cert)
588{ 588{
589 const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert); 589 const ASN1_OCTET_STRING *cert_keyid = X509_get0_subject_key_id(cert);
590 590
591 if (cert_keyid == NULL) 591 if (cert_keyid == NULL)
592 return -1; 592 return -1;
593 return ASN1_OCTET_STRING_cmp(keyid, cert_keyid); 593 return ASN1_OCTET_STRING_cmp(keyid, cert_keyid);
594} 594}
595 595
596int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert) 596int cms_set1_ias(CMS_IssuerAndSerialNumber **pias, X509 *cert)
597{ 597{
598 CMS_IssuerAndSerialNumber *ias; 598 CMS_IssuerAndSerialNumber *ias;
599 ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber); 599 ias = M_ASN1_new_of(CMS_IssuerAndSerialNumber);
600 if (!ias) 600 if (!ias)
601 goto err; 601 goto err;
602 if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert))) 602 if (!X509_NAME_set(&ias->issuer, X509_get_issuer_name(cert)))
603 goto err; 603 goto err;
604 if (!ASN1_STRING_copy(ias->serialNumber, X509_get_serialNumber(cert))) 604 if (!ASN1_STRING_copy(ias->serialNumber, X509_get_serialNumber(cert)))
605 goto err; 605 goto err;
606 M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber); 606 M_ASN1_free_of(*pias, CMS_IssuerAndSerialNumber);
607 *pias = ias; 607 *pias = ias;
608 return 1; 608 return 1;
609 err: 609 err:
610 M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber); 610 M_ASN1_free_of(ias, CMS_IssuerAndSerialNumber);
611 CMSerr(CMS_F_CMS_SET1_IAS, ERR_R_MALLOC_FAILURE); 611 CMSerr(CMS_F_CMS_SET1_IAS, ERR_R_MALLOC_FAILURE);
612 return 0; 612 return 0;
613} 613}
614 614
615int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert) 615int cms_set1_keyid(ASN1_OCTET_STRING **pkeyid, X509 *cert)
616{ 616{
617 ASN1_OCTET_STRING *keyid = NULL; 617 ASN1_OCTET_STRING *keyid = NULL;
618 const ASN1_OCTET_STRING *cert_keyid; 618 const ASN1_OCTET_STRING *cert_keyid;
619 cert_keyid = X509_get0_subject_key_id(cert); 619 cert_keyid = X509_get0_subject_key_id(cert);
620 if (cert_keyid == NULL) { 620 if (cert_keyid == NULL) {
621 CMSerr(CMS_F_CMS_SET1_KEYID, CMS_R_CERTIFICATE_HAS_NO_KEYID); 621 CMSerr(CMS_F_CMS_SET1_KEYID, CMS_R_CERTIFICATE_HAS_NO_KEYID);
622 return 0; 622 return 0;
623 } 623 }
624 keyid = ASN1_STRING_dup(cert_keyid); 624 keyid = ASN1_STRING_dup(cert_keyid);
625 if (!keyid) { 625 if (!keyid) {
626 CMSerr(CMS_F_CMS_SET1_KEYID, ERR_R_MALLOC_FAILURE); 626 CMSerr(CMS_F_CMS_SET1_KEYID, ERR_R_MALLOC_FAILURE);
627 return 0; 627 return 0;
628 } 628 }
629 ASN1_OCTET_STRING_free(*pkeyid); 629 ASN1_OCTET_STRING_free(*pkeyid);
630 *pkeyid = keyid; 630 *pkeyid = keyid;
631 return 1; 631 return 1;
632} 632}
diff --git a/src/lib/libcrypto/cms/cms_pwri.c b/src/lib/libcrypto/cms/cms_pwri.c
index a8030471e6..d7f5697ff0 100644
--- a/src/lib/libcrypto/cms/cms_pwri.c
+++ b/src/lib/libcrypto/cms/cms_pwri.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_pwri.c,v 1.15 2019/08/10 16:39:17 jsing Exp $ */ 1/* $OpenBSD: cms_pwri.c,v 1.16 2019/08/10 16:42:20 jsing Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -64,152 +64,152 @@
64#include "asn1/asn1_locl.h" 64#include "asn1/asn1_locl.h"
65 65
66int CMS_RecipientInfo_set0_password(CMS_RecipientInfo *ri, 66int CMS_RecipientInfo_set0_password(CMS_RecipientInfo *ri,
67 unsigned char *pass, ossl_ssize_t passlen) 67 unsigned char *pass, ossl_ssize_t passlen)
68{ 68{
69 CMS_PasswordRecipientInfo *pwri; 69 CMS_PasswordRecipientInfo *pwri;
70 if (ri->type != CMS_RECIPINFO_PASS) { 70 if (ri->type != CMS_RECIPINFO_PASS) {
71 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PASSWORD, CMS_R_NOT_PWRI); 71 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PASSWORD, CMS_R_NOT_PWRI);
72 return 0; 72 return 0;
73 } 73 }
74 74
75 pwri = ri->d.pwri; 75 pwri = ri->d.pwri;
76 pwri->pass = pass; 76 pwri->pass = pass;
77 if (pass && passlen < 0) 77 if (pass && passlen < 0)
78 passlen = strlen((char *)pass); 78 passlen = strlen((char *)pass);
79 pwri->passlen = passlen; 79 pwri->passlen = passlen;
80 return 1; 80 return 1;
81} 81}
82 82
83CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms, 83CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
84 int iter, int wrap_nid, 84 int iter, int wrap_nid,
85 int pbe_nid, 85 int pbe_nid,
86 unsigned char *pass, 86 unsigned char *pass,
87 ossl_ssize_t passlen, 87 ossl_ssize_t passlen,
88 const EVP_CIPHER *kekciph) 88 const EVP_CIPHER *kekciph)
89{ 89{
90 CMS_RecipientInfo *ri = NULL; 90 CMS_RecipientInfo *ri = NULL;
91 CMS_EnvelopedData *env; 91 CMS_EnvelopedData *env;
92 CMS_PasswordRecipientInfo *pwri; 92 CMS_PasswordRecipientInfo *pwri;
93 EVP_CIPHER_CTX *ctx = NULL; 93 EVP_CIPHER_CTX *ctx = NULL;
94 X509_ALGOR *encalg = NULL; 94 X509_ALGOR *encalg = NULL;
95 unsigned char iv[EVP_MAX_IV_LENGTH]; 95 unsigned char iv[EVP_MAX_IV_LENGTH];
96 int ivlen; 96 int ivlen;
97 97
98 env = cms_get0_enveloped(cms); 98 env = cms_get0_enveloped(cms);
99 if (!env) 99 if (!env)
100 return NULL; 100 return NULL;
101 101
102 if (wrap_nid <= 0) 102 if (wrap_nid <= 0)
103 wrap_nid = NID_id_alg_PWRI_KEK; 103 wrap_nid = NID_id_alg_PWRI_KEK;
104 104
105 if (pbe_nid <= 0) 105 if (pbe_nid <= 0)
106 pbe_nid = NID_id_pbkdf2; 106 pbe_nid = NID_id_pbkdf2;
107 107
108 /* Get from enveloped data */ 108 /* Get from enveloped data */
109 if (kekciph == NULL) 109 if (kekciph == NULL)
110 kekciph = env->encryptedContentInfo->cipher; 110 kekciph = env->encryptedContentInfo->cipher;
111 111
112 if (kekciph == NULL) { 112 if (kekciph == NULL) {
113 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, CMS_R_NO_CIPHER); 113 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, CMS_R_NO_CIPHER);
114 return NULL; 114 return NULL;
115 } 115 }
116 if (wrap_nid != NID_id_alg_PWRI_KEK) { 116 if (wrap_nid != NID_id_alg_PWRI_KEK) {
117 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, 117 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
118 CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM); 118 CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
119 return NULL; 119 return NULL;
120 } 120 }
121 121
122 /* Setup algorithm identifier for cipher */ 122 /* Setup algorithm identifier for cipher */
123 encalg = X509_ALGOR_new(); 123 encalg = X509_ALGOR_new();
124 if (encalg == NULL) { 124 if (encalg == NULL) {
125 goto merr; 125 goto merr;
126 } 126 }
127 ctx = EVP_CIPHER_CTX_new(); 127 ctx = EVP_CIPHER_CTX_new();
128 128
129 if (EVP_EncryptInit_ex(ctx, kekciph, NULL, NULL, NULL) <= 0) { 129 if (EVP_EncryptInit_ex(ctx, kekciph, NULL, NULL, NULL) <= 0) {
130 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB); 130 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB);
131 goto err; 131 goto err;
132 } 132 }
133 133
134 ivlen = EVP_CIPHER_CTX_iv_length(ctx); 134 ivlen = EVP_CIPHER_CTX_iv_length(ctx);
135 135
136 if (ivlen > 0) { 136 if (ivlen > 0) {
137 if (RAND_bytes(iv, ivlen) <= 0) 137 if (RAND_bytes(iv, ivlen) <= 0)
138 goto err; 138 goto err;
139 if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0) { 139 if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0) {
140 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB); 140 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB);
141 goto err; 141 goto err;
142 } 142 }
143 encalg->parameter = ASN1_TYPE_new(); 143 encalg->parameter = ASN1_TYPE_new();
144 if (!encalg->parameter) { 144 if (!encalg->parameter) {
145 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE); 145 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE);
146 goto err; 146 goto err;
147 } 147 }
148 if (EVP_CIPHER_param_to_asn1(ctx, encalg->parameter) <= 0) { 148 if (EVP_CIPHER_param_to_asn1(ctx, encalg->parameter) <= 0) {
149 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, 149 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
150 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR); 150 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
151 goto err; 151 goto err;
152 } 152 }
153 } 153 }
154 154
155 encalg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx)); 155 encalg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
156 156
157 EVP_CIPHER_CTX_free(ctx); 157 EVP_CIPHER_CTX_free(ctx);
158 ctx = NULL; 158 ctx = NULL;
159 159
160 /* Initialize recipient info */ 160 /* Initialize recipient info */
161 ri = M_ASN1_new_of(CMS_RecipientInfo); 161 ri = M_ASN1_new_of(CMS_RecipientInfo);
162 if (ri == NULL) 162 if (ri == NULL)
163 goto merr; 163 goto merr;
164 164
165 ri->d.pwri = M_ASN1_new_of(CMS_PasswordRecipientInfo); 165 ri->d.pwri = M_ASN1_new_of(CMS_PasswordRecipientInfo);
166 if (ri->d.pwri == NULL) 166 if (ri->d.pwri == NULL)
167 goto merr; 167 goto merr;
168 ri->type = CMS_RECIPINFO_PASS; 168 ri->type = CMS_RECIPINFO_PASS;
169 169
170 pwri = ri->d.pwri; 170 pwri = ri->d.pwri;
171 /* Since this is overwritten, free up empty structure already there */ 171 /* Since this is overwritten, free up empty structure already there */
172 X509_ALGOR_free(pwri->keyEncryptionAlgorithm); 172 X509_ALGOR_free(pwri->keyEncryptionAlgorithm);
173 pwri->keyEncryptionAlgorithm = X509_ALGOR_new(); 173 pwri->keyEncryptionAlgorithm = X509_ALGOR_new();
174 if (pwri->keyEncryptionAlgorithm == NULL) 174 if (pwri->keyEncryptionAlgorithm == NULL)
175 goto merr; 175 goto merr;
176 pwri->keyEncryptionAlgorithm->algorithm = OBJ_nid2obj(wrap_nid); 176 pwri->keyEncryptionAlgorithm->algorithm = OBJ_nid2obj(wrap_nid);
177 pwri->keyEncryptionAlgorithm->parameter = ASN1_TYPE_new(); 177 pwri->keyEncryptionAlgorithm->parameter = ASN1_TYPE_new();
178 if (pwri->keyEncryptionAlgorithm->parameter == NULL) 178 if (pwri->keyEncryptionAlgorithm->parameter == NULL)
179 goto merr; 179 goto merr;
180 180
181 if (!ASN1_item_pack(encalg, ASN1_ITEM_rptr(X509_ALGOR), 181 if (!ASN1_item_pack(encalg, ASN1_ITEM_rptr(X509_ALGOR),
182 &pwri->keyEncryptionAlgorithm->parameter-> 182 &pwri->keyEncryptionAlgorithm->parameter->
183 value.sequence)) 183 value.sequence))
184 goto merr; 184 goto merr;
185 pwri->keyEncryptionAlgorithm->parameter->type = V_ASN1_SEQUENCE; 185 pwri->keyEncryptionAlgorithm->parameter->type = V_ASN1_SEQUENCE;
186 186
187 X509_ALGOR_free(encalg); 187 X509_ALGOR_free(encalg);
188 encalg = NULL; 188 encalg = NULL;
189 189
190 /* Setup PBE algorithm */ 190 /* Setup PBE algorithm */
191 191
192 pwri->keyDerivationAlgorithm = PKCS5_pbkdf2_set(iter, NULL, 0, -1, -1); 192 pwri->keyDerivationAlgorithm = PKCS5_pbkdf2_set(iter, NULL, 0, -1, -1);
193 193
194 if (!pwri->keyDerivationAlgorithm) 194 if (!pwri->keyDerivationAlgorithm)
195 goto err; 195 goto err;
196 196
197 CMS_RecipientInfo_set0_password(ri, pass, passlen); 197 CMS_RecipientInfo_set0_password(ri, pass, passlen);
198 pwri->version = 0; 198 pwri->version = 0;
199 199
200 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri)) 200 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
201 goto merr; 201 goto merr;
202 202
203 return ri; 203 return ri;
204 204
205 merr: 205 merr:
206 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE); 206 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE);
207 err: 207 err:
208 EVP_CIPHER_CTX_free(ctx); 208 EVP_CIPHER_CTX_free(ctx);
209 if (ri) 209 if (ri)
210 M_ASN1_free_of(ri, CMS_RecipientInfo); 210 M_ASN1_free_of(ri, CMS_RecipientInfo);
211 X509_ALGOR_free(encalg); 211 X509_ALGOR_free(encalg);
212 return NULL; 212 return NULL;
213 213
214} 214}
215 215
@@ -219,221 +219,221 @@ CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
219 */ 219 */
220 220
221static int kek_unwrap_key(unsigned char *out, size_t *outlen, 221static int kek_unwrap_key(unsigned char *out, size_t *outlen,
222 const unsigned char *in, size_t inlen, 222 const unsigned char *in, size_t inlen,
223 EVP_CIPHER_CTX *ctx) 223 EVP_CIPHER_CTX *ctx)
224{ 224{
225 size_t blocklen = EVP_CIPHER_CTX_block_size(ctx); 225 size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
226 unsigned char *tmp; 226 unsigned char *tmp;
227 int outl, rv = 0; 227 int outl, rv = 0;
228 if (inlen < 2 * blocklen) { 228 if (inlen < 2 * blocklen) {
229 /* too small */ 229 /* too small */
230 return 0; 230 return 0;
231 } 231 }
232 if (inlen % blocklen) { 232 if (inlen % blocklen) {
233 /* Invalid size */ 233 /* Invalid size */
234 return 0; 234 return 0;
235 } 235 }
236 if ((tmp = OPENSSL_malloc(inlen)) == NULL) { 236 if ((tmp = OPENSSL_malloc(inlen)) == NULL) {
237 CMSerr(CMS_F_KEK_UNWRAP_KEY, ERR_R_MALLOC_FAILURE); 237 CMSerr(CMS_F_KEK_UNWRAP_KEY, ERR_R_MALLOC_FAILURE);
238 return 0; 238 return 0;
239 } 239 }
240 /* setup IV by decrypting last two blocks */ 240 /* setup IV by decrypting last two blocks */
241 if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl, 241 if (!EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,
242 in + inlen - 2 * blocklen, blocklen * 2) 242 in + inlen - 2 * blocklen, blocklen * 2)
243 /* 243 /*
244 * Do a decrypt of last decrypted block to set IV to correct value 244 * Do a decrypt of last decrypted block to set IV to correct value
245 * output it to start of buffer so we don't corrupt decrypted block 245 * output it to start of buffer so we don't corrupt decrypted block
246 * this works because buffer is at least two block lengths long. 246 * this works because buffer is at least two block lengths long.
247 */ 247 */
248 || !EVP_DecryptUpdate(ctx, tmp, &outl, 248 || !EVP_DecryptUpdate(ctx, tmp, &outl,
249 tmp + inlen - blocklen, blocklen) 249 tmp + inlen - blocklen, blocklen)
250 /* Can now decrypt first n - 1 blocks */ 250 /* Can now decrypt first n - 1 blocks */
251 || !EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen) 251 || !EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen)
252 252
253 /* Reset IV to original value */ 253 /* Reset IV to original value */
254 || !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL) 254 || !EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL)
255 /* Decrypt again */ 255 /* Decrypt again */
256 || !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen)) 256 || !EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen))
257 goto err; 257 goto err;
258 /* Check check bytes */ 258 /* Check check bytes */
259 if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) { 259 if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff) {
260 /* Check byte failure */ 260 /* Check byte failure */
261 goto err; 261 goto err;
262 } 262 }
263 if (inlen < (size_t)(tmp[0] - 4)) { 263 if (inlen < (size_t)(tmp[0] - 4)) {
264 /* Invalid length value */ 264 /* Invalid length value */
265 goto err; 265 goto err;
266 } 266 }
267 *outlen = (size_t)tmp[0]; 267 *outlen = (size_t)tmp[0];
268 memcpy(out, tmp + 4, *outlen); 268 memcpy(out, tmp + 4, *outlen);
269 rv = 1; 269 rv = 1;
270 err: 270 err:
271 OPENSSL_clear_free(tmp, inlen); 271 OPENSSL_clear_free(tmp, inlen);
272 return rv; 272 return rv;
273 273
274} 274}
275 275
276static int kek_wrap_key(unsigned char *out, size_t *outlen, 276static int kek_wrap_key(unsigned char *out, size_t *outlen,
277 const unsigned char *in, size_t inlen, 277 const unsigned char *in, size_t inlen,
278 EVP_CIPHER_CTX *ctx) 278 EVP_CIPHER_CTX *ctx)
279{ 279{
280 size_t blocklen = EVP_CIPHER_CTX_block_size(ctx); 280 size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
281 size_t olen; 281 size_t olen;
282 int dummy; 282 int dummy;
283 /* 283 /*
284 * First decide length of output buffer: need header and round up to 284 * First decide length of output buffer: need header and round up to
285 * multiple of block length. 285 * multiple of block length.
286 */ 286 */
287 olen = (inlen + 4 + blocklen - 1) / blocklen; 287 olen = (inlen + 4 + blocklen - 1) / blocklen;
288 olen *= blocklen; 288 olen *= blocklen;
289 if (olen < 2 * blocklen) { 289 if (olen < 2 * blocklen) {
290 /* Key too small */ 290 /* Key too small */
291 return 0; 291 return 0;
292 } 292 }
293 if (inlen > 0xFF) { 293 if (inlen > 0xFF) {
294 /* Key too large */ 294 /* Key too large */
295 return 0; 295 return 0;
296 } 296 }
297 if (out) { 297 if (out) {
298 /* Set header */ 298 /* Set header */
299 out[0] = (unsigned char)inlen; 299 out[0] = (unsigned char)inlen;
300 out[1] = in[0] ^ 0xFF; 300 out[1] = in[0] ^ 0xFF;
301 out[2] = in[1] ^ 0xFF; 301 out[2] = in[1] ^ 0xFF;
302 out[3] = in[2] ^ 0xFF; 302 out[3] = in[2] ^ 0xFF;
303 memcpy(out + 4, in, inlen); 303 memcpy(out + 4, in, inlen);
304 /* Add random padding to end */ 304 /* Add random padding to end */
305 if (olen > inlen + 4 305 if (olen > inlen + 4
306 && RAND_bytes(out + 4 + inlen, olen - 4 - inlen) <= 0) 306 && RAND_bytes(out + 4 + inlen, olen - 4 - inlen) <= 0)
307 return 0; 307 return 0;
308 /* Encrypt twice */ 308 /* Encrypt twice */
309 if (!EVP_EncryptUpdate(ctx, out, &dummy, out, olen) 309 if (!EVP_EncryptUpdate(ctx, out, &dummy, out, olen)
310 || !EVP_EncryptUpdate(ctx, out, &dummy, out, olen)) 310 || !EVP_EncryptUpdate(ctx, out, &dummy, out, olen))
311 return 0; 311 return 0;
312 } 312 }
313 313
314 *outlen = olen; 314 *outlen = olen;
315 315
316 return 1; 316 return 1;
317} 317}
318 318
319/* Encrypt/Decrypt content key in PWRI recipient info */ 319/* Encrypt/Decrypt content key in PWRI recipient info */
320 320
321int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri, 321int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
322 int en_de) 322 int en_de)
323{ 323{
324 CMS_EncryptedContentInfo *ec; 324 CMS_EncryptedContentInfo *ec;
325 CMS_PasswordRecipientInfo *pwri; 325 CMS_PasswordRecipientInfo *pwri;
326 int r = 0; 326 int r = 0;
327 X509_ALGOR *algtmp, *kekalg = NULL; 327 X509_ALGOR *algtmp, *kekalg = NULL;
328 EVP_CIPHER_CTX *kekctx = NULL; 328 EVP_CIPHER_CTX *kekctx = NULL;
329 const EVP_CIPHER *kekcipher; 329 const EVP_CIPHER *kekcipher;
330 unsigned char *key = NULL; 330 unsigned char *key = NULL;
331 size_t keylen; 331 size_t keylen;
332 332
333 ec = cms->d.envelopedData->encryptedContentInfo; 333 ec = cms->d.envelopedData->encryptedContentInfo;
334 334
335 pwri = ri->d.pwri; 335 pwri = ri->d.pwri;
336 336
337 if (!pwri->pass) { 337 if (!pwri->pass) {
338 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_NO_PASSWORD); 338 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_NO_PASSWORD);
339 return 0; 339 return 0;
340 } 340 }
341 algtmp = pwri->keyEncryptionAlgorithm; 341 algtmp = pwri->keyEncryptionAlgorithm;
342 342
343 if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) { 343 if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK) {
344 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, 344 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
345 CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM); 345 CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
346 return 0; 346 return 0;
347 } 347 }
348 348
349 kekalg = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR), 349 kekalg = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(X509_ALGOR),
350 algtmp->parameter); 350 algtmp->parameter);
351 351
352 if (kekalg == NULL) { 352 if (kekalg == NULL) {
353 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, 353 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
354 CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER); 354 CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER);
355 return 0; 355 return 0;
356 } 356 }
357 357
358 kekcipher = EVP_get_cipherbyobj(kekalg->algorithm); 358 kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
359 359
360 if (!kekcipher) { 360 if (!kekcipher) {
361 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNKNOWN_CIPHER); 361 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNKNOWN_CIPHER);
362 return 0; 362 return 0;
363 } 363 }
364 364
365 kekctx = EVP_CIPHER_CTX_new(); 365 kekctx = EVP_CIPHER_CTX_new();
366 if (kekctx == NULL) { 366 if (kekctx == NULL) {
367 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE); 367 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE);
368 return 0; 368 return 0;
369 } 369 }
370 /* Fixup cipher based on AlgorithmIdentifier to set IV etc */ 370 /* Fixup cipher based on AlgorithmIdentifier to set IV etc */
371 if (!EVP_CipherInit_ex(kekctx, kekcipher, NULL, NULL, NULL, en_de)) 371 if (!EVP_CipherInit_ex(kekctx, kekcipher, NULL, NULL, NULL, en_de))
372 goto err; 372 goto err;
373 EVP_CIPHER_CTX_set_padding(kekctx, 0); 373 EVP_CIPHER_CTX_set_padding(kekctx, 0);
374 if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0) { 374 if (EVP_CIPHER_asn1_to_param(kekctx, kekalg->parameter) <= 0) {
375 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, 375 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
376 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR); 376 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
377 goto err; 377 goto err;
378 } 378 }
379 379
380 algtmp = pwri->keyDerivationAlgorithm; 380 algtmp = pwri->keyDerivationAlgorithm;
381 381
382 /* Finish password based key derivation to setup key in "ctx" */ 382 /* Finish password based key derivation to setup key in "ctx" */
383 383
384 if (EVP_PBE_CipherInit(algtmp->algorithm, 384 if (EVP_PBE_CipherInit(algtmp->algorithm,
385 (char *)pwri->pass, pwri->passlen, 385 (char *)pwri->pass, pwri->passlen,
386 algtmp->parameter, kekctx, en_de) < 0) { 386 algtmp->parameter, kekctx, en_de) < 0) {
387 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_EVP_LIB); 387 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_EVP_LIB);
388 goto err; 388 goto err;
389 } 389 }
390 390
391 /* Finally wrap/unwrap the key */ 391 /* Finally wrap/unwrap the key */
392 392
393 if (en_de) { 393 if (en_de) {
394 394
395 if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, kekctx)) 395 if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, kekctx))
396 goto err; 396 goto err;
397 397
398 key = OPENSSL_malloc(keylen); 398 key = OPENSSL_malloc(keylen);
399 399
400 if (key == NULL) 400 if (key == NULL)
401 goto err; 401 goto err;
402 402
403 if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, kekctx)) 403 if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, kekctx))
404 goto err; 404 goto err;
405 pwri->encryptedKey->data = key; 405 pwri->encryptedKey->data = key;
406 pwri->encryptedKey->length = keylen; 406 pwri->encryptedKey->length = keylen;
407 } else { 407 } else {
408 key = OPENSSL_malloc(pwri->encryptedKey->length); 408 key = OPENSSL_malloc(pwri->encryptedKey->length);
409 409
410 if (key == NULL) { 410 if (key == NULL) {
411 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE); 411 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_MALLOC_FAILURE);
412 goto err; 412 goto err;
413 } 413 }
414 if (!kek_unwrap_key(key, &keylen, 414 if (!kek_unwrap_key(key, &keylen,
415 pwri->encryptedKey->data, 415 pwri->encryptedKey->data,
416 pwri->encryptedKey->length, kekctx)) { 416 pwri->encryptedKey->length, kekctx)) {
417 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNWRAP_FAILURE); 417 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_UNWRAP_FAILURE);
418 goto err; 418 goto err;
419 } 419 }
420 420
421 OPENSSL_clear_free(ec->key, ec->keylen); 421 OPENSSL_clear_free(ec->key, ec->keylen);
422 ec->key = key; 422 ec->key = key;
423 ec->keylen = keylen; 423 ec->keylen = keylen;
424 424
425 } 425 }
426 426
427 r = 1; 427 r = 1;
428 428
429 err: 429 err:
430 430
431 EVP_CIPHER_CTX_free(kekctx); 431 EVP_CIPHER_CTX_free(kekctx);
432 432
433 if (!r) 433 if (!r)
434 OPENSSL_free(key); 434 OPENSSL_free(key);
435 X509_ALGOR_free(kekalg); 435 X509_ALGOR_free(kekalg);
436 436
437 return r; 437 return r;
438 438
439} 439}
diff --git a/src/lib/libcrypto/cms/cms_sd.c b/src/lib/libcrypto/cms/cms_sd.c
index b5b6dc4e67..57a92b59e3 100644
--- a/src/lib/libcrypto/cms/cms_sd.c
+++ b/src/lib/libcrypto/cms/cms_sd.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_sd.c,v 1.15 2019/08/10 16:39:17 jsing Exp $ */ 1/* $OpenBSD: cms_sd.c,v 1.16 2019/08/10 16:42:20 jsing Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -67,90 +67,90 @@
67 67
68static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms) 68static CMS_SignedData *cms_get0_signed(CMS_ContentInfo *cms)
69{ 69{
70 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed) { 70 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_signed) {
71 CMSerr(CMS_F_CMS_GET0_SIGNED, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA); 71 CMSerr(CMS_F_CMS_GET0_SIGNED, CMS_R_CONTENT_TYPE_NOT_SIGNED_DATA);
72 return NULL; 72 return NULL;
73 } 73 }
74 return cms->d.signedData; 74 return cms->d.signedData;
75} 75}
76 76
77static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms) 77static CMS_SignedData *cms_signed_data_init(CMS_ContentInfo *cms)
78{ 78{
79 if (cms->d.other == NULL) { 79 if (cms->d.other == NULL) {
80 cms->d.signedData = M_ASN1_new_of(CMS_SignedData); 80 cms->d.signedData = M_ASN1_new_of(CMS_SignedData);
81 if (!cms->d.signedData) { 81 if (!cms->d.signedData) {
82 CMSerr(CMS_F_CMS_SIGNED_DATA_INIT, ERR_R_MALLOC_FAILURE); 82 CMSerr(CMS_F_CMS_SIGNED_DATA_INIT, ERR_R_MALLOC_FAILURE);
83 return NULL; 83 return NULL;
84 } 84 }
85 cms->d.signedData->version = 1; 85 cms->d.signedData->version = 1;
86 cms->d.signedData->encapContentInfo->eContentType = 86 cms->d.signedData->encapContentInfo->eContentType =
87 OBJ_nid2obj(NID_pkcs7_data); 87 OBJ_nid2obj(NID_pkcs7_data);
88 cms->d.signedData->encapContentInfo->partial = 1; 88 cms->d.signedData->encapContentInfo->partial = 1;
89 ASN1_OBJECT_free(cms->contentType); 89 ASN1_OBJECT_free(cms->contentType);
90 cms->contentType = OBJ_nid2obj(NID_pkcs7_signed); 90 cms->contentType = OBJ_nid2obj(NID_pkcs7_signed);
91 return cms->d.signedData; 91 return cms->d.signedData;
92 } 92 }
93 return cms_get0_signed(cms); 93 return cms_get0_signed(cms);
94} 94}
95 95
96/* Just initialise SignedData e.g. for certs only structure */ 96/* Just initialise SignedData e.g. for certs only structure */
97 97
98int CMS_SignedData_init(CMS_ContentInfo *cms) 98int CMS_SignedData_init(CMS_ContentInfo *cms)
99{ 99{
100 if (cms_signed_data_init(cms)) 100 if (cms_signed_data_init(cms))
101 return 1; 101 return 1;
102 else 102 else
103 return 0; 103 return 0;
104} 104}
105 105
106/* Check structures and fixup version numbers (if necessary) */ 106/* Check structures and fixup version numbers (if necessary) */
107 107
108static void cms_sd_set_version(CMS_SignedData *sd) 108static void cms_sd_set_version(CMS_SignedData *sd)
109{ 109{
110 int i; 110 int i;
111 CMS_CertificateChoices *cch; 111 CMS_CertificateChoices *cch;
112 CMS_RevocationInfoChoice *rch; 112 CMS_RevocationInfoChoice *rch;
113 CMS_SignerInfo *si; 113 CMS_SignerInfo *si;
114 114
115 for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++) { 115 for (i = 0; i < sk_CMS_CertificateChoices_num(sd->certificates); i++) {
116 cch = sk_CMS_CertificateChoices_value(sd->certificates, i); 116 cch = sk_CMS_CertificateChoices_value(sd->certificates, i);
117 if (cch->type == CMS_CERTCHOICE_OTHER) { 117 if (cch->type == CMS_CERTCHOICE_OTHER) {
118 if (sd->version < 5) 118 if (sd->version < 5)
119 sd->version = 5; 119 sd->version = 5;
120 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) { 120 } else if (cch->type == CMS_CERTCHOICE_V2ACERT) {
121 if (sd->version < 4) 121 if (sd->version < 4)
122 sd->version = 4; 122 sd->version = 4;
123 } else if (cch->type == CMS_CERTCHOICE_V1ACERT) { 123 } else if (cch->type == CMS_CERTCHOICE_V1ACERT) {
124 if (sd->version < 3) 124 if (sd->version < 3)
125 sd->version = 3; 125 sd->version = 3;
126 } 126 }
127 } 127 }
128 128
129 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++) { 129 for (i = 0; i < sk_CMS_RevocationInfoChoice_num(sd->crls); i++) {
130 rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i); 130 rch = sk_CMS_RevocationInfoChoice_value(sd->crls, i);
131 if (rch->type == CMS_REVCHOICE_OTHER) { 131 if (rch->type == CMS_REVCHOICE_OTHER) {
132 if (sd->version < 5) 132 if (sd->version < 5)
133 sd->version = 5; 133 sd->version = 5;
134 } 134 }
135 } 135 }
136 136
137 if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data) 137 if ((OBJ_obj2nid(sd->encapContentInfo->eContentType) != NID_pkcs7_data)
138 && (sd->version < 3)) 138 && (sd->version < 3))
139 sd->version = 3; 139 sd->version = 3;
140 140
141 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) { 141 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
142 si = sk_CMS_SignerInfo_value(sd->signerInfos, i); 142 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
143 if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) { 143 if (si->sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
144 if (si->version < 3) 144 if (si->version < 3)
145 si->version = 3; 145 si->version = 3;
146 if (sd->version < 3) 146 if (sd->version < 3)
147 sd->version = 3; 147 sd->version = 3;
148 } else if (si->version < 1) 148 } else if (si->version < 1)
149 si->version = 1; 149 si->version = 1;
150 } 150 }
151 151
152 if (sd->version < 1) 152 if (sd->version < 1)
153 sd->version = 1; 153 sd->version = 1;
154 154
155} 155}
156 156
@@ -158,814 +158,814 @@ static void cms_sd_set_version(CMS_SignedData *sd)
158 158
159static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si) 159static int cms_copy_messageDigest(CMS_ContentInfo *cms, CMS_SignerInfo *si)
160{ 160{
161 STACK_OF(CMS_SignerInfo) *sinfos; 161 STACK_OF(CMS_SignerInfo) *sinfos;
162 CMS_SignerInfo *sitmp; 162 CMS_SignerInfo *sitmp;
163 int i; 163 int i;
164 sinfos = CMS_get0_SignerInfos(cms); 164 sinfos = CMS_get0_SignerInfos(cms);
165 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) { 165 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
166 ASN1_OCTET_STRING *messageDigest; 166 ASN1_OCTET_STRING *messageDigest;
167 sitmp = sk_CMS_SignerInfo_value(sinfos, i); 167 sitmp = sk_CMS_SignerInfo_value(sinfos, i);
168 if (sitmp == si) 168 if (sitmp == si)
169 continue; 169 continue;
170 if (CMS_signed_get_attr_count(sitmp) < 0) 170 if (CMS_signed_get_attr_count(sitmp) < 0)
171 continue; 171 continue;
172 if (OBJ_cmp(si->digestAlgorithm->algorithm, 172 if (OBJ_cmp(si->digestAlgorithm->algorithm,
173 sitmp->digestAlgorithm->algorithm)) 173 sitmp->digestAlgorithm->algorithm))
174 continue; 174 continue;
175 messageDigest = CMS_signed_get0_data_by_OBJ(sitmp, 175 messageDigest = CMS_signed_get0_data_by_OBJ(sitmp,
176 OBJ_nid2obj 176 OBJ_nid2obj
177 (NID_pkcs9_messageDigest), 177 (NID_pkcs9_messageDigest),
178 -3, V_ASN1_OCTET_STRING); 178 -3, V_ASN1_OCTET_STRING);
179 if (!messageDigest) { 179 if (!messageDigest) {
180 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, 180 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST,
181 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE); 181 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
182 return 0; 182 return 0;
183 } 183 }
184 184
185 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest, 185 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
186 V_ASN1_OCTET_STRING, 186 V_ASN1_OCTET_STRING,
187 messageDigest, -1)) 187 messageDigest, -1))
188 return 1; 188 return 1;
189 else 189 else
190 return 0; 190 return 0;
191 } 191 }
192 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, CMS_R_NO_MATCHING_DIGEST); 192 CMSerr(CMS_F_CMS_COPY_MESSAGEDIGEST, CMS_R_NO_MATCHING_DIGEST);
193 return 0; 193 return 0;
194} 194}
195 195
196int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type) 196int cms_set1_SignerIdentifier(CMS_SignerIdentifier *sid, X509 *cert, int type)
197{ 197{
198 switch (type) { 198 switch (type) {
199 case CMS_SIGNERINFO_ISSUER_SERIAL: 199 case CMS_SIGNERINFO_ISSUER_SERIAL:
200 if (!cms_set1_ias(&sid->d.issuerAndSerialNumber, cert)) 200 if (!cms_set1_ias(&sid->d.issuerAndSerialNumber, cert))
201 return 0; 201 return 0;
202 break; 202 break;
203 203
204 case CMS_SIGNERINFO_KEYIDENTIFIER: 204 case CMS_SIGNERINFO_KEYIDENTIFIER:
205 if (!cms_set1_keyid(&sid->d.subjectKeyIdentifier, cert)) 205 if (!cms_set1_keyid(&sid->d.subjectKeyIdentifier, cert))
206 return 0; 206 return 0;
207 break; 207 break;
208 208
209 default: 209 default:
210 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, CMS_R_UNKNOWN_ID); 210 CMSerr(CMS_F_CMS_SET1_SIGNERIDENTIFIER, CMS_R_UNKNOWN_ID);
211 return 0; 211 return 0;
212 } 212 }
213 213
214 sid->type = type; 214 sid->type = type;
215 215
216 return 1; 216 return 1;
217} 217}
218 218
219int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid, 219int cms_SignerIdentifier_get0_signer_id(CMS_SignerIdentifier *sid,
220 ASN1_OCTET_STRING **keyid, 220 ASN1_OCTET_STRING **keyid,
221 X509_NAME **issuer, 221 X509_NAME **issuer,
222 ASN1_INTEGER **sno) 222 ASN1_INTEGER **sno)
223{ 223{
224 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) { 224 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) {
225 if (issuer) 225 if (issuer)
226 *issuer = sid->d.issuerAndSerialNumber->issuer; 226 *issuer = sid->d.issuerAndSerialNumber->issuer;
227 if (sno) 227 if (sno)
228 *sno = sid->d.issuerAndSerialNumber->serialNumber; 228 *sno = sid->d.issuerAndSerialNumber->serialNumber;
229 } else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) { 229 } else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) {
230 if (keyid) 230 if (keyid)
231 *keyid = sid->d.subjectKeyIdentifier; 231 *keyid = sid->d.subjectKeyIdentifier;
232 } else 232 } else
233 return 0; 233 return 0;
234 return 1; 234 return 1;
235} 235}
236 236
237int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert) 237int cms_SignerIdentifier_cert_cmp(CMS_SignerIdentifier *sid, X509 *cert)
238{ 238{
239 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL) 239 if (sid->type == CMS_SIGNERINFO_ISSUER_SERIAL)
240 return cms_ias_cert_cmp(sid->d.issuerAndSerialNumber, cert); 240 return cms_ias_cert_cmp(sid->d.issuerAndSerialNumber, cert);
241 else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER) 241 else if (sid->type == CMS_SIGNERINFO_KEYIDENTIFIER)
242 return cms_keyid_cert_cmp(sid->d.subjectKeyIdentifier, cert); 242 return cms_keyid_cert_cmp(sid->d.subjectKeyIdentifier, cert);
243 else 243 else
244 return -1; 244 return -1;
245} 245}
246 246
247static int cms_sd_asn1_ctrl(CMS_SignerInfo *si, int cmd) 247static int cms_sd_asn1_ctrl(CMS_SignerInfo *si, int cmd)
248{ 248{
249 EVP_PKEY *pkey = si->pkey; 249 EVP_PKEY *pkey = si->pkey;
250 int i; 250 int i;
251 if (!pkey->ameth || !pkey->ameth->pkey_ctrl) 251 if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
252 return 1; 252 return 1;
253 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_SIGN, cmd, si); 253 i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_SIGN, cmd, si);
254 if (i == -2) { 254 if (i == -2) {
255 CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); 255 CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
256 return 0; 256 return 0;
257 } 257 }
258 if (i <= 0) { 258 if (i <= 0) {
259 CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_CTRL_FAILURE); 259 CMSerr(CMS_F_CMS_SD_ASN1_CTRL, CMS_R_CTRL_FAILURE);
260 return 0; 260 return 0;
261 } 261 }
262 return 1; 262 return 1;
263} 263}
264 264
265CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms, 265CMS_SignerInfo *CMS_add1_signer(CMS_ContentInfo *cms,
266 X509 *signer, EVP_PKEY *pk, const EVP_MD *md, 266 X509 *signer, EVP_PKEY *pk, const EVP_MD *md,
267 unsigned int flags) 267 unsigned int flags)
268{ 268{
269 CMS_SignedData *sd; 269 CMS_SignedData *sd;
270 CMS_SignerInfo *si = NULL; 270 CMS_SignerInfo *si = NULL;
271 X509_ALGOR *alg; 271 X509_ALGOR *alg;
272 int i, type; 272 int i, type;
273 if (!X509_check_private_key(signer, pk)) { 273 if (!X509_check_private_key(signer, pk)) {
274 CMSerr(CMS_F_CMS_ADD1_SIGNER, 274 CMSerr(CMS_F_CMS_ADD1_SIGNER,
275 CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE); 275 CMS_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
276 return NULL; 276 return NULL;
277 } 277 }
278 sd = cms_signed_data_init(cms); 278 sd = cms_signed_data_init(cms);
279 if (!sd) 279 if (!sd)
280 goto err; 280 goto err;
281 si = M_ASN1_new_of(CMS_SignerInfo); 281 si = M_ASN1_new_of(CMS_SignerInfo);
282 if (!si) 282 if (!si)
283 goto merr; 283 goto merr;
284 /* Call for side-effect of computing hash and caching extensions */ 284 /* Call for side-effect of computing hash and caching extensions */
285 X509_check_purpose(signer, -1, -1); 285 X509_check_purpose(signer, -1, -1);
286 286
287 X509_up_ref(signer); 287 X509_up_ref(signer);
288 EVP_PKEY_up_ref(pk); 288 EVP_PKEY_up_ref(pk);
289 289
290 si->pkey = pk; 290 si->pkey = pk;
291 si->signer = signer; 291 si->signer = signer;
292 si->mctx = EVP_MD_CTX_new(); 292 si->mctx = EVP_MD_CTX_new();
293 si->pctx = NULL; 293 si->pctx = NULL;
294 294
295 if (si->mctx == NULL) { 295 if (si->mctx == NULL) {
296 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE); 296 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
297 goto err; 297 goto err;
298 } 298 }
299 299
300 if (flags & CMS_USE_KEYID) { 300 if (flags & CMS_USE_KEYID) {
301 si->version = 3; 301 si->version = 3;
302 if (sd->version < 3) 302 if (sd->version < 3)
303 sd->version = 3; 303 sd->version = 3;
304 type = CMS_SIGNERINFO_KEYIDENTIFIER; 304 type = CMS_SIGNERINFO_KEYIDENTIFIER;
305 } else { 305 } else {
306 type = CMS_SIGNERINFO_ISSUER_SERIAL; 306 type = CMS_SIGNERINFO_ISSUER_SERIAL;
307 si->version = 1; 307 si->version = 1;
308 } 308 }
309 309
310 if (!cms_set1_SignerIdentifier(si->sid, signer, type)) 310 if (!cms_set1_SignerIdentifier(si->sid, signer, type))
311 goto err; 311 goto err;
312 312
313 if (md == NULL) { 313 if (md == NULL) {
314 int def_nid; 314 int def_nid;
315 if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0) 315 if (EVP_PKEY_get_default_digest_nid(pk, &def_nid) <= 0)
316 goto err; 316 goto err;
317 md = EVP_get_digestbynid(def_nid); 317 md = EVP_get_digestbynid(def_nid);
318 if (md == NULL) { 318 if (md == NULL) {
319 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DEFAULT_DIGEST); 319 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DEFAULT_DIGEST);
320 goto err; 320 goto err;
321 } 321 }
322 } 322 }
323 323
324 if (!md) { 324 if (!md) {
325 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DIGEST_SET); 325 CMSerr(CMS_F_CMS_ADD1_SIGNER, CMS_R_NO_DIGEST_SET);
326 goto err; 326 goto err;
327 } 327 }
328 328
329 X509_ALGOR_set_md(si->digestAlgorithm, md); 329 X509_ALGOR_set_md(si->digestAlgorithm, md);
330 330
331 /* See if digest is present in digestAlgorithms */ 331 /* See if digest is present in digestAlgorithms */
332 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) { 332 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
333 const ASN1_OBJECT *aoid; 333 const ASN1_OBJECT *aoid;
334 alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i); 334 alg = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
335 X509_ALGOR_get0(&aoid, NULL, NULL, alg); 335 X509_ALGOR_get0(&aoid, NULL, NULL, alg);
336 if (OBJ_obj2nid(aoid) == EVP_MD_type(md)) 336 if (OBJ_obj2nid(aoid) == EVP_MD_type(md))
337 break; 337 break;
338 } 338 }
339 339
340 if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) { 340 if (i == sk_X509_ALGOR_num(sd->digestAlgorithms)) {
341 alg = X509_ALGOR_new(); 341 alg = X509_ALGOR_new();
342 if (alg == NULL) 342 if (alg == NULL)
343 goto merr; 343 goto merr;
344 X509_ALGOR_set_md(alg, md); 344 X509_ALGOR_set_md(alg, md);
345 if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) { 345 if (!sk_X509_ALGOR_push(sd->digestAlgorithms, alg)) {
346 X509_ALGOR_free(alg); 346 X509_ALGOR_free(alg);
347 goto merr; 347 goto merr;
348 } 348 }
349 } 349 }
350 350
351 if (!(flags & CMS_KEY_PARAM) && !cms_sd_asn1_ctrl(si, 0)) 351 if (!(flags & CMS_KEY_PARAM) && !cms_sd_asn1_ctrl(si, 0))
352 goto err; 352 goto err;
353 if (!(flags & CMS_NOATTR)) { 353 if (!(flags & CMS_NOATTR)) {
354 /* 354 /*
355 * Initialize signed attributes structure so other attributes 355 * Initialize signed attributes structure so other attributes
356 * such as signing time etc are added later even if we add none here. 356 * such as signing time etc are added later even if we add none here.
357 */ 357 */
358 if (!si->signedAttrs) { 358 if (!si->signedAttrs) {
359 si->signedAttrs = sk_X509_ATTRIBUTE_new_null(); 359 si->signedAttrs = sk_X509_ATTRIBUTE_new_null();
360 if (!si->signedAttrs) 360 if (!si->signedAttrs)
361 goto merr; 361 goto merr;
362 } 362 }
363 363
364 if (!(flags & CMS_NOSMIMECAP)) { 364 if (!(flags & CMS_NOSMIMECAP)) {
365 STACK_OF(X509_ALGOR) *smcap = NULL; 365 STACK_OF(X509_ALGOR) *smcap = NULL;
366 i = CMS_add_standard_smimecap(&smcap); 366 i = CMS_add_standard_smimecap(&smcap);
367 if (i) 367 if (i)
368 i = CMS_add_smimecap(si, smcap); 368 i = CMS_add_smimecap(si, smcap);
369 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free); 369 sk_X509_ALGOR_pop_free(smcap, X509_ALGOR_free);
370 if (!i) 370 if (!i)
371 goto merr; 371 goto merr;
372 } 372 }
373 if (flags & CMS_REUSE_DIGEST) { 373 if (flags & CMS_REUSE_DIGEST) {
374 if (!cms_copy_messageDigest(cms, si)) 374 if (!cms_copy_messageDigest(cms, si))
375 goto err; 375 goto err;
376 if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) && 376 if (!(flags & (CMS_PARTIAL | CMS_KEY_PARAM)) &&
377 !CMS_SignerInfo_sign(si)) 377 !CMS_SignerInfo_sign(si))
378 goto err; 378 goto err;
379 } 379 }
380 } 380 }
381 381
382 if (!(flags & CMS_NOCERTS)) { 382 if (!(flags & CMS_NOCERTS)) {
383 /* NB ignore -1 return for duplicate cert */ 383 /* NB ignore -1 return for duplicate cert */
384 if (!CMS_add1_cert(cms, signer)) 384 if (!CMS_add1_cert(cms, signer))
385 goto merr; 385 goto merr;
386 } 386 }
387 387
388 if (flags & CMS_KEY_PARAM) { 388 if (flags & CMS_KEY_PARAM) {
389 if (flags & CMS_NOATTR) { 389 if (flags & CMS_NOATTR) {
390 si->pctx = EVP_PKEY_CTX_new(si->pkey, NULL); 390 si->pctx = EVP_PKEY_CTX_new(si->pkey, NULL);
391 if (si->pctx == NULL) 391 if (si->pctx == NULL)
392 goto err; 392 goto err;
393 if (EVP_PKEY_sign_init(si->pctx) <= 0) 393 if (EVP_PKEY_sign_init(si->pctx) <= 0)
394 goto err; 394 goto err;
395 if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0) 395 if (EVP_PKEY_CTX_set_signature_md(si->pctx, md) <= 0)
396 goto err; 396 goto err;
397 } else if (EVP_DigestSignInit(si->mctx, &si->pctx, md, NULL, pk) <= 397 } else if (EVP_DigestSignInit(si->mctx, &si->pctx, md, NULL, pk) <=
398 0) 398 0)
399 goto err; 399 goto err;
400 } 400 }
401 401
402 if (!sd->signerInfos) 402 if (!sd->signerInfos)
403 sd->signerInfos = sk_CMS_SignerInfo_new_null(); 403 sd->signerInfos = sk_CMS_SignerInfo_new_null();
404 if (!sd->signerInfos || !sk_CMS_SignerInfo_push(sd->signerInfos, si)) 404 if (!sd->signerInfos || !sk_CMS_SignerInfo_push(sd->signerInfos, si))
405 goto merr; 405 goto merr;
406 406
407 return si; 407 return si;
408 408
409 merr: 409 merr:
410 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE); 410 CMSerr(CMS_F_CMS_ADD1_SIGNER, ERR_R_MALLOC_FAILURE);
411 err: 411 err:
412 M_ASN1_free_of(si, CMS_SignerInfo); 412 M_ASN1_free_of(si, CMS_SignerInfo);
413 return NULL; 413 return NULL;
414 414
415} 415}
416 416
417static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t) 417static int cms_add1_signingTime(CMS_SignerInfo *si, ASN1_TIME *t)
418{ 418{
419 ASN1_TIME *tt; 419 ASN1_TIME *tt;
420 int r = 0; 420 int r = 0;
421 if (t) 421 if (t)
422 tt = t; 422 tt = t;
423 else 423 else
424 tt = X509_gmtime_adj(NULL, 0); 424 tt = X509_gmtime_adj(NULL, 0);
425 425
426 if (!tt) 426 if (!tt)
427 goto merr; 427 goto merr;
428 428
429 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime, 429 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_signingTime,
430 tt->type, tt, -1) <= 0) 430 tt->type, tt, -1) <= 0)
431 goto merr; 431 goto merr;
432 432
433 r = 1; 433 r = 1;
434 434
435 merr: 435 merr:
436 436
437 if (!t) 437 if (!t)
438 ASN1_TIME_free(tt); 438 ASN1_TIME_free(tt);
439 439
440 if (!r) 440 if (!r)
441 CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME, ERR_R_MALLOC_FAILURE); 441 CMSerr(CMS_F_CMS_ADD1_SIGNINGTIME, ERR_R_MALLOC_FAILURE);
442 442
443 return r; 443 return r;
444 444
445} 445}
446 446
447EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si) 447EVP_PKEY_CTX *CMS_SignerInfo_get0_pkey_ctx(CMS_SignerInfo *si)
448{ 448{
449 return si->pctx; 449 return si->pctx;
450} 450}
451 451
452EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si) 452EVP_MD_CTX *CMS_SignerInfo_get0_md_ctx(CMS_SignerInfo *si)
453{ 453{
454 return si->mctx; 454 return si->mctx;
455} 455}
456 456
457STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms) 457STACK_OF(CMS_SignerInfo) *CMS_get0_SignerInfos(CMS_ContentInfo *cms)
458{ 458{
459 CMS_SignedData *sd; 459 CMS_SignedData *sd;
460 sd = cms_get0_signed(cms); 460 sd = cms_get0_signed(cms);
461 if (!sd) 461 if (!sd)
462 return NULL; 462 return NULL;
463 return sd->signerInfos; 463 return sd->signerInfos;
464} 464}
465 465
466STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms) 466STACK_OF(X509) *CMS_get0_signers(CMS_ContentInfo *cms)
467{ 467{
468 STACK_OF(X509) *signers = NULL; 468 STACK_OF(X509) *signers = NULL;
469 STACK_OF(CMS_SignerInfo) *sinfos; 469 STACK_OF(CMS_SignerInfo) *sinfos;
470 CMS_SignerInfo *si; 470 CMS_SignerInfo *si;
471 int i; 471 int i;
472 sinfos = CMS_get0_SignerInfos(cms); 472 sinfos = CMS_get0_SignerInfos(cms);
473 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) { 473 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
474 si = sk_CMS_SignerInfo_value(sinfos, i); 474 si = sk_CMS_SignerInfo_value(sinfos, i);
475 if (si->signer) { 475 if (si->signer) {
476 if (!signers) { 476 if (!signers) {
477 signers = sk_X509_new_null(); 477 signers = sk_X509_new_null();
478 if (!signers) 478 if (!signers)
479 return NULL; 479 return NULL;
480 } 480 }
481 if (!sk_X509_push(signers, si->signer)) { 481 if (!sk_X509_push(signers, si->signer)) {
482 sk_X509_free(signers); 482 sk_X509_free(signers);
483 return NULL; 483 return NULL;
484 } 484 }
485 } 485 }
486 } 486 }
487 return signers; 487 return signers;
488} 488}
489 489
490void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer) 490void CMS_SignerInfo_set1_signer_cert(CMS_SignerInfo *si, X509 *signer)
491{ 491{
492 if (signer) { 492 if (signer) {
493 X509_up_ref(signer); 493 X509_up_ref(signer);
494 EVP_PKEY_free(si->pkey); 494 EVP_PKEY_free(si->pkey);
495 si->pkey = X509_get_pubkey(signer); 495 si->pkey = X509_get_pubkey(signer);
496 } 496 }
497 X509_free(si->signer); 497 X509_free(si->signer);
498 si->signer = signer; 498 si->signer = signer;
499} 499}
500 500
501int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si, 501int CMS_SignerInfo_get0_signer_id(CMS_SignerInfo *si,
502 ASN1_OCTET_STRING **keyid, 502 ASN1_OCTET_STRING **keyid,
503 X509_NAME **issuer, ASN1_INTEGER **sno) 503 X509_NAME **issuer, ASN1_INTEGER **sno)
504{ 504{
505 return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno); 505 return cms_SignerIdentifier_get0_signer_id(si->sid, keyid, issuer, sno);
506} 506}
507 507
508int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert) 508int CMS_SignerInfo_cert_cmp(CMS_SignerInfo *si, X509 *cert)
509{ 509{
510 return cms_SignerIdentifier_cert_cmp(si->sid, cert); 510 return cms_SignerIdentifier_cert_cmp(si->sid, cert);
511} 511}
512 512
513int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts, 513int CMS_set1_signers_certs(CMS_ContentInfo *cms, STACK_OF(X509) *scerts,
514 unsigned int flags) 514 unsigned int flags)
515{ 515{
516 CMS_SignedData *sd; 516 CMS_SignedData *sd;
517 CMS_SignerInfo *si; 517 CMS_SignerInfo *si;
518 CMS_CertificateChoices *cch; 518 CMS_CertificateChoices *cch;
519 STACK_OF(CMS_CertificateChoices) *certs; 519 STACK_OF(CMS_CertificateChoices) *certs;
520 X509 *x; 520 X509 *x;
521 int i, j; 521 int i, j;
522 int ret = 0; 522 int ret = 0;
523 sd = cms_get0_signed(cms); 523 sd = cms_get0_signed(cms);
524 if (!sd) 524 if (!sd)
525 return -1; 525 return -1;
526 certs = sd->certificates; 526 certs = sd->certificates;
527 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) { 527 for (i = 0; i < sk_CMS_SignerInfo_num(sd->signerInfos); i++) {
528 si = sk_CMS_SignerInfo_value(sd->signerInfos, i); 528 si = sk_CMS_SignerInfo_value(sd->signerInfos, i);
529 if (si->signer) 529 if (si->signer)
530 continue; 530 continue;
531 531
532 for (j = 0; j < sk_X509_num(scerts); j++) { 532 for (j = 0; j < sk_X509_num(scerts); j++) {
533 x = sk_X509_value(scerts, j); 533 x = sk_X509_value(scerts, j);
534 if (CMS_SignerInfo_cert_cmp(si, x) == 0) { 534 if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
535 CMS_SignerInfo_set1_signer_cert(si, x); 535 CMS_SignerInfo_set1_signer_cert(si, x);
536 ret++; 536 ret++;
537 break; 537 break;
538 } 538 }
539 } 539 }
540 540
541 if (si->signer || (flags & CMS_NOINTERN)) 541 if (si->signer || (flags & CMS_NOINTERN))
542 continue; 542 continue;
543 543
544 for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) { 544 for (j = 0; j < sk_CMS_CertificateChoices_num(certs); j++) {
545 cch = sk_CMS_CertificateChoices_value(certs, j); 545 cch = sk_CMS_CertificateChoices_value(certs, j);
546 if (cch->type != 0) 546 if (cch->type != 0)
547 continue; 547 continue;
548 x = cch->d.certificate; 548 x = cch->d.certificate;
549 if (CMS_SignerInfo_cert_cmp(si, x) == 0) { 549 if (CMS_SignerInfo_cert_cmp(si, x) == 0) {
550 CMS_SignerInfo_set1_signer_cert(si, x); 550 CMS_SignerInfo_set1_signer_cert(si, x);
551 ret++; 551 ret++;
552 break; 552 break;
553 } 553 }
554 } 554 }
555 } 555 }
556 return ret; 556 return ret;
557} 557}
558 558
559void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk, 559void CMS_SignerInfo_get0_algs(CMS_SignerInfo *si, EVP_PKEY **pk,
560 X509 **signer, X509_ALGOR **pdig, 560 X509 **signer, X509_ALGOR **pdig,
561 X509_ALGOR **psig) 561 X509_ALGOR **psig)
562{ 562{
563 if (pk) 563 if (pk)
564 *pk = si->pkey; 564 *pk = si->pkey;
565 if (signer) 565 if (signer)
566 *signer = si->signer; 566 *signer = si->signer;
567 if (pdig) 567 if (pdig)
568 *pdig = si->digestAlgorithm; 568 *pdig = si->digestAlgorithm;
569 if (psig) 569 if (psig)
570 *psig = si->signatureAlgorithm; 570 *psig = si->signatureAlgorithm;
571} 571}
572 572
573ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si) 573ASN1_OCTET_STRING *CMS_SignerInfo_get0_signature(CMS_SignerInfo *si)
574{ 574{
575 return si->signature; 575 return si->signature;
576} 576}
577 577
578static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms, 578static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
579 CMS_SignerInfo *si, BIO *chain) 579 CMS_SignerInfo *si, BIO *chain)
580{ 580{
581 EVP_MD_CTX *mctx = EVP_MD_CTX_new(); 581 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
582 int r = 0; 582 int r = 0;
583 EVP_PKEY_CTX *pctx = NULL; 583 EVP_PKEY_CTX *pctx = NULL;
584 584
585 if (mctx == NULL) { 585 if (mctx == NULL) {
586 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE); 586 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
587 return 0; 587 return 0;
588 } 588 }
589 589
590 if (!si->pkey) { 590 if (!si->pkey) {
591 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY); 591 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_NO_PRIVATE_KEY);
592 goto err; 592 goto err;
593 } 593 }
594 594
595 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm)) 595 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
596 goto err; 596 goto err;
597 /* Set SignerInfo algorithm details if we used custom parameter */ 597 /* Set SignerInfo algorithm details if we used custom parameter */
598 if (si->pctx && !cms_sd_asn1_ctrl(si, 0)) 598 if (si->pctx && !cms_sd_asn1_ctrl(si, 0))
599 goto err; 599 goto err;
600 600
601 /* 601 /*
602 * If any signed attributes calculate and add messageDigest attribute 602 * If any signed attributes calculate and add messageDigest attribute
603 */ 603 */
604 604
605 if (CMS_signed_get_attr_count(si) >= 0) { 605 if (CMS_signed_get_attr_count(si) >= 0) {
606 ASN1_OBJECT *ctype = 606 ASN1_OBJECT *ctype =
607 cms->d.signedData->encapContentInfo->eContentType; 607 cms->d.signedData->encapContentInfo->eContentType;
608 unsigned char md[EVP_MAX_MD_SIZE]; 608 unsigned char md[EVP_MAX_MD_SIZE];
609 unsigned int mdlen; 609 unsigned int mdlen;
610 if (!EVP_DigestFinal_ex(mctx, md, &mdlen)) 610 if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
611 goto err; 611 goto err;
612 if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest, 612 if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
613 V_ASN1_OCTET_STRING, md, mdlen)) 613 V_ASN1_OCTET_STRING, md, mdlen))
614 goto err; 614 goto err;
615 /* Copy content type across */ 615 /* Copy content type across */
616 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType, 616 if (CMS_signed_add1_attr_by_NID(si, NID_pkcs9_contentType,
617 V_ASN1_OBJECT, ctype, -1) <= 0) 617 V_ASN1_OBJECT, ctype, -1) <= 0)
618 goto err; 618 goto err;
619 if (!CMS_SignerInfo_sign(si)) 619 if (!CMS_SignerInfo_sign(si))
620 goto err; 620 goto err;
621 } else if (si->pctx) { 621 } else if (si->pctx) {
622 unsigned char *sig; 622 unsigned char *sig;
623 size_t siglen; 623 size_t siglen;
624 unsigned char md[EVP_MAX_MD_SIZE]; 624 unsigned char md[EVP_MAX_MD_SIZE];
625 unsigned int mdlen; 625 unsigned int mdlen;
626 pctx = si->pctx; 626 pctx = si->pctx;
627 if (!EVP_DigestFinal_ex(mctx, md, &mdlen)) 627 if (!EVP_DigestFinal_ex(mctx, md, &mdlen))
628 goto err; 628 goto err;
629 siglen = EVP_PKEY_size(si->pkey); 629 siglen = EVP_PKEY_size(si->pkey);
630 sig = OPENSSL_malloc(siglen); 630 sig = OPENSSL_malloc(siglen);
631 if (sig == NULL) { 631 if (sig == NULL) {
632 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE); 632 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
633 goto err; 633 goto err;
634 } 634 }
635 if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) { 635 if (EVP_PKEY_sign(pctx, sig, &siglen, md, mdlen) <= 0) {
636 OPENSSL_free(sig); 636 OPENSSL_free(sig);
637 goto err; 637 goto err;
638 } 638 }
639 ASN1_STRING_set0(si->signature, sig, siglen); 639 ASN1_STRING_set0(si->signature, sig, siglen);
640 } else { 640 } else {
641 unsigned char *sig; 641 unsigned char *sig;
642 unsigned int siglen; 642 unsigned int siglen;
643 sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey)); 643 sig = OPENSSL_malloc(EVP_PKEY_size(si->pkey));
644 if (sig == NULL) { 644 if (sig == NULL) {
645 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE); 645 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, ERR_R_MALLOC_FAILURE);
646 goto err; 646 goto err;
647 } 647 }
648 if (!EVP_SignFinal(mctx, sig, &siglen, si->pkey)) { 648 if (!EVP_SignFinal(mctx, sig, &siglen, si->pkey)) {
649 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_SIGNFINAL_ERROR); 649 CMSerr(CMS_F_CMS_SIGNERINFO_CONTENT_SIGN, CMS_R_SIGNFINAL_ERROR);
650 OPENSSL_free(sig); 650 OPENSSL_free(sig);
651 goto err; 651 goto err;
652 } 652 }
653 ASN1_STRING_set0(si->signature, sig, siglen); 653 ASN1_STRING_set0(si->signature, sig, siglen);
654 } 654 }
655 655
656 r = 1; 656 r = 1;
657 657
658 err: 658 err:
659 EVP_MD_CTX_free(mctx); 659 EVP_MD_CTX_free(mctx);
660 EVP_PKEY_CTX_free(pctx); 660 EVP_PKEY_CTX_free(pctx);
661 return r; 661 return r;
662 662
663} 663}
664 664
665int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain) 665int cms_SignedData_final(CMS_ContentInfo *cms, BIO *chain)
666{ 666{
667 STACK_OF(CMS_SignerInfo) *sinfos; 667 STACK_OF(CMS_SignerInfo) *sinfos;
668 CMS_SignerInfo *si; 668 CMS_SignerInfo *si;
669 int i; 669 int i;
670 sinfos = CMS_get0_SignerInfos(cms); 670 sinfos = CMS_get0_SignerInfos(cms);
671 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) { 671 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
672 si = sk_CMS_SignerInfo_value(sinfos, i); 672 si = sk_CMS_SignerInfo_value(sinfos, i);
673 if (!cms_SignerInfo_content_sign(cms, si, chain)) 673 if (!cms_SignerInfo_content_sign(cms, si, chain))
674 return 0; 674 return 0;
675 } 675 }
676 cms->d.signedData->encapContentInfo->partial = 0; 676 cms->d.signedData->encapContentInfo->partial = 0;
677 return 1; 677 return 1;
678} 678}
679 679
680int CMS_SignerInfo_sign(CMS_SignerInfo *si) 680int CMS_SignerInfo_sign(CMS_SignerInfo *si)
681{ 681{
682 EVP_MD_CTX *mctx = si->mctx; 682 EVP_MD_CTX *mctx = si->mctx;
683 EVP_PKEY_CTX *pctx = NULL; 683 EVP_PKEY_CTX *pctx = NULL;
684 unsigned char *abuf = NULL; 684 unsigned char *abuf = NULL;
685 int alen; 685 int alen;
686 size_t siglen; 686 size_t siglen;
687 const EVP_MD *md = NULL; 687 const EVP_MD *md = NULL;
688 688
689 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm); 689 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
690 if (md == NULL) 690 if (md == NULL)
691 return 0; 691 return 0;
692 692
693 if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) { 693 if (CMS_signed_get_attr_by_NID(si, NID_pkcs9_signingTime, -1) < 0) {
694 if (!cms_add1_signingTime(si, NULL)) 694 if (!cms_add1_signingTime(si, NULL))
695 goto err; 695 goto err;
696 } 696 }
697 697
698 if (si->pctx) 698 if (si->pctx)
699 pctx = si->pctx; 699 pctx = si->pctx;
700 else { 700 else {
701 EVP_MD_CTX_reset(mctx); 701 EVP_MD_CTX_reset(mctx);
702 if (EVP_DigestSignInit(mctx, &pctx, md, NULL, si->pkey) <= 0) 702 if (EVP_DigestSignInit(mctx, &pctx, md, NULL, si->pkey) <= 0)
703 goto err; 703 goto err;
704 si->pctx = pctx; 704 si->pctx = pctx;
705 } 705 }
706 706
707 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, 707 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
708 EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0) { 708 EVP_PKEY_CTRL_CMS_SIGN, 0, si) <= 0) {
709 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR); 709 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
710 goto err; 710 goto err;
711 } 711 }
712 712
713 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf, 713 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
714 ASN1_ITEM_rptr(CMS_Attributes_Sign)); 714 ASN1_ITEM_rptr(CMS_Attributes_Sign));
715 if (!abuf) 715 if (!abuf)
716 goto err; 716 goto err;
717 if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0) 717 if (EVP_DigestSignUpdate(mctx, abuf, alen) <= 0)
718 goto err; 718 goto err;
719 if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0) 719 if (EVP_DigestSignFinal(mctx, NULL, &siglen) <= 0)
720 goto err; 720 goto err;
721 OPENSSL_free(abuf); 721 OPENSSL_free(abuf);
722 abuf = OPENSSL_malloc(siglen); 722 abuf = OPENSSL_malloc(siglen);
723 if (abuf == NULL) 723 if (abuf == NULL)
724 goto err; 724 goto err;
725 if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0) 725 if (EVP_DigestSignFinal(mctx, abuf, &siglen) <= 0)
726 goto err; 726 goto err;
727 727
728 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN, 728 if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_SIGN,
729 EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0) { 729 EVP_PKEY_CTRL_CMS_SIGN, 1, si) <= 0) {
730 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR); 730 CMSerr(CMS_F_CMS_SIGNERINFO_SIGN, CMS_R_CTRL_ERROR);
731 goto err; 731 goto err;
732 } 732 }
733 733
734 EVP_MD_CTX_reset(mctx); 734 EVP_MD_CTX_reset(mctx);
735 735
736 ASN1_STRING_set0(si->signature, abuf, siglen); 736 ASN1_STRING_set0(si->signature, abuf, siglen);
737 737
738 return 1; 738 return 1;
739 739
740 err: 740 err:
741 OPENSSL_free(abuf); 741 OPENSSL_free(abuf);
742 EVP_MD_CTX_reset(mctx); 742 EVP_MD_CTX_reset(mctx);
743 return 0; 743 return 0;
744 744
745} 745}
746 746
747int CMS_SignerInfo_verify(CMS_SignerInfo *si) 747int CMS_SignerInfo_verify(CMS_SignerInfo *si)
748{ 748{
749 EVP_MD_CTX *mctx = NULL; 749 EVP_MD_CTX *mctx = NULL;
750 unsigned char *abuf = NULL; 750 unsigned char *abuf = NULL;
751 int alen, r = -1; 751 int alen, r = -1;
752 const EVP_MD *md = NULL; 752 const EVP_MD *md = NULL;
753 753
754 if (!si->pkey) { 754 if (!si->pkey) {
755 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY); 755 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_NO_PUBLIC_KEY);
756 return -1; 756 return -1;
757 } 757 }
758 758
759 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm); 759 md = EVP_get_digestbyobj(si->digestAlgorithm->algorithm);
760 if (md == NULL) 760 if (md == NULL)
761 return -1; 761 return -1;
762 if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) { 762 if (si->mctx == NULL && (si->mctx = EVP_MD_CTX_new()) == NULL) {
763 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, ERR_R_MALLOC_FAILURE); 763 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, ERR_R_MALLOC_FAILURE);
764 return -1; 764 return -1;
765 } 765 }
766 mctx = si->mctx; 766 mctx = si->mctx;
767 if (EVP_DigestVerifyInit(mctx, &si->pctx, md, NULL, si->pkey) <= 0) 767 if (EVP_DigestVerifyInit(mctx, &si->pctx, md, NULL, si->pkey) <= 0)
768 goto err; 768 goto err;
769 769
770 if (!cms_sd_asn1_ctrl(si, 1)) 770 if (!cms_sd_asn1_ctrl(si, 1))
771 goto err; 771 goto err;
772 772
773 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf, 773 alen = ASN1_item_i2d((ASN1_VALUE *)si->signedAttrs, &abuf,
774 ASN1_ITEM_rptr(CMS_Attributes_Verify)); 774 ASN1_ITEM_rptr(CMS_Attributes_Verify));
775 if (!abuf) 775 if (!abuf)
776 goto err; 776 goto err;
777 r = EVP_DigestVerifyUpdate(mctx, abuf, alen); 777 r = EVP_DigestVerifyUpdate(mctx, abuf, alen);
778 OPENSSL_free(abuf); 778 OPENSSL_free(abuf);
779 if (r <= 0) { 779 if (r <= 0) {
780 r = -1; 780 r = -1;
781 goto err; 781 goto err;
782 } 782 }
783 r = EVP_DigestVerifyFinal(mctx, 783 r = EVP_DigestVerifyFinal(mctx,
784 si->signature->data, si->signature->length); 784 si->signature->data, si->signature->length);
785 if (r <= 0) 785 if (r <= 0)
786 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE); 786 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY, CMS_R_VERIFICATION_FAILURE);
787 err: 787 err:
788 EVP_MD_CTX_reset(mctx); 788 EVP_MD_CTX_reset(mctx);
789 return r; 789 return r;
790} 790}
791 791
792/* Create a chain of digest BIOs from a CMS ContentInfo */ 792/* Create a chain of digest BIOs from a CMS ContentInfo */
793 793
794BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms) 794BIO *cms_SignedData_init_bio(CMS_ContentInfo *cms)
795{ 795{
796 int i; 796 int i;
797 CMS_SignedData *sd; 797 CMS_SignedData *sd;
798 BIO *chain = NULL; 798 BIO *chain = NULL;
799 sd = cms_get0_signed(cms); 799 sd = cms_get0_signed(cms);
800 if (!sd) 800 if (!sd)
801 return NULL; 801 return NULL;
802 if (cms->d.signedData->encapContentInfo->partial) 802 if (cms->d.signedData->encapContentInfo->partial)
803 cms_sd_set_version(sd); 803 cms_sd_set_version(sd);
804 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) { 804 for (i = 0; i < sk_X509_ALGOR_num(sd->digestAlgorithms); i++) {
805 X509_ALGOR *digestAlgorithm; 805 X509_ALGOR *digestAlgorithm;
806 BIO *mdbio; 806 BIO *mdbio;
807 digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i); 807 digestAlgorithm = sk_X509_ALGOR_value(sd->digestAlgorithms, i);
808 mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm); 808 mdbio = cms_DigestAlgorithm_init_bio(digestAlgorithm);
809 if (!mdbio) 809 if (!mdbio)
810 goto err; 810 goto err;
811 if (chain) 811 if (chain)
812 BIO_push(chain, mdbio); 812 BIO_push(chain, mdbio);
813 else 813 else
814 chain = mdbio; 814 chain = mdbio;
815 } 815 }
816 return chain; 816 return chain;
817 err: 817 err:
818 BIO_free_all(chain); 818 BIO_free_all(chain);
819 return NULL; 819 return NULL;
820} 820}
821 821
822int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain) 822int CMS_SignerInfo_verify_content(CMS_SignerInfo *si, BIO *chain)
823{ 823{
824 ASN1_OCTET_STRING *os = NULL; 824 ASN1_OCTET_STRING *os = NULL;
825 EVP_MD_CTX *mctx = EVP_MD_CTX_new(); 825 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
826 EVP_PKEY_CTX *pkctx = NULL; 826 EVP_PKEY_CTX *pkctx = NULL;
827 int r = -1; 827 int r = -1;
828 unsigned char mval[EVP_MAX_MD_SIZE]; 828 unsigned char mval[EVP_MAX_MD_SIZE];
829 unsigned int mlen; 829 unsigned int mlen;
830 830
831 if (mctx == NULL) { 831 if (mctx == NULL) {
832 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE); 832 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, ERR_R_MALLOC_FAILURE);
833 goto err; 833 goto err;
834 } 834 }
835 /* If we have any signed attributes look for messageDigest value */ 835 /* If we have any signed attributes look for messageDigest value */
836 if (CMS_signed_get_attr_count(si) >= 0) { 836 if (CMS_signed_get_attr_count(si) >= 0) {
837 os = CMS_signed_get0_data_by_OBJ(si, 837 os = CMS_signed_get0_data_by_OBJ(si,
838 OBJ_nid2obj(NID_pkcs9_messageDigest), 838 OBJ_nid2obj(NID_pkcs9_messageDigest),
839 -3, V_ASN1_OCTET_STRING); 839 -3, V_ASN1_OCTET_STRING);
840 if (!os) { 840 if (!os) {
841 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, 841 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
842 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE); 842 CMS_R_ERROR_READING_MESSAGEDIGEST_ATTRIBUTE);
843 goto err; 843 goto err;
844 } 844 }
845 } 845 }
846 846
847 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm)) 847 if (!cms_DigestAlgorithm_find_ctx(mctx, chain, si->digestAlgorithm))
848 goto err; 848 goto err;
849 849
850 if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) { 850 if (EVP_DigestFinal_ex(mctx, mval, &mlen) <= 0) {
851 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, 851 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
852 CMS_R_UNABLE_TO_FINALIZE_CONTEXT); 852 CMS_R_UNABLE_TO_FINALIZE_CONTEXT);
853 goto err; 853 goto err;
854 } 854 }
855 855
856 /* If messageDigest found compare it */ 856 /* If messageDigest found compare it */
857 857
858 if (os) { 858 if (os) {
859 if (mlen != (unsigned int)os->length) { 859 if (mlen != (unsigned int)os->length) {
860 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, 860 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
861 CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH); 861 CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH);
862 goto err; 862 goto err;
863 } 863 }
864 864
865 if (memcmp(mval, os->data, mlen)) { 865 if (memcmp(mval, os->data, mlen)) {
866 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, 866 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
867 CMS_R_VERIFICATION_FAILURE); 867 CMS_R_VERIFICATION_FAILURE);
868 r = 0; 868 r = 0;
869 } else 869 } else
870 r = 1; 870 r = 1;
871 } else { 871 } else {
872 const EVP_MD *md = EVP_MD_CTX_md(mctx); 872 const EVP_MD *md = EVP_MD_CTX_md(mctx);
873 pkctx = EVP_PKEY_CTX_new(si->pkey, NULL); 873 pkctx = EVP_PKEY_CTX_new(si->pkey, NULL);
874 if (pkctx == NULL) 874 if (pkctx == NULL)
875 goto err; 875 goto err;
876 if (EVP_PKEY_verify_init(pkctx) <= 0) 876 if (EVP_PKEY_verify_init(pkctx) <= 0)
877 goto err; 877 goto err;
878 if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0) 878 if (EVP_PKEY_CTX_set_signature_md(pkctx, md) <= 0)
879 goto err; 879 goto err;
880 si->pctx = pkctx; 880 si->pctx = pkctx;
881 if (!cms_sd_asn1_ctrl(si, 1)) 881 if (!cms_sd_asn1_ctrl(si, 1))
882 goto err; 882 goto err;
883 r = EVP_PKEY_verify(pkctx, si->signature->data, 883 r = EVP_PKEY_verify(pkctx, si->signature->data,
884 si->signature->length, mval, mlen); 884 si->signature->length, mval, mlen);
885 if (r <= 0) { 885 if (r <= 0) {
886 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT, 886 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CONTENT,
887 CMS_R_VERIFICATION_FAILURE); 887 CMS_R_VERIFICATION_FAILURE);
888 r = 0; 888 r = 0;
889 } 889 }
890 } 890 }
891 891
892 err: 892 err:
893 EVP_PKEY_CTX_free(pkctx); 893 EVP_PKEY_CTX_free(pkctx);
894 EVP_MD_CTX_free(mctx); 894 EVP_MD_CTX_free(mctx);
895 return r; 895 return r;
896 896
897} 897}
898 898
899int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs) 899int CMS_add_smimecap(CMS_SignerInfo *si, STACK_OF(X509_ALGOR) *algs)
900{ 900{
901 unsigned char *smder = NULL; 901 unsigned char *smder = NULL;
902 int smderlen, r; 902 int smderlen, r;
903 smderlen = i2d_X509_ALGORS(algs, &smder); 903 smderlen = i2d_X509_ALGORS(algs, &smder);
904 if (smderlen <= 0) 904 if (smderlen <= 0)
905 return 0; 905 return 0;
906 r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities, 906 r = CMS_signed_add1_attr_by_NID(si, NID_SMIMECapabilities,
907 V_ASN1_SEQUENCE, smder, smderlen); 907 V_ASN1_SEQUENCE, smder, smderlen);
908 OPENSSL_free(smder); 908 OPENSSL_free(smder);
909 return r; 909 return r;
910} 910}
911 911
912int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs, 912int CMS_add_simple_smimecap(STACK_OF(X509_ALGOR) **algs,
913 int algnid, int keysize) 913 int algnid, int keysize)
914{ 914{
915 X509_ALGOR *alg; 915 X509_ALGOR *alg;
916 ASN1_INTEGER *key = NULL; 916 ASN1_INTEGER *key = NULL;
917 if (keysize > 0) { 917 if (keysize > 0) {
918 key = ASN1_INTEGER_new(); 918 key = ASN1_INTEGER_new();
919 if (key == NULL || !ASN1_INTEGER_set(key, keysize)) 919 if (key == NULL || !ASN1_INTEGER_set(key, keysize))
920 return 0; 920 return 0;
921 } 921 }
922 alg = X509_ALGOR_new(); 922 alg = X509_ALGOR_new();
923 if (alg == NULL) { 923 if (alg == NULL) {
924 ASN1_INTEGER_free(key); 924 ASN1_INTEGER_free(key);
925 return 0; 925 return 0;
926 } 926 }
927 927
928 X509_ALGOR_set0(alg, OBJ_nid2obj(algnid), 928 X509_ALGOR_set0(alg, OBJ_nid2obj(algnid),
929 key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key); 929 key ? V_ASN1_INTEGER : V_ASN1_UNDEF, key);
930 if (*algs == NULL) 930 if (*algs == NULL)
931 *algs = sk_X509_ALGOR_new_null(); 931 *algs = sk_X509_ALGOR_new_null();
932 if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) { 932 if (*algs == NULL || !sk_X509_ALGOR_push(*algs, alg)) {
933 X509_ALGOR_free(alg); 933 X509_ALGOR_free(alg);
934 return 0; 934 return 0;
935 } 935 }
936 return 1; 936 return 1;
937} 937}
938 938
939/* Check to see if a cipher exists and if so add S/MIME capabilities */ 939/* Check to see if a cipher exists and if so add S/MIME capabilities */
940 940
941static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg) 941static int cms_add_cipher_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
942{ 942{
943 if (EVP_get_cipherbynid(nid)) 943 if (EVP_get_cipherbynid(nid))
944 return CMS_add_simple_smimecap(sk, nid, arg); 944 return CMS_add_simple_smimecap(sk, nid, arg);
945 return 1; 945 return 1;
946} 946}
947 947
948static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg) 948static int cms_add_digest_smcap(STACK_OF(X509_ALGOR) **sk, int nid, int arg)
949{ 949{
950 if (EVP_get_digestbynid(nid)) 950 if (EVP_get_digestbynid(nid))
951 return CMS_add_simple_smimecap(sk, nid, arg); 951 return CMS_add_simple_smimecap(sk, nid, arg);
952 return 1; 952 return 1;
953} 953}
954 954
955int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap) 955int CMS_add_standard_smimecap(STACK_OF(X509_ALGOR) **smcap)
956{ 956{
957 if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1) 957 if (!cms_add_cipher_smcap(smcap, NID_aes_256_cbc, -1)
958 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1) 958 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_256, -1)
959 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1) 959 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_2012_512, -1)
960 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1) 960 || !cms_add_digest_smcap(smcap, NID_id_GostR3411_94, -1)
961 || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1) 961 || !cms_add_cipher_smcap(smcap, NID_id_Gost28147_89, -1)
962 || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1) 962 || !cms_add_cipher_smcap(smcap, NID_aes_192_cbc, -1)
963 || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1) 963 || !cms_add_cipher_smcap(smcap, NID_aes_128_cbc, -1)
964 || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1) 964 || !cms_add_cipher_smcap(smcap, NID_des_ede3_cbc, -1)
965 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128) 965 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 128)
966 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64) 966 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 64)
967 || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1) 967 || !cms_add_cipher_smcap(smcap, NID_des_cbc, -1)
968 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40)) 968 || !cms_add_cipher_smcap(smcap, NID_rc2_cbc, 40))
969 return 0; 969 return 0;
970 return 1; 970 return 1;
971} 971}
diff --git a/src/lib/libcrypto/cms/cms_smime.c b/src/lib/libcrypto/cms/cms_smime.c
index fcb263da64..f3dcd05ee1 100644
--- a/src/lib/libcrypto/cms/cms_smime.c
+++ b/src/lib/libcrypto/cms/cms_smime.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: cms_smime.c,v 1.18 2019/08/10 16:39:17 jsing Exp $ */ 1/* $OpenBSD: cms_smime.c,v 1.19 2019/08/10 16:42:20 jsing Exp $ */
2/* 2/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
4 * project. 4 * project.
@@ -63,826 +63,826 @@
63 63
64static BIO *cms_get_text_bio(BIO *out, unsigned int flags) 64static BIO *cms_get_text_bio(BIO *out, unsigned int flags)
65{ 65{
66 BIO *rbio; 66 BIO *rbio;
67 if (out == NULL) 67 if (out == NULL)
68 rbio = BIO_new(BIO_s_null()); 68 rbio = BIO_new(BIO_s_null());
69 else if (flags & CMS_TEXT) { 69 else if (flags & CMS_TEXT) {
70 rbio = BIO_new(BIO_s_mem()); 70 rbio = BIO_new(BIO_s_mem());
71 BIO_set_mem_eof_return(rbio, 0); 71 BIO_set_mem_eof_return(rbio, 0);
72 } else 72 } else
73 rbio = out; 73 rbio = out;
74 return rbio; 74 return rbio;
75} 75}
76 76
77static int cms_copy_content(BIO *out, BIO *in, unsigned int flags) 77static int cms_copy_content(BIO *out, BIO *in, unsigned int flags)
78{ 78{
79 unsigned char buf[4096]; 79 unsigned char buf[4096];
80 int r = 0, i; 80 int r = 0, i;
81 BIO *tmpout; 81 BIO *tmpout;
82 82
83 tmpout = cms_get_text_bio(out, flags); 83 tmpout = cms_get_text_bio(out, flags);
84 84
85 if (tmpout == NULL) { 85 if (tmpout == NULL) {
86 CMSerr(CMS_F_CMS_COPY_CONTENT, ERR_R_MALLOC_FAILURE); 86 CMSerr(CMS_F_CMS_COPY_CONTENT, ERR_R_MALLOC_FAILURE);
87 goto err; 87 goto err;
88 } 88 }
89 89
90 /* Read all content through chain to process digest, decrypt etc */ 90 /* Read all content through chain to process digest, decrypt etc */
91 for (;;) { 91 for (;;) {
92 i = BIO_read(in, buf, sizeof(buf)); 92 i = BIO_read(in, buf, sizeof(buf));
93 if (i <= 0) { 93 if (i <= 0) {
94 if (BIO_method_type(in) == BIO_TYPE_CIPHER) { 94 if (BIO_method_type(in) == BIO_TYPE_CIPHER) {
95 if (!BIO_get_cipher_status(in)) 95 if (!BIO_get_cipher_status(in))
96 goto err; 96 goto err;
97 } 97 }
98 if (i < 0) 98 if (i < 0)
99 goto err; 99 goto err;
100 break; 100 break;
101 } 101 }
102 102
103 if (tmpout && (BIO_write(tmpout, buf, i) != i)) 103 if (tmpout && (BIO_write(tmpout, buf, i) != i))
104 goto err; 104 goto err;
105 } 105 }
106 106
107 if (flags & CMS_TEXT) { 107 if (flags & CMS_TEXT) {
108 if (!SMIME_text(tmpout, out)) { 108 if (!SMIME_text(tmpout, out)) {
109 CMSerr(CMS_F_CMS_COPY_CONTENT, CMS_R_SMIME_TEXT_ERROR); 109 CMSerr(CMS_F_CMS_COPY_CONTENT, CMS_R_SMIME_TEXT_ERROR);
110 goto err; 110 goto err;
111 } 111 }
112 } 112 }
113 113
114 r = 1; 114 r = 1;
115 115
116 err: 116 err:
117 if (tmpout != out) 117 if (tmpout != out)
118 BIO_free(tmpout); 118 BIO_free(tmpout);
119 return r; 119 return r;
120 120
121} 121}
122 122
123static int check_content(CMS_ContentInfo *cms) 123static int check_content(CMS_ContentInfo *cms)
124{ 124{
125 ASN1_OCTET_STRING **pos = CMS_get0_content(cms); 125 ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
126 if (!pos || !*pos) { 126 if (!pos || !*pos) {
127 CMSerr(CMS_F_CHECK_CONTENT, CMS_R_NO_CONTENT); 127 CMSerr(CMS_F_CHECK_CONTENT, CMS_R_NO_CONTENT);
128 return 0; 128 return 0;
129 } 129 }
130 return 1; 130 return 1;
131} 131}
132 132
133static void do_free_upto(BIO *f, BIO *upto) 133static void do_free_upto(BIO *f, BIO *upto)
134{ 134{
135 if (upto) { 135 if (upto) {
136 BIO *tbio; 136 BIO *tbio;
137 do { 137 do {
138 tbio = BIO_pop(f); 138 tbio = BIO_pop(f);
139 BIO_free(f); 139 BIO_free(f);
140 f = tbio; 140 f = tbio;
141 } 141 }
142 while (f && f != upto); 142 while (f && f != upto);
143 } else 143 } else
144 BIO_free_all(f); 144 BIO_free_all(f);
145} 145}
146 146
147int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags) 147int CMS_data(CMS_ContentInfo *cms, BIO *out, unsigned int flags)
148{ 148{
149 BIO *cont; 149 BIO *cont;
150 int r; 150 int r;
151 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) { 151 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_data) {
152 CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA); 152 CMSerr(CMS_F_CMS_DATA, CMS_R_TYPE_NOT_DATA);
153 return 0; 153 return 0;
154 } 154 }
155 cont = CMS_dataInit(cms, NULL); 155 cont = CMS_dataInit(cms, NULL);
156 if (!cont) 156 if (!cont)
157 return 0; 157 return 0;
158 r = cms_copy_content(out, cont, flags); 158 r = cms_copy_content(out, cont, flags);
159 BIO_free_all(cont); 159 BIO_free_all(cont);
160 return r; 160 return r;
161} 161}
162 162
163CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags) 163CMS_ContentInfo *CMS_data_create(BIO *in, unsigned int flags)
164{ 164{
165 CMS_ContentInfo *cms; 165 CMS_ContentInfo *cms;
166 cms = cms_Data_create(); 166 cms = cms_Data_create();
167 if (!cms) 167 if (!cms)
168 return NULL; 168 return NULL;
169 169
170 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags)) 170 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
171 return cms; 171 return cms;
172 172
173 CMS_ContentInfo_free(cms); 173 CMS_ContentInfo_free(cms);
174 174
175 return NULL; 175 return NULL;
176} 176}
177 177
178int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out, 178int CMS_digest_verify(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
179 unsigned int flags) 179 unsigned int flags)
180{ 180{
181 BIO *cont; 181 BIO *cont;
182 int r; 182 int r;
183 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) { 183 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_digest) {
184 CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA); 184 CMSerr(CMS_F_CMS_DIGEST_VERIFY, CMS_R_TYPE_NOT_DIGESTED_DATA);
185 return 0; 185 return 0;
186 } 186 }
187 187
188 if (!dcont && !check_content(cms)) 188 if (!dcont && !check_content(cms))
189 return 0; 189 return 0;
190 190
191 cont = CMS_dataInit(cms, dcont); 191 cont = CMS_dataInit(cms, dcont);
192 if (!cont) 192 if (!cont)
193 return 0; 193 return 0;
194 r = cms_copy_content(out, cont, flags); 194 r = cms_copy_content(out, cont, flags);
195 if (r) 195 if (r)
196 r = cms_DigestedData_do_final(cms, cont, 1); 196 r = cms_DigestedData_do_final(cms, cont, 1);
197 do_free_upto(cont, dcont); 197 do_free_upto(cont, dcont);
198 return r; 198 return r;
199} 199}
200 200
201CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md, 201CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
202 unsigned int flags) 202 unsigned int flags)
203{ 203{
204 CMS_ContentInfo *cms; 204 CMS_ContentInfo *cms;
205 if (!md) 205 if (!md)
206 md = EVP_sha1(); 206 md = EVP_sha1();
207 cms = cms_DigestedData_create(md); 207 cms = cms_DigestedData_create(md);
208 if (!cms) 208 if (!cms)
209 return NULL; 209 return NULL;
210 210
211 if (!(flags & CMS_DETACHED)) 211 if (!(flags & CMS_DETACHED))
212 CMS_set_detached(cms, 0); 212 CMS_set_detached(cms, 0);
213 213
214 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags)) 214 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
215 return cms; 215 return cms;
216 216
217 CMS_ContentInfo_free(cms); 217 CMS_ContentInfo_free(cms);
218 return NULL; 218 return NULL;
219} 219}
220 220
221int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms, 221int CMS_EncryptedData_decrypt(CMS_ContentInfo *cms,
222 const unsigned char *key, size_t keylen, 222 const unsigned char *key, size_t keylen,
223 BIO *dcont, BIO *out, unsigned int flags) 223 BIO *dcont, BIO *out, unsigned int flags)
224{ 224{
225 BIO *cont; 225 BIO *cont;
226 int r; 226 int r;
227 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) { 227 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_encrypted) {
228 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT, 228 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_DECRYPT,
229 CMS_R_TYPE_NOT_ENCRYPTED_DATA); 229 CMS_R_TYPE_NOT_ENCRYPTED_DATA);
230 return 0; 230 return 0;
231 } 231 }
232 232
233 if (!dcont && !check_content(cms)) 233 if (!dcont && !check_content(cms))
234 return 0; 234 return 0;
235 235
236 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0) 236 if (CMS_EncryptedData_set1_key(cms, NULL, key, keylen) <= 0)
237 return 0; 237 return 0;
238 cont = CMS_dataInit(cms, dcont); 238 cont = CMS_dataInit(cms, dcont);
239 if (!cont) 239 if (!cont)
240 return 0; 240 return 0;
241 r = cms_copy_content(out, cont, flags); 241 r = cms_copy_content(out, cont, flags);
242 do_free_upto(cont, dcont); 242 do_free_upto(cont, dcont);
243 return r; 243 return r;
244} 244}
245 245
246CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher, 246CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in, const EVP_CIPHER *cipher,
247 const unsigned char *key, 247 const unsigned char *key,
248 size_t keylen, unsigned int flags) 248 size_t keylen, unsigned int flags)
249{ 249{
250 CMS_ContentInfo *cms; 250 CMS_ContentInfo *cms;
251 if (!cipher) { 251 if (!cipher) {
252 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER); 252 CMSerr(CMS_F_CMS_ENCRYPTEDDATA_ENCRYPT, CMS_R_NO_CIPHER);
253 return NULL; 253 return NULL;
254 } 254 }
255 cms = CMS_ContentInfo_new(); 255 cms = CMS_ContentInfo_new();
256 if (cms == NULL) 256 if (cms == NULL)
257 return NULL; 257 return NULL;
258 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen)) 258 if (!CMS_EncryptedData_set1_key(cms, cipher, key, keylen))
259 return NULL; 259 return NULL;
260 260
261 if (!(flags & CMS_DETACHED)) 261 if (!(flags & CMS_DETACHED))
262 CMS_set_detached(cms, 0); 262 CMS_set_detached(cms, 0);
263 263
264 if ((flags & (CMS_STREAM | CMS_PARTIAL)) 264 if ((flags & (CMS_STREAM | CMS_PARTIAL))
265 || CMS_final(cms, in, NULL, flags)) 265 || CMS_final(cms, in, NULL, flags))
266 return cms; 266 return cms;
267 267
268 CMS_ContentInfo_free(cms); 268 CMS_ContentInfo_free(cms);
269 return NULL; 269 return NULL;
270} 270}
271 271
272static int cms_signerinfo_verify_cert(CMS_SignerInfo *si, 272static int cms_signerinfo_verify_cert(CMS_SignerInfo *si,
273 X509_STORE *store, 273 X509_STORE *store,
274 STACK_OF(X509) *certs, 274 STACK_OF(X509) *certs,
275 STACK_OF(X509_CRL) *crls) 275 STACK_OF(X509_CRL) *crls)
276{ 276{
277 X509_STORE_CTX *ctx = X509_STORE_CTX_new(); 277 X509_STORE_CTX *ctx = X509_STORE_CTX_new();
278 X509 *signer; 278 X509 *signer;
279 int i, j, r = 0; 279 int i, j, r = 0;
280 280
281 if (ctx == NULL) { 281 if (ctx == NULL) {
282 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, ERR_R_MALLOC_FAILURE); 282 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
283 goto err; 283 goto err;
284 } 284 }
285 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL); 285 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
286 if (!X509_STORE_CTX_init(ctx, store, signer, certs)) { 286 if (!X509_STORE_CTX_init(ctx, store, signer, certs)) {
287 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, CMS_R_STORE_INIT_ERROR); 287 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, CMS_R_STORE_INIT_ERROR);
288 goto err; 288 goto err;
289 } 289 }
290 X509_STORE_CTX_set_default(ctx, "smime_sign"); 290 X509_STORE_CTX_set_default(ctx, "smime_sign");
291 if (crls) 291 if (crls)
292 X509_STORE_CTX_set0_crls(ctx, crls); 292 X509_STORE_CTX_set0_crls(ctx, crls);
293 293
294 i = X509_verify_cert(ctx); 294 i = X509_verify_cert(ctx);
295 if (i <= 0) { 295 if (i <= 0) {
296 j = X509_STORE_CTX_get_error(ctx); 296 j = X509_STORE_CTX_get_error(ctx);
297 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT, 297 CMSerr(CMS_F_CMS_SIGNERINFO_VERIFY_CERT,
298 CMS_R_CERTIFICATE_VERIFY_ERROR); 298 CMS_R_CERTIFICATE_VERIFY_ERROR);
299 ERR_add_error_data(2, "Verify error:", 299 ERR_add_error_data(2, "Verify error:",
300 X509_verify_cert_error_string(j)); 300 X509_verify_cert_error_string(j));
301 goto err; 301 goto err;
302 } 302 }
303 r = 1; 303 r = 1;
304 err: 304 err:
305 X509_STORE_CTX_free(ctx); 305 X509_STORE_CTX_free(ctx);
306 return r; 306 return r;
307 307
308} 308}
309 309
310int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs, 310int CMS_verify(CMS_ContentInfo *cms, STACK_OF(X509) *certs,
311 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags) 311 X509_STORE *store, BIO *dcont, BIO *out, unsigned int flags)
312{ 312{
313 CMS_SignerInfo *si; 313 CMS_SignerInfo *si;
314 STACK_OF(CMS_SignerInfo) *sinfos; 314 STACK_OF(CMS_SignerInfo) *sinfos;
315 STACK_OF(X509) *cms_certs = NULL; 315 STACK_OF(X509) *cms_certs = NULL;
316 STACK_OF(X509_CRL) *crls = NULL; 316 STACK_OF(X509_CRL) *crls = NULL;
317 X509 *signer; 317 X509 *signer;
318 int i, scount = 0, ret = 0; 318 int i, scount = 0, ret = 0;
319 BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL; 319 BIO *cmsbio = NULL, *tmpin = NULL, *tmpout = NULL;
320 320
321 if (!dcont && !check_content(cms)) 321 if (!dcont && !check_content(cms))
322 return 0; 322 return 0;
323 if (dcont && !(flags & CMS_BINARY)) { 323 if (dcont && !(flags & CMS_BINARY)) {
324 const ASN1_OBJECT *coid = CMS_get0_eContentType(cms); 324 const ASN1_OBJECT *coid = CMS_get0_eContentType(cms);
325 if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF) 325 if (OBJ_obj2nid(coid) == NID_id_ct_asciiTextWithCRLF)
326 flags |= CMS_ASCIICRLF; 326 flags |= CMS_ASCIICRLF;
327 } 327 }
328 328
329 /* Attempt to find all signer certificates */ 329 /* Attempt to find all signer certificates */
330 330
331 sinfos = CMS_get0_SignerInfos(cms); 331 sinfos = CMS_get0_SignerInfos(cms);
332 332
333 if (sk_CMS_SignerInfo_num(sinfos) <= 0) { 333 if (sk_CMS_SignerInfo_num(sinfos) <= 0) {
334 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS); 334 CMSerr(CMS_F_CMS_VERIFY, CMS_R_NO_SIGNERS);
335 goto err; 335 goto err;
336 } 336 }
337 337
338 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) { 338 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
339 si = sk_CMS_SignerInfo_value(sinfos, i); 339 si = sk_CMS_SignerInfo_value(sinfos, i);
340 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL); 340 CMS_SignerInfo_get0_algs(si, NULL, &signer, NULL, NULL);
341 if (signer) 341 if (signer)
342 scount++; 342 scount++;
343 } 343 }
344 344
345 if (scount != sk_CMS_SignerInfo_num(sinfos)) 345 if (scount != sk_CMS_SignerInfo_num(sinfos))
346 scount += CMS_set1_signers_certs(cms, certs, flags); 346 scount += CMS_set1_signers_certs(cms, certs, flags);
347 347
348 if (scount != sk_CMS_SignerInfo_num(sinfos)) { 348 if (scount != sk_CMS_SignerInfo_num(sinfos)) {
349 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND); 349 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SIGNER_CERTIFICATE_NOT_FOUND);
350 goto err; 350 goto err;
351 } 351 }
352 352
353 /* Attempt to verify all signers certs */ 353 /* Attempt to verify all signers certs */
354 354
355 if (!(flags & CMS_NO_SIGNER_CERT_VERIFY)) { 355 if (!(flags & CMS_NO_SIGNER_CERT_VERIFY)) {
356 cms_certs = CMS_get1_certs(cms); 356 cms_certs = CMS_get1_certs(cms);
357 if (!(flags & CMS_NOCRL)) 357 if (!(flags & CMS_NOCRL))
358 crls = CMS_get1_crls(cms); 358 crls = CMS_get1_crls(cms);
359 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) { 359 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
360 si = sk_CMS_SignerInfo_value(sinfos, i); 360 si = sk_CMS_SignerInfo_value(sinfos, i);
361 if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls)) 361 if (!cms_signerinfo_verify_cert(si, store, cms_certs, crls))
362 goto err; 362 goto err;
363 } 363 }
364 } 364 }
365 365
366 /* Attempt to verify all SignerInfo signed attribute signatures */ 366 /* Attempt to verify all SignerInfo signed attribute signatures */
367 367
368 if (!(flags & CMS_NO_ATTR_VERIFY)) { 368 if (!(flags & CMS_NO_ATTR_VERIFY)) {
369 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) { 369 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
370 si = sk_CMS_SignerInfo_value(sinfos, i); 370 si = sk_CMS_SignerInfo_value(sinfos, i);
371 if (CMS_signed_get_attr_count(si) < 0) 371 if (CMS_signed_get_attr_count(si) < 0)
372 continue; 372 continue;
373 if (CMS_SignerInfo_verify(si) <= 0) 373 if (CMS_SignerInfo_verify(si) <= 0)
374 goto err; 374 goto err;
375 } 375 }
376 } 376 }
377 377
378 /* 378 /*
379 * Performance optimization: if the content is a memory BIO then store 379 * Performance optimization: if the content is a memory BIO then store
380 * its contents in a temporary read only memory BIO. This avoids 380 * its contents in a temporary read only memory BIO. This avoids
381 * potentially large numbers of slow copies of data which will occur when 381 * potentially large numbers of slow copies of data which will occur when
382 * reading from a read write memory BIO when signatures are calculated. 382 * reading from a read write memory BIO when signatures are calculated.
383 */ 383 */
384 384
385 if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM)) { 385 if (dcont && (BIO_method_type(dcont) == BIO_TYPE_MEM)) {
386 char *ptr; 386 char *ptr;
387 long len; 387 long len;
388 len = BIO_get_mem_data(dcont, &ptr); 388 len = BIO_get_mem_data(dcont, &ptr);
389 tmpin = BIO_new_mem_buf(ptr, len); 389 tmpin = BIO_new_mem_buf(ptr, len);
390 if (tmpin == NULL) { 390 if (tmpin == NULL) {
391 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE); 391 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
392 goto err2; 392 goto err2;
393 } 393 }
394 } else 394 } else
395 tmpin = dcont; 395 tmpin = dcont;
396 /* 396 /*
397 * If not binary mode and detached generate digests by *writing* through 397 * If not binary mode and detached generate digests by *writing* through
398 * the BIO. That makes it possible to canonicalise the input. 398 * the BIO. That makes it possible to canonicalise the input.
399 */ 399 */
400 if (!(flags & SMIME_BINARY) && dcont) { 400 if (!(flags & SMIME_BINARY) && dcont) {
401 /* 401 /*
402 * Create output BIO so we can either handle text or to ensure 402 * Create output BIO so we can either handle text or to ensure
403 * included content doesn't override detached content. 403 * included content doesn't override detached content.
404 */ 404 */
405 tmpout = cms_get_text_bio(out, flags); 405 tmpout = cms_get_text_bio(out, flags);
406 if (!tmpout) { 406 if (!tmpout) {
407 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE); 407 CMSerr(CMS_F_CMS_VERIFY, ERR_R_MALLOC_FAILURE);
408 goto err; 408 goto err;
409 } 409 }
410 cmsbio = CMS_dataInit(cms, tmpout); 410 cmsbio = CMS_dataInit(cms, tmpout);
411 if (!cmsbio) 411 if (!cmsbio)
412 goto err; 412 goto err;
413 /* 413 /*
414 * Don't use SMIME_TEXT for verify: it adds headers and we want to 414 * Don't use SMIME_TEXT for verify: it adds headers and we want to
415 * remove them. 415 * remove them.
416 */ 416 */
417 SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT); 417 SMIME_crlf_copy(dcont, cmsbio, flags & ~SMIME_TEXT);
418 418
419 if (flags & CMS_TEXT) { 419 if (flags & CMS_TEXT) {
420 if (!SMIME_text(tmpout, out)) { 420 if (!SMIME_text(tmpout, out)) {
421 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SMIME_TEXT_ERROR); 421 CMSerr(CMS_F_CMS_VERIFY, CMS_R_SMIME_TEXT_ERROR);
422 goto err; 422 goto err;
423 } 423 }
424 } 424 }
425 } else { 425 } else {
426 cmsbio = CMS_dataInit(cms, tmpin); 426 cmsbio = CMS_dataInit(cms, tmpin);
427 if (!cmsbio) 427 if (!cmsbio)
428 goto err; 428 goto err;
429 429
430 if (!cms_copy_content(out, cmsbio, flags)) 430 if (!cms_copy_content(out, cmsbio, flags))
431 goto err; 431 goto err;
432 432
433 } 433 }
434 if (!(flags & CMS_NO_CONTENT_VERIFY)) { 434 if (!(flags & CMS_NO_CONTENT_VERIFY)) {
435 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) { 435 for (i = 0; i < sk_CMS_SignerInfo_num(sinfos); i++) {
436 si = sk_CMS_SignerInfo_value(sinfos, i); 436 si = sk_CMS_SignerInfo_value(sinfos, i);
437 if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) { 437 if (CMS_SignerInfo_verify_content(si, cmsbio) <= 0) {
438 CMSerr(CMS_F_CMS_VERIFY, CMS_R_CONTENT_VERIFY_ERROR); 438 CMSerr(CMS_F_CMS_VERIFY, CMS_R_CONTENT_VERIFY_ERROR);
439 goto err; 439 goto err;
440 } 440 }
441 } 441 }
442 } 442 }
443 443
444 ret = 1; 444 ret = 1;
445 445
446 err: 446 err:
447 if (!(flags & SMIME_BINARY) && dcont) { 447 if (!(flags & SMIME_BINARY) && dcont) {
448 do_free_upto(cmsbio, tmpout); 448 do_free_upto(cmsbio, tmpout);
449 if (tmpin != dcont) 449 if (tmpin != dcont)
450 BIO_free(tmpin); 450 BIO_free(tmpin);
451 } else { 451 } else {
452 if (dcont && (tmpin == dcont)) 452 if (dcont && (tmpin == dcont))
453 do_free_upto(cmsbio, dcont); 453 do_free_upto(cmsbio, dcont);
454 else 454 else
455 BIO_free_all(cmsbio); 455 BIO_free_all(cmsbio);
456 } 456 }
457 457
458 if (out != tmpout) 458 if (out != tmpout)
459 BIO_free_all(tmpout); 459 BIO_free_all(tmpout);
460 460
461 err2: 461 err2:
462 sk_X509_pop_free(cms_certs, X509_free); 462 sk_X509_pop_free(cms_certs, X509_free);
463 sk_X509_CRL_pop_free(crls, X509_CRL_free); 463 sk_X509_CRL_pop_free(crls, X509_CRL_free);
464 464
465 return ret; 465 return ret;
466} 466}
467 467
468int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms, 468int CMS_verify_receipt(CMS_ContentInfo *rcms, CMS_ContentInfo *ocms,
469 STACK_OF(X509) *certs, 469 STACK_OF(X509) *certs,
470 X509_STORE *store, unsigned int flags) 470 X509_STORE *store, unsigned int flags)
471{ 471{
472 int r; 472 int r;
473 flags &= ~(CMS_DETACHED | CMS_TEXT); 473 flags &= ~(CMS_DETACHED | CMS_TEXT);
474 r = CMS_verify(rcms, certs, store, NULL, NULL, flags); 474 r = CMS_verify(rcms, certs, store, NULL, NULL, flags);
475 if (r <= 0) 475 if (r <= 0)
476 return r; 476 return r;
477 return cms_Receipt_verify(rcms, ocms); 477 return cms_Receipt_verify(rcms, ocms);
478} 478}
479 479
480CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey, 480CMS_ContentInfo *CMS_sign(X509 *signcert, EVP_PKEY *pkey,
481 STACK_OF(X509) *certs, BIO *data, 481 STACK_OF(X509) *certs, BIO *data,
482 unsigned int flags) 482 unsigned int flags)
483{ 483{
484 CMS_ContentInfo *cms; 484 CMS_ContentInfo *cms;
485 int i; 485 int i;
486 486
487 cms = CMS_ContentInfo_new(); 487 cms = CMS_ContentInfo_new();
488 if (cms == NULL || !CMS_SignedData_init(cms)) 488 if (cms == NULL || !CMS_SignedData_init(cms))
489 goto merr; 489 goto merr;
490 if (flags & CMS_ASCIICRLF 490 if (flags & CMS_ASCIICRLF
491 && !CMS_set1_eContentType(cms, 491 && !CMS_set1_eContentType(cms,
492 OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF))) 492 OBJ_nid2obj(NID_id_ct_asciiTextWithCRLF)))
493 goto err; 493 goto err;
494 494
495 if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) { 495 if (pkey && !CMS_add1_signer(cms, signcert, pkey, NULL, flags)) {
496 CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR); 496 CMSerr(CMS_F_CMS_SIGN, CMS_R_ADD_SIGNER_ERROR);
497 goto err; 497 goto err;
498 } 498 }
499 499
500 for (i = 0; i < sk_X509_num(certs); i++) { 500 for (i = 0; i < sk_X509_num(certs); i++) {
501 X509 *x = sk_X509_value(certs, i); 501 X509 *x = sk_X509_value(certs, i);
502 if (!CMS_add1_cert(cms, x)) 502 if (!CMS_add1_cert(cms, x))
503 goto merr; 503 goto merr;
504 } 504 }
505 505
506 if (!(flags & CMS_DETACHED)) 506 if (!(flags & CMS_DETACHED))
507 CMS_set_detached(cms, 0); 507 CMS_set_detached(cms, 0);
508 508
509 if ((flags & (CMS_STREAM | CMS_PARTIAL)) 509 if ((flags & (CMS_STREAM | CMS_PARTIAL))
510 || CMS_final(cms, data, NULL, flags)) 510 || CMS_final(cms, data, NULL, flags))
511 return cms; 511 return cms;
512 else 512 else
513 goto err; 513 goto err;
514 514
515 merr: 515 merr:
516 CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE); 516 CMSerr(CMS_F_CMS_SIGN, ERR_R_MALLOC_FAILURE);
517 517
518 err: 518 err:
519 CMS_ContentInfo_free(cms); 519 CMS_ContentInfo_free(cms);
520 return NULL; 520 return NULL;
521} 521}
522 522
523CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si, 523CMS_ContentInfo *CMS_sign_receipt(CMS_SignerInfo *si,
524 X509 *signcert, EVP_PKEY *pkey, 524 X509 *signcert, EVP_PKEY *pkey,
525 STACK_OF(X509) *certs, unsigned int flags) 525 STACK_OF(X509) *certs, unsigned int flags)
526{ 526{
527 CMS_SignerInfo *rct_si; 527 CMS_SignerInfo *rct_si;
528 CMS_ContentInfo *cms = NULL; 528 CMS_ContentInfo *cms = NULL;
529 ASN1_OCTET_STRING **pos, *os; 529 ASN1_OCTET_STRING **pos, *os;
530 BIO *rct_cont = NULL; 530 BIO *rct_cont = NULL;
531 int r = 0; 531 int r = 0;
532 532
533 flags &= ~(CMS_STREAM | CMS_TEXT); 533 flags &= ~(CMS_STREAM | CMS_TEXT);
534 /* Not really detached but avoids content being allocated */ 534 /* Not really detached but avoids content being allocated */
535 flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED; 535 flags |= CMS_PARTIAL | CMS_BINARY | CMS_DETACHED;
536 if (!pkey || !signcert) { 536 if (!pkey || !signcert) {
537 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT); 537 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_NO_KEY_OR_CERT);
538 return NULL; 538 return NULL;
539 } 539 }
540 540
541 /* Initialize signed data */ 541 /* Initialize signed data */
542 542
543 cms = CMS_sign(NULL, NULL, certs, NULL, flags); 543 cms = CMS_sign(NULL, NULL, certs, NULL, flags);
544 if (!cms) 544 if (!cms)
545 goto err; 545 goto err;
546 546
547 /* Set inner content type to signed receipt */ 547 /* Set inner content type to signed receipt */
548 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt))) 548 if (!CMS_set1_eContentType(cms, OBJ_nid2obj(NID_id_smime_ct_receipt)))
549 goto err; 549 goto err;
550 550
551 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags); 551 rct_si = CMS_add1_signer(cms, signcert, pkey, NULL, flags);
552 if (!rct_si) { 552 if (!rct_si) {
553 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR); 553 CMSerr(CMS_F_CMS_SIGN_RECEIPT, CMS_R_ADD_SIGNER_ERROR);
554 goto err; 554 goto err;
555 } 555 }
556 556
557 os = cms_encode_Receipt(si); 557 os = cms_encode_Receipt(si);
558 558
559 if (!os) 559 if (!os)
560 goto err; 560 goto err;
561 561
562 /* Set content to digest */ 562 /* Set content to digest */
563 rct_cont = BIO_new_mem_buf(os->data, os->length); 563 rct_cont = BIO_new_mem_buf(os->data, os->length);
564 if (!rct_cont) 564 if (!rct_cont)
565 goto err; 565 goto err;
566 566
567 /* Add msgSigDigest attribute */ 567 /* Add msgSigDigest attribute */
568 568
569 if (!cms_msgSigDigest_add1(rct_si, si)) 569 if (!cms_msgSigDigest_add1(rct_si, si))
570 goto err; 570 goto err;
571 571
572 /* Finalize structure */ 572 /* Finalize structure */
573 if (!CMS_final(cms, rct_cont, NULL, flags)) 573 if (!CMS_final(cms, rct_cont, NULL, flags))
574 goto err; 574 goto err;
575 575
576 /* Set embedded content */ 576 /* Set embedded content */
577 pos = CMS_get0_content(cms); 577 pos = CMS_get0_content(cms);
578 *pos = os; 578 *pos = os;
579 579
580 r = 1; 580 r = 1;
581 581
582 err: 582 err:
583 BIO_free(rct_cont); 583 BIO_free(rct_cont);
584 if (r) 584 if (r)
585 return cms; 585 return cms;
586 CMS_ContentInfo_free(cms); 586 CMS_ContentInfo_free(cms);
587 return NULL; 587 return NULL;
588 588
589} 589}
590 590
591CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data, 591CMS_ContentInfo *CMS_encrypt(STACK_OF(X509) *certs, BIO *data,
592 const EVP_CIPHER *cipher, unsigned int flags) 592 const EVP_CIPHER *cipher, unsigned int flags)
593{ 593{
594 CMS_ContentInfo *cms; 594 CMS_ContentInfo *cms;
595 int i; 595 int i;
596 X509 *recip; 596 X509 *recip;
597 cms = CMS_EnvelopedData_create(cipher); 597 cms = CMS_EnvelopedData_create(cipher);
598 if (!cms) 598 if (!cms)
599 goto merr; 599 goto merr;
600 for (i = 0; i < sk_X509_num(certs); i++) { 600 for (i = 0; i < sk_X509_num(certs); i++) {
601 recip = sk_X509_value(certs, i); 601 recip = sk_X509_value(certs, i);
602 if (!CMS_add1_recipient_cert(cms, recip, flags)) { 602 if (!CMS_add1_recipient_cert(cms, recip, flags)) {
603 CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR); 603 CMSerr(CMS_F_CMS_ENCRYPT, CMS_R_RECIPIENT_ERROR);
604 goto err; 604 goto err;
605 } 605 }
606 } 606 }
607 607
608 if (!(flags & CMS_DETACHED)) 608 if (!(flags & CMS_DETACHED))
609 CMS_set_detached(cms, 0); 609 CMS_set_detached(cms, 0);
610 610
611 if ((flags & (CMS_STREAM | CMS_PARTIAL)) 611 if ((flags & (CMS_STREAM | CMS_PARTIAL))
612 || CMS_final(cms, data, NULL, flags)) 612 || CMS_final(cms, data, NULL, flags))
613 return cms; 613 return cms;
614 else 614 else
615 goto err; 615 goto err;
616 616
617 merr: 617 merr:
618 CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE); 618 CMSerr(CMS_F_CMS_ENCRYPT, ERR_R_MALLOC_FAILURE);
619 err: 619 err:
620 CMS_ContentInfo_free(cms); 620 CMS_ContentInfo_free(cms);
621 return NULL; 621 return NULL;
622} 622}
623 623
624static int cms_kari_set1_pkey(CMS_ContentInfo *cms, CMS_RecipientInfo *ri, 624static int cms_kari_set1_pkey(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
625 EVP_PKEY *pk, X509 *cert) 625 EVP_PKEY *pk, X509 *cert)
626{ 626{
627 int i; 627 int i;
628 STACK_OF(CMS_RecipientEncryptedKey) *reks; 628 STACK_OF(CMS_RecipientEncryptedKey) *reks;
629 CMS_RecipientEncryptedKey *rek; 629 CMS_RecipientEncryptedKey *rek;
630 reks = CMS_RecipientInfo_kari_get0_reks(ri); 630 reks = CMS_RecipientInfo_kari_get0_reks(ri);
631 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) { 631 for (i = 0; i < sk_CMS_RecipientEncryptedKey_num(reks); i++) {
632 int rv; 632 int rv;
633 rek = sk_CMS_RecipientEncryptedKey_value(reks, i); 633 rek = sk_CMS_RecipientEncryptedKey_value(reks, i);
634 if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert)) 634 if (cert != NULL && CMS_RecipientEncryptedKey_cert_cmp(rek, cert))
635 continue; 635 continue;
636 CMS_RecipientInfo_kari_set0_pkey(ri, pk); 636 CMS_RecipientInfo_kari_set0_pkey(ri, pk);
637 rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek); 637 rv = CMS_RecipientInfo_kari_decrypt(cms, ri, rek);
638 CMS_RecipientInfo_kari_set0_pkey(ri, NULL); 638 CMS_RecipientInfo_kari_set0_pkey(ri, NULL);
639 if (rv > 0) 639 if (rv > 0)
640 return 1; 640 return 1;
641 return cert == NULL ? 0 : -1; 641 return cert == NULL ? 0 : -1;
642 } 642 }
643 return 0; 643 return 0;
644} 644}
645 645
646int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert) 646int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert)
647{ 647{
648 STACK_OF(CMS_RecipientInfo) *ris; 648 STACK_OF(CMS_RecipientInfo) *ris;
649 CMS_RecipientInfo *ri; 649 CMS_RecipientInfo *ri;
650 int i, r, ri_type; 650 int i, r, ri_type;
651 int debug = 0, match_ri = 0; 651 int debug = 0, match_ri = 0;
652 ris = CMS_get0_RecipientInfos(cms); 652 ris = CMS_get0_RecipientInfos(cms);
653 if (ris) 653 if (ris)
654 debug = cms->d.envelopedData->encryptedContentInfo->debug; 654 debug = cms->d.envelopedData->encryptedContentInfo->debug;
655 ri_type = cms_pkey_get_ri_type(pk); 655 ri_type = cms_pkey_get_ri_type(pk);
656 if (ri_type == CMS_RECIPINFO_NONE) { 656 if (ri_type == CMS_RECIPINFO_NONE) {
657 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, 657 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY,
658 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); 658 CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
659 return 0; 659 return 0;
660 } 660 }
661 661
662 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) { 662 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
663 ri = sk_CMS_RecipientInfo_value(ris, i); 663 ri = sk_CMS_RecipientInfo_value(ris, i);
664 if (CMS_RecipientInfo_type(ri) != ri_type) 664 if (CMS_RecipientInfo_type(ri) != ri_type)
665 continue; 665 continue;
666 match_ri = 1; 666 match_ri = 1;
667 if (ri_type == CMS_RECIPINFO_AGREE) { 667 if (ri_type == CMS_RECIPINFO_AGREE) {
668 r = cms_kari_set1_pkey(cms, ri, pk, cert); 668 r = cms_kari_set1_pkey(cms, ri, pk, cert);
669 if (r > 0) 669 if (r > 0)
670 return 1; 670 return 1;
671 if (r < 0) 671 if (r < 0)
672 return 0; 672 return 0;
673 } 673 }
674 /* 674 /*
675 * If we have a cert try matching RecipientInfo otherwise try them 675 * If we have a cert try matching RecipientInfo otherwise try them
676 * all. 676 * all.
677 */ 677 */
678 else if (!cert || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) { 678 else if (!cert || !CMS_RecipientInfo_ktri_cert_cmp(ri, cert)) {
679 EVP_PKEY_up_ref(pk); 679 EVP_PKEY_up_ref(pk);
680 CMS_RecipientInfo_set0_pkey(ri, pk); 680 CMS_RecipientInfo_set0_pkey(ri, pk);
681 r = CMS_RecipientInfo_decrypt(cms, ri); 681 r = CMS_RecipientInfo_decrypt(cms, ri);
682 CMS_RecipientInfo_set0_pkey(ri, NULL); 682 CMS_RecipientInfo_set0_pkey(ri, NULL);
683 if (cert) { 683 if (cert) {
684 /* 684 /*
685 * If not debugging clear any error and return success to 685 * If not debugging clear any error and return success to
686 * avoid leaking of information useful to MMA 686 * avoid leaking of information useful to MMA
687 */ 687 */
688 if (!debug) { 688 if (!debug) {
689 ERR_clear_error(); 689 ERR_clear_error();
690 return 1; 690 return 1;
691 } 691 }
692 if (r > 0) 692 if (r > 0)
693 return 1; 693 return 1;
694 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_DECRYPT_ERROR); 694 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_DECRYPT_ERROR);
695 return 0; 695 return 0;
696 } 696 }
697 /* 697 /*
698 * If no cert and not debugging don't leave loop after first 698 * If no cert and not debugging don't leave loop after first
699 * successful decrypt. Always attempt to decrypt all recipients 699 * successful decrypt. Always attempt to decrypt all recipients
700 * to avoid leaking timing of a successful decrypt. 700 * to avoid leaking timing of a successful decrypt.
701 */ 701 */
702 else if (r > 0 && debug) 702 else if (r > 0 && debug)
703 return 1; 703 return 1;
704 } 704 }
705 } 705 }
706 /* If no cert, key transport and not debugging always return success */ 706 /* If no cert, key transport and not debugging always return success */
707 if (cert == NULL && ri_type == CMS_RECIPINFO_TRANS && match_ri && !debug) { 707 if (cert == NULL && ri_type == CMS_RECIPINFO_TRANS && match_ri && !debug) {
708 ERR_clear_error(); 708 ERR_clear_error();
709 return 1; 709 return 1;
710 } 710 }
711 711
712 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT); 712 CMSerr(CMS_F_CMS_DECRYPT_SET1_PKEY, CMS_R_NO_MATCHING_RECIPIENT);
713 return 0; 713 return 0;
714 714
715} 715}
716 716
717int CMS_decrypt_set1_key(CMS_ContentInfo *cms, 717int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
718 unsigned char *key, size_t keylen, 718 unsigned char *key, size_t keylen,
719 const unsigned char *id, size_t idlen) 719 const unsigned char *id, size_t idlen)
720{ 720{
721 STACK_OF(CMS_RecipientInfo) *ris; 721 STACK_OF(CMS_RecipientInfo) *ris;
722 CMS_RecipientInfo *ri; 722 CMS_RecipientInfo *ri;
723 int i, r; 723 int i, r;
724 ris = CMS_get0_RecipientInfos(cms); 724 ris = CMS_get0_RecipientInfos(cms);
725 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) { 725 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
726 ri = sk_CMS_RecipientInfo_value(ris, i); 726 ri = sk_CMS_RecipientInfo_value(ris, i);
727 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK) 727 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_KEK)
728 continue; 728 continue;
729 729
730 /* 730 /*
731 * If we have an id try matching RecipientInfo otherwise try them 731 * If we have an id try matching RecipientInfo otherwise try them
732 * all. 732 * all.
733 */ 733 */
734 if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) { 734 if (!id || (CMS_RecipientInfo_kekri_id_cmp(ri, id, idlen) == 0)) {
735 CMS_RecipientInfo_set0_key(ri, key, keylen); 735 CMS_RecipientInfo_set0_key(ri, key, keylen);
736 r = CMS_RecipientInfo_decrypt(cms, ri); 736 r = CMS_RecipientInfo_decrypt(cms, ri);
737 CMS_RecipientInfo_set0_key(ri, NULL, 0); 737 CMS_RecipientInfo_set0_key(ri, NULL, 0);
738 if (r > 0) 738 if (r > 0)
739 return 1; 739 return 1;
740 if (id) { 740 if (id) {
741 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_DECRYPT_ERROR); 741 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_DECRYPT_ERROR);
742 return 0; 742 return 0;
743 } 743 }
744 ERR_clear_error(); 744 ERR_clear_error();
745 } 745 }
746 } 746 }
747 747
748 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT); 748 CMSerr(CMS_F_CMS_DECRYPT_SET1_KEY, CMS_R_NO_MATCHING_RECIPIENT);
749 return 0; 749 return 0;
750 750
751} 751}
752 752
753int CMS_decrypt_set1_password(CMS_ContentInfo *cms, 753int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
754 unsigned char *pass, ossl_ssize_t passlen) 754 unsigned char *pass, ossl_ssize_t passlen)
755{ 755{
756 STACK_OF(CMS_RecipientInfo) *ris; 756 STACK_OF(CMS_RecipientInfo) *ris;
757 CMS_RecipientInfo *ri; 757 CMS_RecipientInfo *ri;
758 int i, r; 758 int i, r;
759 ris = CMS_get0_RecipientInfos(cms); 759 ris = CMS_get0_RecipientInfos(cms);
760 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) { 760 for (i = 0; i < sk_CMS_RecipientInfo_num(ris); i++) {
761 ri = sk_CMS_RecipientInfo_value(ris, i); 761 ri = sk_CMS_RecipientInfo_value(ris, i);
762 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS) 762 if (CMS_RecipientInfo_type(ri) != CMS_RECIPINFO_PASS)
763 continue; 763 continue;
764 CMS_RecipientInfo_set0_password(ri, pass, passlen); 764 CMS_RecipientInfo_set0_password(ri, pass, passlen);
765 r = CMS_RecipientInfo_decrypt(cms, ri); 765 r = CMS_RecipientInfo_decrypt(cms, ri);
766 CMS_RecipientInfo_set0_password(ri, NULL, 0); 766 CMS_RecipientInfo_set0_password(ri, NULL, 0);
767 if (r > 0) 767 if (r > 0)
768 return 1; 768 return 1;
769 } 769 }
770 770
771 CMSerr(CMS_F_CMS_DECRYPT_SET1_PASSWORD, CMS_R_NO_MATCHING_RECIPIENT); 771 CMSerr(CMS_F_CMS_DECRYPT_SET1_PASSWORD, CMS_R_NO_MATCHING_RECIPIENT);
772 return 0; 772 return 0;
773 773
774} 774}
775 775
776int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert, 776int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert,
777 BIO *dcont, BIO *out, unsigned int flags) 777 BIO *dcont, BIO *out, unsigned int flags)
778{ 778{
779 int r; 779 int r;
780 BIO *cont; 780 BIO *cont;
781 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped) { 781 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_pkcs7_enveloped) {
782 CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA); 782 CMSerr(CMS_F_CMS_DECRYPT, CMS_R_TYPE_NOT_ENVELOPED_DATA);
783 return 0; 783 return 0;
784 } 784 }
785 if (!dcont && !check_content(cms)) 785 if (!dcont && !check_content(cms))
786 return 0; 786 return 0;
787 if (flags & CMS_DEBUG_DECRYPT) 787 if (flags & CMS_DEBUG_DECRYPT)
788 cms->d.envelopedData->encryptedContentInfo->debug = 1; 788 cms->d.envelopedData->encryptedContentInfo->debug = 1;
789 else 789 else
790 cms->d.envelopedData->encryptedContentInfo->debug = 0; 790 cms->d.envelopedData->encryptedContentInfo->debug = 0;
791 if (!pk && !cert && !dcont && !out) 791 if (!pk && !cert && !dcont && !out)
792 return 1; 792 return 1;
793 if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert)) 793 if (pk && !CMS_decrypt_set1_pkey(cms, pk, cert))
794 return 0; 794 return 0;
795 cont = CMS_dataInit(cms, dcont); 795 cont = CMS_dataInit(cms, dcont);
796 if (!cont) 796 if (!cont)
797 return 0; 797 return 0;
798 r = cms_copy_content(out, cont, flags); 798 r = cms_copy_content(out, cont, flags);
799 do_free_upto(cont, dcont); 799 do_free_upto(cont, dcont);
800 return r; 800 return r;
801} 801}
802 802
803int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags) 803int CMS_final(CMS_ContentInfo *cms, BIO *data, BIO *dcont, unsigned int flags)
804{ 804{
805 BIO *cmsbio; 805 BIO *cmsbio;
806 int ret = 0; 806 int ret = 0;
807 807
808 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) { 808 if ((cmsbio = CMS_dataInit(cms, dcont)) == NULL) {
809 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_LIB); 809 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_LIB);
810 return 0; 810 return 0;
811 } 811 }
812 812
813 SMIME_crlf_copy(data, cmsbio, flags); 813 SMIME_crlf_copy(data, cmsbio, flags);
814 814
815 (void)BIO_flush(cmsbio); 815 (void)BIO_flush(cmsbio);
816 816
817 if (!CMS_dataFinal(cms, cmsbio)) { 817 if (!CMS_dataFinal(cms, cmsbio)) {
818 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_DATAFINAL_ERROR); 818 CMSerr(CMS_F_CMS_FINAL, CMS_R_CMS_DATAFINAL_ERROR);
819 goto err; 819 goto err;
820 } 820 }
821 821
822 ret = 1; 822 ret = 1;
823 823
824 err: 824 err:
825 do_free_upto(cmsbio, dcont); 825 do_free_upto(cmsbio, dcont);
826 826
827 return ret; 827 return ret;
828 828
829} 829}
830 830
831#ifdef ZLIB 831#ifdef ZLIB
832 832
833int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out, 833int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
834 unsigned int flags) 834 unsigned int flags)
835{ 835{
836 BIO *cont; 836 BIO *cont;
837 int r; 837 int r;
838 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) { 838 if (OBJ_obj2nid(CMS_get0_type(cms)) != NID_id_smime_ct_compressedData) {
839 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_TYPE_NOT_COMPRESSED_DATA); 839 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_TYPE_NOT_COMPRESSED_DATA);
840 return 0; 840 return 0;
841 } 841 }
842 842
843 if (!dcont && !check_content(cms)) 843 if (!dcont && !check_content(cms))
844 return 0; 844 return 0;
845 845
846 cont = CMS_dataInit(cms, dcont); 846 cont = CMS_dataInit(cms, dcont);
847 if (!cont) 847 if (!cont)
848 return 0; 848 return 0;
849 r = cms_copy_content(out, cont, flags); 849 r = cms_copy_content(out, cont, flags);
850 do_free_upto(cont, dcont); 850 do_free_upto(cont, dcont);
851 return r; 851 return r;
852} 852}
853 853
854CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags) 854CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
855{ 855{
856 CMS_ContentInfo *cms; 856 CMS_ContentInfo *cms;
857 if (comp_nid <= 0) 857 if (comp_nid <= 0)
858 comp_nid = NID_zlib_compression; 858 comp_nid = NID_zlib_compression;
859 cms = cms_CompressedData_create(comp_nid); 859 cms = cms_CompressedData_create(comp_nid);
860 if (!cms) 860 if (!cms)
861 return NULL; 861 return NULL;
862 862
863 if (!(flags & CMS_DETACHED)) 863 if (!(flags & CMS_DETACHED))
864 CMS_set_detached(cms, 0); 864 CMS_set_detached(cms, 0);
865 865
866 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags)) 866 if ((flags & CMS_STREAM) || CMS_final(cms, in, NULL, flags))
867 return cms; 867 return cms;
868 868
869 CMS_ContentInfo_free(cms); 869 CMS_ContentInfo_free(cms);
870 return NULL; 870 return NULL;
871} 871}
872 872
873#else 873#else
874 874
875int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out, 875int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
876 unsigned int flags) 876 unsigned int flags)
877{ 877{
878 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM); 878 CMSerr(CMS_F_CMS_UNCOMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
879 return 0; 879 return 0;
880} 880}
881 881
882CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags) 882CMS_ContentInfo *CMS_compress(BIO *in, int comp_nid, unsigned int flags)
883{ 883{
884 CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM); 884 CMSerr(CMS_F_CMS_COMPRESS, CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
885 return NULL; 885 return NULL;
886} 886}
887 887
888#endif 888#endif