]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_smime/lib/cmsencode.c
Security-58286.251.4.tar.gz
[apple/security.git] / OSX / libsecurity_smime / lib / cmsencode.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 encoding.
36 */
37
38 #include <Security/SecCmsEncoder.h>
39 #include <Security/SecCmsContentInfo.h>
40 #include <Security/SecCmsDigestContext.h>
41 #include <Security/SecCmsMessage.h>
42
43 #include "cmslocal.h"
44
45 #include "secoid.h"
46 #include "secitem.h"
47
48 #include <security_asn1/secasn1.h>
49 #include <security_asn1/secerr.h>
50 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
51
52 struct nss_cms_encoder_output {
53 SecCmsContentCallback outputfn;
54 void *outputarg;
55 PLArenaPool *destpoolp;
56 CSSM_DATA_PTR dest;
57 };
58
59 struct SecCmsEncoderStr {
60 SEC_ASN1EncoderContext * ecx; /* ASN.1 encoder context */
61 Boolean ecxupdated; /* true if data was handed in */
62 SecCmsMessageRef cmsg; /* pointer to the root message */
63 SECOidTag type; /* type tag of the current content */
64 SecCmsContent content; /* pointer to current content */
65 struct nss_cms_encoder_output output; /* output function */
66 int error; /* error code */
67 SecCmsEncoderRef childp7ecx; /* link to child encoder context */
68 };
69
70 static OSStatus nss_cms_before_data(SecCmsEncoderRef p7ecx);
71 static OSStatus nss_cms_after_data(SecCmsEncoderRef p7ecx);
72 static OSStatus nss_cms_encoder_update(SecCmsEncoderRef p7ecx, const char *data, size_t len);
73 static OSStatus nss_cms_encoder_work_data(SecCmsEncoderRef p7ecx, CSSM_DATA_PTR dest,
74 const unsigned char *data, size_t len,
75 Boolean final, Boolean innermost);
76
77 extern const SecAsn1Template SecCmsMessageTemplate[];
78
79 /*
80 * The little output function that the ASN.1 encoder calls to hand
81 * us bytes which we in turn hand back to our caller (via the callback
82 * they gave us).
83 */
84 static void
85 nss_cms_encoder_out(void *arg, const char *buf, size_t len,
86 int depth, SEC_ASN1EncodingPart data_kind)
87 {
88 struct nss_cms_encoder_output *output = (struct nss_cms_encoder_output *)arg;
89 unsigned char *dest;
90 CSSM_SIZE offset;
91
92 #ifdef CMSDEBUG
93 int i;
94
95 fprintf(stderr, "kind = %d, depth = %d, len = %lu\n", data_kind, depth, len);
96 for (i=0; i < len; i++) {
97 fprintf(stderr, " %02x%s", (unsigned int)buf[i] & 0xff, ((i % 16) == 15) ? "\n" : "");
98 }
99 if ((i % 16) != 0)
100 fprintf(stderr, "\n");
101 #endif
102
103 if (output->outputfn != NULL)
104 /* call output callback with DER data */
105 output->outputfn(output->outputarg, buf, len);
106
107 if (output->dest != NULL) {
108 /* store DER data in CSSM_DATA */
109 offset = output->dest->Length;
110 if (offset == 0) {
111 dest = (unsigned char *)PORT_ArenaAlloc(output->destpoolp, len);
112 } else {
113 dest = (unsigned char *)PORT_ArenaGrow(output->destpoolp,
114 output->dest->Data,
115 output->dest->Length,
116 output->dest->Length + len);
117 }
118 if (dest == NULL)
119 /* oops */
120 return;
121
122 output->dest->Data = dest;
123 output->dest->Length += len;
124
125 /* copy it in */
126 PORT_Memcpy(output->dest->Data + offset, buf, len);
127 }
128 }
129
130 /*
131 * nss_cms_encoder_notify - ASN.1 encoder callback
132 *
133 * this function is called by the ASN.1 encoder before and after the encoding of
134 * every object. here, it is used to keep track of data structures, set up
135 * encryption and/or digesting and possibly set up child encoders.
136 */
137 static void
138 nss_cms_encoder_notify(void *arg, Boolean before, void *dest, int depth)
139 {
140 SecCmsEncoderRef p7ecx;
141 SecCmsContentInfoRef rootcinfo, cinfo;
142 Boolean after = !before;
143 SECOidTag childtype;
144 CSSM_DATA_PTR item;
145
146 p7ecx = (SecCmsEncoderRef)arg;
147 PORT_Assert(p7ecx != NULL);
148
149 rootcinfo = &(p7ecx->cmsg->contentInfo);
150
151 #ifdef CMSDEBUG
152 fprintf(stderr, "%6.6s, dest = %p, depth = %d\n", before ? "before" : "after", dest, depth);
153 #endif
154
155 /*
156 * Watch for the content field, at which point we want to instruct
157 * the ASN.1 encoder to start taking bytes from the buffer.
158 */
159 switch (p7ecx->type) {
160 default:
161 case SEC_OID_UNKNOWN:
162 /* we're still in the root message */
163 if (after && dest == &(rootcinfo->contentType)) {
164 /* got the content type OID now - so find out the type tag */
165 p7ecx->type = SecCmsContentInfoGetContentTypeTag(rootcinfo);
166 /* set up a pointer to our current content */
167 p7ecx->content = rootcinfo->content;
168 }
169 break;
170
171 case SEC_OID_PKCS7_DATA:
172 case SEC_OID_OTHER:
173 if (before && dest == &(rootcinfo->rawContent)) {
174 /* just set up encoder to grab from user - no encryption or digesting */
175 if ((item = rootcinfo->content.data) != NULL)
176 (void)nss_cms_encoder_work_data(p7ecx, NULL, item->Data, item->Length, PR_TRUE, PR_TRUE);
177 else
178 SEC_ASN1EncoderSetTakeFromBuf(p7ecx->ecx);
179 SEC_ASN1EncoderClearNotifyProc(p7ecx->ecx); /* no need to get notified anymore */
180 }
181 break;
182
183 case SEC_OID_PKCS7_SIGNED_DATA:
184 case SEC_OID_PKCS7_ENVELOPED_DATA:
185 case SEC_OID_PKCS7_DIGESTED_DATA:
186 case SEC_OID_PKCS7_ENCRYPTED_DATA:
187
188 /* when we know what the content is, we encode happily until we reach the inner content */
189 cinfo = SecCmsContentGetContentInfo(p7ecx->content.pointer, p7ecx->type);
190 childtype = SecCmsContentInfoGetContentTypeTag(cinfo);
191
192 if (after && dest == &(cinfo->contentType)) {
193 /* we're right before encoding the data (if we have some or not) */
194 /* (for encrypted data, we're right before the contentEncAlg which may change */
195 /* in nss_cms_before_data because of IV calculation when setting up encryption) */
196 if (nss_cms_before_data(p7ecx) != SECSuccess) {
197 p7ecx->error = PORT_GetError();
198 PORT_SetError(0); // Clean the thread error since we've returned the error
199 }
200 }
201 if (before && dest == &(cinfo->rawContent)) {
202 if ( ((childtype == SEC_OID_PKCS7_DATA) || (childtype == SEC_OID_OTHER)) &&
203 ((item = cinfo->content.data) != NULL))
204 /* we have data - feed it in */
205 (void)nss_cms_encoder_work_data(p7ecx, NULL, item->Data, item->Length, PR_TRUE, PR_TRUE);
206 else
207 /* else try to get it from user */
208 SEC_ASN1EncoderSetTakeFromBuf(p7ecx->ecx);
209 }
210 if (after && dest == &(cinfo->rawContent)) {
211 if (nss_cms_after_data(p7ecx) != SECSuccess) {
212 p7ecx->error = PORT_GetError();
213 PORT_SetError(0); // Clean the thread error since we've returned the error
214 }
215 SEC_ASN1EncoderClearNotifyProc(p7ecx->ecx); /* no need to get notified anymore */
216 }
217 break;
218 }
219 }
220
221 /*
222 * nss_cms_before_data - setup the current encoder to receive data
223 */
224 static OSStatus
225 nss_cms_before_data(SecCmsEncoderRef p7ecx)
226 {
227 OSStatus rv;
228 SECOidTag childtype;
229 SecCmsContentInfoRef cinfo;
230 SecCmsEncoderRef childp7ecx;
231 const SecAsn1Template *template;
232
233 /* call _Encode_BeforeData handlers */
234 switch (p7ecx->type) {
235 case SEC_OID_PKCS7_SIGNED_DATA:
236 /* we're encoding a signedData, so set up the digests */
237 rv = SecCmsSignedDataEncodeBeforeData(p7ecx->content.signedData);
238 break;
239 case SEC_OID_PKCS7_DIGESTED_DATA:
240 /* we're encoding a digestedData, so set up the digest */
241 rv = SecCmsDigestedDataEncodeBeforeData(p7ecx->content.digestedData);
242 break;
243 case SEC_OID_PKCS7_ENVELOPED_DATA:
244 rv = SecCmsEnvelopedDataEncodeBeforeData(p7ecx->content.envelopedData);
245 break;
246 case SEC_OID_PKCS7_ENCRYPTED_DATA:
247 rv = SecCmsEncryptedDataEncodeBeforeData(p7ecx->content.encryptedData);
248 break;
249 default:
250 rv = SECFailure;
251 }
252 if (rv != SECSuccess)
253 return SECFailure;
254
255 /* ok, now we have a pointer to cinfo */
256 /* find out what kind of data is encapsulated */
257
258 cinfo = SecCmsContentGetContentInfo(p7ecx->content.pointer, p7ecx->type);
259 childtype = SecCmsContentInfoGetContentTypeTag(cinfo);
260
261 switch (childtype) {
262 case SEC_OID_PKCS7_SIGNED_DATA:
263 case SEC_OID_PKCS7_ENVELOPED_DATA:
264 case SEC_OID_PKCS7_ENCRYPTED_DATA:
265 case SEC_OID_PKCS7_DIGESTED_DATA:
266 #if 0
267 case SEC_OID_PKCS7_DATA: /* XXX here also??? maybe yes! */
268 #endif
269 /* in these cases, we need to set up a child encoder! */
270 /* create new encoder context */
271 childp7ecx = PORT_ZAlloc(sizeof(struct SecCmsEncoderStr));
272 if (childp7ecx == NULL)
273 return SECFailure;
274
275 /* the CHILD encoder needs to hand its encoded data to the CURRENT encoder
276 * (which will encrypt and/or digest it)
277 * this needs to route back into our update function
278 * which finds the lowest encoding context & encrypts and computes digests */
279 childp7ecx->type = childtype;
280 childp7ecx->content = cinfo->content;
281 /* use the non-recursive update function here, of course */
282 childp7ecx->output.outputfn = (SecCmsContentCallback)nss_cms_encoder_update;
283 childp7ecx->output.outputarg = p7ecx;
284 childp7ecx->output.destpoolp = NULL;
285 childp7ecx->output.dest = NULL;
286 childp7ecx->cmsg = p7ecx->cmsg;
287
288 template = SecCmsUtilGetTemplateByTypeTag(childtype);
289 if (template == NULL)
290 goto loser; /* cannot happen */
291
292 /* now initialize the data for encoding the first third */
293 switch (childp7ecx->type) {
294 case SEC_OID_PKCS7_SIGNED_DATA:
295 rv = SecCmsSignedDataEncodeBeforeStart(cinfo->content.signedData);
296 break;
297 case SEC_OID_PKCS7_ENVELOPED_DATA:
298 rv = SecCmsEnvelopedDataEncodeBeforeStart(cinfo->content.envelopedData);
299 break;
300 case SEC_OID_PKCS7_DIGESTED_DATA:
301 rv = SecCmsDigestedDataEncodeBeforeStart(cinfo->content.digestedData);
302 break;
303 case SEC_OID_PKCS7_ENCRYPTED_DATA:
304 rv = SecCmsEncryptedDataEncodeBeforeStart(cinfo->content.encryptedData);
305 break;
306 case SEC_OID_PKCS7_DATA:
307 case SEC_OID_OTHER:
308 rv = SECSuccess;
309 break;
310 default:
311 PORT_Assert(0);
312 break;
313 }
314 if (rv != SECSuccess)
315 goto loser;
316
317 /*
318 * Initialize the BER encoder.
319 */
320 childp7ecx->ecx = SEC_ASN1EncoderStart(cinfo->content.pointer, template,
321 nss_cms_encoder_out, &(childp7ecx->output));
322 if (childp7ecx->ecx == NULL)
323 goto loser;
324
325 childp7ecx->ecxupdated = PR_FALSE;
326
327 /*
328 * Indicate that we are streaming. We will be streaming until we
329 * get past the contents bytes.
330 */
331 SEC_ASN1EncoderSetStreaming(childp7ecx->ecx);
332
333 /*
334 * The notify function will watch for the contents field.
335 */
336 SEC_ASN1EncoderSetNotifyProc(childp7ecx->ecx, nss_cms_encoder_notify, childp7ecx);
337
338 /* please note that we are NOT calling SEC_ASN1EncoderUpdate here to kick off the */
339 /* encoding process - we'll do that from the update function instead */
340 /* otherwise we'd be encoding data from a call of the notify function of the */
341 /* parent encoder (which would not work) */
342
343 /* this will kick off the encoding process & encode everything up to the content bytes,
344 * at which point the notify function sets streaming mode (and possibly creates
345 * another child encoder). */
346 if (SEC_ASN1EncoderUpdate(childp7ecx->ecx, NULL, 0) != SECSuccess)
347 goto loser;
348
349 p7ecx->childp7ecx = childp7ecx;
350 break;
351
352 case SEC_OID_PKCS7_DATA:
353 case SEC_OID_OTHER:
354 p7ecx->childp7ecx = NULL;
355 break;
356 default:
357 /* we do not know this type */
358 p7ecx->error = SEC_ERROR_BAD_DER;
359 break;
360 }
361
362 return SECSuccess;
363
364 loser:
365 if (childp7ecx) {
366 if (childp7ecx->ecx)
367 SEC_ASN1EncoderFinish(childp7ecx->ecx);
368 PORT_Free(childp7ecx);
369 }
370 return SECFailure;
371 }
372
373 static OSStatus
374 nss_cms_after_data(SecCmsEncoderRef p7ecx)
375 {
376 OSStatus rv = SECFailure;
377
378 switch (p7ecx->type) {
379 case SEC_OID_PKCS7_SIGNED_DATA:
380 /* this will finish the digests and sign */
381 rv = SecCmsSignedDataEncodeAfterData(p7ecx->content.signedData);
382 break;
383 case SEC_OID_PKCS7_ENVELOPED_DATA:
384 rv = SecCmsEnvelopedDataEncodeAfterData(p7ecx->content.envelopedData);
385 break;
386 case SEC_OID_PKCS7_DIGESTED_DATA:
387 rv = SecCmsDigestedDataEncodeAfterData(p7ecx->content.digestedData);
388 break;
389 case SEC_OID_PKCS7_ENCRYPTED_DATA:
390 rv = SecCmsEncryptedDataEncodeAfterData(p7ecx->content.encryptedData);
391 break;
392 case SEC_OID_PKCS7_DATA:
393 case SEC_OID_OTHER:
394 /* do nothing */
395 break;
396 default:
397 rv = SECFailure;
398 break;
399 }
400 return rv;
401 }
402
403 /*
404 * nss_cms_encoder_work_data - process incoming data
405 *
406 * (from the user or the next encoding layer)
407 * Here, we need to digest and/or encrypt, then pass it on
408 */
409 static OSStatus
410 nss_cms_encoder_work_data(SecCmsEncoderRef p7ecx, CSSM_DATA_PTR dest,
411 const unsigned char *data, size_t len,
412 Boolean final, Boolean innermost)
413 {
414 unsigned char *buf = NULL;
415 OSStatus rv;
416 SecCmsContentInfoRef cinfo;
417
418 rv = SECSuccess; /* may as well be optimistic */
419
420 /*
421 * We should really have data to process, or we should be trying
422 * to finish/flush the last block. (This is an overly paranoid
423 * check since all callers are in this file and simple inspection
424 * proves they do it right. But it could find a bug in future
425 * modifications/development, that is why it is here.)
426 */
427 PORT_Assert ((data != NULL && len) || final);
428
429 /* we got data (either from the caller, or from a lower level encoder) */
430 cinfo = SecCmsContentGetContentInfo(p7ecx->content.pointer, p7ecx->type);
431
432 /* Update the running digest. */
433 if (len && cinfo->digcx != NULL)
434 SecCmsDigestContextUpdate(cinfo->digcx, data, len);
435
436 /* Encrypt this chunk. */
437 if (cinfo->ciphcx != NULL) {
438 CSSM_SIZE inlen; /* length of data being encrypted */
439 CSSM_SIZE outlen = 0; /* length of encrypted data */
440 CSSM_SIZE buflen; /* length available for encrypted data */
441
442 inlen = len;
443 buflen = SecCmsCipherContextEncryptLength(cinfo->ciphcx, inlen, final);
444 if (buflen == 0) {
445 /*
446 * No output is expected, but the input data may be buffered
447 * so we still have to call Encrypt.
448 */
449 rv = SecCmsCipherContextEncrypt(cinfo->ciphcx, NULL, NULL, 0,
450 data, inlen, final);
451 if (final) {
452 len = 0;
453 goto done;
454 }
455 return rv;
456 }
457
458 if (dest != NULL)
459 buf = (unsigned char*)PORT_ArenaAlloc(p7ecx->cmsg->poolp, buflen);
460 else
461 buf = (unsigned char*)PORT_Alloc(buflen);
462
463 if (buf == NULL) {
464 rv = SECFailure;
465 } else {
466 rv = SecCmsCipherContextEncrypt(cinfo->ciphcx, buf, &outlen, buflen,
467 data, inlen, final);
468 data = buf;
469 len = outlen;
470 }
471 if (rv != SECSuccess)
472 /* encryption or malloc failed? */
473 return rv;
474 }
475
476
477 /*
478 * at this point (data,len) has everything we'd like to give to the CURRENT encoder
479 * (which will encode it, then hand it back to the user or the parent encoder)
480 * We don't encode the data if we're innermost and we're told not to include the data
481 */
482 if (p7ecx->ecx != NULL && len && (!innermost || cinfo->rawContent != NULL))
483 rv = SEC_ASN1EncoderUpdate(p7ecx->ecx, (const char *)data, len);
484
485 done:
486
487 if (cinfo->ciphcx != NULL) {
488 if (dest != NULL) {
489 dest->Data = buf;
490 dest->Length = len;
491 } else if (buf != NULL) {
492 PORT_Free (buf);
493 }
494 }
495 return rv;
496 }
497
498 /*
499 * nss_cms_encoder_update - deliver encoded data to the next higher level
500 *
501 * no recursion here because we REALLY want to end up at the next higher encoder!
502 */
503 static OSStatus
504 nss_cms_encoder_update(SecCmsEncoderRef p7ecx, const char *data, size_t len)
505 {
506 /* XXX Error handling needs help. Return what? Do "Finish" on failure? */
507 return nss_cms_encoder_work_data (p7ecx, NULL, (const unsigned char *)data, len, PR_FALSE, PR_FALSE);
508 }
509
510 /*
511 * SecCmsEncoderCreate - set up encoding of a CMS message
512 *
513 * "cmsg" - message to encode
514 * "outputfn", "outputarg" - callback function for delivery of DER-encoded output
515 * will not be called if NULL.
516 * "dest" - if non-NULL, pointer to CSSM_DATA that will hold the DER-encoded output
517 * "destpoolp" - pool to allocate DER-encoded output in
518 * "pwfn", pwfn_arg" - callback function for getting token password
519 * "decrypt_key_cb", "decrypt_key_cb_arg" - callback function for getting bulk key for encryptedData
520 * "detached_digestalgs", "detached_digests" - digests from detached content
521 */
522 OSStatus
523 SecCmsEncoderCreate(SecCmsMessageRef cmsg,
524 SecCmsContentCallback outputfn, void *outputarg,
525 CSSM_DATA_PTR dest, SecArenaPoolRef destpool,
526 PK11PasswordFunc pwfn, void *pwfn_arg,
527 SecCmsGetDecryptKeyCallback decrypt_key_cb, void *decrypt_key_cb_arg,
528 SECAlgorithmID **detached_digestalgs, CSSM_DATA_PTR *detached_digests,
529 SecCmsEncoderRef *outEncoder)
530 {
531 SecCmsEncoderRef p7ecx;
532 OSStatus result;
533 SecCmsContentInfoRef cinfo;
534
535 /* Clear the thread error to clean up dirty threads */
536 PORT_SetError(0);
537
538 SecCmsMessageSetEncodingParams(cmsg, pwfn, pwfn_arg, decrypt_key_cb, decrypt_key_cb_arg,
539 detached_digestalgs, detached_digests);
540
541 p7ecx = (SecCmsEncoderRef)PORT_ZAlloc(sizeof(struct SecCmsEncoderStr));
542 if (p7ecx == NULL) {
543 result = memFullErr;
544 goto loser;
545 }
546
547 p7ecx->cmsg = cmsg;
548 p7ecx->output.outputfn = outputfn;
549 p7ecx->output.outputarg = outputarg;
550 p7ecx->output.dest = dest;
551 p7ecx->output.destpoolp = (PLArenaPool *)destpool;
552 p7ecx->type = SEC_OID_UNKNOWN;
553
554 cinfo = SecCmsMessageGetContentInfo(cmsg);
555
556 switch (SecCmsContentInfoGetContentTypeTag(cinfo)) {
557 case SEC_OID_PKCS7_SIGNED_DATA:
558 result = SecCmsSignedDataEncodeBeforeStart(cinfo->content.signedData);
559 break;
560 case SEC_OID_PKCS7_ENVELOPED_DATA:
561 result = SecCmsEnvelopedDataEncodeBeforeStart(cinfo->content.envelopedData);
562 break;
563 case SEC_OID_PKCS7_DIGESTED_DATA:
564 result = SecCmsDigestedDataEncodeBeforeStart(cinfo->content.digestedData);
565 break;
566 case SEC_OID_PKCS7_ENCRYPTED_DATA:
567 result = SecCmsEncryptedDataEncodeBeforeStart(cinfo->content.encryptedData);
568 break;
569 default:
570 /* @@@ We need a better error for unsupported message types. */
571 result = paramErr;
572 break;
573 }
574
575 if (result) {
576 PORT_Free(p7ecx);
577 goto loser;
578 }
579
580 /* Initialize the BER encoder.
581 * Note that this will not encode anything until the first call to SEC_ASN1EncoderUpdate */
582 p7ecx->ecx = SEC_ASN1EncoderStart(cmsg, SecCmsMessageTemplate,
583 nss_cms_encoder_out, &(p7ecx->output));
584 if (p7ecx->ecx == NULL) {
585 result = PORT_GetError();
586 PORT_Free(p7ecx);
587 PORT_SetError(0); // Clean the thread error since we've returned the error
588 goto loser;
589 }
590 p7ecx->ecxupdated = PR_FALSE;
591
592 /*
593 * Indicate that we are streaming. We will be streaming until we
594 * get past the contents bytes.
595 */
596 SEC_ASN1EncoderSetStreaming(p7ecx->ecx);
597
598 /*
599 * The notify function will watch for the contents field.
600 */
601 SEC_ASN1EncoderSetNotifyProc(p7ecx->ecx, nss_cms_encoder_notify, p7ecx);
602
603 /* this will kick off the encoding process & encode everything up to the content bytes,
604 * at which point the notify function sets streaming mode (and possibly creates
605 * a child encoder). */
606 if (SEC_ASN1EncoderUpdate(p7ecx->ecx, NULL, 0) != SECSuccess) {
607 result = PORT_GetError();
608 PORT_Free(p7ecx);
609 PORT_SetError(0); // Clean the thread error since we've returned the error
610 goto loser;
611 }
612
613 *outEncoder = p7ecx;
614 loser:
615 return result;
616 }
617
618 /*
619 * SecCmsEncoderUpdate - take content data delivery from the user
620 *
621 * "p7ecx" - encoder context
622 * "data" - content data
623 * "len" - length of content data
624 *
625 * need to find the lowest level (and call SEC_ASN1EncoderUpdate on the way down),
626 * then hand the data to the work_data fn
627 */
628 OSStatus
629 SecCmsEncoderUpdate(SecCmsEncoderRef p7ecx, const void *data, CFIndex len)
630 {
631 OSStatus result;
632 SecCmsContentInfoRef cinfo;
633 SECOidTag childtype;
634
635 if (!p7ecx) {
636 return errSecParam;
637 }
638
639 if (p7ecx->error)
640 return p7ecx->error;
641
642 /* hand data to the innermost decoder */
643 if (p7ecx->childp7ecx) {
644 /* recursion here */
645 result = SecCmsEncoderUpdate(p7ecx->childp7ecx, data, len);
646 } else {
647 /* we are at innermost decoder */
648 /* find out about our inner content type - must be data */
649 cinfo = SecCmsContentGetContentInfo(p7ecx->content.pointer, p7ecx->type);
650 childtype = SecCmsContentInfoGetContentTypeTag(cinfo);
651 if ((childtype != SEC_OID_PKCS7_DATA) && (childtype != SEC_OID_OTHER))
652 return paramErr; /* @@@ Maybe come up with a better error? */
653 /* and we must not have preset data */
654 if (cinfo->content.data != NULL)
655 return paramErr; /* @@@ Maybe come up with a better error? */
656
657 /* hand it the data so it can encode it (let DER trickle up the chain) */
658 result = nss_cms_encoder_work_data(p7ecx, NULL, (const unsigned char *)data, len, PR_FALSE, PR_TRUE);
659 if (result) {
660 result = PORT_GetError();
661 PORT_SetError(0); // Clean the thread error since we've returned the error
662 }
663 }
664 return result;
665 }
666
667 /*
668 * SecCmsEncoderDestroy - stop all encoding
669 *
670 * we need to walk down the chain of encoders and the finish them from the innermost out
671 */
672 void
673 SecCmsEncoderDestroy(SecCmsEncoderRef p7ecx)
674 {
675 /* XXX do this right! */
676
677 /*
678 * Finish any inner decoders before us so that all the encoded data is flushed
679 * This basically finishes all the decoders from the innermost to the outermost.
680 * Finishing an inner decoder may result in data being updated to the outer decoder
681 * while we are already in SecCmsEncoderFinish, but that's allright.
682 */
683 if (p7ecx->childp7ecx)
684 SecCmsEncoderDestroy(p7ecx->childp7ecx); /* frees p7ecx->childp7ecx */
685
686 /*
687 * On the way back up, there will be no more data (if we had an
688 * inner encoder, it is done now!)
689 * Flush out any remaining data and/or finish digests.
690 */
691 if (nss_cms_encoder_work_data(p7ecx, NULL, NULL, 0, PR_TRUE, (p7ecx->childp7ecx == NULL)))
692 goto loser;
693
694 p7ecx->childp7ecx = NULL;
695
696 /* kick the encoder back into working mode again.
697 * We turn off streaming stuff (which will cause the encoder to continue
698 * encoding happily, now that we have all the data (like digests) ready for it).
699 */
700 SEC_ASN1EncoderClearTakeFromBuf(p7ecx->ecx);
701 SEC_ASN1EncoderClearStreaming(p7ecx->ecx);
702
703 /* now that TakeFromBuf is off, this will kick this encoder to finish encoding */
704 SEC_ASN1EncoderUpdate(p7ecx->ecx, NULL, 0);
705
706 loser:
707 SEC_ASN1EncoderFinish(p7ecx->ecx);
708 PORT_Free (p7ecx);
709 }
710
711 /*
712 * SecCmsEncoderFinish - signal the end of data
713 *
714 * we need to walk down the chain of encoders and the finish them from the innermost out
715 */
716 OSStatus
717 SecCmsEncoderFinish(SecCmsEncoderRef p7ecx)
718 {
719 OSStatus result;
720 SecCmsContentInfoRef cinfo;
721 SECOidTag childtype;
722
723 /*
724 * Finish any inner decoders before us so that all the encoded data is flushed
725 * This basically finishes all the decoders from the innermost to the outermost.
726 * Finishing an inner decoder may result in data being updated to the outer decoder
727 * while we are already in SecCmsEncoderFinish, but that's allright.
728 */
729 if (p7ecx->childp7ecx) {
730 result = SecCmsEncoderFinish(p7ecx->childp7ecx); /* frees p7ecx->childp7ecx */
731 if (result)
732 goto loser;
733 }
734
735 /*
736 * On the way back up, there will be no more data (if we had an
737 * inner encoder, it is done now!)
738 * Flush out any remaining data and/or finish digests.
739 */
740 result = nss_cms_encoder_work_data(p7ecx, NULL, NULL, 0, PR_TRUE, (p7ecx->childp7ecx == NULL));
741 if (result) {
742 result = PORT_GetError();
743 goto loser;
744 }
745
746 p7ecx->childp7ecx = NULL;
747
748 /* find out about our inner content type - must be data */
749 cinfo = SecCmsContentGetContentInfo(p7ecx->content.pointer, p7ecx->type);
750 childtype = SecCmsContentInfoGetContentTypeTag(cinfo);
751 if ( ((childtype == SEC_OID_PKCS7_DATA) || (childtype == SEC_OID_OTHER)) &&
752 (cinfo->content.data == NULL)) {
753 SEC_ASN1EncoderClearTakeFromBuf(p7ecx->ecx);
754 /* now that TakeFromBuf is off, this will kick this encoder to finish encoding */
755 result = SEC_ASN1EncoderUpdate(p7ecx->ecx, NULL, 0);
756 if (result)
757 result = PORT_GetError();
758 }
759
760 SEC_ASN1EncoderClearStreaming(p7ecx->ecx);
761
762 if (p7ecx->error && !result)
763 result = p7ecx->error;
764
765 loser:
766 SEC_ASN1EncoderFinish(p7ecx->ecx);
767 PORT_Free (p7ecx);
768 PORT_SetError(0); // Clean the thread error since we've returned the error
769 return result;
770 }
771
772 OSStatus
773 SecCmsMessageEncode(SecCmsMessageRef cmsg, const CSSM_DATA *input, SecArenaPoolRef arena,
774 CSSM_DATA_PTR outBer)
775 {
776 SecCmsEncoderRef encoder;
777 OSStatus result;
778
779 if (!cmsg || !outBer || !arena) {
780 result = paramErr;
781 goto loser;
782 }
783
784 result = SecCmsEncoderCreate(cmsg, 0, 0, outBer, arena, 0, 0, 0, 0, 0, 0, &encoder);
785 if (result)
786 goto loser;
787
788 if (input) {
789 result = SecCmsEncoderUpdate(encoder, input->Data, input->Length);
790 if (result) {
791 SecCmsEncoderDestroy(encoder);
792 goto loser;
793 }
794 }
795 result = SecCmsEncoderFinish(encoder);
796
797 loser:
798 return result;
799 }