1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14 * The Original Code is the Netscape security libraries.
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.
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.
35 * ***** END LICENSE BLOCK ***** */
38 * CMS digestedData methods.
40 * $Id: cmsdigdata.c,v 1.5 2004/04/25 15:03:16 gerv%gerv.net Exp $
51 * NSS_CMSDigestedData_Create - create a digestedData object (presumably for encoding)
53 * version will be set by NSS_CMSDigestedData_Encode_BeforeStart
54 * digestAlg is passed as parameter
55 * contentInfo must be filled by the user
56 * digest will be calculated while encoding
59 NSS_CMSDigestedData_Create(NSSCMSMessage
*cmsg
, SECAlgorithmID
*digestalg
)
62 NSSCMSDigestedData
*digd
;
67 mark
= PORT_ArenaMark(poolp
);
69 digd
= (NSSCMSDigestedData
*)PORT_ArenaZAlloc(poolp
, sizeof(NSSCMSDigestedData
));
75 if (SECOID_CopyAlgorithmID (poolp
, &(digd
->digestAlg
), digestalg
) != SECSuccess
)
78 PORT_ArenaUnmark(poolp
, mark
);
82 PORT_ArenaRelease(poolp
, mark
);
87 * NSS_CMSDigestedData_Destroy - destroy a digestedData object
90 NSS_CMSDigestedData_Destroy(NSSCMSDigestedData
*digd
)
92 /* everything's in a pool, so don't worry about the storage */
93 NSS_CMSContentInfo_Destroy(&(digd
->contentInfo
));
98 * NSS_CMSDigestedData_GetContentInfo - return pointer to digestedData object's contentInfo
101 NSS_CMSDigestedData_GetContentInfo(NSSCMSDigestedData
*digd
)
103 return &(digd
->contentInfo
);
107 * NSS_CMSDigestedData_Encode_BeforeStart - do all the necessary things to a DigestedData
108 * before encoding begins.
111 * - set the right version number. The contentInfo's content type must be set up already.
114 NSS_CMSDigestedData_Encode_BeforeStart(NSSCMSDigestedData
*digd
)
116 unsigned long version
;
119 version
= NSS_CMS_DIGESTED_DATA_VERSION_DATA
;
120 if (NSS_CMSContentInfo_GetContentTypeTag(&(digd
->contentInfo
)) != SEC_OID_PKCS7_DATA
)
121 version
= NSS_CMS_DIGESTED_DATA_VERSION_ENCAP
;
123 dummy
= SEC_ASN1EncodeInteger(digd
->cmsg
->poolp
, &(digd
->version
), version
);
124 return (dummy
== NULL
) ? SECFailure
: SECSuccess
;
128 * NSS_CMSDigestedData_Encode_BeforeData - do all the necessary things to a DigestedData
129 * before the encapsulated data is passed through the encoder.
132 * - set up the digests if necessary
135 NSS_CMSDigestedData_Encode_BeforeData(NSSCMSDigestedData
*digd
)
137 /* set up the digests */
138 if (digd
->digestAlg
.algorithm
.len
!= 0 && digd
->digest
.len
== 0) {
139 /* if digest is already there, do nothing */
140 digd
->contentInfo
.digcx
= NSS_CMSDigestContext_StartSingle(&(digd
->digestAlg
));
141 if (digd
->contentInfo
.digcx
== NULL
)
148 * NSS_CMSDigestedData_Encode_AfterData - do all the necessary things to a DigestedData
149 * after all the encapsulated data was passed through the encoder.
152 * - finish the digests
155 NSS_CMSDigestedData_Encode_AfterData(NSSCMSDigestedData
*digd
)
157 SECStatus rv
= SECSuccess
;
158 /* did we have digest calculation going on? */
159 if (digd
->contentInfo
.digcx
) {
160 rv
= NSS_CMSDigestContext_FinishSingle(digd
->contentInfo
.digcx
,
163 /* error has been set by NSS_CMSDigestContext_FinishSingle */
164 digd
->contentInfo
.digcx
= NULL
;
171 * NSS_CMSDigestedData_Decode_BeforeData - do all the necessary things to a DigestedData
172 * before the encapsulated data is passed through the encoder.
175 * - set up the digests if necessary
178 NSS_CMSDigestedData_Decode_BeforeData(NSSCMSDigestedData
*digd
)
180 /* is there a digest algorithm yet? */
181 if (digd
->digestAlg
.algorithm
.len
== 0)
184 digd
->contentInfo
.digcx
= NSS_CMSDigestContext_StartSingle(&(digd
->digestAlg
));
185 if (digd
->contentInfo
.digcx
== NULL
)
192 * NSS_CMSDigestedData_Decode_AfterData - do all the necessary things to a DigestedData
193 * after all the encapsulated data was passed through the encoder.
196 * - finish the digests
199 NSS_CMSDigestedData_Decode_AfterData(NSSCMSDigestedData
*digd
)
201 SECStatus rv
= SECSuccess
;
202 /* did we have digest calculation going on? */
203 if (digd
->contentInfo
.digcx
) {
204 rv
= NSS_CMSDigestContext_FinishSingle(digd
->contentInfo
.digcx
,
207 /* error has been set by NSS_CMSDigestContext_FinishSingle */
208 digd
->contentInfo
.digcx
= NULL
;
215 * NSS_CMSDigestedData_Decode_AfterEnd - finalize a digestedData.
218 * - check the digests for equality
221 NSS_CMSDigestedData_Decode_AfterEnd(NSSCMSDigestedData
*digd
)
223 /* did we have digest calculation going on? */
224 if (digd
->cdigest
.len
!= 0) {
225 /* XXX comparision btw digest & cdigest */