cmsencode.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * CMS encoding.
  6. */
  7. #include "cmslocal.h"
  8. #include "cert.h"
  9. #include "keyhi.h"
  10. #include "secasn1.h"
  11. #include "secoid.h"
  12. #include "secitem.h"
  13. #include "pk11func.h"
  14. #include "secerr.h"
  15. struct nss_cms_encoder_output {
  16. NSSCMSContentCallback outputfn;
  17. void *outputarg;
  18. PLArenaPool *destpoolp;
  19. SECItem *dest;
  20. };
  21. struct NSSCMSEncoderContextStr {
  22. SEC_ASN1EncoderContext *ecx; /* ASN.1 encoder context */
  23. PRBool ecxupdated; /* true if data was handed in */
  24. NSSCMSMessage *cmsg; /* pointer to the root message */
  25. SECOidTag type; /* type tag of the current content */
  26. NSSCMSContent content; /* pointer to current content */
  27. struct nss_cms_encoder_output output; /* output function */
  28. int error; /* error code */
  29. NSSCMSEncoderContext *childp7ecx; /* link to child encoder context */
  30. };
  31. static SECStatus nss_cms_before_data(NSSCMSEncoderContext *p7ecx);
  32. static SECStatus nss_cms_after_data(NSSCMSEncoderContext *p7ecx);
  33. static SECStatus nss_cms_encoder_update(NSSCMSEncoderContext *p7ecx,
  34. const char *data, unsigned long len);
  35. static SECStatus nss_cms_encoder_work_data(NSSCMSEncoderContext *p7ecx, SECItem *dest,
  36. const unsigned char *data, unsigned long len,
  37. PRBool final, PRBool innermost);
  38. extern const SEC_ASN1Template NSSCMSMessageTemplate[];
  39. /*
  40. * The little output function that the ASN.1 encoder calls to hand
  41. * us bytes which we in turn hand back to our caller (via the callback
  42. * they gave us).
  43. */
  44. static void
  45. nss_cms_encoder_out(void *arg, const char *buf, unsigned long len,
  46. int depth, SEC_ASN1EncodingPart data_kind)
  47. {
  48. struct nss_cms_encoder_output *output = (struct nss_cms_encoder_output *)arg;
  49. unsigned char *dest;
  50. unsigned long offset;
  51. #ifdef CMSDEBUG
  52. int i;
  53. const char *data_name = "unknown";
  54. switch (data_kind) {
  55. case SEC_ASN1_Identifier:
  56. data_name = "identifier";
  57. break;
  58. case SEC_ASN1_Length:
  59. data_name = "length";
  60. break;
  61. case SEC_ASN1_Contents:
  62. data_name = "contents";
  63. break;
  64. case SEC_ASN1_EndOfContents:
  65. data_name = "end-of-contents";
  66. break;
  67. }
  68. fprintf(stderr, "kind = %s, depth = %d, len = %d\n", data_name, depth, len);
  69. for (i = 0; i < len; i++) {
  70. fprintf(stderr, " %02x%s", (unsigned int)buf[i] & 0xff, ((i % 16) == 15) ? "\n" : "");
  71. }
  72. if ((i % 16) != 0)
  73. fprintf(stderr, "\n");
  74. #endif
  75. if (output->outputfn != NULL)
  76. /* call output callback with DER data */
  77. output->outputfn(output->outputarg, buf, len);
  78. if (output->dest != NULL) {
  79. /* store DER data in SECItem */
  80. offset = output->dest->len;
  81. if (offset == 0) {
  82. dest = (unsigned char *)PORT_ArenaAlloc(output->destpoolp, len);
  83. } else {
  84. dest = (unsigned char *)PORT_ArenaGrow(output->destpoolp,
  85. output->dest->data,
  86. output->dest->len,
  87. output->dest->len + len);
  88. }
  89. if (dest == NULL)
  90. /* oops */
  91. return;
  92. output->dest->data = dest;
  93. output->dest->len += len;
  94. /* copy it in */
  95. if (len) {
  96. PORT_Memcpy(output->dest->data + offset, buf, len);
  97. }
  98. }
  99. }
  100. /*
  101. * nss_cms_encoder_notify - ASN.1 encoder callback
  102. *
  103. * this function is called by the ASN.1 encoder before and after the encoding of
  104. * every object. here, it is used to keep track of data structures, set up
  105. * encryption and/or digesting and possibly set up child encoders.
  106. */
  107. static void
  108. nss_cms_encoder_notify(void *arg, PRBool before, void *dest, int depth)
  109. {
  110. NSSCMSEncoderContext *p7ecx;
  111. NSSCMSContentInfo *rootcinfo, *cinfo;
  112. PRBool after = !before;
  113. SECOidTag childtype;
  114. SECItem *item;
  115. p7ecx = (NSSCMSEncoderContext *)arg;
  116. PORT_Assert(p7ecx != NULL);
  117. rootcinfo = &(p7ecx->cmsg->contentInfo);
  118. #ifdef CMSDEBUG
  119. fprintf(stderr, "%6.6s, dest = 0x%08x, depth = %d\n", before ? "before" : "after",
  120. dest, depth);
  121. #endif
  122. /*
  123. * Watch for the content field, at which point we want to instruct
  124. * the ASN.1 encoder to start taking bytes from the buffer.
  125. */
  126. if (NSS_CMSType_IsData(p7ecx->type)) {
  127. cinfo = NSS_CMSContent_GetContentInfo(p7ecx->content.pointer, p7ecx->type);
  128. if (before && dest == &(cinfo->rawContent)) {
  129. /* just set up encoder to grab from user - no encryption or digesting */
  130. if ((item = cinfo->content.data) != NULL)
  131. (void)nss_cms_encoder_work_data(p7ecx, NULL, item->data,
  132. item->len, PR_TRUE, PR_TRUE);
  133. else
  134. SEC_ASN1EncoderSetTakeFromBuf(p7ecx->ecx);
  135. SEC_ASN1EncoderClearNotifyProc(p7ecx->ecx); /* no need to get notified anymore */
  136. }
  137. } else if (NSS_CMSType_IsWrapper(p7ecx->type)) {
  138. /* when we know what the content is, we encode happily until we reach the inner content */
  139. cinfo = NSS_CMSContent_GetContentInfo(p7ecx->content.pointer, p7ecx->type);
  140. childtype = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
  141. if (after && dest == &(cinfo->contentType)) {
  142. /* we're right before encoding the data (if we have some or not) */
  143. /* (for encrypted data, we're right before the contentEncAlg which may change */
  144. /* in nss_cms_before_data because of IV calculation when setting up encryption) */
  145. if (nss_cms_before_data(p7ecx) != SECSuccess)
  146. p7ecx->error = PORT_GetError();
  147. }
  148. if (before && dest == &(cinfo->rawContent)) {
  149. if (p7ecx->childp7ecx == NULL) {
  150. if ((NSS_CMSType_IsData(childtype) && (item = cinfo->content.data) != NULL)) {
  151. /* we are the innermost non-data and we have data - feed it in */
  152. (void)nss_cms_encoder_work_data(p7ecx, NULL, item->data,
  153. item->len, PR_TRUE, PR_TRUE);
  154. } else {
  155. /* else we'll have to get data from user */
  156. SEC_ASN1EncoderSetTakeFromBuf(p7ecx->ecx);
  157. }
  158. } else {
  159. /* if we have a nested encoder, wait for its data */
  160. SEC_ASN1EncoderSetTakeFromBuf(p7ecx->ecx);
  161. }
  162. }
  163. if (after && dest == &(cinfo->rawContent)) {
  164. if (nss_cms_after_data(p7ecx) != SECSuccess)
  165. p7ecx->error = PORT_GetError();
  166. SEC_ASN1EncoderClearNotifyProc(p7ecx->ecx); /* no need to get notified anymore */
  167. }
  168. } else {
  169. /* we're still in the root message */
  170. if (after && dest == &(rootcinfo->contentType)) {
  171. /* got the content type OID now - so find out the type tag */
  172. p7ecx->type = NSS_CMSContentInfo_GetContentTypeTag(rootcinfo);
  173. /* set up a pointer to our current content */
  174. p7ecx->content = rootcinfo->content;
  175. }
  176. }
  177. }
  178. /*
  179. * nss_cms_before_data - setup the current encoder to receive data
  180. */
  181. static SECStatus
  182. nss_cms_before_data(NSSCMSEncoderContext *p7ecx)
  183. {
  184. SECStatus rv;
  185. SECOidTag childtype;
  186. NSSCMSContentInfo *cinfo;
  187. NSSCMSEncoderContext *childp7ecx;
  188. const SEC_ASN1Template *template;
  189. /* call _Encode_BeforeData handlers */
  190. switch (p7ecx->type) {
  191. case SEC_OID_PKCS7_SIGNED_DATA:
  192. /* we're encoding a signedData, so set up the digests */
  193. rv = NSS_CMSSignedData_Encode_BeforeData(p7ecx->content.signedData);
  194. break;
  195. case SEC_OID_PKCS7_DIGESTED_DATA:
  196. /* we're encoding a digestedData, so set up the digest */
  197. rv = NSS_CMSDigestedData_Encode_BeforeData(p7ecx->content.digestedData);
  198. break;
  199. case SEC_OID_PKCS7_ENVELOPED_DATA:
  200. rv = NSS_CMSEnvelopedData_Encode_BeforeData(p7ecx->content.envelopedData);
  201. break;
  202. case SEC_OID_PKCS7_ENCRYPTED_DATA:
  203. rv = NSS_CMSEncryptedData_Encode_BeforeData(p7ecx->content.encryptedData);
  204. break;
  205. default:
  206. if (NSS_CMSType_IsWrapper(p7ecx->type)) {
  207. rv = NSS_CMSGenericWrapperData_Encode_BeforeData(p7ecx->type,
  208. p7ecx->content.genericData);
  209. } else {
  210. rv = SECFailure;
  211. }
  212. }
  213. if (rv != SECSuccess)
  214. return SECFailure;
  215. /* ok, now we have a pointer to cinfo */
  216. /* find out what kind of data is encapsulated */
  217. cinfo = NSS_CMSContent_GetContentInfo(p7ecx->content.pointer, p7ecx->type);
  218. childtype = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
  219. if (NSS_CMSType_IsWrapper(childtype)) {
  220. /* in these cases, we need to set up a child encoder! */
  221. /* create new encoder context */
  222. childp7ecx = PORT_ZAlloc(sizeof(NSSCMSEncoderContext));
  223. if (childp7ecx == NULL)
  224. return SECFailure;
  225. /* the CHILD encoder needs to hand its encoded data to the CURRENT encoder
  226. * (which will encrypt and/or digest it)
  227. * this needs to route back into our update function
  228. * which finds the lowest encoding context & encrypts and computes digests */
  229. childp7ecx->type = childtype;
  230. childp7ecx->content = cinfo->content;
  231. /* use the non-recursive update function here, of course */
  232. childp7ecx->output.outputfn = (NSSCMSContentCallback)nss_cms_encoder_update;
  233. childp7ecx->output.outputarg = p7ecx;
  234. childp7ecx->output.destpoolp = NULL;
  235. childp7ecx->output.dest = NULL;
  236. childp7ecx->cmsg = p7ecx->cmsg;
  237. childp7ecx->ecxupdated = PR_FALSE;
  238. childp7ecx->childp7ecx = NULL;
  239. template = NSS_CMSUtil_GetTemplateByTypeTag(childtype);
  240. if (template == NULL)
  241. goto loser; /* cannot happen */
  242. /* now initialize the data for encoding the first third */
  243. switch (childp7ecx->type) {
  244. case SEC_OID_PKCS7_SIGNED_DATA:
  245. rv = NSS_CMSSignedData_Encode_BeforeStart(cinfo->content.signedData);
  246. break;
  247. case SEC_OID_PKCS7_ENVELOPED_DATA:
  248. rv = NSS_CMSEnvelopedData_Encode_BeforeStart(cinfo->content.envelopedData);
  249. break;
  250. case SEC_OID_PKCS7_DIGESTED_DATA:
  251. rv = NSS_CMSDigestedData_Encode_BeforeStart(cinfo->content.digestedData);
  252. break;
  253. case SEC_OID_PKCS7_ENCRYPTED_DATA:
  254. rv = NSS_CMSEncryptedData_Encode_BeforeStart(cinfo->content.encryptedData);
  255. break;
  256. default:
  257. rv = NSS_CMSGenericWrapperData_Encode_BeforeStart(childp7ecx->type,
  258. cinfo->content.genericData);
  259. break;
  260. }
  261. if (rv != SECSuccess)
  262. goto loser;
  263. /*
  264. * Initialize the BER encoder.
  265. */
  266. childp7ecx->ecx = SEC_ASN1EncoderStart(cinfo->content.pointer, template,
  267. nss_cms_encoder_out, &(childp7ecx->output));
  268. if (childp7ecx->ecx == NULL)
  269. goto loser;
  270. /*
  271. * Indicate that we are streaming. We will be streaming until we
  272. * get past the contents bytes.
  273. */
  274. if (!cinfo->privateInfo || !cinfo->privateInfo->dontStream)
  275. SEC_ASN1EncoderSetStreaming(childp7ecx->ecx);
  276. /*
  277. * The notify function will watch for the contents field.
  278. */
  279. p7ecx->childp7ecx = childp7ecx;
  280. SEC_ASN1EncoderSetNotifyProc(childp7ecx->ecx, nss_cms_encoder_notify,
  281. childp7ecx);
  282. /* please note that we are NOT calling SEC_ASN1EncoderUpdate here to kick off the */
  283. /* encoding process - we'll do that from the update function instead */
  284. /* otherwise we'd be encoding data from a call of the notify function of the */
  285. /* parent encoder (which would not work) */
  286. } else if (NSS_CMSType_IsData(childtype)) {
  287. p7ecx->childp7ecx = NULL;
  288. } else {
  289. /* we do not know this type */
  290. p7ecx->error = SEC_ERROR_BAD_DER;
  291. }
  292. return SECSuccess;
  293. loser:
  294. if (childp7ecx) {
  295. if (childp7ecx->ecx)
  296. SEC_ASN1EncoderFinish(childp7ecx->ecx);
  297. PORT_Free(childp7ecx);
  298. p7ecx->childp7ecx = NULL;
  299. }
  300. return SECFailure;
  301. }
  302. static SECStatus
  303. nss_cms_after_data(NSSCMSEncoderContext *p7ecx)
  304. {
  305. SECStatus rv = SECFailure;
  306. switch (p7ecx->type) {
  307. case SEC_OID_PKCS7_SIGNED_DATA:
  308. /* this will finish the digests and sign */
  309. rv = NSS_CMSSignedData_Encode_AfterData(p7ecx->content.signedData);
  310. break;
  311. case SEC_OID_PKCS7_ENVELOPED_DATA:
  312. rv = NSS_CMSEnvelopedData_Encode_AfterData(p7ecx->content.envelopedData);
  313. break;
  314. case SEC_OID_PKCS7_DIGESTED_DATA:
  315. rv = NSS_CMSDigestedData_Encode_AfterData(p7ecx->content.digestedData);
  316. break;
  317. case SEC_OID_PKCS7_ENCRYPTED_DATA:
  318. rv = NSS_CMSEncryptedData_Encode_AfterData(p7ecx->content.encryptedData);
  319. break;
  320. default:
  321. if (NSS_CMSType_IsWrapper(p7ecx->type)) {
  322. rv = NSS_CMSGenericWrapperData_Encode_AfterData(p7ecx->type,
  323. p7ecx->content.genericData);
  324. } else {
  325. rv = SECFailure;
  326. }
  327. break;
  328. }
  329. return rv;
  330. }
  331. /*
  332. * nss_cms_encoder_work_data - process incoming data
  333. *
  334. * (from the user or the next encoding layer)
  335. * Here, we need to digest and/or encrypt, then pass it on
  336. */
  337. static SECStatus
  338. nss_cms_encoder_work_data(NSSCMSEncoderContext *p7ecx, SECItem *dest,
  339. const unsigned char *data, unsigned long len,
  340. PRBool final, PRBool innermost)
  341. {
  342. unsigned char *buf = NULL;
  343. SECStatus rv;
  344. NSSCMSContentInfo *cinfo;
  345. rv = SECSuccess; /* may as well be optimistic */
  346. /*
  347. * We should really have data to process, or we should be trying
  348. * to finish/flush the last block. (This is an overly paranoid
  349. * check since all callers are in this file and simple inspection
  350. * proves they do it right. But it could find a bug in future
  351. * modifications/development, that is why it is here.)
  352. */
  353. PORT_Assert((data != NULL && len) || final);
  354. /* we got data (either from the caller, or from a lower level encoder) */
  355. cinfo = NSS_CMSContent_GetContentInfo(p7ecx->content.pointer, p7ecx->type);
  356. if (!cinfo) {
  357. /* The original programmer didn't expect this to happen */
  358. p7ecx->error = SEC_ERROR_LIBRARY_FAILURE;
  359. return SECFailure;
  360. }
  361. /* Update the running digest. */
  362. if (len && cinfo->privateInfo && cinfo->privateInfo->digcx != NULL)
  363. NSS_CMSDigestContext_Update(cinfo->privateInfo->digcx, data, len);
  364. /* Encrypt this chunk. */
  365. if (cinfo->privateInfo && cinfo->privateInfo->ciphcx != NULL) {
  366. unsigned int inlen; /* length of data being encrypted */
  367. unsigned int outlen; /* length of encrypted data */
  368. unsigned int buflen; /* length available for encrypted data */
  369. inlen = len;
  370. buflen = NSS_CMSCipherContext_EncryptLength(cinfo->privateInfo->ciphcx,
  371. inlen, final);
  372. if (buflen == 0) {
  373. /*
  374. * No output is expected, but the input data may be buffered
  375. * so we still have to call Encrypt.
  376. */
  377. rv = NSS_CMSCipherContext_Encrypt(cinfo->privateInfo->ciphcx, NULL, NULL, 0,
  378. data, inlen, final);
  379. if (final) {
  380. len = 0;
  381. goto done;
  382. }
  383. return rv;
  384. }
  385. if (dest != NULL)
  386. buf = (unsigned char *)PORT_ArenaAlloc(p7ecx->cmsg->poolp, buflen);
  387. else
  388. buf = (unsigned char *)PORT_Alloc(buflen);
  389. if (buf == NULL) {
  390. rv = SECFailure;
  391. } else {
  392. rv = NSS_CMSCipherContext_Encrypt(cinfo->privateInfo->ciphcx, buf,
  393. &outlen, buflen,
  394. data, inlen, final);
  395. data = buf;
  396. len = outlen;
  397. }
  398. if (rv != SECSuccess)
  399. /* encryption or malloc failed? */
  400. return rv;
  401. }
  402. /*
  403. * at this point (data,len) has everything we'd like to give to the CURRENT encoder
  404. * (which will encode it, then hand it back to the user or the parent encoder)
  405. * We don't encode the data if we're innermost and we're told not to include the data
  406. */
  407. if (p7ecx->ecx != NULL && len &&
  408. (!innermost || cinfo->rawContent != cinfo->content.pointer))
  409. rv = SEC_ASN1EncoderUpdate(p7ecx->ecx, (const char *)data, len);
  410. done:
  411. if (cinfo->privateInfo && cinfo->privateInfo->ciphcx != NULL) {
  412. if (dest != NULL) {
  413. dest->data = buf;
  414. dest->len = len;
  415. } else if (buf != NULL) {
  416. PORT_Free(buf);
  417. }
  418. }
  419. return rv;
  420. }
  421. /*
  422. * nss_cms_encoder_update - deliver encoded data to the next higher level
  423. *
  424. * no recursion here because we REALLY want to end up at the next higher encoder!
  425. */
  426. static SECStatus
  427. nss_cms_encoder_update(NSSCMSEncoderContext *p7ecx, const char *data, unsigned long len)
  428. {
  429. /* XXX Error handling needs help. Return what? Do "Finish" on failure? */
  430. return nss_cms_encoder_work_data(p7ecx, NULL, (const unsigned char *)data,
  431. len, PR_FALSE, PR_FALSE);
  432. }
  433. /*
  434. * NSS_CMSEncoder_Start - set up encoding of a CMS message
  435. *
  436. * "cmsg" - message to encode
  437. * "outputfn", "outputarg" - callback function for delivery of DER-encoded output
  438. * will not be called if NULL.
  439. * "dest" - if non-NULL, pointer to SECItem that will hold the DER-encoded output
  440. * "destpoolp" - pool to allocate DER-encoded output in
  441. * "pwfn", pwfn_arg" - callback function for getting token password
  442. * "decrypt_key_cb", "decrypt_key_cb_arg" - callback function for getting bulk key for encryptedData
  443. * "detached_digestalgs", "detached_digests" - digests from detached content
  444. */
  445. NSSCMSEncoderContext *
  446. NSS_CMSEncoder_Start(NSSCMSMessage *cmsg,
  447. NSSCMSContentCallback outputfn, void *outputarg,
  448. SECItem *dest, PLArenaPool *destpoolp,
  449. PK11PasswordFunc pwfn, void *pwfn_arg,
  450. NSSCMSGetDecryptKeyCallback decrypt_key_cb, void *decrypt_key_cb_arg,
  451. SECAlgorithmID **detached_digestalgs, SECItem **detached_digests)
  452. {
  453. NSSCMSEncoderContext *p7ecx;
  454. SECStatus rv;
  455. NSSCMSContentInfo *cinfo;
  456. SECOidTag tag;
  457. NSS_CMSMessage_SetEncodingParams(cmsg, pwfn, pwfn_arg, decrypt_key_cb, decrypt_key_cb_arg,
  458. detached_digestalgs, detached_digests);
  459. p7ecx = (NSSCMSEncoderContext *)PORT_ZAlloc(sizeof(NSSCMSEncoderContext));
  460. if (p7ecx == NULL) {
  461. PORT_SetError(SEC_ERROR_NO_MEMORY);
  462. return NULL;
  463. }
  464. p7ecx->cmsg = cmsg;
  465. p7ecx->output.outputfn = outputfn;
  466. p7ecx->output.outputarg = outputarg;
  467. p7ecx->output.dest = dest;
  468. p7ecx->output.destpoolp = destpoolp;
  469. p7ecx->type = SEC_OID_UNKNOWN;
  470. cinfo = NSS_CMSMessage_GetContentInfo(cmsg);
  471. tag = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
  472. switch (tag) {
  473. case SEC_OID_PKCS7_SIGNED_DATA:
  474. rv = NSS_CMSSignedData_Encode_BeforeStart(cinfo->content.signedData);
  475. break;
  476. case SEC_OID_PKCS7_ENVELOPED_DATA:
  477. rv = NSS_CMSEnvelopedData_Encode_BeforeStart(cinfo->content.envelopedData);
  478. break;
  479. case SEC_OID_PKCS7_DIGESTED_DATA:
  480. rv = NSS_CMSDigestedData_Encode_BeforeStart(cinfo->content.digestedData);
  481. break;
  482. case SEC_OID_PKCS7_ENCRYPTED_DATA:
  483. rv = NSS_CMSEncryptedData_Encode_BeforeStart(cinfo->content.encryptedData);
  484. break;
  485. default:
  486. if (NSS_CMSType_IsWrapper(tag)) {
  487. rv = NSS_CMSGenericWrapperData_Encode_BeforeStart(tag,
  488. p7ecx->content.genericData);
  489. } else {
  490. rv = SECFailure;
  491. }
  492. break;
  493. }
  494. if (rv != SECSuccess) {
  495. PORT_Free(p7ecx);
  496. return NULL;
  497. }
  498. /* Initialize the BER encoder.
  499. * Note that this will not encode anything until the first call to SEC_ASN1EncoderUpdate */
  500. p7ecx->ecx = SEC_ASN1EncoderStart(cmsg, NSSCMSMessageTemplate,
  501. nss_cms_encoder_out, &(p7ecx->output));
  502. if (p7ecx->ecx == NULL) {
  503. PORT_Free(p7ecx);
  504. return NULL;
  505. }
  506. p7ecx->ecxupdated = PR_FALSE;
  507. /*
  508. * Indicate that we are streaming. We will be streaming until we
  509. * get past the contents bytes.
  510. */
  511. if (!cinfo->privateInfo || !cinfo->privateInfo->dontStream)
  512. SEC_ASN1EncoderSetStreaming(p7ecx->ecx);
  513. /*
  514. * The notify function will watch for the contents field.
  515. */
  516. SEC_ASN1EncoderSetNotifyProc(p7ecx->ecx, nss_cms_encoder_notify, p7ecx);
  517. /* this will kick off the encoding process & encode everything up to the content bytes,
  518. * at which point the notify function sets streaming mode (and possibly creates
  519. * a child encoder). */
  520. p7ecx->ecxupdated = PR_TRUE;
  521. if (SEC_ASN1EncoderUpdate(p7ecx->ecx, NULL, 0) != SECSuccess) {
  522. PORT_Free(p7ecx);
  523. return NULL;
  524. }
  525. return p7ecx;
  526. }
  527. /*
  528. * NSS_CMSEncoder_Update - take content data delivery from the user
  529. *
  530. * "p7ecx" - encoder context
  531. * "data" - content data
  532. * "len" - length of content data
  533. *
  534. * need to find the lowest level (and call SEC_ASN1EncoderUpdate on the way down),
  535. * then hand the data to the work_data fn
  536. */
  537. SECStatus
  538. NSS_CMSEncoder_Update(NSSCMSEncoderContext *p7ecx, const char *data, unsigned long len)
  539. {
  540. SECStatus rv;
  541. NSSCMSContentInfo *cinfo;
  542. SECOidTag childtype;
  543. if (p7ecx->error)
  544. return SECFailure;
  545. /* hand data to the innermost decoder */
  546. if (p7ecx->childp7ecx) {
  547. /* tell the child to start encoding, up to its first data byte, if it
  548. * hasn't started yet */
  549. if (!p7ecx->childp7ecx->ecxupdated) {
  550. p7ecx->childp7ecx->ecxupdated = PR_TRUE;
  551. if (SEC_ASN1EncoderUpdate(p7ecx->childp7ecx->ecx, NULL, 0) != SECSuccess)
  552. return SECFailure;
  553. }
  554. /* recursion here */
  555. rv = NSS_CMSEncoder_Update(p7ecx->childp7ecx, data, len);
  556. } else {
  557. /* we are at innermost decoder */
  558. /* find out about our inner content type - must be data */
  559. cinfo = NSS_CMSContent_GetContentInfo(p7ecx->content.pointer, p7ecx->type);
  560. if (!cinfo) {
  561. /* The original programmer didn't expect this to happen */
  562. p7ecx->error = SEC_ERROR_LIBRARY_FAILURE;
  563. return SECFailure;
  564. }
  565. childtype = NSS_CMSContentInfo_GetContentTypeTag(cinfo);
  566. if (!NSS_CMSType_IsData(childtype))
  567. return SECFailure;
  568. /* and we must not have preset data */
  569. if (cinfo->content.data != NULL)
  570. return SECFailure;
  571. /* hand it the data so it can encode it (let DER trickle up the chain) */
  572. rv = nss_cms_encoder_work_data(p7ecx, NULL, (const unsigned char *)data,
  573. len, PR_FALSE, PR_TRUE);
  574. }
  575. return rv;
  576. }
  577. /*
  578. * NSS_CMSEncoder_Cancel - stop all encoding
  579. *
  580. * we need to walk down the chain of encoders and the finish them from the innermost out
  581. */
  582. SECStatus
  583. NSS_CMSEncoder_Cancel(NSSCMSEncoderContext *p7ecx)
  584. {
  585. SECStatus rv = SECFailure;
  586. /* XXX do this right! */
  587. /*
  588. * Finish any inner decoders before us so that all the encoded data is flushed
  589. * This basically finishes all the decoders from the innermost to the outermost.
  590. * Finishing an inner decoder may result in data being updated to the outer decoder
  591. * while we are already in NSS_CMSEncoder_Finish, but that's allright.
  592. */
  593. if (p7ecx->childp7ecx) {
  594. rv = NSS_CMSEncoder_Cancel(p7ecx->childp7ecx); /* frees p7ecx->childp7ecx */
  595. /* remember rv for now */
  596. #ifdef CMSDEBUG
  597. if (rv != SECSuccess) {
  598. fprintf(stderr, "Fail to cancel inner encoder\n");
  599. }
  600. #endif
  601. }
  602. /*
  603. * On the way back up, there will be no more data (if we had an
  604. * inner encoder, it is done now!)
  605. * Flush out any remaining data and/or finish digests.
  606. */
  607. rv = nss_cms_encoder_work_data(p7ecx, NULL, NULL, 0, PR_TRUE, (p7ecx->childp7ecx == NULL));
  608. if (rv != SECSuccess)
  609. goto loser;
  610. p7ecx->childp7ecx = NULL;
  611. /* kick the encoder back into working mode again.
  612. * We turn off streaming stuff (which will cause the encoder to continue
  613. * encoding happily, now that we have all the data (like digests) ready for it).
  614. */
  615. SEC_ASN1EncoderClearTakeFromBuf(p7ecx->ecx);
  616. SEC_ASN1EncoderClearStreaming(p7ecx->ecx);
  617. /* now that TakeFromBuf is off, this will kick this encoder to finish encoding */
  618. rv = SEC_ASN1EncoderUpdate(p7ecx->ecx, NULL, 0);
  619. loser:
  620. SEC_ASN1EncoderFinish(p7ecx->ecx);
  621. PORT_Free(p7ecx);
  622. return rv;
  623. }
  624. /*
  625. * NSS_CMSEncoder_Finish - signal the end of data
  626. *
  627. * we need to walk down the chain of encoders and the finish them from the innermost out
  628. */
  629. SECStatus
  630. NSS_CMSEncoder_Finish(NSSCMSEncoderContext *p7ecx)
  631. {
  632. SECStatus rv = SECFailure;
  633. NSSCMSContentInfo *cinfo;
  634. /*
  635. * Finish any inner decoders before us so that all the encoded data is flushed
  636. * This basically finishes all the decoders from the innermost to the outermost.
  637. * Finishing an inner decoder may result in data being updated to the outer decoder
  638. * while we are already in NSS_CMSEncoder_Finish, but that's allright.
  639. */
  640. if (p7ecx->childp7ecx) {
  641. /* tell the child to start encoding, up to its first data byte, if it
  642. * hasn't yet */
  643. if (!p7ecx->childp7ecx->ecxupdated) {
  644. p7ecx->childp7ecx->ecxupdated = PR_TRUE;
  645. rv = SEC_ASN1EncoderUpdate(p7ecx->childp7ecx->ecx, NULL, 0);
  646. if (rv != SECSuccess) {
  647. NSS_CMSEncoder_Finish(p7ecx->childp7ecx); /* frees p7ecx->childp7ecx */
  648. goto loser;
  649. }
  650. }
  651. rv = NSS_CMSEncoder_Finish(p7ecx->childp7ecx); /* frees p7ecx->childp7ecx */
  652. if (rv != SECSuccess)
  653. goto loser;
  654. }
  655. /*
  656. * On the way back up, there will be no more data (if we had an
  657. * inner encoder, it is done now!)
  658. * Flush out any remaining data and/or finish digests.
  659. */
  660. rv = nss_cms_encoder_work_data(p7ecx, NULL, NULL, 0, PR_TRUE, (p7ecx->childp7ecx == NULL));
  661. if (rv != SECSuccess)
  662. goto loser;
  663. p7ecx->childp7ecx = NULL;
  664. cinfo = NSS_CMSContent_GetContentInfo(p7ecx->content.pointer, p7ecx->type);
  665. if (!cinfo) {
  666. /* The original programmer didn't expect this to happen */
  667. p7ecx->error = SEC_ERROR_LIBRARY_FAILURE;
  668. rv = SECFailure;
  669. goto loser;
  670. }
  671. SEC_ASN1EncoderClearTakeFromBuf(p7ecx->ecx);
  672. SEC_ASN1EncoderClearStreaming(p7ecx->ecx);
  673. /* now that TakeFromBuf is off, this will kick this encoder to finish encoding */
  674. rv = SEC_ASN1EncoderUpdate(p7ecx->ecx, NULL, 0);
  675. if (p7ecx->error)
  676. rv = SECFailure;
  677. loser:
  678. SEC_ASN1EncoderFinish(p7ecx->ecx);
  679. PORT_Free(p7ecx);
  680. return rv;
  681. }