/security/nss/lib/smime/cmsdigdata.c

http://github.com/zpao/v8monkey · C · 244 lines · 108 code · 29 blank · 107 comment · 26 complexity · 23df21814c4ec54db893f5c4b28d9a07 MD5 · raw file

  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. *
  4. * The contents of this file are subject to the Mozilla Public License Version
  5. * 1.1 (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. * http://www.mozilla.org/MPL/
  8. *
  9. * Software distributed under the License is distributed on an "AS IS" basis,
  10. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. * for the specific language governing rights and limitations under the
  12. * License.
  13. *
  14. * The Original Code is the Netscape security libraries.
  15. *
  16. * The Initial Developer of the Original Code is
  17. * Netscape Communications Corporation.
  18. * Portions created by the Initial Developer are Copyright (C) 1994-2000
  19. * the Initial Developer. All Rights Reserved.
  20. *
  21. * Contributor(s):
  22. *
  23. * Alternatively, the contents of this file may be used under the terms of
  24. * either the GNU General Public License Version 2 or later (the "GPL"), or
  25. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26. * in which case the provisions of the GPL or the LGPL are applicable instead
  27. * of those above. If you wish to allow use of your version of this file only
  28. * under the terms of either the GPL or the LGPL, and not to allow others to
  29. * use your version of this file under the terms of the MPL, indicate your
  30. * decision by deleting the provisions above and replace them with the notice
  31. * and other provisions required by the GPL or the LGPL. If you do not delete
  32. * the provisions above, a recipient may use your version of this file under
  33. * the terms of any one of the MPL, the GPL or the LGPL.
  34. *
  35. * ***** END LICENSE BLOCK ***** */
  36. /*
  37. * CMS digestedData methods.
  38. *
  39. * $Id: cmsdigdata.c,v 1.7 2011/02/11 01:53:17 emaldona%redhat.com Exp $
  40. */
  41. #include "cmslocal.h"
  42. #include "secitem.h"
  43. #include "secasn1.h"
  44. #include "secoid.h"
  45. #include "secerr.h"
  46. /*
  47. * NSS_CMSDigestedData_Create - create a digestedData object (presumably for encoding)
  48. *
  49. * version will be set by NSS_CMSDigestedData_Encode_BeforeStart
  50. * digestAlg is passed as parameter
  51. * contentInfo must be filled by the user
  52. * digest will be calculated while encoding
  53. */
  54. NSSCMSDigestedData *
  55. NSS_CMSDigestedData_Create(NSSCMSMessage *cmsg, SECAlgorithmID *digestalg)
  56. {
  57. void *mark;
  58. NSSCMSDigestedData *digd;
  59. PLArenaPool *poolp;
  60. poolp = cmsg->poolp;
  61. mark = PORT_ArenaMark(poolp);
  62. digd = (NSSCMSDigestedData *)PORT_ArenaZAlloc(poolp, sizeof(NSSCMSDigestedData));
  63. if (digd == NULL)
  64. goto loser;
  65. digd->cmsg = cmsg;
  66. if (SECOID_CopyAlgorithmID (poolp, &(digd->digestAlg), digestalg) != SECSuccess)
  67. goto loser;
  68. PORT_ArenaUnmark(poolp, mark);
  69. return digd;
  70. loser:
  71. PORT_ArenaRelease(poolp, mark);
  72. return NULL;
  73. }
  74. /*
  75. * NSS_CMSDigestedData_Destroy - destroy a digestedData object
  76. */
  77. void
  78. NSS_CMSDigestedData_Destroy(NSSCMSDigestedData *digd)
  79. {
  80. /* everything's in a pool, so don't worry about the storage */
  81. NSS_CMSContentInfo_Destroy(&(digd->contentInfo));
  82. return;
  83. }
  84. /*
  85. * NSS_CMSDigestedData_GetContentInfo - return pointer to digestedData object's contentInfo
  86. */
  87. NSSCMSContentInfo *
  88. NSS_CMSDigestedData_GetContentInfo(NSSCMSDigestedData *digd)
  89. {
  90. return &(digd->contentInfo);
  91. }
  92. /*
  93. * NSS_CMSDigestedData_Encode_BeforeStart - do all the necessary things to a DigestedData
  94. * before encoding begins.
  95. *
  96. * In particular:
  97. * - set the right version number. The contentInfo's content type must be set up already.
  98. */
  99. SECStatus
  100. NSS_CMSDigestedData_Encode_BeforeStart(NSSCMSDigestedData *digd)
  101. {
  102. unsigned long version;
  103. SECItem *dummy;
  104. version = NSS_CMS_DIGESTED_DATA_VERSION_DATA;
  105. if (!NSS_CMSType_IsData(NSS_CMSContentInfo_GetContentTypeTag(
  106. &(digd->contentInfo))))
  107. version = NSS_CMS_DIGESTED_DATA_VERSION_ENCAP;
  108. dummy = SEC_ASN1EncodeInteger(digd->cmsg->poolp, &(digd->version), version);
  109. return (dummy == NULL) ? SECFailure : SECSuccess;
  110. }
  111. /*
  112. * NSS_CMSDigestedData_Encode_BeforeData - do all the necessary things to a DigestedData
  113. * before the encapsulated data is passed through the encoder.
  114. *
  115. * In detail:
  116. * - set up the digests if necessary
  117. */
  118. SECStatus
  119. NSS_CMSDigestedData_Encode_BeforeData(NSSCMSDigestedData *digd)
  120. {
  121. SECStatus rv =NSS_CMSContentInfo_Private_Init(&digd->contentInfo);
  122. if (rv != SECSuccess) {
  123. return SECFailure;
  124. }
  125. /* set up the digests */
  126. if (digd->digestAlg.algorithm.len != 0 && digd->digest.len == 0) {
  127. /* if digest is already there, do nothing */
  128. digd->contentInfo.privateInfo->digcx = NSS_CMSDigestContext_StartSingle(&(digd->digestAlg));
  129. if (digd->contentInfo.privateInfo->digcx == NULL)
  130. return SECFailure;
  131. }
  132. return SECSuccess;
  133. }
  134. /*
  135. * NSS_CMSDigestedData_Encode_AfterData - do all the necessary things to a DigestedData
  136. * after all the encapsulated data was passed through the encoder.
  137. *
  138. * In detail:
  139. * - finish the digests
  140. */
  141. SECStatus
  142. NSS_CMSDigestedData_Encode_AfterData(NSSCMSDigestedData *digd)
  143. {
  144. SECStatus rv = SECSuccess;
  145. /* did we have digest calculation going on? */
  146. if (digd->contentInfo.privateInfo && digd->contentInfo.privateInfo->digcx) {
  147. rv = NSS_CMSDigestContext_FinishSingle(digd->contentInfo.privateInfo->digcx,
  148. digd->cmsg->poolp,
  149. &(digd->digest));
  150. /* error has been set by NSS_CMSDigestContext_FinishSingle */
  151. digd->contentInfo.privateInfo->digcx = NULL;
  152. }
  153. return rv;
  154. }
  155. /*
  156. * NSS_CMSDigestedData_Decode_BeforeData - do all the necessary things to a DigestedData
  157. * before the encapsulated data is passed through the encoder.
  158. *
  159. * In detail:
  160. * - set up the digests if necessary
  161. */
  162. SECStatus
  163. NSS_CMSDigestedData_Decode_BeforeData(NSSCMSDigestedData *digd)
  164. {
  165. SECStatus rv;
  166. /* is there a digest algorithm yet? */
  167. if (digd->digestAlg.algorithm.len == 0)
  168. return SECFailure;
  169. rv = NSS_CMSContentInfo_Private_Init(&digd->contentInfo);
  170. if (rv != SECSuccess) {
  171. return SECFailure;
  172. }
  173. digd->contentInfo.privateInfo->digcx = NSS_CMSDigestContext_StartSingle(&(digd->digestAlg));
  174. if (digd->contentInfo.privateInfo->digcx == NULL)
  175. return SECFailure;
  176. return SECSuccess;
  177. }
  178. /*
  179. * NSS_CMSDigestedData_Decode_AfterData - do all the necessary things to a DigestedData
  180. * after all the encapsulated data was passed through the encoder.
  181. *
  182. * In detail:
  183. * - finish the digests
  184. */
  185. SECStatus
  186. NSS_CMSDigestedData_Decode_AfterData(NSSCMSDigestedData *digd)
  187. {
  188. SECStatus rv = SECSuccess;
  189. /* did we have digest calculation going on? */
  190. if (digd->contentInfo.privateInfo && digd->contentInfo.privateInfo->digcx) {
  191. rv = NSS_CMSDigestContext_FinishSingle(digd->contentInfo.privateInfo->digcx,
  192. digd->cmsg->poolp,
  193. &(digd->cdigest));
  194. /* error has been set by NSS_CMSDigestContext_FinishSingle */
  195. digd->contentInfo.privateInfo->digcx = NULL;
  196. }
  197. return rv;
  198. }
  199. /*
  200. * NSS_CMSDigestedData_Decode_AfterEnd - finalize a digestedData.
  201. *
  202. * In detail:
  203. * - check the digests for equality
  204. */
  205. SECStatus
  206. NSS_CMSDigestedData_Decode_AfterEnd(NSSCMSDigestedData *digd)
  207. {
  208. /* did we have digest calculation going on? */
  209. if (digd->cdigest.len != 0) {
  210. /* XXX comparision btw digest & cdigest */
  211. /* XXX set status */
  212. /* TODO!!!! */
  213. }
  214. return SECSuccess;
  215. }