cmssigdata.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  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 signedData methods.
  6. */
  7. #include "cmslocal.h"
  8. #include "cert.h"
  9. /*#include "cdbhdl.h"*/
  10. #include "secasn1.h"
  11. #include "secitem.h"
  12. #include "secoid.h"
  13. #include "pk11func.h"
  14. #include "secerr.h"
  15. NSSCMSSignedData *
  16. NSS_CMSSignedData_Create(NSSCMSMessage *cmsg)
  17. {
  18. void *mark;
  19. NSSCMSSignedData *sigd;
  20. PLArenaPool *poolp;
  21. if (!cmsg) {
  22. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  23. return NULL;
  24. }
  25. poolp = cmsg->poolp;
  26. mark = PORT_ArenaMark(poolp);
  27. sigd = (NSSCMSSignedData *)PORT_ArenaZAlloc(poolp, sizeof(NSSCMSSignedData));
  28. if (sigd == NULL)
  29. goto loser;
  30. sigd->cmsg = cmsg;
  31. /* signerInfos, certs, certlists, crls are all empty */
  32. /* version is set in NSS_CMSSignedData_Finalize() */
  33. PORT_ArenaUnmark(poolp, mark);
  34. return sigd;
  35. loser:
  36. PORT_ArenaRelease(poolp, mark);
  37. return NULL;
  38. }
  39. void
  40. NSS_CMSSignedData_Destroy(NSSCMSSignedData *sigd)
  41. {
  42. CERTCertificate **certs, **tempCerts, *cert;
  43. CERTCertificateList **certlists, *certlist;
  44. NSSCMSSignerInfo **signerinfos, *si;
  45. if (sigd == NULL)
  46. return;
  47. certs = sigd->certs;
  48. tempCerts = sigd->tempCerts;
  49. certlists = sigd->certLists;
  50. signerinfos = sigd->signerInfos;
  51. if (certs != NULL) {
  52. while ((cert = *certs++) != NULL)
  53. CERT_DestroyCertificate(cert);
  54. }
  55. if (tempCerts != NULL) {
  56. while ((cert = *tempCerts++) != NULL)
  57. CERT_DestroyCertificate(cert);
  58. }
  59. if (certlists != NULL) {
  60. while ((certlist = *certlists++) != NULL)
  61. CERT_DestroyCertificateList(certlist);
  62. }
  63. if (signerinfos != NULL) {
  64. while ((si = *signerinfos++) != NULL)
  65. NSS_CMSSignerInfo_Destroy(si);
  66. }
  67. /* everything's in a pool, so don't worry about the storage */
  68. NSS_CMSContentInfo_Destroy(&(sigd->contentInfo));
  69. }
  70. /*
  71. * NSS_CMSSignedData_Encode_BeforeStart - do all the necessary things to a SignedData
  72. * before start of encoding.
  73. *
  74. * In detail:
  75. * - find out about the right value to put into sigd->version
  76. * - come up with a list of digestAlgorithms (which should be the union of the algorithms
  77. * in the signerinfos).
  78. * If we happen to have a pre-set list of algorithms (and digest values!), we
  79. * check if we have all the signerinfos' algorithms. If not, this is an error.
  80. */
  81. SECStatus
  82. NSS_CMSSignedData_Encode_BeforeStart(NSSCMSSignedData *sigd)
  83. {
  84. NSSCMSSignerInfo *signerinfo;
  85. SECOidTag digestalgtag;
  86. SECItem *dummy;
  87. int version;
  88. SECStatus rv;
  89. PRBool haveDigests = PR_FALSE;
  90. int n, i;
  91. PLArenaPool *poolp;
  92. if (!sigd) {
  93. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  94. return SECFailure;
  95. }
  96. poolp = sigd->cmsg->poolp;
  97. /* we assume that we have precomputed digests if there is a list of algorithms, and */
  98. /* a chunk of data for each of those algorithms */
  99. if (sigd->digestAlgorithms != NULL && sigd->digests != NULL) {
  100. for (i = 0; sigd->digestAlgorithms[i] != NULL; i++) {
  101. if (sigd->digests[i] == NULL)
  102. break;
  103. }
  104. if (sigd->digestAlgorithms[i] == NULL) /* reached the end of the array? */
  105. haveDigests = PR_TRUE; /* yes: we must have all the digests */
  106. }
  107. version = NSS_CMS_SIGNED_DATA_VERSION_BASIC;
  108. /* RFC2630 5.1 "version is the syntax version number..." */
  109. if (NSS_CMSContentInfo_GetContentTypeTag(&(sigd->contentInfo)) != SEC_OID_PKCS7_DATA)
  110. version = NSS_CMS_SIGNED_DATA_VERSION_EXT;
  111. /* prepare all the SignerInfos (there may be none) */
  112. for (i = 0; i < NSS_CMSSignedData_SignerInfoCount(sigd); i++) {
  113. signerinfo = NSS_CMSSignedData_GetSignerInfo(sigd, i);
  114. /* RFC2630 5.1 "version is the syntax version number..." */
  115. if (NSS_CMSSignerInfo_GetVersion(signerinfo) != NSS_CMS_SIGNER_INFO_VERSION_ISSUERSN)
  116. version = NSS_CMS_SIGNED_DATA_VERSION_EXT;
  117. /* collect digestAlgorithms from SignerInfos */
  118. /* (we need to know which algorithms we have when the content comes in) */
  119. /* do not overwrite any existing digestAlgorithms (and digest) */
  120. digestalgtag = NSS_CMSSignerInfo_GetDigestAlgTag(signerinfo);
  121. n = NSS_CMSAlgArray_GetIndexByAlgTag(sigd->digestAlgorithms, digestalgtag);
  122. if (n < 0 && haveDigests) {
  123. /* oops, there is a digestalg we do not have a digest for */
  124. /* but we were supposed to have all the digests already... */
  125. goto loser;
  126. } else if (n < 0) {
  127. /* add the digestAlgorithm & a NULL digest */
  128. rv = NSS_CMSSignedData_AddDigest(poolp, sigd, digestalgtag, NULL);
  129. if (rv != SECSuccess)
  130. goto loser;
  131. } else {
  132. /* found it, nothing to do */
  133. }
  134. }
  135. dummy = SEC_ASN1EncodeInteger(poolp, &(sigd->version), (long)version);
  136. if (dummy == NULL)
  137. return SECFailure;
  138. /* this is a SET OF, so we need to sort them guys */
  139. rv = NSS_CMSArray_SortByDER((void **)sigd->digestAlgorithms,
  140. SEC_ASN1_GET(SECOID_AlgorithmIDTemplate),
  141. (void **)sigd->digests);
  142. if (rv != SECSuccess)
  143. return SECFailure;
  144. return SECSuccess;
  145. loser:
  146. return SECFailure;
  147. }
  148. SECStatus
  149. NSS_CMSSignedData_Encode_BeforeData(NSSCMSSignedData *sigd)
  150. {
  151. SECStatus rv;
  152. if (!sigd) {
  153. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  154. return SECFailure;
  155. }
  156. rv = NSS_CMSContentInfo_Private_Init(&sigd->contentInfo);
  157. if (rv != SECSuccess) {
  158. return SECFailure;
  159. }
  160. /* set up the digests */
  161. if (sigd->digests && sigd->digests[0]) {
  162. sigd->contentInfo.privateInfo->digcx = NULL; /* don't attempt to make new ones. */
  163. } else if (sigd->digestAlgorithms != NULL) {
  164. sigd->contentInfo.privateInfo->digcx =
  165. NSS_CMSDigestContext_StartMultiple(sigd->digestAlgorithms);
  166. if (sigd->contentInfo.privateInfo->digcx == NULL)
  167. return SECFailure;
  168. }
  169. return SECSuccess;
  170. }
  171. /*
  172. * NSS_CMSSignedData_Encode_AfterData - do all the necessary things to a SignedData
  173. * after all the encapsulated data was passed through the encoder.
  174. *
  175. * In detail:
  176. * - create the signatures in all the SignerInfos
  177. *
  178. * Please note that nothing is done to the Certificates and CRLs in the message - this
  179. * is entirely the responsibility of our callers.
  180. */
  181. SECStatus
  182. NSS_CMSSignedData_Encode_AfterData(NSSCMSSignedData *sigd)
  183. {
  184. NSSCMSSignerInfo **signerinfos, *signerinfo;
  185. NSSCMSContentInfo *cinfo;
  186. SECOidTag digestalgtag;
  187. SECStatus ret = SECFailure;
  188. SECStatus rv;
  189. SECItem *contentType;
  190. int certcount;
  191. int i, ci, cli, n, rci, si;
  192. PLArenaPool *poolp;
  193. CERTCertificateList *certlist;
  194. extern const SEC_ASN1Template NSSCMSSignerInfoTemplate[];
  195. if (!sigd) {
  196. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  197. return SECFailure;
  198. }
  199. poolp = sigd->cmsg->poolp;
  200. cinfo = &(sigd->contentInfo);
  201. /* did we have digest calculation going on? */
  202. if (cinfo->privateInfo && cinfo->privateInfo->digcx) {
  203. rv = NSS_CMSDigestContext_FinishMultiple(cinfo->privateInfo->digcx, poolp,
  204. &(sigd->digests));
  205. /* error has been set by NSS_CMSDigestContext_FinishMultiple */
  206. cinfo->privateInfo->digcx = NULL;
  207. if (rv != SECSuccess)
  208. goto loser;
  209. }
  210. signerinfos = sigd->signerInfos;
  211. certcount = 0;
  212. /* prepare all the SignerInfos (there may be none) */
  213. for (i = 0; i < NSS_CMSSignedData_SignerInfoCount(sigd); i++) {
  214. signerinfo = NSS_CMSSignedData_GetSignerInfo(sigd, i);
  215. /* find correct digest for this signerinfo */
  216. digestalgtag = NSS_CMSSignerInfo_GetDigestAlgTag(signerinfo);
  217. n = NSS_CMSAlgArray_GetIndexByAlgTag(sigd->digestAlgorithms, digestalgtag);
  218. if (n < 0 || sigd->digests == NULL || sigd->digests[n] == NULL) {
  219. /* oops - digest not found */
  220. PORT_SetError(SEC_ERROR_DIGEST_NOT_FOUND);
  221. goto loser;
  222. }
  223. /* XXX if our content is anything else but data, we need to force the
  224. * presence of signed attributes (RFC2630 5.3 "signedAttributes is a
  225. * collection...") */
  226. /* pass contentType here as we want a contentType attribute */
  227. if ((contentType = NSS_CMSContentInfo_GetContentTypeOID(cinfo)) == NULL)
  228. goto loser;
  229. /* sign the thing */
  230. rv = NSS_CMSSignerInfo_Sign(signerinfo, sigd->digests[n], contentType);
  231. if (rv != SECSuccess)
  232. goto loser;
  233. /* while we're at it, count number of certs in certLists */
  234. certlist = NSS_CMSSignerInfo_GetCertList(signerinfo);
  235. if (certlist)
  236. certcount += certlist->len;
  237. }
  238. /* this is a SET OF, so we need to sort them guys */
  239. rv = NSS_CMSArray_SortByDER((void **)signerinfos, NSSCMSSignerInfoTemplate, NULL);
  240. if (rv != SECSuccess)
  241. goto loser;
  242. /*
  243. * now prepare certs & crls
  244. */
  245. /* count the rest of the certs */
  246. if (sigd->certs != NULL) {
  247. for (ci = 0; sigd->certs[ci] != NULL; ci++)
  248. certcount++;
  249. }
  250. if (sigd->certLists != NULL) {
  251. for (cli = 0; sigd->certLists[cli] != NULL; cli++)
  252. certcount += sigd->certLists[cli]->len;
  253. }
  254. if (certcount == 0) {
  255. sigd->rawCerts = NULL;
  256. } else {
  257. /*
  258. * Combine all of the certs and cert chains into rawcerts.
  259. * Note: certcount is an upper bound; we may not need that many slots
  260. * but we will allocate anyway to avoid having to do another pass.
  261. * (The temporary space saving is not worth it.)
  262. *
  263. * XXX ARGH - this NEEDS to be fixed. need to come up with a decent
  264. * SetOfDERcertficates implementation
  265. */
  266. sigd->rawCerts = (SECItem **)PORT_ArenaAlloc(poolp, (certcount + 1) * sizeof(SECItem *));
  267. if (sigd->rawCerts == NULL)
  268. return SECFailure;
  269. /*
  270. * XXX Want to check for duplicates and not add *any* cert that is
  271. * already in the set. This will be more important when we start
  272. * dealing with larger sets of certs, dual-key certs (signing and
  273. * encryption), etc. For the time being we can slide by...
  274. *
  275. * XXX ARGH - this NEEDS to be fixed. need to come up with a decent
  276. * SetOfDERcertficates implementation
  277. */
  278. rci = 0;
  279. if (signerinfos != NULL) {
  280. for (si = 0; signerinfos[si] != NULL; si++) {
  281. signerinfo = signerinfos[si];
  282. for (ci = 0; ci < signerinfo->certList->len; ci++)
  283. sigd->rawCerts[rci++] = &(signerinfo->certList->certs[ci]);
  284. }
  285. }
  286. if (sigd->certs != NULL) {
  287. for (ci = 0; sigd->certs[ci] != NULL; ci++)
  288. sigd->rawCerts[rci++] = &(sigd->certs[ci]->derCert);
  289. }
  290. if (sigd->certLists != NULL) {
  291. for (cli = 0; sigd->certLists[cli] != NULL; cli++) {
  292. for (ci = 0; ci < sigd->certLists[cli]->len; ci++)
  293. sigd->rawCerts[rci++] = &(sigd->certLists[cli]->certs[ci]);
  294. }
  295. }
  296. sigd->rawCerts[rci] = NULL;
  297. /* this is a SET OF, so we need to sort them guys - we have the DER already, though */
  298. NSS_CMSArray_Sort((void **)sigd->rawCerts, NSS_CMSUtil_DERCompare, NULL, NULL);
  299. }
  300. ret = SECSuccess;
  301. loser:
  302. return ret;
  303. }
  304. SECStatus
  305. NSS_CMSSignedData_Decode_BeforeData(NSSCMSSignedData *sigd)
  306. {
  307. SECStatus rv;
  308. if (!sigd) {
  309. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  310. return SECFailure;
  311. }
  312. rv = NSS_CMSContentInfo_Private_Init(&sigd->contentInfo);
  313. if (rv != SECSuccess) {
  314. return SECFailure;
  315. }
  316. /* handle issue with Windows 2003 servers and kerberos */
  317. if (sigd->digestAlgorithms != NULL) {
  318. int i;
  319. for (i = 0; sigd->digestAlgorithms[i] != NULL; i++) {
  320. SECAlgorithmID *algid = sigd->digestAlgorithms[i];
  321. SECOidTag senttag = SECOID_FindOIDTag(&algid->algorithm);
  322. SECOidTag maptag = NSS_CMSUtil_MapSignAlgs(senttag);
  323. if (maptag != senttag) {
  324. SECOidData *hashoid = SECOID_FindOIDByTag(maptag);
  325. rv = SECITEM_CopyItem(sigd->cmsg->poolp, &algid->algorithm, &hashoid->oid);
  326. if (rv != SECSuccess) {
  327. return rv;
  328. }
  329. }
  330. }
  331. }
  332. /* set up the digests */
  333. if (sigd->digestAlgorithms != NULL && sigd->digests == NULL) {
  334. /* if digests are already there, do nothing */
  335. sigd->contentInfo.privateInfo->digcx = NSS_CMSDigestContext_StartMultiple(sigd->digestAlgorithms);
  336. if (sigd->contentInfo.privateInfo->digcx == NULL)
  337. return SECFailure;
  338. }
  339. return SECSuccess;
  340. }
  341. /*
  342. * NSS_CMSSignedData_Decode_AfterData - do all the necessary things to a
  343. * SignedData after all the encapsulated data was passed through the decoder.
  344. */
  345. SECStatus
  346. NSS_CMSSignedData_Decode_AfterData(NSSCMSSignedData *sigd)
  347. {
  348. SECStatus rv = SECSuccess;
  349. if (!sigd) {
  350. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  351. return SECFailure;
  352. }
  353. /* did we have digest calculation going on? */
  354. if (sigd->contentInfo.privateInfo && sigd->contentInfo.privateInfo->digcx) {
  355. rv = NSS_CMSDigestContext_FinishMultiple(sigd->contentInfo.privateInfo->digcx,
  356. sigd->cmsg->poolp, &(sigd->digests));
  357. /* error set by NSS_CMSDigestContext_FinishMultiple */
  358. sigd->contentInfo.privateInfo->digcx = NULL;
  359. }
  360. return rv;
  361. }
  362. /*
  363. * NSS_CMSSignedData_Decode_AfterEnd - do all the necessary things to a SignedData
  364. * after all decoding is finished.
  365. */
  366. SECStatus
  367. NSS_CMSSignedData_Decode_AfterEnd(NSSCMSSignedData *sigd)
  368. {
  369. NSSCMSSignerInfo **signerinfos = NULL;
  370. int i;
  371. if (!sigd) {
  372. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  373. return SECFailure;
  374. }
  375. /* set cmsg for all the signerinfos */
  376. signerinfos = sigd->signerInfos;
  377. /* set cmsg for all the signerinfos */
  378. if (signerinfos) {
  379. for (i = 0; signerinfos[i] != NULL; i++)
  380. signerinfos[i]->cmsg = sigd->cmsg;
  381. }
  382. return SECSuccess;
  383. }
  384. /*
  385. * NSS_CMSSignedData_GetSignerInfos - retrieve the SignedData's signer list
  386. */
  387. NSSCMSSignerInfo **
  388. NSS_CMSSignedData_GetSignerInfos(NSSCMSSignedData *sigd)
  389. {
  390. if (!sigd) {
  391. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  392. return NULL;
  393. }
  394. return sigd->signerInfos;
  395. }
  396. int
  397. NSS_CMSSignedData_SignerInfoCount(NSSCMSSignedData *sigd)
  398. {
  399. if (!sigd) {
  400. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  401. return 0;
  402. }
  403. return NSS_CMSArray_Count((void **)sigd->signerInfos);
  404. }
  405. NSSCMSSignerInfo *
  406. NSS_CMSSignedData_GetSignerInfo(NSSCMSSignedData *sigd, int i)
  407. {
  408. if (!sigd || !sigd->signerInfos) {
  409. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  410. return NULL;
  411. }
  412. return sigd->signerInfos[i];
  413. }
  414. /*
  415. * NSS_CMSSignedData_GetDigestAlgs - retrieve the SignedData's digest algorithm list
  416. */
  417. SECAlgorithmID **
  418. NSS_CMSSignedData_GetDigestAlgs(NSSCMSSignedData *sigd)
  419. {
  420. if (!sigd) {
  421. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  422. return NULL;
  423. }
  424. return sigd->digestAlgorithms;
  425. }
  426. /*
  427. * NSS_CMSSignedData_GetContentInfo - return pointer to this signedData's contentinfo
  428. */
  429. NSSCMSContentInfo *
  430. NSS_CMSSignedData_GetContentInfo(NSSCMSSignedData *sigd)
  431. {
  432. if (!sigd) {
  433. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  434. return NULL;
  435. }
  436. return &(sigd->contentInfo);
  437. }
  438. /*
  439. * NSS_CMSSignedData_GetCertificateList - retrieve the SignedData's certificate list
  440. */
  441. SECItem **
  442. NSS_CMSSignedData_GetCertificateList(NSSCMSSignedData *sigd)
  443. {
  444. if (!sigd) {
  445. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  446. return NULL;
  447. }
  448. return sigd->rawCerts;
  449. }
  450. SECStatus
  451. NSS_CMSSignedData_ImportCerts(NSSCMSSignedData *sigd, CERTCertDBHandle *certdb,
  452. SECCertUsage certusage, PRBool keepcerts)
  453. {
  454. int certcount;
  455. CERTCertificate **certArray = NULL;
  456. CERTCertList *certList = NULL;
  457. CERTCertListNode *node;
  458. SECStatus rv;
  459. SECItem **rawArray;
  460. int i;
  461. PRTime now;
  462. if (!sigd) {
  463. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  464. return SECFailure;
  465. }
  466. certcount = NSS_CMSArray_Count((void **)sigd->rawCerts);
  467. /* get the certs in the temp DB */
  468. rv = CERT_ImportCerts(certdb, certusage, certcount, sigd->rawCerts,
  469. &certArray, PR_FALSE, PR_FALSE, NULL);
  470. if (rv != SECSuccess) {
  471. goto loser;
  472. }
  473. /* save the certs so they don't get destroyed */
  474. for (i = 0; i < certcount; i++) {
  475. CERTCertificate *cert = certArray[i];
  476. if (cert)
  477. NSS_CMSSignedData_AddTempCertificate(sigd, cert);
  478. }
  479. if (!keepcerts) {
  480. goto done;
  481. }
  482. /* build a CertList for filtering */
  483. certList = CERT_NewCertList();
  484. if (certList == NULL) {
  485. rv = SECFailure;
  486. goto loser;
  487. }
  488. for (i = 0; i < certcount; i++) {
  489. CERTCertificate *cert = certArray[i];
  490. if (cert)
  491. cert = CERT_DupCertificate(cert);
  492. if (cert)
  493. CERT_AddCertToListTail(certList, cert);
  494. }
  495. /* filter out the certs we don't want */
  496. rv = CERT_FilterCertListByUsage(certList, certusage, PR_FALSE);
  497. if (rv != SECSuccess) {
  498. goto loser;
  499. }
  500. /* go down the remaining list of certs and verify that they have
  501. * valid chains, then import them.
  502. */
  503. now = PR_Now();
  504. for (node = CERT_LIST_HEAD(certList); !CERT_LIST_END(node, certList);
  505. node = CERT_LIST_NEXT(node)) {
  506. CERTCertificateList *certChain;
  507. if (CERT_VerifyCert(certdb, node->cert,
  508. PR_TRUE, certusage, now, NULL, NULL) != SECSuccess) {
  509. continue;
  510. }
  511. certChain = CERT_CertChainFromCert(node->cert, certusage, PR_FALSE);
  512. if (!certChain) {
  513. continue;
  514. }
  515. /*
  516. * CertChain returns an array of SECItems, import expects an array of
  517. * SECItem pointers. Create the SECItem Pointers from the array of
  518. * SECItems.
  519. */
  520. rawArray = (SECItem **)PORT_Alloc(certChain->len * sizeof(SECItem *));
  521. if (!rawArray) {
  522. CERT_DestroyCertificateList(certChain);
  523. continue;
  524. }
  525. for (i = 0; i < certChain->len; i++) {
  526. rawArray[i] = &certChain->certs[i];
  527. }
  528. (void)CERT_ImportCerts(certdb, certusage, certChain->len,
  529. rawArray, NULL, keepcerts, PR_FALSE, NULL);
  530. PORT_Free(rawArray);
  531. CERT_DestroyCertificateList(certChain);
  532. }
  533. rv = SECSuccess;
  534. /* XXX CRL handling */
  535. done:
  536. if (sigd->signerInfos != NULL) {
  537. /* fill in all signerinfo's certs */
  538. for (i = 0; sigd->signerInfos[i] != NULL; i++)
  539. (void)NSS_CMSSignerInfo_GetSigningCertificate(
  540. sigd->signerInfos[i], certdb);
  541. }
  542. loser:
  543. /* now free everything */
  544. if (certArray) {
  545. CERT_DestroyCertArray(certArray, certcount);
  546. }
  547. if (certList) {
  548. CERT_DestroyCertList(certList);
  549. }
  550. return rv;
  551. }
  552. /*
  553. * XXX the digests need to be passed in BETWEEN the decoding and the verification in case
  554. * of external signatures!
  555. */
  556. /*
  557. * NSS_CMSSignedData_VerifySignerInfo - check the signatures.
  558. *
  559. * The digests were either calculated during decoding (and are stored in the
  560. * signedData itself) or set after decoding using NSS_CMSSignedData_SetDigests.
  561. *
  562. * The verification checks if the signing cert is valid and has a trusted chain
  563. * for the purpose specified by "certusage".
  564. */
  565. SECStatus
  566. NSS_CMSSignedData_VerifySignerInfo(NSSCMSSignedData *sigd, int i,
  567. CERTCertDBHandle *certdb, SECCertUsage certusage)
  568. {
  569. NSSCMSSignerInfo *signerinfo;
  570. NSSCMSContentInfo *cinfo;
  571. SECOidData *algiddata;
  572. SECItem *contentType, *digest;
  573. SECOidTag oidTag;
  574. SECStatus rv;
  575. if (!sigd || !sigd->signerInfos) {
  576. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  577. return SECFailure;
  578. }
  579. cinfo = &(sigd->contentInfo);
  580. signerinfo = sigd->signerInfos[i];
  581. /* verify certificate */
  582. rv = NSS_CMSSignerInfo_VerifyCertificate(signerinfo, certdb, certusage);
  583. if (rv != SECSuccess)
  584. return rv; /* error is set */
  585. /* find digest and contentType for signerinfo */
  586. algiddata = NSS_CMSSignerInfo_GetDigestAlg(signerinfo);
  587. oidTag = algiddata ? algiddata->offset : SEC_OID_UNKNOWN;
  588. digest = NSS_CMSSignedData_GetDigestValue(sigd, oidTag);
  589. /* NULL digest is acceptable. */
  590. contentType = NSS_CMSContentInfo_GetContentTypeOID(cinfo);
  591. /* NULL contentType is acceptable. */
  592. /* now verify signature */
  593. rv = NSS_CMSSignerInfo_Verify(signerinfo, digest, contentType);
  594. return rv;
  595. }
  596. /*
  597. * NSS_CMSSignedData_VerifyCertsOnly - verify the certs in a certs-only message
  598. */
  599. SECStatus
  600. NSS_CMSSignedData_VerifyCertsOnly(NSSCMSSignedData *sigd,
  601. CERTCertDBHandle *certdb,
  602. SECCertUsage usage)
  603. {
  604. CERTCertificate *cert;
  605. SECStatus rv = SECSuccess;
  606. int i;
  607. int count;
  608. PRTime now;
  609. void *pwarg = NULL;
  610. if (!sigd || !certdb || !sigd->rawCerts) {
  611. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  612. return SECFailure;
  613. }
  614. count = NSS_CMSArray_Count((void **)sigd->rawCerts);
  615. now = PR_Now();
  616. for (i = 0; i < count; i++) {
  617. if (sigd->certs && sigd->certs[i]) {
  618. cert = CERT_DupCertificate(sigd->certs[i]);
  619. } else {
  620. cert = CERT_FindCertByDERCert(certdb, sigd->rawCerts[i]);
  621. if (!cert) {
  622. rv = SECFailure;
  623. break;
  624. }
  625. }
  626. if (sigd->cmsg) {
  627. pwarg = sigd->cmsg->pwfn_arg;
  628. }
  629. rv |= CERT_VerifyCert(certdb, cert, PR_TRUE, usage, now,
  630. pwarg, NULL);
  631. CERT_DestroyCertificate(cert);
  632. }
  633. return rv;
  634. }
  635. /*
  636. * NSS_CMSSignedData_HasDigests - see if we have digests in place
  637. */
  638. PRBool
  639. NSS_CMSSignedData_HasDigests(NSSCMSSignedData *sigd)
  640. {
  641. if (!sigd) {
  642. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  643. return PR_FALSE;
  644. }
  645. return (sigd->digests != NULL);
  646. }
  647. SECStatus
  648. NSS_CMSSignedData_AddCertList(NSSCMSSignedData *sigd, CERTCertificateList *certlist)
  649. {
  650. SECStatus rv;
  651. if (!sigd || !certlist) {
  652. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  653. return SECFailure;
  654. }
  655. /* XXX memory?? a certlist has an arena of its own and is not refcounted!?!? */
  656. rv = NSS_CMSArray_Add(sigd->cmsg->poolp, (void ***)&(sigd->certLists), (void *)certlist);
  657. return rv;
  658. }
  659. /*
  660. * NSS_CMSSignedData_AddCertChain - add cert and its entire chain to the set of certs
  661. */
  662. SECStatus
  663. NSS_CMSSignedData_AddCertChain(NSSCMSSignedData *sigd, CERTCertificate *cert)
  664. {
  665. CERTCertificateList *certlist;
  666. SECCertUsage usage;
  667. SECStatus rv;
  668. usage = certUsageEmailSigner;
  669. if (!sigd || !cert) {
  670. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  671. return SECFailure;
  672. }
  673. /* do not include root */
  674. certlist = CERT_CertChainFromCert(cert, usage, PR_FALSE);
  675. if (certlist == NULL)
  676. return SECFailure;
  677. rv = NSS_CMSSignedData_AddCertList(sigd, certlist);
  678. return rv;
  679. }
  680. extern SECStatus
  681. NSS_CMSSignedData_AddTempCertificate(NSSCMSSignedData *sigd, CERTCertificate *cert)
  682. {
  683. CERTCertificate *c;
  684. SECStatus rv;
  685. if (!sigd || !cert) {
  686. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  687. return SECFailure;
  688. }
  689. c = CERT_DupCertificate(cert);
  690. rv = NSS_CMSArray_Add(sigd->cmsg->poolp, (void ***)&(sigd->tempCerts), (void *)c);
  691. return rv;
  692. }
  693. SECStatus
  694. NSS_CMSSignedData_AddCertificate(NSSCMSSignedData *sigd, CERTCertificate *cert)
  695. {
  696. CERTCertificate *c;
  697. SECStatus rv;
  698. if (!sigd || !cert) {
  699. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  700. return SECFailure;
  701. }
  702. c = CERT_DupCertificate(cert);
  703. rv = NSS_CMSArray_Add(sigd->cmsg->poolp, (void ***)&(sigd->certs), (void *)c);
  704. return rv;
  705. }
  706. PRBool
  707. NSS_CMSSignedData_ContainsCertsOrCrls(NSSCMSSignedData *sigd)
  708. {
  709. if (!sigd) {
  710. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  711. return PR_FALSE;
  712. }
  713. if (sigd->rawCerts != NULL && sigd->rawCerts[0] != NULL)
  714. return PR_TRUE;
  715. else if (sigd->crls != NULL && sigd->crls[0] != NULL)
  716. return PR_TRUE;
  717. else
  718. return PR_FALSE;
  719. }
  720. SECStatus
  721. NSS_CMSSignedData_AddSignerInfo(NSSCMSSignedData *sigd,
  722. NSSCMSSignerInfo *signerinfo)
  723. {
  724. void *mark;
  725. SECStatus rv;
  726. SECOidTag digestalgtag;
  727. PLArenaPool *poolp;
  728. if (!sigd || !signerinfo) {
  729. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  730. return SECFailure;
  731. }
  732. poolp = sigd->cmsg->poolp;
  733. mark = PORT_ArenaMark(poolp);
  734. /* add signerinfo */
  735. rv = NSS_CMSArray_Add(poolp, (void ***)&(sigd->signerInfos), (void *)signerinfo);
  736. if (rv != SECSuccess)
  737. goto loser;
  738. /*
  739. * add empty digest
  740. * Empty because we don't have it yet. Either it gets created during encoding
  741. * (if the data is present) or has to be set externally.
  742. * XXX maybe pass it in optionally?
  743. */
  744. digestalgtag = NSS_CMSSignerInfo_GetDigestAlgTag(signerinfo);
  745. rv = NSS_CMSSignedData_SetDigestValue(sigd, digestalgtag, NULL);
  746. if (rv != SECSuccess)
  747. goto loser;
  748. /*
  749. * The last thing to get consistency would be adding the digest.
  750. */
  751. PORT_ArenaUnmark(poolp, mark);
  752. return SECSuccess;
  753. loser:
  754. PORT_ArenaRelease(poolp, mark);
  755. return SECFailure;
  756. }
  757. /*
  758. * NSS_CMSSignedData_SetDigests - set a signedData's digests member
  759. *
  760. * "digestalgs" - array of digest algorithm IDs
  761. * "digests" - array of digests corresponding to the digest algorithms
  762. */
  763. SECStatus
  764. NSS_CMSSignedData_SetDigests(NSSCMSSignedData *sigd,
  765. SECAlgorithmID **digestalgs,
  766. SECItem **digests)
  767. {
  768. int cnt, i, idx;
  769. if (!sigd || !digestalgs || !digests) {
  770. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  771. return SECFailure;
  772. }
  773. if (sigd->digestAlgorithms == NULL) {
  774. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  775. return SECFailure;
  776. }
  777. /* we assume that the digests array is just not there yet */
  778. PORT_Assert(sigd->digests == NULL);
  779. if (sigd->digests != NULL) {
  780. PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
  781. return SECFailure;
  782. }
  783. /* now allocate one (same size as digestAlgorithms) */
  784. cnt = NSS_CMSArray_Count((void **)sigd->digestAlgorithms);
  785. sigd->digests = PORT_ArenaZAlloc(sigd->cmsg->poolp, (cnt + 1) * sizeof(SECItem *));
  786. if (sigd->digests == NULL) {
  787. PORT_SetError(SEC_ERROR_NO_MEMORY);
  788. return SECFailure;
  789. }
  790. for (i = 0; sigd->digestAlgorithms[i] != NULL; i++) {
  791. /* try to find the sigd's i'th digest algorithm in the array we passed in */
  792. idx = NSS_CMSAlgArray_GetIndexByAlgID(digestalgs, sigd->digestAlgorithms[i]);
  793. if (idx < 0) {
  794. PORT_SetError(SEC_ERROR_DIGEST_NOT_FOUND);
  795. return SECFailure;
  796. }
  797. if (!digests[idx]) {
  798. /* We have no digest for this algorithm, probably because it is
  799. ** unrecognized or unsupported. We'll ignore this here. If this
  800. ** digest is needed later, an error will be be generated then.
  801. */
  802. continue;
  803. }
  804. /* found it - now set it */
  805. if ((sigd->digests[i] = SECITEM_AllocItem(sigd->cmsg->poolp, NULL, 0)) == NULL ||
  806. SECITEM_CopyItem(sigd->cmsg->poolp, sigd->digests[i], digests[idx]) != SECSuccess) {
  807. PORT_SetError(SEC_ERROR_NO_MEMORY);
  808. return SECFailure;
  809. }
  810. }
  811. return SECSuccess;
  812. }
  813. SECStatus
  814. NSS_CMSSignedData_SetDigestValue(NSSCMSSignedData *sigd,
  815. SECOidTag digestalgtag,
  816. SECItem *digestdata)
  817. {
  818. SECItem *digest = NULL;
  819. PLArenaPool *poolp;
  820. void *mark;
  821. int n, cnt;
  822. if (!sigd) {
  823. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  824. return SECFailure;
  825. }
  826. poolp = sigd->cmsg->poolp;
  827. mark = PORT_ArenaMark(poolp);
  828. if (digestdata) {
  829. digest = (SECItem *)PORT_ArenaZAlloc(poolp, sizeof(SECItem));
  830. /* copy digestdata item to arena (in case we have it and are not only making room) */
  831. if (SECITEM_CopyItem(poolp, digest, digestdata) != SECSuccess)
  832. goto loser;
  833. }
  834. /* now allocate one (same size as digestAlgorithms) */
  835. if (sigd->digests == NULL) {
  836. cnt = NSS_CMSArray_Count((void **)sigd->digestAlgorithms);
  837. sigd->digests = PORT_ArenaZAlloc(sigd->cmsg->poolp, (cnt + 1) * sizeof(SECItem *));
  838. if (sigd->digests == NULL) {
  839. PORT_SetError(SEC_ERROR_NO_MEMORY);
  840. return SECFailure;
  841. }
  842. }
  843. n = -1;
  844. if (sigd->digestAlgorithms != NULL)
  845. n = NSS_CMSAlgArray_GetIndexByAlgTag(sigd->digestAlgorithms, digestalgtag);
  846. /* if not found, add a digest */
  847. if (n < 0) {
  848. if (NSS_CMSSignedData_AddDigest(poolp, sigd, digestalgtag, digest) != SECSuccess)
  849. goto loser;
  850. } else {
  851. /* replace NULL pointer with digest item (and leak previous value) */
  852. sigd->digests[n] = digest;
  853. }
  854. PORT_ArenaUnmark(poolp, mark);
  855. return SECSuccess;
  856. loser:
  857. PORT_ArenaRelease(poolp, mark);
  858. return SECFailure;
  859. }
  860. SECStatus
  861. NSS_CMSSignedData_AddDigest(PLArenaPool *poolp,
  862. NSSCMSSignedData *sigd,
  863. SECOidTag digestalgtag,
  864. SECItem *digest)
  865. {
  866. SECAlgorithmID *digestalg;
  867. void *mark;
  868. if (!sigd || !poolp) {
  869. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  870. return SECFailure;
  871. }
  872. mark = PORT_ArenaMark(poolp);
  873. digestalg = PORT_ArenaZAlloc(poolp, sizeof(SECAlgorithmID));
  874. if (digestalg == NULL)
  875. goto loser;
  876. if (SECOID_SetAlgorithmID(poolp, digestalg, digestalgtag, NULL) != SECSuccess) /* no params */
  877. goto loser;
  878. if (NSS_CMSArray_Add(poolp, (void ***)&(sigd->digestAlgorithms),
  879. (void *)digestalg) != SECSuccess ||
  880. /* even if digest is NULL, add dummy to have same-size array */
  881. NSS_CMSArray_Add(poolp, (void ***)&(sigd->digests),
  882. (void *)digest) != SECSuccess) {
  883. goto loser;
  884. }
  885. PORT_ArenaUnmark(poolp, mark);
  886. return SECSuccess;
  887. loser:
  888. PORT_ArenaRelease(poolp, mark);
  889. return SECFailure;
  890. }
  891. /* XXX This function doesn't set the error code on failure. */
  892. SECItem *
  893. NSS_CMSSignedData_GetDigestValue(NSSCMSSignedData *sigd, SECOidTag digestalgtag)
  894. {
  895. int n;
  896. if (!sigd) {
  897. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  898. return NULL;
  899. }
  900. if (sigd->digestAlgorithms == NULL || sigd->digests == NULL) {
  901. PORT_SetError(SEC_ERROR_DIGEST_NOT_FOUND);
  902. return NULL;
  903. }
  904. n = NSS_CMSAlgArray_GetIndexByAlgTag(sigd->digestAlgorithms, digestalgtag);
  905. return (n < 0) ? NULL : sigd->digests[n];
  906. }
  907. /* =============================================================================
  908. * Misc. utility functions
  909. */
  910. /*
  911. * NSS_CMSSignedData_CreateCertsOnly - create a certs-only SignedData.
  912. *
  913. * cert - base certificates that will be included
  914. * include_chain - if true, include the complete cert chain for cert
  915. *
  916. * More certs and chains can be added via AddCertificate and AddCertChain.
  917. *
  918. * An error results in a return value of NULL and an error set.
  919. *
  920. * XXXX CRLs
  921. */
  922. NSSCMSSignedData *
  923. NSS_CMSSignedData_CreateCertsOnly(NSSCMSMessage *cmsg, CERTCertificate *cert, PRBool include_chain)
  924. {
  925. NSSCMSSignedData *sigd;
  926. void *mark;
  927. PLArenaPool *poolp;
  928. SECStatus rv;
  929. if (!cmsg || !cert) {
  930. PORT_SetError(SEC_ERROR_INVALID_ARGS);
  931. return NULL;
  932. }
  933. poolp = cmsg->poolp;
  934. mark = PORT_ArenaMark(poolp);
  935. sigd = NSS_CMSSignedData_Create(cmsg);
  936. if (sigd == NULL)
  937. goto loser;
  938. /* no signerinfos, thus no digestAlgorithms */
  939. /* but certs */
  940. if (include_chain) {
  941. rv = NSS_CMSSignedData_AddCertChain(sigd, cert);
  942. } else {
  943. rv = NSS_CMSSignedData_AddCertificate(sigd, cert);
  944. }
  945. if (rv != SECSuccess)
  946. goto loser;
  947. /* RFC2630 5.2 sez:
  948. * In the degenerate case where there are no signers, the
  949. * EncapsulatedContentInfo value being "signed" is irrelevant. In this
  950. * case, the content type within the EncapsulatedContentInfo value being
  951. * "signed" should be id-data (as defined in section 4), and the content
  952. * field of the EncapsulatedContentInfo value should be omitted.
  953. */
  954. rv = NSS_CMSContentInfo_SetContent_Data(cmsg, &(sigd->contentInfo), NULL, PR_TRUE);
  955. if (rv != SECSuccess)
  956. goto loser;
  957. PORT_ArenaUnmark(poolp, mark);
  958. return sigd;
  959. loser:
  960. if (sigd)
  961. NSS_CMSSignedData_Destroy(sigd);
  962. PORT_ArenaRelease(poolp, mark);
  963. return NULL;
  964. }
  965. /* TODO:
  966. * NSS_CMSSignerInfo_GetReceiptRequest()
  967. * NSS_CMSSignedData_HasReceiptRequest()
  968. * easy way to iterate over signers
  969. */