]>
Commit | Line | Data |
---|---|---|
b1ab9ed8 A |
1 | /* |
2 | * The contents of this file are subject to the Mozilla Public | |
3 | * License Version 1.1 (the "License"); you may not use this file | |
4 | * except in compliance with the License. You may obtain a copy of | |
5 | * the License at http://www.mozilla.org/MPL/ | |
6 | * | |
7 | * Software distributed under the License is distributed on an "AS | |
8 | * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | |
9 | * implied. See the License for the specific language governing | |
10 | * rights and limitations under the License. | |
11 | * | |
12 | * The Original Code is the Netscape security libraries. | |
13 | * | |
14 | * The Initial Developer of the Original Code is Netscape | |
15 | * Communications Corporation. Portions created by Netscape are | |
16 | * Copyright (C) 1994-2000 Netscape Communications Corporation. All | |
17 | * Rights Reserved. | |
18 | * | |
19 | * Contributor(s): | |
20 | * | |
21 | * Alternatively, the contents of this file may be used under the | |
22 | * terms of the GNU General Public License Version 2 or later (the | |
23 | * "GPL"), in which case the provisions of the GPL are applicable | |
24 | * instead of those above. If you wish to allow use of your | |
25 | * version of this file only under the terms of the GPL and not to | |
26 | * allow others to use your version of this file under the MPL, | |
27 | * indicate your decision by deleting the provisions above and | |
28 | * replace them with the notice and other provisions required by | |
29 | * the GPL. If you do not delete the provisions above, a recipient | |
30 | * may use your version of this file under either the MPL or the | |
31 | * GPL. | |
32 | */ | |
33 | ||
34 | /* | |
35 | * CMS signerInfo methods. | |
36 | */ | |
37 | ||
38 | #include <Security/SecCmsSignerInfo.h> | |
39 | #include "SecSMIMEPriv.h" | |
40 | ||
41 | #include "cmslocal.h" | |
42 | ||
43 | #include "cert.h" | |
d8f41ccd | 44 | #include "SecAsn1Item.h" |
b1ab9ed8 A |
45 | #include "secoid.h" |
46 | #include "cryptohi.h" | |
47 | ||
48 | #include <security_asn1/secasn1.h> | |
49 | #include <security_asn1/secerr.h> | |
d8f41ccd A |
50 | #include <security_asn1/secport.h> |
51 | ||
52 | #if USE_CDSA_CRYPTO | |
b1ab9ed8 | 53 | #include <Security/SecKeychain.h> |
d8f41ccd A |
54 | #endif |
55 | ||
b1ab9ed8 | 56 | #include <Security/SecIdentity.h> |
d8f41ccd A |
57 | #include <Security/SecCertificateInternal.h> |
58 | #include <Security/SecInternal.h> | |
b1ab9ed8 | 59 | #include <Security/SecKeyPriv.h> |
d8f41ccd | 60 | #include <utilities/SecCFWrappers.h> |
b1ab9ed8 | 61 | #include <CoreFoundation/CFTimeZone.h> |
b1ab9ed8 | 62 | |
b1ab9ed8 A |
63 | |
64 | #define HIDIGIT(v) (((v) / 10) + '0') | |
65 | #define LODIGIT(v) (((v) % 10) + '0') | |
66 | ||
67 | #define ISDIGIT(dig) (((dig) >= '0') && ((dig) <= '9')) | |
68 | #define CAPTURE(var,p,label) \ | |
69 | { \ | |
70 | if (!ISDIGIT((p)[0]) || !ISDIGIT((p)[1])) goto label; \ | |
71 | (var) = ((p)[0] - '0') * 10 + ((p)[1] - '0'); \ | |
72 | } | |
73 | ||
b1ab9ed8 A |
74 | |
75 | static OSStatus | |
d8f41ccd | 76 | DER_UTCTimeToCFDate(const SecAsn1Item * utcTime, CFAbsoluteTime *date) |
b1ab9ed8 | 77 | { |
b1ab9ed8 | 78 | char *string = (char *)utcTime->Data; |
d8f41ccd | 79 | int year, month, mday, hour, minute, second, hourOff, minOff; |
b1ab9ed8 A |
80 | |
81 | /* Verify time is formatted properly and capture information */ | |
82 | second = 0; | |
83 | hourOff = 0; | |
84 | minOff = 0; | |
85 | CAPTURE(year,string+0,loser); | |
86 | if (year < 50) { | |
87 | /* ASSUME that year # is in the 2000's, not the 1900's */ | |
d8f41ccd A |
88 | year += 2000; |
89 | } else { | |
90 | year += 1900; | |
b1ab9ed8 A |
91 | } |
92 | CAPTURE(month,string+2,loser); | |
93 | if ((month == 0) || (month > 12)) goto loser; | |
94 | CAPTURE(mday,string+4,loser); | |
95 | if ((mday == 0) || (mday > 31)) goto loser; | |
96 | CAPTURE(hour,string+6,loser); | |
97 | if (hour > 23) goto loser; | |
98 | CAPTURE(minute,string+8,loser); | |
99 | if (minute > 59) goto loser; | |
100 | if (ISDIGIT(string[10])) { | |
101 | CAPTURE(second,string+10,loser); | |
102 | if (second > 59) goto loser; | |
103 | string += 2; | |
104 | } | |
105 | if (string[10] == '+') { | |
106 | CAPTURE(hourOff,string+11,loser); | |
107 | if (hourOff > 23) goto loser; | |
108 | CAPTURE(minOff,string+13,loser); | |
109 | if (minOff > 59) goto loser; | |
110 | } else if (string[10] == '-') { | |
111 | CAPTURE(hourOff,string+11,loser); | |
112 | if (hourOff > 23) goto loser; | |
113 | hourOff = -hourOff; | |
114 | CAPTURE(minOff,string+13,loser); | |
115 | if (minOff > 59) goto loser; | |
116 | minOff = -minOff; | |
117 | } else if (string[10] != 'Z') { | |
118 | goto loser; | |
119 | } | |
120 | ||
d8f41ccd A |
121 | if (hourOff == 0 && minOff == 0) { |
122 | *date = CFAbsoluteTimeForGregorianZuluMoment(year, month, mday, hour, minute, second); | |
123 | } else { | |
124 | CFTimeZoneRef tz = CFTimeZoneCreateWithTimeIntervalFromGMT(kCFAllocatorDefault, (hourOff * 60 + minOff) * 60); | |
125 | *date = CFAbsoluteTimeForGregorianMoment(tz, year, month, mday, hour, minute, second); | |
126 | CFReleaseSafe(tz); | |
b1ab9ed8 A |
127 | } |
128 | ||
b1ab9ed8 A |
129 | return SECSuccess; |
130 | ||
131 | loser: | |
132 | return SECFailure; | |
133 | } | |
134 | ||
135 | static OSStatus | |
d8f41ccd | 136 | DER_CFDateToUTCTime(CFAbsoluteTime date, SecAsn1Item * utcTime) |
b1ab9ed8 | 137 | { |
b1ab9ed8 | 138 | unsigned char *d; |
b1ab9ed8 A |
139 | |
140 | utcTime->Length = 13; | |
141 | utcTime->Data = d = PORT_Alloc(13); | |
142 | if (!utcTime->Data) | |
143 | return SECFailure; | |
d8f41ccd | 144 | |
d87e1158 A |
145 | __block int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0; |
146 | __block bool result; | |
147 | SecCFCalendarDoWithZuluCalendar(^(CFCalendarRef zuluCalendar) { | |
148 | result = CFCalendarDecomposeAbsoluteTime(zuluCalendar, date, "yMdHms", &year, &month, &day, &hour, &minute, &second); | |
149 | }); | |
150 | if (!result) | |
d8f41ccd A |
151 | return SECFailure; |
152 | ||
b1ab9ed8 | 153 | /* UTC time does not handle the years before 1950 */ |
d8f41ccd A |
154 | if (year < 1950) |
155 | return SECFailure; | |
b1ab9ed8 A |
156 | |
157 | /* remove the century since it's added to the year by the | |
158 | CFAbsoluteTimeGetGregorianDate routine, but is not needed for UTC time */ | |
d8f41ccd A |
159 | year %= 100; |
160 | ||
161 | d[0] = HIDIGIT(year); | |
162 | d[1] = LODIGIT(year); | |
163 | d[2] = HIDIGIT(month); | |
164 | d[3] = LODIGIT(month); | |
165 | d[4] = HIDIGIT(day); | |
166 | d[5] = LODIGIT(day); | |
167 | d[6] = HIDIGIT(hour); | |
168 | d[7] = LODIGIT(hour); | |
169 | d[8] = HIDIGIT(minute); | |
170 | d[9] = LODIGIT(minute); | |
b1ab9ed8 A |
171 | d[10] = HIDIGIT(second); |
172 | d[11] = LODIGIT(second); | |
173 | d[12] = 'Z'; | |
174 | return SECSuccess; | |
175 | } | |
176 | ||
177 | /* ============================================================================= | |
178 | * SIGNERINFO | |
179 | */ | |
180 | SecCmsSignerInfoRef | |
d8f41ccd | 181 | nss_cmssignerinfo_create(SecCmsSignedDataRef sigd, SecCmsSignerIDSelector type, SecCertificateRef cert, const SecAsn1Item *subjKeyID, SecPublicKeyRef pubKey, SecPrivateKeyRef signingKey, SECOidTag digestalgtag); |
b1ab9ed8 A |
182 | |
183 | SecCmsSignerInfoRef | |
d8f41ccd | 184 | SecCmsSignerInfoCreateWithSubjKeyID(SecCmsSignedDataRef sigd, const SecAsn1Item *subjKeyID, SecPublicKeyRef pubKey, SecPrivateKeyRef signingKey, SECOidTag digestalgtag) |
b1ab9ed8 | 185 | { |
d8f41ccd | 186 | return nss_cmssignerinfo_create(sigd, SecCmsSignerIDSubjectKeyID, NULL, subjKeyID, pubKey, signingKey, digestalgtag); |
b1ab9ed8 A |
187 | } |
188 | ||
189 | SecCmsSignerInfoRef | |
d8f41ccd | 190 | SecCmsSignerInfoCreate(SecCmsSignedDataRef sigd, SecIdentityRef identity, SECOidTag digestalgtag) |
b1ab9ed8 A |
191 | { |
192 | SecCmsSignerInfoRef signerInfo = NULL; | |
193 | SecCertificateRef cert = NULL; | |
194 | SecPrivateKeyRef signingKey = NULL; | |
195 | ||
196 | if (SecIdentityCopyCertificate(identity, &cert)) | |
197 | goto loser; | |
198 | if (SecIdentityCopyPrivateKey(identity, &signingKey)) | |
199 | goto loser; | |
200 | ||
d8f41ccd | 201 | signerInfo = nss_cmssignerinfo_create(sigd, SecCmsSignerIDIssuerSN, cert, NULL, NULL, signingKey, digestalgtag); |
b1ab9ed8 A |
202 | |
203 | loser: | |
204 | if (cert) | |
205 | CFRelease(cert); | |
206 | if (signingKey) | |
207 | CFRelease(signingKey); | |
208 | ||
209 | return signerInfo; | |
210 | } | |
211 | ||
212 | SecCmsSignerInfoRef | |
d8f41ccd | 213 | nss_cmssignerinfo_create(SecCmsSignedDataRef sigd, SecCmsSignerIDSelector type, SecCertificateRef cert, const SecAsn1Item *subjKeyID, SecPublicKeyRef pubKey, SecPrivateKeyRef signingKey, SECOidTag digestalgtag) |
b1ab9ed8 A |
214 | { |
215 | void *mark; | |
216 | SecCmsSignerInfoRef signerinfo; | |
217 | int version; | |
218 | PLArenaPool *poolp; | |
219 | ||
d8f41ccd | 220 | poolp = sigd->contentInfo.cmsg->poolp; |
b1ab9ed8 A |
221 | |
222 | mark = PORT_ArenaMark(poolp); | |
223 | ||
224 | signerinfo = (SecCmsSignerInfoRef)PORT_ArenaZAlloc(poolp, sizeof(SecCmsSignerInfo)); | |
225 | if (signerinfo == NULL) { | |
226 | PORT_ArenaRelease(poolp, mark); | |
227 | return NULL; | |
228 | } | |
229 | ||
230 | ||
d8f41ccd | 231 | signerinfo->signedData = sigd; |
b1ab9ed8 A |
232 | |
233 | switch(type) { | |
234 | case SecCmsSignerIDIssuerSN: | |
235 | signerinfo->signerIdentifier.identifierType = SecCmsSignerIDIssuerSN; | |
236 | if ((signerinfo->cert = CERT_DupCertificate(cert)) == NULL) | |
237 | goto loser; | |
238 | if ((signerinfo->signerIdentifier.id.issuerAndSN = CERT_GetCertIssuerAndSN(poolp, cert)) == NULL) | |
239 | goto loser; | |
b1ab9ed8 A |
240 | break; |
241 | case SecCmsSignerIDSubjectKeyID: | |
242 | signerinfo->signerIdentifier.identifierType = SecCmsSignerIDSubjectKeyID; | |
243 | PORT_Assert(subjKeyID); | |
244 | if (!subjKeyID) | |
245 | goto loser; | |
d8f41ccd | 246 | signerinfo->signerIdentifier.id.subjectKeyID = PORT_ArenaNew(poolp, SecAsn1Item); |
b1ab9ed8 A |
247 | SECITEM_CopyItem(poolp, signerinfo->signerIdentifier.id.subjectKeyID, |
248 | subjKeyID); | |
249 | signerinfo->pubKey = SECKEY_CopyPublicKey(pubKey); | |
250 | if (!signerinfo->pubKey) | |
251 | goto loser; | |
252 | break; | |
253 | default: | |
254 | goto loser; | |
255 | } | |
256 | ||
257 | if (!signingKey) | |
258 | goto loser; | |
259 | ||
260 | signerinfo->signingKey = SECKEY_CopyPrivateKey(signingKey); | |
261 | if (!signerinfo->signingKey) | |
262 | goto loser; | |
263 | ||
264 | /* set version right now */ | |
265 | version = SEC_CMS_SIGNER_INFO_VERSION_ISSUERSN; | |
266 | /* RFC2630 5.3 "version is the syntax version number. If the .... " */ | |
267 | if (signerinfo->signerIdentifier.identifierType == SecCmsSignerIDSubjectKeyID) | |
268 | version = SEC_CMS_SIGNER_INFO_VERSION_SUBJKEY; | |
269 | (void)SEC_ASN1EncodeInteger(poolp, &(signerinfo->version), (long)version); | |
270 | ||
271 | if (SECOID_SetAlgorithmID(poolp, &signerinfo->digestAlg, digestalgtag, NULL) != SECSuccess) | |
272 | goto loser; | |
273 | ||
d8f41ccd A |
274 | if (SecCmsSignedDataAddSignerInfo(sigd, signerinfo)) |
275 | goto loser; | |
276 | ||
b1ab9ed8 A |
277 | PORT_ArenaUnmark(poolp, mark); |
278 | return signerinfo; | |
279 | ||
280 | loser: | |
281 | PORT_ArenaRelease(poolp, mark); | |
282 | return NULL; | |
283 | } | |
284 | ||
285 | /* | |
286 | * SecCmsSignerInfoDestroy - destroy a SignerInfo data structure | |
287 | */ | |
288 | void | |
289 | SecCmsSignerInfoDestroy(SecCmsSignerInfoRef si) | |
290 | { | |
d8f41ccd | 291 | if (si->cert != NULL) |
b1ab9ed8 | 292 | CERT_DestroyCertificate(si->cert); |
d8f41ccd A |
293 | |
294 | if (si->certList != NULL) | |
b1ab9ed8 | 295 | CFRelease(si->certList); |
d8f41ccd | 296 | |
b1ab9ed8 A |
297 | /* XXX storage ??? */ |
298 | } | |
299 | ||
d8f41ccd A |
300 | static SecAsn1AlgId SecCertificateGetPublicKeyAlgorithmID(SecCertificateRef cert) |
301 | { | |
302 | const DERAlgorithmId *length_data_swapped = SecCertificateGetPublicKeyAlgorithm(cert); | |
303 | SecAsn1AlgId temp = { | |
304 | { length_data_swapped->oid.length, length_data_swapped->oid.data }, | |
305 | { length_data_swapped->params.length, length_data_swapped->params.data } }; | |
306 | ||
307 | return temp; | |
308 | } | |
309 | ||
b1ab9ed8 A |
310 | /* |
311 | * SecCmsSignerInfoSign - sign something | |
312 | * | |
313 | */ | |
314 | OSStatus | |
d8f41ccd | 315 | SecCmsSignerInfoSign(SecCmsSignerInfoRef signerinfo, SecAsn1Item * digest, SecAsn1Item * contentType) |
b1ab9ed8 A |
316 | { |
317 | SecCertificateRef cert; | |
318 | SecPrivateKeyRef privkey = NULL; | |
319 | SECOidTag digestalgtag; | |
320 | SECOidTag pubkAlgTag; | |
d8f41ccd | 321 | SecAsn1Item signature = { 0 }; |
b1ab9ed8 | 322 | OSStatus rv; |
60c433a9 | 323 | PLArenaPool *poolp, *tmppoolp = NULL; |
d8f41ccd | 324 | const SECAlgorithmID *algID = NULL; |
b1ab9ed8 A |
325 | //CERTSubjectPublicKeyInfo *spki; |
326 | ||
327 | PORT_Assert (digest != NULL); | |
328 | ||
d8f41ccd | 329 | poolp = signerinfo->signedData->contentInfo.cmsg->poolp; |
b1ab9ed8 A |
330 | |
331 | switch (signerinfo->signerIdentifier.identifierType) { | |
332 | case SecCmsSignerIDIssuerSN: | |
333 | privkey = signerinfo->signingKey; | |
334 | signerinfo->signingKey = NULL; | |
335 | cert = signerinfo->cert; | |
d8f41ccd | 336 | #if USE_CDSA_CRYPTO |
b1ab9ed8 A |
337 | if (SecCertificateGetAlgorithmID(cert,&algID)) { |
338 | PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); | |
339 | goto loser; | |
340 | } | |
d8f41ccd A |
341 | #else |
342 | SecAsn1AlgId _algID = SecCertificateGetPublicKeyAlgorithmID(cert); | |
343 | algID = &_algID; | |
344 | #endif | |
b1ab9ed8 A |
345 | break; |
346 | case SecCmsSignerIDSubjectKeyID: | |
347 | privkey = signerinfo->signingKey; | |
348 | signerinfo->signingKey = NULL; | |
349 | #if 0 | |
350 | spki = SECKEY_CreateSubjectPublicKeyInfo(signerinfo->pubKey); | |
351 | SECKEY_DestroyPublicKey(signerinfo->pubKey); | |
352 | signerinfo->pubKey = NULL; | |
353 | SECOID_CopyAlgorithmID(NULL, &freeAlgID, &spki->algorithm); | |
354 | SECKEY_DestroySubjectPublicKeyInfo(spki); | |
355 | algID = &freeAlgID; | |
356 | #else | |
d8f41ccd | 357 | #if USE_CDSA_CRYPTO |
b1ab9ed8 A |
358 | if (SecKeyGetAlgorithmID(signerinfo->pubKey,&algID)) { |
359 | PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); | |
360 | goto loser; | |
361 | } | |
d8f41ccd A |
362 | #endif |
363 | #endif | |
b1ab9ed8 A |
364 | CFRelease(signerinfo->pubKey); |
365 | signerinfo->pubKey = NULL; | |
b1ab9ed8 A |
366 | break; |
367 | default: | |
368 | PORT_SetError(SEC_ERROR_UNSUPPORTED_MESSAGE_TYPE); | |
369 | goto loser; | |
370 | } | |
371 | digestalgtag = SecCmsSignerInfoGetDigestAlgTag(signerinfo); | |
b1ab9ed8 | 372 | pubkAlgTag = SECOID_GetAlgorithmTag(algID); |
d8f41ccd | 373 | #if USE_CDSA_CRYPTO |
b1ab9ed8 A |
374 | if (signerinfo->signerIdentifier.identifierType == SecCmsSignerIDSubjectKeyID) { |
375 | SECOID_DestroyAlgorithmID(&freeAlgID, PR_FALSE); | |
376 | } | |
d8f41ccd | 377 | #endif |
b1ab9ed8 A |
378 | #if 0 |
379 | // @@@ Not yet | |
380 | /* Fortezza MISSI have weird signature formats. | |
381 | * Map them to standard DSA formats | |
382 | */ | |
383 | pubkAlgTag = PK11_FortezzaMapSig(pubkAlgTag); | |
384 | #endif | |
385 | ||
386 | if (signerinfo->authAttr != NULL) { | |
d8f41ccd | 387 | SecAsn1Item encoded_attrs; |
b1ab9ed8 A |
388 | |
389 | /* find and fill in the message digest attribute. */ | |
390 | rv = SecCmsAttributeArraySetAttr(poolp, &(signerinfo->authAttr), | |
391 | SEC_OID_PKCS9_MESSAGE_DIGEST, digest, PR_FALSE); | |
392 | if (rv != SECSuccess) | |
393 | goto loser; | |
394 | ||
395 | if (contentType != NULL) { | |
396 | /* if the caller wants us to, find and fill in the content type attribute. */ | |
397 | rv = SecCmsAttributeArraySetAttr(poolp, &(signerinfo->authAttr), | |
398 | SEC_OID_PKCS9_CONTENT_TYPE, contentType, PR_FALSE); | |
399 | if (rv != SECSuccess) | |
400 | goto loser; | |
401 | } | |
402 | ||
403 | if ((tmppoolp = PORT_NewArena (1024)) == NULL) { | |
404 | PORT_SetError(SEC_ERROR_NO_MEMORY); | |
405 | goto loser; | |
406 | } | |
407 | ||
408 | /* | |
409 | * Before encoding, reorder the attributes so that when they | |
410 | * are encoded, they will be conforming DER, which is required | |
411 | * to have a specific order and that is what must be used for | |
412 | * the hash/signature. We do this here, rather than building | |
413 | * it into EncodeAttributes, because we do not want to do | |
414 | * such reordering on incoming messages (which also uses | |
415 | * EncodeAttributes) or our old signatures (and other "broken" | |
416 | * implementations) will not verify. So, we want to guarantee | |
417 | * that we send out good DER encodings of attributes, but not | |
418 | * to expect to receive them. | |
419 | */ | |
420 | if (SecCmsAttributeArrayReorder(signerinfo->authAttr) != SECSuccess) | |
421 | goto loser; | |
422 | ||
423 | encoded_attrs.Data = NULL; | |
424 | encoded_attrs.Length = 0; | |
425 | if (SecCmsAttributeArrayEncode(tmppoolp, &(signerinfo->authAttr), | |
426 | &encoded_attrs) == NULL) | |
427 | goto loser; | |
428 | ||
d8f41ccd A |
429 | #if USE_CDSA_CRYPTO |
430 | rv = SEC_SignData(&signature, encoded_attrs.Data, encoded_attrs.Length, | |
b1ab9ed8 | 431 | privkey, digestalgtag, pubkAlgTag); |
d8f41ccd A |
432 | #else |
433 | signature.Length = SecKeyGetSize(privkey, kSecKeySignatureSize); | |
434 | signature.Data = PORT_ZAlloc(signature.Length); | |
435 | if (!signature.Data) { | |
436 | signature.Length = 0; | |
437 | goto loser; | |
438 | } | |
439 | rv = SecKeyDigestAndSign(privkey, &signerinfo->digestAlg, encoded_attrs.Data, encoded_attrs.Length, signature.Data, &signature.Length); | |
440 | if (rv) { | |
441 | PORT_ZFree(signature.Data, signature.Length); | |
442 | signature.Length = 0; | |
443 | } | |
444 | #endif | |
445 | ||
b1ab9ed8 | 446 | PORT_FreeArena(tmppoolp, PR_FALSE); /* awkward memory management :-( */ |
60c433a9 | 447 | tmppoolp = 0; |
b1ab9ed8 | 448 | } else { |
d8f41ccd A |
449 | signature.Length = SecKeyGetSize(privkey, kSecKeySignatureSize); |
450 | signature.Data = PORT_ZAlloc(signature.Length); | |
451 | if (!signature.Data) { | |
452 | signature.Length = 0; | |
453 | goto loser; | |
454 | } | |
455 | rv = SecKeySignDigest(privkey, &signerinfo->digestAlg, digest->Data, digest->Length, | |
456 | signature.Data, &signature.Length); | |
457 | if (rv) { | |
458 | PORT_ZFree(signature.Data, signature.Length); | |
459 | signature.Length = 0; | |
460 | } | |
b1ab9ed8 A |
461 | } |
462 | SECKEY_DestroyPrivateKey(privkey); | |
463 | privkey = NULL; | |
464 | ||
465 | if (rv != SECSuccess) | |
466 | goto loser; | |
467 | ||
468 | if (SECITEM_CopyItem(poolp, &(signerinfo->encDigest), &signature) | |
469 | != SECSuccess) | |
470 | goto loser; | |
471 | ||
472 | SECITEM_FreeItem(&signature, PR_FALSE); | |
473 | ||
b1ab9ed8 A |
474 | if (SECOID_SetAlgorithmID(poolp, &(signerinfo->digestEncAlg), pubkAlgTag, |
475 | NULL) != SECSuccess) | |
476 | goto loser; | |
477 | ||
478 | return SECSuccess; | |
479 | ||
480 | loser: | |
481 | if (signature.Length != 0) | |
482 | SECITEM_FreeItem (&signature, PR_FALSE); | |
483 | if (privkey) | |
484 | SECKEY_DestroyPrivateKey(privkey); | |
60c433a9 A |
485 | if (tmppoolp) |
486 | PORT_FreeArena(tmppoolp, PR_FALSE); | |
b1ab9ed8 A |
487 | return SECFailure; |
488 | } | |
489 | ||
d8f41ccd A |
490 | #if !USE_CDSA_CRYPTO |
491 | static CFArrayRef | |
492 | SecCmsSignerInfoCopySigningCertificates(SecCmsSignerInfoRef signerinfo) | |
493 | { | |
494 | CFMutableArrayRef certs = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks); | |
495 | SecAsn1Item **cert_datas = signerinfo->signedData->rawCerts; | |
496 | SecAsn1Item *cert_data; | |
497 | if (cert_datas) while ((cert_data = *cert_datas) != NULL) { | |
498 | SecCertificateRef cert = SecCertificateCreateWithBytes(NULL, cert_data->Data, cert_data->Length); | |
499 | if (cert) { | |
500 | switch (signerinfo->signerIdentifier.identifierType) { | |
501 | case SecCmsSignerIDIssuerSN: | |
502 | if (CERT_CheckIssuerAndSerial(cert, | |
503 | &(signerinfo->signerIdentifier.id.issuerAndSN->derIssuer), | |
504 | &(signerinfo->signerIdentifier.id.issuerAndSN->serialNumber))) | |
505 | CFArrayInsertValueAtIndex(certs, 0, cert); | |
506 | else | |
507 | CFArrayAppendValue(certs, cert); | |
508 | break; | |
509 | case SecCmsSignerIDSubjectKeyID: | |
510 | { | |
511 | CFDataRef cert_keyid = SecCertificateGetSubjectKeyID(cert); | |
512 | SecAsn1Item *tbf_keyid = signerinfo->signerIdentifier.id.subjectKeyID; | |
513 | if (tbf_keyid->Length == (size_t)CFDataGetLength(cert_keyid) && | |
514 | !memcmp(tbf_keyid->Data, CFDataGetBytePtr(cert_keyid), tbf_keyid->Length)) | |
515 | CFArrayInsertValueAtIndex(certs, 0, cert); | |
516 | else | |
517 | CFArrayAppendValue(certs, cert); | |
518 | break; | |
519 | } | |
520 | } | |
521 | CFReleaseNull(cert); | |
522 | } | |
523 | cert_datas++; | |
524 | } | |
525 | ||
526 | if ((CFArrayGetCount(certs) == 0) && | |
527 | (signerinfo->signerIdentifier.identifierType == SecCmsSignerIDIssuerSN)) | |
528 | { | |
529 | SecCertificateRef cert = CERT_FindCertificateByIssuerAndSN(signerinfo->signedData->certs, signerinfo->signerIdentifier.id.issuerAndSN); | |
530 | if (cert) { | |
531 | CFArrayAppendValue(certs, cert); | |
532 | CFRelease(cert); | |
533 | } | |
534 | } | |
535 | return certs; | |
536 | } | |
537 | #endif | |
538 | ||
b1ab9ed8 A |
539 | OSStatus |
540 | SecCmsSignerInfoVerifyCertificate(SecCmsSignerInfoRef signerinfo, SecKeychainRef keychainOrArray, | |
541 | CFTypeRef policies, SecTrustRef *trustRef) | |
542 | { | |
b1ab9ed8 A |
543 | CFAbsoluteTime stime; |
544 | OSStatus rv; | |
d8f41ccd A |
545 | |
546 | #if USE_CDSA_CRYPTO | |
547 | SecCertificateRef cert; | |
b1ab9ed8 A |
548 | |
549 | if ((cert = SecCmsSignerInfoGetSigningCertificate(signerinfo, keychainOrArray)) == NULL) { | |
d8f41ccd A |
550 | #else |
551 | CFArrayRef certs; | |
552 | ||
553 | if ((certs = SecCmsSignerInfoCopySigningCertificates(signerinfo)) == NULL) { | |
554 | #endif | |
b1ab9ed8 A |
555 | signerinfo->verificationStatus = SecCmsVSSigningCertNotFound; |
556 | return SECFailure; | |
557 | } | |
b1ab9ed8 A |
558 | /* |
559 | * Get and convert the signing time; if available, it will be used | |
560 | * both on the cert verification and for importing the sender | |
561 | * email profile. | |
562 | */ | |
d8f41ccd A |
563 | if (SecCmsSignerInfoGetSigningTime(signerinfo, &stime) != SECSuccess) |
564 | stime = CFAbsoluteTimeGetCurrent(); | |
565 | ||
566 | #if USE_CDSA_CRYPTO | |
567 | rv = CERT_VerifyCert(keychainOrArray, cert, policies, stime, trustRef); | |
568 | #else | |
569 | rv = CERT_VerifyCert(keychainOrArray, certs, policies, stime, trustRef); | |
570 | CFRelease(certs); | |
571 | #endif | |
b1ab9ed8 A |
572 | if (rv || !trustRef) |
573 | { | |
574 | if (PORT_GetError() == SEC_ERROR_UNTRUSTED_CERT) | |
575 | { | |
576 | /* Signature or digest level verificationStatus errors should supercede certificate level errors, so only change the verificationStatus if the status was GoodSignature. */ | |
577 | if (signerinfo->verificationStatus == SecCmsVSGoodSignature) | |
578 | signerinfo->verificationStatus = SecCmsVSSigningCertNotTrusted; | |
579 | } | |
580 | } | |
b1ab9ed8 | 581 | |
d8f41ccd | 582 | return rv; |
b1ab9ed8 A |
583 | } |
584 | ||
585 | /* | |
586 | * SecCmsSignerInfoVerify - verify the signature of a single SignerInfo | |
587 | * | |
588 | * Just verifies the signature. The assumption is that verification of the certificate | |
589 | * is done already. | |
590 | */ | |
591 | OSStatus | |
d8f41ccd | 592 | SecCmsSignerInfoVerify(SecCmsSignerInfoRef signerinfo, SecAsn1Item * digest, SecAsn1Item * contentType) |
b1ab9ed8 A |
593 | { |
594 | SecPublicKeyRef publickey = NULL; | |
595 | SecCmsAttribute *attr; | |
d8f41ccd | 596 | SecAsn1Item encoded_attrs; |
b1ab9ed8 A |
597 | SecCertificateRef cert; |
598 | SecCmsVerificationStatus vs = SecCmsVSUnverified; | |
599 | PLArenaPool *poolp; | |
600 | SECOidTag digestAlgTag, digestEncAlgTag; | |
d8f41ccd | 601 | |
b1ab9ed8 A |
602 | if (signerinfo == NULL) |
603 | return SECFailure; | |
d8f41ccd | 604 | |
b1ab9ed8 A |
605 | /* SecCmsSignerInfoGetSigningCertificate will fail if 2nd parm is NULL and */ |
606 | /* cert has not been verified */ | |
607 | if ((cert = SecCmsSignerInfoGetSigningCertificate(signerinfo, NULL)) == NULL) { | |
b1ab9ed8 A |
608 | vs = SecCmsVSSigningCertNotFound; |
609 | goto loser; | |
610 | } | |
611 | ||
d8f41ccd | 612 | #if USE_CDSA_CRYPTO |
b1ab9ed8 A |
613 | if (SecCertificateCopyPublicKey(cert, &publickey)) { |
614 | vs = SecCmsVSProcessingError; | |
615 | goto loser; | |
616 | } | |
d8f41ccd A |
617 | #else |
618 | publickey = SecCertificateCopyPublicKey(cert); | |
619 | if (publickey == NULL) | |
620 | goto loser; | |
621 | #endif | |
b1ab9ed8 A |
622 | |
623 | digestAlgTag = SECOID_GetAlgorithmTag(&(signerinfo->digestAlg)); | |
624 | digestEncAlgTag = SECOID_GetAlgorithmTag(&(signerinfo->digestEncAlg)); | |
b1ab9ed8 A |
625 | if (!SecCmsArrayIsEmpty((void **)signerinfo->authAttr)) { |
626 | if (contentType) { | |
627 | /* | |
628 | * Check content type | |
629 | * | |
630 | * RFC2630 sez that if there are any authenticated attributes, | |
631 | * then there must be one for content type which matches the | |
632 | * content type of the content being signed, and there must | |
633 | * be one for message digest which matches our message digest. | |
634 | * So check these things first. | |
635 | */ | |
636 | if ((attr = SecCmsAttributeArrayFindAttrByOidTag(signerinfo->authAttr, | |
637 | SEC_OID_PKCS9_CONTENT_TYPE, PR_TRUE)) == NULL) | |
638 | { | |
639 | vs = SecCmsVSMalformedSignature; | |
640 | goto loser; | |
641 | } | |
642 | ||
643 | if (SecCmsAttributeCompareValue(attr, contentType) == PR_FALSE) { | |
644 | vs = SecCmsVSMalformedSignature; | |
645 | goto loser; | |
646 | } | |
647 | } | |
648 | ||
649 | /* | |
650 | * Check digest | |
651 | */ | |
652 | if ((attr = SecCmsAttributeArrayFindAttrByOidTag(signerinfo->authAttr, SEC_OID_PKCS9_MESSAGE_DIGEST, PR_TRUE)) == NULL) | |
653 | { | |
654 | vs = SecCmsVSMalformedSignature; | |
655 | goto loser; | |
656 | } | |
657 | if (SecCmsAttributeCompareValue(attr, digest) == PR_FALSE) { | |
658 | vs = SecCmsVSDigestMismatch; | |
659 | goto loser; | |
660 | } | |
661 | ||
662 | if ((poolp = PORT_NewArena (1024)) == NULL) { | |
663 | vs = SecCmsVSProcessingError; | |
664 | goto loser; | |
665 | } | |
666 | ||
667 | /* | |
668 | * Check signature | |
669 | * | |
670 | * The signature is based on a digest of the DER-encoded authenticated | |
671 | * attributes. So, first we encode and then we digest/verify. | |
672 | * we trust the decoder to have the attributes in the right (sorted) order | |
673 | */ | |
674 | encoded_attrs.Data = NULL; | |
675 | encoded_attrs.Length = 0; | |
676 | ||
677 | if (SecCmsAttributeArrayEncode(poolp, &(signerinfo->authAttr), &encoded_attrs) == NULL || | |
678 | encoded_attrs.Data == NULL || encoded_attrs.Length == 0) | |
679 | { | |
680 | vs = SecCmsVSProcessingError; | |
681 | goto loser; | |
682 | } | |
d8f41ccd A |
683 | if (errSecSuccess == SecKeyDigestAndVerify(publickey, &signerinfo->digestAlg, encoded_attrs.Data, encoded_attrs.Length, signerinfo->encDigest.Data, signerinfo->encDigest.Length)) |
684 | vs = SecCmsVSGoodSignature; | |
685 | else | |
686 | vs = SecCmsVSBadSignature; | |
b1ab9ed8 A |
687 | |
688 | PORT_FreeArena(poolp, PR_FALSE); /* awkward memory management :-( */ | |
689 | ||
690 | } else { | |
d8f41ccd | 691 | SecAsn1Item * sig; |
b1ab9ed8 A |
692 | |
693 | /* No authenticated attributes. The signature is based on the plain message digest. */ | |
694 | sig = &(signerinfo->encDigest); | |
695 | if (sig->Length == 0) | |
696 | goto loser; | |
697 | ||
d8f41ccd A |
698 | if (SecKeyVerifyDigest(publickey, &signerinfo->digestAlg, digest->Data, digest->Length, sig->Data, sig->Length)) |
699 | vs = SecCmsVSBadSignature; | |
700 | else | |
701 | vs = SecCmsVSGoodSignature; | |
b1ab9ed8 A |
702 | } |
703 | ||
704 | if (vs == SecCmsVSBadSignature) { | |
705 | /* | |
706 | * XXX Change the generic error into our specific one, because | |
707 | * in that case we get a better explanation out of the Security | |
708 | * Advisor. This is really a bug in our error strings (the | |
709 | * "generic" error has a lousy/wrong message associated with it | |
710 | * which assumes the signature verification was done for the | |
711 | * purposes of checking the issuer signature on a certificate) | |
712 | * but this is at least an easy workaround and/or in the | |
713 | * Security Advisor, which specifically checks for the error | |
714 | * SEC_ERROR_PKCS7_BAD_SIGNATURE and gives more explanation | |
715 | * in that case but does not similarly check for | |
716 | * SEC_ERROR_BAD_SIGNATURE. It probably should, but then would | |
717 | * probably say the wrong thing in the case that it *was* the | |
718 | * certificate signature check that failed during the cert | |
719 | * verification done above. Our error handling is really a mess. | |
720 | */ | |
721 | if (PORT_GetError() == SEC_ERROR_BAD_SIGNATURE) | |
722 | PORT_SetError(SEC_ERROR_PKCS7_BAD_SIGNATURE); | |
723 | } | |
724 | ||
725 | if (publickey != NULL) | |
726 | CFRelease(publickey); | |
727 | ||
728 | signerinfo->verificationStatus = vs; | |
b1ab9ed8 A |
729 | |
730 | return (vs == SecCmsVSGoodSignature) ? SECSuccess : SECFailure; | |
731 | ||
732 | loser: | |
733 | if (publickey != NULL) | |
734 | SECKEY_DestroyPublicKey (publickey); | |
735 | ||
b1ab9ed8 A |
736 | signerinfo->verificationStatus = vs; |
737 | ||
738 | PORT_SetError (SEC_ERROR_PKCS7_BAD_SIGNATURE); | |
739 | return SECFailure; | |
740 | } | |
741 | ||
b1ab9ed8 A |
742 | SecCmsVerificationStatus |
743 | SecCmsSignerInfoGetVerificationStatus(SecCmsSignerInfoRef signerinfo) | |
744 | { | |
745 | return signerinfo->verificationStatus; | |
746 | } | |
747 | ||
748 | SECOidData * | |
749 | SecCmsSignerInfoGetDigestAlg(SecCmsSignerInfoRef signerinfo) | |
750 | { | |
751 | return SECOID_FindOID (&(signerinfo->digestAlg.algorithm)); | |
752 | } | |
753 | ||
754 | SECOidTag | |
755 | SecCmsSignerInfoGetDigestAlgTag(SecCmsSignerInfoRef signerinfo) | |
756 | { | |
757 | SECOidData *algdata; | |
758 | ||
759 | algdata = SECOID_FindOID (&(signerinfo->digestAlg.algorithm)); | |
760 | if (algdata != NULL) | |
761 | return algdata->offset; | |
762 | else | |
763 | return SEC_OID_UNKNOWN; | |
764 | } | |
765 | ||
766 | CFArrayRef | |
767 | SecCmsSignerInfoGetCertList(SecCmsSignerInfoRef signerinfo) | |
768 | { | |
b1ab9ed8 A |
769 | return signerinfo->certList; |
770 | } | |
771 | ||
b1ab9ed8 A |
772 | int |
773 | SecCmsSignerInfoGetVersion(SecCmsSignerInfoRef signerinfo) | |
774 | { | |
775 | unsigned long version; | |
776 | ||
d8f41ccd | 777 | /* always take apart the SecAsn1Item */ |
b1ab9ed8 A |
778 | if (SEC_ASN1DecodeInteger(&(signerinfo->version), &version) != SECSuccess) |
779 | return 0; | |
780 | else | |
781 | return (int)version; | |
782 | } | |
783 | ||
784 | /* | |
785 | * SecCmsSignerInfoGetSigningTime - return the signing time, | |
786 | * in UTCTime format, of a CMS signerInfo. | |
787 | * | |
788 | * sinfo - signerInfo data for this signer | |
789 | * | |
790 | * Returns a pointer to XXXX (what?) | |
791 | * A return value of NULL is an error. | |
792 | */ | |
793 | OSStatus | |
794 | SecCmsSignerInfoGetSigningTime(SecCmsSignerInfoRef sinfo, CFAbsoluteTime *stime) | |
795 | { | |
796 | SecCmsAttribute *attr; | |
d8f41ccd | 797 | SecAsn1Item * value; |
b1ab9ed8 A |
798 | |
799 | if (sinfo == NULL) | |
d8f41ccd | 800 | return SECFailure; |
b1ab9ed8 A |
801 | |
802 | if (sinfo->signingTime != 0) { | |
803 | *stime = sinfo->signingTime; /* cached copy */ | |
804 | return SECSuccess; | |
805 | } | |
806 | ||
807 | attr = SecCmsAttributeArrayFindAttrByOidTag(sinfo->authAttr, SEC_OID_PKCS9_SIGNING_TIME, PR_TRUE); | |
808 | /* XXXX multi-valued attributes NIH */ | |
809 | if (attr == NULL || (value = SecCmsAttributeGetValue(attr)) == NULL) | |
d8f41ccd | 810 | return SECFailure; |
b1ab9ed8 | 811 | if (DER_UTCTimeToCFDate(value, stime) != SECSuccess) |
d8f41ccd | 812 | return SECFailure; |
b1ab9ed8 A |
813 | sinfo->signingTime = *stime; /* make cached copy */ |
814 | return SECSuccess; | |
815 | } | |
816 | ||
b1ab9ed8 A |
817 | /* |
818 | * Return the signing cert of a CMS signerInfo. | |
819 | * | |
820 | * the certs in the enclosing SignedData must have been imported already | |
821 | */ | |
822 | SecCertificateRef | |
823 | SecCmsSignerInfoGetSigningCertificate(SecCmsSignerInfoRef signerinfo, SecKeychainRef keychainOrArray) | |
824 | { | |
d8f41ccd A |
825 | SecCertificateRef cert = NULL; |
826 | ||
827 | if (signerinfo->cert != NULL) | |
b1ab9ed8 | 828 | return signerinfo->cert; |
d8f41ccd A |
829 | |
830 | /* @@@ Make sure we search though all the certs in the cms message itself as well, it's silly | |
831 | to require them to be added to a keychain first. */ | |
832 | ||
833 | #if USE_CDSA_CRYPTO | |
834 | SecCmsSignerIdentifier *sid; | |
835 | ||
b1ab9ed8 A |
836 | /* |
837 | * This cert will also need to be freed, but since we save it | |
838 | * in signerinfo for later, we do not want to destroy it when | |
839 | * we leave this function -- we let the clean-up of the entire | |
840 | * cinfo structure later do the destroy of this cert. | |
841 | */ | |
842 | sid = &signerinfo->signerIdentifier; | |
843 | switch (sid->identifierType) { | |
844 | case SecCmsSignerIDIssuerSN: | |
d8f41ccd | 845 | cert = CERT_FindCertByIssuerAndSN(keychainOrArray, sid->id.issuerAndSN); |
b1ab9ed8 A |
846 | break; |
847 | case SecCmsSignerIDSubjectKeyID: | |
d8f41ccd | 848 | cert = CERT_FindCertBySubjectKeyID(keychainOrArray, sid->id.subjectKeyID); |
b1ab9ed8 A |
849 | break; |
850 | default: | |
851 | cert = NULL; | |
852 | break; | |
853 | } | |
854 | ||
855 | /* cert can be NULL at that point */ | |
856 | signerinfo->cert = cert; /* earmark it */ | |
d8f41ccd A |
857 | #else |
858 | SecAsn1Item **cert_datas = signerinfo->signedData->rawCerts; | |
859 | SecAsn1Item *cert_data; | |
860 | if (cert_datas) while ((cert_data = *cert_datas) != NULL) { | |
861 | cert = SecCertificateCreateWithBytes(NULL, cert_data->Data, cert_data->Length); | |
862 | if (cert) { | |
863 | switch (signerinfo->signerIdentifier.identifierType) { | |
864 | case SecCmsSignerIDIssuerSN: | |
865 | if (CERT_CheckIssuerAndSerial(cert, | |
866 | &(signerinfo->signerIdentifier.id.issuerAndSN->derIssuer), | |
867 | &(signerinfo->signerIdentifier.id.issuerAndSN->serialNumber))) | |
868 | signerinfo->cert = cert; | |
869 | break; | |
870 | case SecCmsSignerIDSubjectKeyID: { | |
871 | CFDataRef cert_keyid = SecCertificateGetSubjectKeyID(cert); | |
872 | SecAsn1Item *tbf_keyid = signerinfo->signerIdentifier.id.subjectKeyID; | |
873 | if (tbf_keyid->Length == (size_t)CFDataGetLength(cert_keyid) && | |
874 | !memcmp(tbf_keyid->Data, CFDataGetBytePtr(cert_keyid), tbf_keyid->Length)) | |
875 | signerinfo->cert = cert; | |
876 | } | |
877 | } | |
878 | if (signerinfo->cert) | |
879 | break; | |
880 | CFReleaseNull(cert); | |
881 | } | |
882 | cert_datas++; | |
883 | } | |
884 | ||
885 | if (!signerinfo->cert && (signerinfo->signerIdentifier.identifierType == SecCmsSignerIDIssuerSN)) { | |
886 | cert = CERT_FindCertificateByIssuerAndSN(signerinfo->signedData->certs, signerinfo->signerIdentifier.id.issuerAndSN); | |
887 | signerinfo->cert = cert; | |
888 | } | |
889 | #endif | |
b1ab9ed8 A |
890 | |
891 | return cert; | |
892 | } | |
893 | ||
d8f41ccd | 894 | |
b1ab9ed8 A |
895 | /* |
896 | * SecCmsSignerInfoGetSignerCommonName - return the common name of the signer | |
897 | * | |
898 | * sinfo - signerInfo data for this signer | |
899 | * | |
900 | * Returns a CFStringRef containing the common name of the signer. | |
901 | * A return value of NULL is an error. | |
902 | */ | |
903 | CFStringRef | |
904 | SecCmsSignerInfoGetSignerCommonName(SecCmsSignerInfoRef sinfo) | |
905 | { | |
906 | SecCertificateRef signercert; | |
907 | CFStringRef commonName = NULL; | |
908 | ||
909 | /* will fail if cert is not verified */ | |
910 | if ((signercert = SecCmsSignerInfoGetSigningCertificate(sinfo, NULL)) == NULL) | |
911 | return NULL; | |
912 | ||
d8f41ccd A |
913 | #if USE_CDSA_CRYPTO |
914 | SecCertificateGetCommonName(signercert, &commonName); | |
915 | #else | |
916 | CFArrayRef commonNames = SecCertificateCopyCommonNames(signercert); | |
917 | if (commonNames) { | |
918 | /* SecCertificateCopyCommonNames doesn't return empty arrays */ | |
919 | commonName = (CFStringRef)CFArrayGetValueAtIndex(commonNames, CFArrayGetCount(commonNames) - 1); | |
920 | CFRetain(commonName); | |
921 | CFRelease(commonNames); | |
922 | } | |
923 | #endif | |
b1ab9ed8 A |
924 | |
925 | return commonName; | |
926 | } | |
927 | ||
928 | /* | |
929 | * SecCmsSignerInfoGetSignerEmailAddress - return the email address of the signer | |
930 | * | |
931 | * sinfo - signerInfo data for this signer | |
932 | * | |
933 | * Returns a CFStringRef containing the name of the signer. | |
934 | * A return value of NULL is an error. | |
935 | */ | |
936 | CFStringRef | |
937 | SecCmsSignerInfoGetSignerEmailAddress(SecCmsSignerInfoRef sinfo) | |
938 | { | |
939 | SecCertificateRef signercert; | |
940 | CFStringRef emailAddress = NULL; | |
941 | ||
942 | if ((signercert = SecCmsSignerInfoGetSigningCertificate(sinfo, NULL)) == NULL) | |
943 | return NULL; | |
944 | ||
d8f41ccd | 945 | #if USE_CDSA_CRYPTO |
b1ab9ed8 | 946 | SecCertificateGetEmailAddress(signercert, &emailAddress); |
d8f41ccd A |
947 | #else |
948 | CFArrayRef names = SecCertificateCopyRFC822Names(signercert); | |
949 | if (names) { | |
950 | if (CFArrayGetCount(names) > 0) | |
951 | emailAddress = (CFStringRef)CFArrayGetValueAtIndex(names, 0); | |
952 | if (emailAddress) | |
953 | CFRetain(emailAddress); | |
954 | CFRelease(names); | |
955 | } | |
956 | #endif | |
b1ab9ed8 A |
957 | return emailAddress; |
958 | } | |
959 | ||
960 | ||
961 | /* | |
962 | * SecCmsSignerInfoAddAuthAttr - add an attribute to the | |
963 | * authenticated (i.e. signed) attributes of "signerinfo". | |
964 | */ | |
965 | OSStatus | |
966 | SecCmsSignerInfoAddAuthAttr(SecCmsSignerInfoRef signerinfo, SecCmsAttribute *attr) | |
967 | { | |
d8f41ccd | 968 | return SecCmsAttributeArrayAddAttr(signerinfo->signedData->contentInfo.cmsg->poolp, &(signerinfo->authAttr), attr); |
b1ab9ed8 A |
969 | } |
970 | ||
971 | /* | |
972 | * SecCmsSignerInfoAddUnauthAttr - add an attribute to the | |
973 | * unauthenticated attributes of "signerinfo". | |
974 | */ | |
975 | OSStatus | |
976 | SecCmsSignerInfoAddUnauthAttr(SecCmsSignerInfoRef signerinfo, SecCmsAttribute *attr) | |
977 | { | |
d8f41ccd | 978 | return SecCmsAttributeArrayAddAttr(signerinfo->signedData->contentInfo.cmsg->poolp, &(signerinfo->unAuthAttr), attr); |
b1ab9ed8 A |
979 | } |
980 | ||
981 | /* | |
982 | * SecCmsSignerInfoAddSigningTime - add the signing time to the | |
983 | * authenticated (i.e. signed) attributes of "signerinfo". | |
984 | * | |
985 | * This is expected to be included in outgoing signed | |
986 | * messages for email (S/MIME) but is likely useful in other situations. | |
987 | * | |
988 | * This should only be added once; a second call will do nothing. | |
989 | * | |
990 | * XXX This will probably just shove the current time into "signerinfo" | |
991 | * but it will not actually get signed until the entire item is | |
992 | * processed for encoding. Is this (expected to be small) delay okay? | |
993 | */ | |
994 | OSStatus | |
995 | SecCmsSignerInfoAddSigningTime(SecCmsSignerInfoRef signerinfo, CFAbsoluteTime t) | |
996 | { | |
997 | SecCmsAttribute *attr; | |
d8f41ccd | 998 | SecAsn1Item stime; |
b1ab9ed8 A |
999 | void *mark; |
1000 | PLArenaPool *poolp; | |
1001 | ||
d8f41ccd | 1002 | poolp = signerinfo->signedData->contentInfo.cmsg->poolp; |
b1ab9ed8 A |
1003 | |
1004 | mark = PORT_ArenaMark(poolp); | |
1005 | ||
1006 | /* create new signing time attribute */ | |
1007 | if (DER_CFDateToUTCTime(t, &stime) != SECSuccess) | |
1008 | goto loser; | |
1009 | ||
1010 | if ((attr = SecCmsAttributeCreate(poolp, SEC_OID_PKCS9_SIGNING_TIME, &stime, PR_FALSE)) == NULL) { | |
1011 | SECITEM_FreeItem (&stime, PR_FALSE); | |
1012 | goto loser; | |
1013 | } | |
1014 | ||
1015 | SECITEM_FreeItem (&stime, PR_FALSE); | |
1016 | ||
1017 | if (SecCmsSignerInfoAddAuthAttr(signerinfo, attr) != SECSuccess) | |
1018 | goto loser; | |
1019 | ||
1020 | PORT_ArenaUnmark (poolp, mark); | |
1021 | ||
1022 | return SECSuccess; | |
1023 | ||
1024 | loser: | |
1025 | PORT_ArenaRelease (poolp, mark); | |
1026 | return SECFailure; | |
1027 | } | |
1028 | ||
1029 | /* | |
1030 | * SecCmsSignerInfoAddSMIMECaps - add a SMIMECapabilities attribute to the | |
1031 | * authenticated (i.e. signed) attributes of "signerinfo". | |
1032 | * | |
1033 | * This is expected to be included in outgoing signed | |
1034 | * messages for email (S/MIME). | |
1035 | */ | |
1036 | OSStatus | |
1037 | SecCmsSignerInfoAddSMIMECaps(SecCmsSignerInfoRef signerinfo) | |
1038 | { | |
1039 | SecCmsAttribute *attr; | |
d8f41ccd | 1040 | SecAsn1Item * smimecaps = NULL; |
b1ab9ed8 A |
1041 | void *mark; |
1042 | PLArenaPool *poolp; | |
1043 | ||
d8f41ccd | 1044 | poolp = signerinfo->signedData->contentInfo.cmsg->poolp; |
b1ab9ed8 A |
1045 | |
1046 | mark = PORT_ArenaMark(poolp); | |
1047 | ||
1048 | smimecaps = SECITEM_AllocItem(poolp, NULL, 0); | |
1049 | if (smimecaps == NULL) | |
1050 | goto loser; | |
1051 | ||
1052 | /* create new signing time attribute */ | |
1053 | #if 1 | |
1054 | // @@@ We don't do Fortezza yet. | |
d8f41ccd | 1055 | if (SecSMIMECreateSMIMECapabilities(poolp, smimecaps, PR_FALSE) != SECSuccess) |
b1ab9ed8 A |
1056 | #else |
1057 | if (SecSMIMECreateSMIMECapabilities(poolp, smimecaps, | |
1058 | PK11_FortezzaHasKEA(signerinfo->cert)) != SECSuccess) | |
1059 | #endif | |
1060 | goto loser; | |
1061 | ||
1062 | if ((attr = SecCmsAttributeCreate(poolp, SEC_OID_PKCS9_SMIME_CAPABILITIES, smimecaps, PR_TRUE)) == NULL) | |
1063 | goto loser; | |
1064 | ||
1065 | if (SecCmsSignerInfoAddAuthAttr(signerinfo, attr) != SECSuccess) | |
1066 | goto loser; | |
1067 | ||
1068 | PORT_ArenaUnmark (poolp, mark); | |
1069 | return SECSuccess; | |
1070 | ||
1071 | loser: | |
1072 | PORT_ArenaRelease (poolp, mark); | |
1073 | return SECFailure; | |
1074 | } | |
1075 | ||
1076 | /* | |
1077 | * SecCmsSignerInfoAddSMIMEEncKeyPrefs - add a SMIMEEncryptionKeyPreferences attribute to the | |
1078 | * authenticated (i.e. signed) attributes of "signerinfo". | |
1079 | * | |
1080 | * This is expected to be included in outgoing signed messages for email (S/MIME). | |
1081 | */ | |
1082 | OSStatus | |
1083 | SecCmsSignerInfoAddSMIMEEncKeyPrefs(SecCmsSignerInfoRef signerinfo, SecCertificateRef cert, SecKeychainRef keychainOrArray) | |
1084 | { | |
1085 | SecCmsAttribute *attr; | |
d8f41ccd | 1086 | SecAsn1Item * smimeekp = NULL; |
b1ab9ed8 A |
1087 | void *mark; |
1088 | PLArenaPool *poolp; | |
1089 | ||
1090 | #if 0 | |
1091 | CFTypeRef policy; | |
1092 | ||
1093 | /* verify this cert for encryption */ | |
1094 | policy = CERT_PolicyForCertUsage(certUsageEmailRecipient); | |
1095 | if (CERT_VerifyCert(keychainOrArray, cert, policy, CFAbsoluteTimeGetCurrent(), NULL) != SECSuccess) { | |
1096 | CFRelease(policy); | |
1097 | return SECFailure; | |
1098 | } | |
1099 | CFRelease(policy); | |
1100 | #endif | |
1101 | ||
d8f41ccd | 1102 | poolp = signerinfo->signedData->contentInfo.cmsg->poolp; |
b1ab9ed8 A |
1103 | mark = PORT_ArenaMark(poolp); |
1104 | ||
1105 | smimeekp = SECITEM_AllocItem(poolp, NULL, 0); | |
1106 | if (smimeekp == NULL) | |
1107 | goto loser; | |
1108 | ||
1109 | /* create new signing time attribute */ | |
d8f41ccd | 1110 | if (SecSMIMECreateSMIMEEncKeyPrefs(poolp, smimeekp, cert) != SECSuccess) |
b1ab9ed8 A |
1111 | goto loser; |
1112 | ||
1113 | if ((attr = SecCmsAttributeCreate(poolp, SEC_OID_SMIME_ENCRYPTION_KEY_PREFERENCE, smimeekp, PR_TRUE)) == NULL) | |
1114 | goto loser; | |
1115 | ||
1116 | if (SecCmsSignerInfoAddAuthAttr(signerinfo, attr) != SECSuccess) | |
1117 | goto loser; | |
1118 | ||
1119 | PORT_ArenaUnmark (poolp, mark); | |
1120 | return SECSuccess; | |
1121 | ||
1122 | loser: | |
1123 | PORT_ArenaRelease (poolp, mark); | |
1124 | return SECFailure; | |
1125 | } | |
1126 | ||
1127 | /* | |
1128 | * SecCmsSignerInfoAddMSSMIMEEncKeyPrefs - add a SMIMEEncryptionKeyPreferences attribute to the | |
1129 | * authenticated (i.e. signed) attributes of "signerinfo", using the OID prefered by Microsoft. | |
1130 | * | |
1131 | * This is expected to be included in outgoing signed messages for email (S/MIME), | |
1132 | * if compatibility with Microsoft mail clients is wanted. | |
1133 | */ | |
1134 | OSStatus | |
1135 | SecCmsSignerInfoAddMSSMIMEEncKeyPrefs(SecCmsSignerInfoRef signerinfo, SecCertificateRef cert, SecKeychainRef keychainOrArray) | |
1136 | { | |
1137 | SecCmsAttribute *attr; | |
d8f41ccd | 1138 | SecAsn1Item * smimeekp = NULL; |
b1ab9ed8 A |
1139 | void *mark; |
1140 | PLArenaPool *poolp; | |
1141 | ||
1142 | #if 0 | |
1143 | CFTypeRef policy; | |
1144 | ||
1145 | /* verify this cert for encryption */ | |
1146 | policy = CERT_PolicyForCertUsage(certUsageEmailRecipient); | |
1147 | if (CERT_VerifyCert(keychainOrArray, cert, policy, CFAbsoluteTimeGetCurrent(), NULL) != SECSuccess) { | |
1148 | CFRelease(policy); | |
1149 | return SECFailure; | |
1150 | } | |
1151 | CFRelease(policy); | |
1152 | #endif | |
1153 | ||
d8f41ccd | 1154 | poolp = signerinfo->signedData->contentInfo.cmsg->poolp; |
b1ab9ed8 A |
1155 | mark = PORT_ArenaMark(poolp); |
1156 | ||
1157 | smimeekp = SECITEM_AllocItem(poolp, NULL, 0); | |
1158 | if (smimeekp == NULL) | |
1159 | goto loser; | |
1160 | ||
1161 | /* create new signing time attribute */ | |
d8f41ccd | 1162 | if (SecSMIMECreateMSSMIMEEncKeyPrefs(poolp, smimeekp, cert) != SECSuccess) |
b1ab9ed8 A |
1163 | goto loser; |
1164 | ||
1165 | if ((attr = SecCmsAttributeCreate(poolp, SEC_OID_MS_SMIME_ENCRYPTION_KEY_PREFERENCE, smimeekp, PR_TRUE)) == NULL) | |
1166 | goto loser; | |
1167 | ||
1168 | if (SecCmsSignerInfoAddAuthAttr(signerinfo, attr) != SECSuccess) | |
1169 | goto loser; | |
1170 | ||
1171 | PORT_ArenaUnmark (poolp, mark); | |
1172 | return SECSuccess; | |
1173 | ||
1174 | loser: | |
1175 | PORT_ArenaRelease (poolp, mark); | |
1176 | return SECFailure; | |
1177 | } | |
1178 | ||
b1ab9ed8 A |
1179 | /* |
1180 | * SecCmsSignerInfoAddCounterSignature - countersign a signerinfo | |
1181 | * | |
1182 | * 1. digest the DER-encoded signature value of the original signerinfo | |
1183 | * 2. create new signerinfo with correct version, sid, digestAlg | |
1184 | * 3. add message-digest authAttr, but NO content-type | |
1185 | * 4. sign the authAttrs | |
1186 | * 5. DER-encode the new signerInfo | |
1187 | * 6. add the whole thing to original signerInfo's unAuthAttrs | |
1188 | * as a SEC_OID_PKCS9_COUNTER_SIGNATURE attribute | |
1189 | * | |
1190 | * XXXX give back the new signerinfo? | |
1191 | */ | |
1192 | OSStatus | |
1193 | SecCmsSignerInfoAddCounterSignature(SecCmsSignerInfoRef signerinfo, | |
1194 | SECOidTag digestalg, SecIdentityRef identity) | |
1195 | { | |
1196 | /* XXXX TBD XXXX */ | |
1197 | return SECFailure; | |
1198 | } | |
1199 | ||
1200 | /* | |
1201 | * XXXX the following needs to be done in the S/MIME layer code | |
1202 | * after signature of a signerinfo is verified | |
1203 | */ | |
1204 | OSStatus | |
1205 | SecCmsSignerInfoSaveSMIMEProfile(SecCmsSignerInfoRef signerinfo) | |
1206 | { | |
d8f41ccd | 1207 | return -4 /*unImp*/; |
b1ab9ed8 A |
1208 | } |
1209 | ||
1210 | /* | |
1211 | * SecCmsSignerInfoIncludeCerts - set cert chain inclusion mode for this signer | |
1212 | */ | |
1213 | OSStatus | |
1214 | SecCmsSignerInfoIncludeCerts(SecCmsSignerInfoRef signerinfo, SecCmsCertChainMode cm, SECCertUsage usage) | |
1215 | { | |
1216 | if (signerinfo->cert == NULL) | |
1217 | return SECFailure; | |
1218 | ||
1219 | /* don't leak if we get called twice */ | |
1220 | if (signerinfo->certList != NULL) { | |
1221 | CFRelease(signerinfo->certList); | |
1222 | signerinfo->certList = NULL; | |
1223 | } | |
1224 | ||
1225 | switch (cm) { | |
1226 | case SecCmsCMNone: | |
1227 | signerinfo->certList = NULL; | |
1228 | break; | |
1229 | case SecCmsCMCertOnly: | |
1230 | signerinfo->certList = CERT_CertListFromCert(signerinfo->cert); | |
1231 | break; | |
1232 | case SecCmsCMCertChain: | |
1233 | signerinfo->certList = CERT_CertChainFromCert(signerinfo->cert, usage, PR_FALSE); | |
1234 | break; | |
1235 | case SecCmsCMCertChainWithRoot: | |
1236 | signerinfo->certList = CERT_CertChainFromCert(signerinfo->cert, usage, PR_TRUE); | |
1237 | break; | |
1238 | } | |
1239 | ||
1240 | if (cm != SecCmsCMNone && signerinfo->certList == NULL) | |
1241 | return SECFailure; | |
1242 | ||
1243 | return SECSuccess; | |
1244 | } |