]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_smime/lib/cmsdecode.c
421072220a7df1649ae9afa2010d6ea4f5b035f3
[apple/security.git] / OSX / libsecurity_smime / lib / cmsdecode.c
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 decoding.
36 */
37
38 #include <Security/SecCmsDecoder.h>
39 #include <Security/SecCmsContentInfo.h>
40 #include <Security/SecCmsDigestContext.h>
41 #include <Security/SecCmsMessage.h>
42
43 #include "cmslocal.h"
44
45 #include "secitem.h"
46 #include "secoid.h"
47 #include <security_asn1/secasn1.h>
48 #include <security_asn1/secerr.h>
49
50 struct SecCmsDecoderStr {
51 SEC_ASN1DecoderContext * dcx; /* ASN.1 decoder context */
52 SecCmsMessageRef cmsg; /* backpointer to the root message */
53 SECOidTag type; /* type of message */
54 SecCmsContent content; /* pointer to message */
55 SecCmsDecoderRef childp7dcx; /* inner CMS decoder context */
56 Boolean saw_contents;
57 int error;
58 SecCmsContentCallback cb;
59 void * cb_arg;
60 };
61
62 static void nss_cms_decoder_update_filter (void *arg, const char *data, size_t len,
63 int depth, SEC_ASN1EncodingPart data_kind);
64 static OSStatus nss_cms_before_data(SecCmsDecoderRef p7dcx);
65 static OSStatus nss_cms_after_data(SecCmsDecoderRef p7dcx);
66 static OSStatus nss_cms_after_end(SecCmsDecoderRef p7dcx);
67 static void nss_cms_decoder_work_data(SecCmsDecoderRef p7dcx,
68 const unsigned char *data, size_t len, Boolean final);
69
70 extern const SecAsn1Template SecCmsMessageTemplate[];
71
72 /*
73 * nss_cms_decoder_notify -
74 * this is the driver of the decoding process. It gets called by the ASN.1
75 * decoder before and after an object is decoded.
76 * at various points in the decoding process, we intercept to set up and do
77 * further processing.
78 */
79 static void
80 nss_cms_decoder_notify(void *arg, Boolean before, void *dest, int depth)
81 {
82 SecCmsDecoderRef p7dcx;
83 SecCmsContentInfoRef rootcinfo, cinfo;
84 Boolean after = !before;
85
86 p7dcx = (SecCmsDecoderRef)arg;
87 rootcinfo = &(p7dcx->cmsg->contentInfo);
88
89 /* XXX error handling: need to set p7dcx->error */
90
91 #ifdef CMSDEBUG
92 fprintf(stderr, "%6.6s, dest = %p, depth = %d\n", before ? "before" : "after", dest, depth);
93 #endif
94
95 /* so what are we working on right now? */
96 switch (p7dcx->type) {
97 case SEC_OID_UNKNOWN:
98 /*
99 * right now, we are still decoding the OUTER (root) cinfo
100 * As soon as we know the inner content type, set up the info,
101 * but NO inner decoder or filter. The root decoder handles the first
102 * level children by itself - only for encapsulated contents (which
103 * are encoded as DER inside of an OCTET STRING) we need to set up a
104 * child decoder...
105 */
106 if (after && dest == &(rootcinfo->contentType)) {
107 p7dcx->type = SecCmsContentInfoGetContentTypeTag(rootcinfo);
108 p7dcx->content = rootcinfo->content; /* is this ready already ? need to alloc? */
109 /* XXX yes we need to alloc -- continue here */
110 }
111 break;
112 case SEC_OID_PKCS7_DATA:
113 case SEC_OID_OTHER:
114 /* this can only happen if the outermost cinfo has DATA in it */
115 /* otherwise, we handle this type implicitely in the inner decoders */
116
117 if (before && dest == &(rootcinfo->content)) {
118 /* fake it to cause the filter to put the data in the right place... */
119 /* we want the ASN.1 decoder to deliver the decoded bytes to us from now on */
120 SEC_ASN1DecoderSetFilterProc(p7dcx->dcx,
121 nss_cms_decoder_update_filter,
122 p7dcx,
123 (Boolean)(p7dcx->cb != NULL));
124 break;
125 }
126
127 if (after && dest == &(rootcinfo->content.data)) {
128 /* remove the filter */
129 SEC_ASN1DecoderClearFilterProc(p7dcx->dcx);
130 }
131 break;
132
133 case SEC_OID_PKCS7_SIGNED_DATA:
134 case SEC_OID_PKCS7_ENVELOPED_DATA:
135 case SEC_OID_PKCS7_DIGESTED_DATA:
136 case SEC_OID_PKCS7_ENCRYPTED_DATA:
137
138 if (before && dest == &(rootcinfo->content))
139 break; /* we're not there yet */
140
141 if (p7dcx->content.pointer == NULL)
142 p7dcx->content = rootcinfo->content;
143
144 /* get this data type's inner contentInfo */
145 cinfo = SecCmsContentGetContentInfo(p7dcx->content.pointer, p7dcx->type);
146
147 if (before && dest == &(cinfo->contentType)) {
148 /* at this point, set up the &%$&$ back pointer */
149 /* we cannot do it later, because the content itself is optional! */
150 /* please give me C++ */
151 switch (p7dcx->type) {
152 case SEC_OID_PKCS7_SIGNED_DATA:
153 p7dcx->content.signedData->cmsg = p7dcx->cmsg;
154 break;
155 case SEC_OID_PKCS7_DIGESTED_DATA:
156 p7dcx->content.digestedData->cmsg = p7dcx->cmsg;
157 break;
158 case SEC_OID_PKCS7_ENVELOPED_DATA:
159 p7dcx->content.envelopedData->cmsg = p7dcx->cmsg;
160 break;
161 case SEC_OID_PKCS7_ENCRYPTED_DATA:
162 p7dcx->content.encryptedData->cmsg = p7dcx->cmsg;
163 break;
164 default:
165 PORT_Assert(0);
166 break;
167 }
168 }
169
170 if (before && dest == &(cinfo->rawContent)) {
171 /* we want the ASN.1 decoder to deliver the decoded bytes to us from now on */
172 SEC_ASN1DecoderSetFilterProc(p7dcx->dcx, nss_cms_decoder_update_filter,
173 p7dcx, (Boolean)(p7dcx->cb != NULL));
174
175
176 /* we're right in front of the data */
177 if (nss_cms_before_data(p7dcx) != SECSuccess) {
178 SEC_ASN1DecoderClearFilterProc(p7dcx->dcx); /* stop all processing */
179 p7dcx->error = PORT_GetError();
180 }
181 }
182 if (after && dest == &(cinfo->rawContent)) {
183 /* we're right after of the data */
184 if (nss_cms_after_data(p7dcx) != SECSuccess)
185 p7dcx->error = PORT_GetError();
186
187 /* we don't need to see the contents anymore */
188 SEC_ASN1DecoderClearFilterProc(p7dcx->dcx);
189 }
190 break;
191
192 #if 0 /* NIH */
193 case SEC_OID_PKCS7_AUTHENTICATED_DATA:
194 #endif
195 default:
196 /* unsupported or unknown message type - fail (more or less) gracefully */
197 p7dcx->error = SEC_ERROR_UNSUPPORTED_MESSAGE_TYPE;
198 break;
199 }
200 }
201
202 /*
203 * nss_cms_before_data - set up the current encoder to receive data
204 */
205 static OSStatus
206 nss_cms_before_data(SecCmsDecoderRef p7dcx)
207 {
208 OSStatus rv;
209 SECOidTag childtype;
210 PLArenaPool *poolp;
211 SecCmsDecoderRef childp7dcx;
212 SecCmsContentInfoRef cinfo;
213 const SecAsn1Template *template;
214 void *mark = NULL;
215 size_t size;
216
217 poolp = p7dcx->cmsg->poolp;
218
219 /* call _Decode_BeforeData handlers */
220 switch (p7dcx->type) {
221 case SEC_OID_PKCS7_SIGNED_DATA:
222 /* we're decoding a signedData, so set up the digests */
223 rv = SecCmsSignedDataDecodeBeforeData(p7dcx->content.signedData);
224 if (rv != SECSuccess)
225 return SECFailure;
226 break;
227 case SEC_OID_PKCS7_DIGESTED_DATA:
228 /* we're encoding a digestedData, so set up the digest */
229 rv = SecCmsDigestedDataDecodeBeforeData(p7dcx->content.digestedData);
230 if (rv != SECSuccess)
231 return SECFailure;
232 break;
233 case SEC_OID_PKCS7_ENVELOPED_DATA:
234 rv = SecCmsEnvelopedDataDecodeBeforeData(p7dcx->content.envelopedData);
235 if (rv != SECSuccess)
236 return SECFailure;
237 break;
238 case SEC_OID_PKCS7_ENCRYPTED_DATA:
239 rv = SecCmsEncryptedDataDecodeBeforeData(p7dcx->content.encryptedData);
240 if (rv != SECSuccess)
241 return SECFailure;
242 break;
243 default:
244 return SECFailure;
245 }
246
247 /* ok, now we have a pointer to cinfo */
248 /* find out what kind of data is encapsulated */
249
250 cinfo = SecCmsContentGetContentInfo(p7dcx->content.pointer, p7dcx->type);
251 childtype = SecCmsContentInfoGetContentTypeTag(cinfo);
252
253 /* special case for SignedData: "unknown" child type maps to SEC_OID_OTHER */
254 if((childtype == SEC_OID_UNKNOWN) && (p7dcx->type == SEC_OID_PKCS7_SIGNED_DATA)) {
255 childtype = SEC_OID_OTHER;
256 }
257
258 if ((childtype == SEC_OID_PKCS7_DATA) || (childtype == SEC_OID_OTHER)){
259 cinfo->content.data = SECITEM_AllocItem(poolp, NULL, 0);
260 if (cinfo->content.data == NULL)
261 /* set memory error */
262 return SECFailure;
263
264 p7dcx->childp7dcx = NULL;
265 return SECSuccess;
266 }
267
268 /* set up inner decoder */
269
270 if ((template = SecCmsUtilGetTemplateByTypeTag(childtype)) == NULL)
271 return SECFailure;
272
273 childp7dcx = (SecCmsDecoderRef)PORT_ZAlloc(sizeof(struct SecCmsDecoderStr));
274 if (childp7dcx == NULL)
275 return SECFailure;
276
277 mark = PORT_ArenaMark(poolp);
278
279 /* allocate space for the stuff we're creating */
280 size = SecCmsUtilGetSizeByTypeTag(childtype);
281 childp7dcx->content.pointer = (void *)PORT_ArenaZAlloc(poolp, size);
282 if (childp7dcx->content.pointer == NULL)
283 goto loser;
284
285 /* Apple: link the new content to parent ContentInfo */
286 cinfo->content.pointer = childp7dcx->content.pointer;
287
288 /* start the child decoder */
289 childp7dcx->dcx = SEC_ASN1DecoderStart(poolp, childp7dcx->content.pointer, template, NULL, 0);
290 if (childp7dcx->dcx == NULL)
291 goto loser;
292
293 /* the new decoder needs to notify, too */
294 SEC_ASN1DecoderSetNotifyProc(childp7dcx->dcx, nss_cms_decoder_notify, childp7dcx);
295
296 /* tell the parent decoder that it needs to feed us the content data */
297 p7dcx->childp7dcx = childp7dcx;
298
299 childp7dcx->type = childtype; /* our type */
300
301 childp7dcx->cmsg = p7dcx->cmsg; /* backpointer to root message */
302
303 /* should the child decoder encounter real data, it needs to give it to the caller */
304 childp7dcx->cb = p7dcx->cb;
305 childp7dcx->cb_arg = p7dcx->cb_arg;
306
307 /* now set up the parent to hand decoded data to the next level decoder */
308 p7dcx->cb = (SecCmsContentCallback)SecCmsDecoderUpdate;
309 p7dcx->cb_arg = childp7dcx;
310
311 PORT_ArenaUnmark(poolp, mark);
312
313 return SECSuccess;
314
315 loser:
316 if (mark)
317 PORT_ArenaRelease(poolp, mark);
318 if (childp7dcx)
319 PORT_Free(childp7dcx);
320 p7dcx->childp7dcx = NULL;
321 return SECFailure;
322 }
323
324 static OSStatus
325 nss_cms_after_data(SecCmsDecoderRef p7dcx)
326 {
327 PLArenaPool *poolp;
328 SecCmsDecoderRef childp7dcx;
329 OSStatus rv = SECFailure;
330
331 poolp = p7dcx->cmsg->poolp;
332
333 /* Handle last block. This is necessary to flush out the last bytes
334 * of a possibly incomplete block */
335 nss_cms_decoder_work_data(p7dcx, NULL, 0, PR_TRUE);
336
337 /* finish any "inner" decoders - there's no more data coming... */
338 if (p7dcx->childp7dcx != NULL) {
339 childp7dcx = p7dcx->childp7dcx;
340 if (childp7dcx->dcx != NULL) {
341 if (SEC_ASN1DecoderFinish(childp7dcx->dcx) != SECSuccess) {
342 /* do what? free content? */
343 rv = SECFailure;
344 } else {
345 rv = nss_cms_after_end(childp7dcx);
346 }
347 if (rv != SECSuccess)
348 goto done;
349 }
350 PORT_Free(p7dcx->childp7dcx);
351 p7dcx->childp7dcx = NULL;
352 }
353
354 switch (p7dcx->type) {
355 case SEC_OID_PKCS7_SIGNED_DATA:
356 /* this will finish the digests and verify */
357 rv = SecCmsSignedDataDecodeAfterData(p7dcx->content.signedData);
358 break;
359 case SEC_OID_PKCS7_ENVELOPED_DATA:
360 rv = SecCmsEnvelopedDataDecodeAfterData(p7dcx->content.envelopedData);
361 break;
362 case SEC_OID_PKCS7_DIGESTED_DATA:
363 rv = SecCmsDigestedDataDecodeAfterData(p7dcx->content.digestedData);
364 break;
365 case SEC_OID_PKCS7_ENCRYPTED_DATA:
366 rv = SecCmsEncryptedDataDecodeAfterData(p7dcx->content.encryptedData);
367 break;
368 case SEC_OID_PKCS7_DATA:
369 /* do nothing */
370 break;
371 default:
372 rv = SECFailure;
373 break;
374 }
375 done:
376 return rv;
377 }
378
379 static OSStatus
380 nss_cms_after_end(SecCmsDecoderRef p7dcx)
381 {
382 OSStatus rv;
383
384 switch (p7dcx->type) {
385 case SEC_OID_PKCS7_SIGNED_DATA:
386 rv = SecCmsSignedDataDecodeAfterEnd(p7dcx->content.signedData);
387 break;
388 case SEC_OID_PKCS7_ENVELOPED_DATA:
389 rv = SecCmsEnvelopedDataDecodeAfterEnd(p7dcx->content.envelopedData);
390 break;
391 case SEC_OID_PKCS7_DIGESTED_DATA:
392 rv = SecCmsDigestedDataDecodeAfterEnd(p7dcx->content.digestedData);
393 break;
394 case SEC_OID_PKCS7_ENCRYPTED_DATA:
395 rv = SecCmsEncryptedDataDecodeAfterEnd(p7dcx->content.encryptedData);
396 break;
397 case SEC_OID_PKCS7_DATA:
398 rv = SECSuccess;
399 break;
400 default:
401 rv = SECFailure; /* we should not have got that far... */
402 break;
403 }
404 return rv;
405 }
406
407 /*
408 * nss_cms_decoder_work_data - handle decoded data bytes.
409 *
410 * This function either decrypts the data if needed, and/or calculates digests
411 * on it, then either stores it or passes it on to the next level decoder.
412 */
413 static void
414 nss_cms_decoder_work_data(SecCmsDecoderRef p7dcx,
415 const unsigned char *data, size_t len,
416 Boolean final)
417 {
418 SecCmsContentInfoRef cinfo;
419 unsigned char *buf = NULL;
420 unsigned char *dest;
421 CSSM_SIZE offset;
422 OSStatus rv;
423 CSSM_DATA_PTR storage;
424
425 /*
426 * We should really have data to process, or we should be trying
427 * to finish/flush the last block. (This is an overly paranoid
428 * check since all callers are in this file and simple inspection
429 * proves they do it right. But it could find a bug in future
430 * modifications/development, that is why it is here.)
431 */
432 PORT_Assert ((data != NULL && len) || final);
433
434 if (!p7dcx->content.pointer) // might be ExContent??
435 return;
436
437 cinfo = SecCmsContentGetContentInfo(p7dcx->content.pointer, p7dcx->type);
438
439 if (cinfo->ciphcx != NULL) {
440 /*
441 * we are decrypting.
442 *
443 * XXX If we get an error, we do not want to do the digest or callback,
444 * but we want to keep decoding. Or maybe we want to stop decoding
445 * altogether if there is a callback, because obviously we are not
446 * sending the data back and they want to know that.
447 */
448
449 CSSM_SIZE outlen = 0; /* length of decrypted data */
450 CSSM_SIZE buflen; /* length available for decrypted data */
451
452 /* find out about the length of decrypted data */
453 buflen = SecCmsCipherContextDecryptLength(cinfo->ciphcx, len, final);
454
455 /*
456 * it might happen that we did not provide enough data for a full
457 * block (decryption unit), and that there is no output available
458 */
459
460 /* no output available, AND no input? */
461 if (buflen == 0 && len == 0)
462 goto loser; /* bail out */
463
464 /*
465 * have inner decoder: pass the data on (means inner content type is NOT data)
466 * no inner decoder: we have DATA in here: either call callback or store
467 */
468 if (buflen != 0) {
469 /* there will be some output - need to make room for it */
470 /* allocate buffer from the heap */
471 buf = (unsigned char *)PORT_Alloc(buflen);
472 if (buf == NULL) {
473 p7dcx->error = SEC_ERROR_NO_MEMORY;
474 goto loser;
475 }
476 }
477
478 /*
479 * decrypt incoming data
480 * buf can still be NULL here (and buflen == 0) here if we don't expect
481 * any output (see above), but we still need to call SecCmsCipherContextDecrypt to
482 * keep track of incoming data
483 */
484 rv = SecCmsCipherContextDecrypt(cinfo->ciphcx, buf, &outlen, buflen,
485 data, len, final);
486 if (rv != SECSuccess) {
487 p7dcx->error = PORT_GetError();
488 goto loser;
489 }
490
491 PORT_Assert (final || outlen == buflen);
492
493 /* swap decrypted data in */
494 data = buf;
495 len = outlen;
496 }
497
498 if (len == 0)
499 goto done; /* nothing more to do */
500
501 /*
502 * Update the running digests with plaintext bytes (if we need to).
503 */
504 if (cinfo->digcx)
505 SecCmsDigestContextUpdate(cinfo->digcx, data, len);
506
507 /* at this point, we have the plain decoded & decrypted data */
508 /* which is either more encoded DER which we need to hand to the child decoder */
509 /* or data we need to hand back to our caller */
510
511 /* pass the content back to our caller or */
512 /* feed our freshly decrypted and decoded data into child decoder */
513 if (p7dcx->cb != NULL) {
514 (*p7dcx->cb)(p7dcx->cb_arg, (const char *)data, len);
515 }
516 #if 1
517 else
518 #endif
519 switch(SecCmsContentInfoGetContentTypeTag(cinfo)) {
520 default:
521 break;
522 case SEC_OID_PKCS7_DATA:
523 case SEC_OID_OTHER:
524 /* store it in "inner" data item as well */
525 /* find the DATA item in the encapsulated cinfo and store it there */
526 storage = cinfo->content.data;
527
528 offset = storage->Length;
529
530 /* check for potential overflow */
531 if (len >= (size_t)(INT_MAX - storage->Length)) {
532 p7dcx->error = SEC_ERROR_NO_MEMORY;
533 goto loser;
534 }
535
536 if (storage->Length == 0) {
537 dest = (unsigned char *)PORT_ArenaAlloc(p7dcx->cmsg->poolp, len);
538 } else {
539 dest = (unsigned char *)PORT_ArenaGrow(p7dcx->cmsg->poolp,
540 storage->Data,
541 storage->Length,
542 storage->Length + len);
543 }
544 if (dest == NULL) {
545 p7dcx->error = SEC_ERROR_NO_MEMORY;
546 goto loser;
547 }
548
549 storage->Data = dest;
550 storage->Length += len;
551
552 /* copy it in */
553 PORT_Memcpy(storage->Data + offset, data, len);
554 }
555
556 done:
557 loser:
558 if (buf)
559 PORT_Free (buf);
560 }
561
562 /*
563 * nss_cms_decoder_update_filter - process ASN.1 data
564 *
565 * once we have set up a filter in nss_cms_decoder_notify(),
566 * all data processed by the ASN.1 decoder is also passed through here.
567 * we pass the content bytes (as opposed to length and tag bytes) on to
568 * nss_cms_decoder_work_data().
569 */
570 static void
571 nss_cms_decoder_update_filter (void *arg, const char *data, size_t len,
572 int depth, SEC_ASN1EncodingPart data_kind)
573 {
574 SecCmsDecoderRef p7dcx;
575
576 PORT_Assert (len); /* paranoia */
577 if (len == 0)
578 return;
579
580 p7dcx = (SecCmsDecoderRef)arg;
581
582 p7dcx->saw_contents = PR_TRUE;
583
584 /* pass on the content bytes only */
585 if (data_kind == SEC_ASN1_Contents)
586 nss_cms_decoder_work_data(p7dcx, (const unsigned char *) data, len, PR_FALSE);
587 }
588
589 /*
590 * SecCmsDecoderCreate - set up decoding of a BER-encoded CMS message
591 */
592 OSStatus
593 SecCmsDecoderCreate(SecArenaPoolRef pool,
594 SecCmsContentCallback cb, void *cb_arg,
595 PK11PasswordFunc pwfn, void *pwfn_arg,
596 SecCmsGetDecryptKeyCallback decrypt_key_cb, void *decrypt_key_cb_arg,
597 SecCmsDecoderRef *outDecoder)
598 {
599 SecCmsDecoderRef p7dcx;
600 SecCmsMessageRef cmsg;
601 OSStatus result;
602
603 cmsg = SecCmsMessageCreate(pool);
604 if (cmsg == NULL)
605 goto loser;
606
607 SecCmsMessageSetEncodingParams(cmsg, pwfn, pwfn_arg, decrypt_key_cb, decrypt_key_cb_arg,
608 NULL, NULL);
609
610 p7dcx = (SecCmsDecoderRef)PORT_ZAlloc(sizeof(struct SecCmsDecoderStr));
611 if (p7dcx == NULL) {
612 SecCmsMessageDestroy(cmsg);
613 goto loser;
614 }
615
616 p7dcx->dcx = SEC_ASN1DecoderStart(cmsg->poolp, cmsg, SecCmsMessageTemplate, NULL, 0);
617 if (p7dcx->dcx == NULL) {
618 PORT_Free (p7dcx);
619 SecCmsMessageDestroy(cmsg);
620 goto loser;
621 }
622
623 SEC_ASN1DecoderSetNotifyProc (p7dcx->dcx, nss_cms_decoder_notify, p7dcx);
624
625 p7dcx->cmsg = cmsg;
626 p7dcx->type = SEC_OID_UNKNOWN;
627
628 p7dcx->cb = cb;
629 p7dcx->cb_arg = cb_arg;
630
631 *outDecoder = p7dcx;
632 return noErr;
633
634 loser:
635 result = PORT_GetError();
636 return result;
637 }
638
639 /*
640 * SecCmsDecoderUpdate - feed DER-encoded data to decoder
641 */
642 OSStatus
643 SecCmsDecoderUpdate(SecCmsDecoderRef p7dcx, const void *buf, CFIndex len)
644 {
645 if (!p7dcx) {
646 return errSecParam;
647 }
648
649 if (p7dcx->dcx != NULL && p7dcx->error == 0) { /* if error is set already, don't bother */
650 if (SEC_ASN1DecoderUpdate (p7dcx->dcx, buf, len) != SECSuccess) {
651 p7dcx->error = PORT_GetError();
652 PORT_Assert (p7dcx->error);
653 if (p7dcx->error == 0)
654 p7dcx->error = -1;
655 }
656 }
657
658 if (p7dcx->error == 0)
659 return 0;
660
661 /* there has been a problem, let's finish the decoder */
662 if (p7dcx->dcx != NULL) {
663 /* @@@ Change this to SEC_ASN1DecoderAbort()? */
664 (void) SEC_ASN1DecoderFinish (p7dcx->dcx);
665 p7dcx->dcx = NULL;
666 }
667 PORT_SetError (p7dcx->error);
668
669 return p7dcx->error;
670 }
671
672 /*
673 * SecCmsDecoderDestroy - stop decoding in case of error
674 */
675 void
676 SecCmsDecoderDestroy(SecCmsDecoderRef p7dcx)
677 {
678 /* SecCmsMessageDestroy frees inner decoders and digests. */
679 if (p7dcx->cmsg) {
680 SecCmsMessageDestroy(p7dcx->cmsg);
681 }
682 if (p7dcx->dcx) {
683 (void)SEC_ASN1DecoderFinish(p7dcx->dcx);
684 }
685 /* Clear out references */
686 p7dcx->cmsg = NULL;
687 p7dcx->dcx = NULL;
688 p7dcx->childp7dcx = NULL;
689 PORT_Free(p7dcx);
690 }
691
692 /*
693 * SecCmsDecoderFinish - mark the end of inner content and finish decoding
694 */
695 OSStatus
696 SecCmsDecoderFinish(SecCmsDecoderRef p7dcx, SecCmsMessageRef *outMessage)
697 {
698 SecCmsMessageRef cmsg;
699 OSStatus result;
700
701 cmsg = p7dcx->cmsg;
702
703 if (p7dcx->dcx == NULL || SEC_ASN1DecoderFinish(p7dcx->dcx) != SECSuccess ||
704 nss_cms_after_end(p7dcx) != SECSuccess)
705 {
706 if (p7dcx->cmsg) {
707 SecCmsMessageDestroy(cmsg); /* needs to get rid of pool if it's ours */
708 }
709 result = PORT_GetError();
710 goto loser;
711 }
712
713 *outMessage = cmsg;
714 result = noErr;
715
716 loser:
717 /* Clear out references */
718 p7dcx->cmsg = NULL;
719 p7dcx->dcx = NULL;
720 p7dcx->childp7dcx = NULL;
721 PORT_Free(p7dcx);
722 return result;
723 }
724
725 OSStatus
726 SecCmsMessageDecode(const CSSM_DATA *encodedMessage,
727 SecCmsContentCallback cb, void *cb_arg,
728 PK11PasswordFunc pwfn, void *pwfn_arg,
729 SecCmsGetDecryptKeyCallback decrypt_key_cb, void *decrypt_key_cb_arg,
730 SecCmsMessageRef *outMessage)
731 {
732 OSStatus result;
733 SecCmsDecoderRef decoder;
734
735 result = SecCmsDecoderCreate(NULL, cb, cb_arg, pwfn, pwfn_arg, decrypt_key_cb, decrypt_key_cb_arg, &decoder);
736 if (result)
737 goto loser;
738 result = SecCmsDecoderUpdate(decoder, encodedMessage->Data, encodedMessage->Length);
739 if (result) {
740 SecCmsDecoderDestroy(decoder);
741 goto loser;
742 }
743
744 result = SecCmsDecoderFinish(decoder, outMessage);
745 loser:
746 return result;
747 }
748