cmsdigdata.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 digestedData methods.
  6. */
  7. #include "cmslocal.h"
  8. #include "secitem.h"
  9. #include "secasn1.h"
  10. #include "secoid.h"
  11. #include "secerr.h"
  12. /*
  13. * NSS_CMSDigestedData_Create - create a digestedData object (presumably for encoding)
  14. *
  15. * version will be set by NSS_CMSDigestedData_Encode_BeforeStart
  16. * digestAlg is passed as parameter
  17. * contentInfo must be filled by the user
  18. * digest will be calculated while encoding
  19. */
  20. NSSCMSDigestedData *
  21. NSS_CMSDigestedData_Create(NSSCMSMessage *cmsg, SECAlgorithmID *digestalg)
  22. {
  23. void *mark;
  24. NSSCMSDigestedData *digd;
  25. PLArenaPool *poolp;
  26. poolp = cmsg->poolp;
  27. mark = PORT_ArenaMark(poolp);
  28. digd = (NSSCMSDigestedData *)PORT_ArenaZAlloc(poolp, sizeof(NSSCMSDigestedData));
  29. if (digd == NULL)
  30. goto loser;
  31. digd->cmsg = cmsg;
  32. if (SECOID_CopyAlgorithmID(poolp, &(digd->digestAlg), digestalg) != SECSuccess)
  33. goto loser;
  34. PORT_ArenaUnmark(poolp, mark);
  35. return digd;
  36. loser:
  37. PORT_ArenaRelease(poolp, mark);
  38. return NULL;
  39. }
  40. /*
  41. * NSS_CMSDigestedData_Destroy - destroy a digestedData object
  42. */
  43. void
  44. NSS_CMSDigestedData_Destroy(NSSCMSDigestedData *digd)
  45. {
  46. /* everything's in a pool, so don't worry about the storage */
  47. if (digd != NULL) {
  48. NSS_CMSContentInfo_Destroy(&(digd->contentInfo));
  49. }
  50. return;
  51. }
  52. /*
  53. * NSS_CMSDigestedData_GetContentInfo - return pointer to digestedData object's contentInfo
  54. */
  55. NSSCMSContentInfo *
  56. NSS_CMSDigestedData_GetContentInfo(NSSCMSDigestedData *digd)
  57. {
  58. return &(digd->contentInfo);
  59. }
  60. /*
  61. * NSS_CMSDigestedData_Encode_BeforeStart - do all the necessary things to a DigestedData
  62. * before encoding begins.
  63. *
  64. * In particular:
  65. * - set the right version number. The contentInfo's content type must be set up already.
  66. */
  67. SECStatus
  68. NSS_CMSDigestedData_Encode_BeforeStart(NSSCMSDigestedData *digd)
  69. {
  70. unsigned long version;
  71. SECItem *dummy;
  72. version = NSS_CMS_DIGESTED_DATA_VERSION_DATA;
  73. if (!NSS_CMSType_IsData(NSS_CMSContentInfo_GetContentTypeTag(
  74. &(digd->contentInfo))))
  75. version = NSS_CMS_DIGESTED_DATA_VERSION_ENCAP;
  76. dummy = SEC_ASN1EncodeInteger(digd->cmsg->poolp, &(digd->version), version);
  77. return (dummy == NULL) ? SECFailure : SECSuccess;
  78. }
  79. /*
  80. * NSS_CMSDigestedData_Encode_BeforeData - do all the necessary things to a DigestedData
  81. * before the encapsulated data is passed through the encoder.
  82. *
  83. * In detail:
  84. * - set up the digests if necessary
  85. */
  86. SECStatus
  87. NSS_CMSDigestedData_Encode_BeforeData(NSSCMSDigestedData *digd)
  88. {
  89. SECStatus rv = NSS_CMSContentInfo_Private_Init(&digd->contentInfo);
  90. if (rv != SECSuccess) {
  91. return SECFailure;
  92. }
  93. /* set up the digests */
  94. if (digd->digestAlg.algorithm.len != 0 && digd->digest.len == 0) {
  95. /* if digest is already there, do nothing */
  96. digd->contentInfo.privateInfo->digcx = NSS_CMSDigestContext_StartSingle(&(digd->digestAlg));
  97. if (digd->contentInfo.privateInfo->digcx == NULL)
  98. return SECFailure;
  99. }
  100. return SECSuccess;
  101. }
  102. /*
  103. * NSS_CMSDigestedData_Encode_AfterData - do all the necessary things to a DigestedData
  104. * after all the encapsulated data was passed through the encoder.
  105. *
  106. * In detail:
  107. * - finish the digests
  108. */
  109. SECStatus
  110. NSS_CMSDigestedData_Encode_AfterData(NSSCMSDigestedData *digd)
  111. {
  112. SECStatus rv = SECSuccess;
  113. /* did we have digest calculation going on? */
  114. if (digd->contentInfo.privateInfo && digd->contentInfo.privateInfo->digcx) {
  115. rv = NSS_CMSDigestContext_FinishSingle(digd->contentInfo.privateInfo->digcx,
  116. digd->cmsg->poolp,
  117. &(digd->digest));
  118. /* error has been set by NSS_CMSDigestContext_FinishSingle */
  119. digd->contentInfo.privateInfo->digcx = NULL;
  120. }
  121. return rv;
  122. }
  123. /*
  124. * NSS_CMSDigestedData_Decode_BeforeData - do all the necessary things to a DigestedData
  125. * before the encapsulated data is passed through the encoder.
  126. *
  127. * In detail:
  128. * - set up the digests if necessary
  129. */
  130. SECStatus
  131. NSS_CMSDigestedData_Decode_BeforeData(NSSCMSDigestedData *digd)
  132. {
  133. SECStatus rv;
  134. /* is there a digest algorithm yet? */
  135. if (digd->digestAlg.algorithm.len == 0)
  136. return SECFailure;
  137. rv = NSS_CMSContentInfo_Private_Init(&digd->contentInfo);
  138. if (rv != SECSuccess) {
  139. return SECFailure;
  140. }
  141. digd->contentInfo.privateInfo->digcx = NSS_CMSDigestContext_StartSingle(&(digd->digestAlg));
  142. if (digd->contentInfo.privateInfo->digcx == NULL)
  143. return SECFailure;
  144. return SECSuccess;
  145. }
  146. /*
  147. * NSS_CMSDigestedData_Decode_AfterData - do all the necessary things to a DigestedData
  148. * after all the encapsulated data was passed through the encoder.
  149. *
  150. * In detail:
  151. * - finish the digests
  152. */
  153. SECStatus
  154. NSS_CMSDigestedData_Decode_AfterData(NSSCMSDigestedData *digd)
  155. {
  156. SECStatus rv = SECSuccess;
  157. /* did we have digest calculation going on? */
  158. if (digd->contentInfo.privateInfo && digd->contentInfo.privateInfo->digcx) {
  159. rv = NSS_CMSDigestContext_FinishSingle(digd->contentInfo.privateInfo->digcx,
  160. digd->cmsg->poolp,
  161. &(digd->cdigest));
  162. /* error has been set by NSS_CMSDigestContext_FinishSingle */
  163. digd->contentInfo.privateInfo->digcx = NULL;
  164. }
  165. return rv;
  166. }
  167. /*
  168. * NSS_CMSDigestedData_Decode_AfterEnd - finalize a digestedData.
  169. *
  170. * In detail:
  171. * - check the digests for equality
  172. */
  173. SECStatus
  174. NSS_CMSDigestedData_Decode_AfterEnd(NSSCMSDigestedData *digd)
  175. {
  176. /* did we have digest calculation going on? */
  177. if (digd->cdigest.len != 0) {
  178. /* XXX comparision btw digest & cdigest */
  179. /* XXX set status */
  180. /* TODO!!!! */
  181. }
  182. return SECSuccess;
  183. }