2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
12 * The Original Code is the Netscape security libraries.
14 * The Initial Developer of the Original Code is Netscape
15 * Communications Corporation. Portions created by Netscape are
16 * Copyright (C) 1994-2000 Netscape Communications Corporation. All
21 * Alternatively, the contents of this file may be used under the
22 * terms of the GNU General Public License Version 2 or later (the
23 * "GPL"), in which case the provisions of the GPL are applicable
24 * instead of those above. If you wish to allow use of your
25 * version of this file only under the terms of the GPL and not to
26 * allow others to use your version of this file under the MPL,
27 * indicate your decision by deleting the provisions above and
28 * replace them with the notice and other provisions required by
29 * the GPL. If you do not delete the provisions above, a recipient
30 * may use your version of this file under either the MPL or the
35 * CMS digestedData methods.
38 #include <Security/SecCmsDigestedData.h>
40 #include <Security/SecCmsContentInfo.h>
41 #include <Security/SecCmsDigestContext.h>
45 #include "SecAsn1Item.h"
48 #include <security_asn1/secasn1.h>
49 #include <security_asn1/secerr.h>
50 #include <security_asn1/secport.h>
53 * SecCmsDigestedDataCreate - create a digestedData object (presumably for encoding)
55 * version will be set by SecCmsDigestedDataEncodeBeforeStart
56 * digestAlg is passed as parameter
57 * contentInfo must be filled by the user
58 * digest will be calculated while encoding
61 SecCmsDigestedDataCreate(SecCmsMessageRef cmsg
, SECAlgorithmID
*digestalg
)
64 SecCmsDigestedDataRef digd
;
69 mark
= PORT_ArenaMark(poolp
);
71 digd
= (SecCmsDigestedDataRef
)PORT_ArenaZAlloc(poolp
, sizeof(SecCmsDigestedData
));
75 digd
->contentInfo
.cmsg
= cmsg
;
77 if (SECOID_CopyAlgorithmID (poolp
, &(digd
->digestAlg
), digestalg
) != SECSuccess
)
80 PORT_ArenaUnmark(poolp
, mark
);
84 PORT_ArenaRelease(poolp
, mark
);
89 * SecCmsDigestedDataDestroy - destroy a digestedData object
92 SecCmsDigestedDataDestroy(SecCmsDigestedDataRef digd
)
97 /* everything's in a pool, so don't worry about the storage */
98 SecCmsContentInfoDestroy(&(digd
->contentInfo
));
103 * SecCmsDigestedDataGetContentInfo - return pointer to digestedData object's contentInfo
106 SecCmsDigestedDataGetContentInfo(SecCmsDigestedDataRef digd
)
108 return &(digd
->contentInfo
);
112 * SecCmsDigestedDataEncodeBeforeStart - do all the necessary things to a DigestedData
113 * before encoding begins.
116 * - set the right version number. The contentInfo's content type must be set up already.
119 SecCmsDigestedDataEncodeBeforeStart(SecCmsDigestedDataRef digd
)
121 unsigned long version
;
124 version
= SEC_CMS_DIGESTED_DATA_VERSION_DATA
;
125 if (SecCmsContentInfoGetContentTypeTag(&(digd
->contentInfo
)) != SEC_OID_PKCS7_DATA
)
126 version
= SEC_CMS_DIGESTED_DATA_VERSION_ENCAP
;
128 dummy
= SEC_ASN1EncodeInteger(digd
->contentInfo
.cmsg
->poolp
, &(digd
->version
), version
);
129 return (dummy
== NULL
) ? SECFailure
: SECSuccess
;
133 * SecCmsDigestedDataEncodeBeforeData - do all the necessary things to a DigestedData
134 * before the encapsulated data is passed through the encoder.
137 * - set up the digests if necessary
140 SecCmsDigestedDataEncodeBeforeData(SecCmsDigestedDataRef digd
)
142 /* set up the digests */
143 if (digd
->digestAlg
.algorithm
.Length
!= 0 && digd
->digest
.Length
== 0) {
144 /* if digest is already there, do nothing */
145 digd
->contentInfo
.digcx
= SecCmsDigestContextStartSingle(&(digd
->digestAlg
));
146 if (digd
->contentInfo
.digcx
== NULL
)
153 * SecCmsDigestedDataEncodeAfterData - do all the necessary things to a DigestedData
154 * after all the encapsulated data was passed through the encoder.
157 * - finish the digests
160 SecCmsDigestedDataEncodeAfterData(SecCmsDigestedDataRef digd
)
162 OSStatus rv
= SECSuccess
;
163 /* did we have digest calculation going on? */
164 if (digd
->contentInfo
.digcx
) {
166 rv
= SecCmsDigestContextFinishSingle(digd
->contentInfo
.digcx
, &data
);
167 if (rv
== SECSuccess
)
168 rv
= SECITEM_CopyItem(digd
->contentInfo
.cmsg
->poolp
, &(digd
->digest
), &data
);
169 if (rv
== SECSuccess
)
170 SecCmsDigestContextDestroy(digd
->contentInfo
.digcx
);
171 digd
->contentInfo
.digcx
= NULL
;
178 * SecCmsDigestedDataDecodeBeforeData - do all the necessary things to a DigestedData
179 * before the encapsulated data is passed through the encoder.
182 * - set up the digests if necessary
185 SecCmsDigestedDataDecodeBeforeData(SecCmsDigestedDataRef digd
)
187 /* is there a digest algorithm yet? */
188 if (digd
->digestAlg
.algorithm
.Length
== 0)
191 digd
->contentInfo
.digcx
= SecCmsDigestContextStartSingle(&(digd
->digestAlg
));
192 if (digd
->contentInfo
.digcx
== NULL
)
199 * SecCmsDigestedDataDecodeAfterData - do all the necessary things to a DigestedData
200 * after all the encapsulated data was passed through the encoder.
203 * - finish the digests
206 SecCmsDigestedDataDecodeAfterData(SecCmsDigestedDataRef digd
)
208 OSStatus rv
= SECSuccess
;
209 /* did we have digest calculation going on? */
210 if (digd
->contentInfo
.digcx
) {
212 rv
= SecCmsDigestContextFinishSingle(digd
->contentInfo
.digcx
, &data
);
213 if (rv
== SECSuccess
)
214 rv
= SECITEM_CopyItem(digd
->contentInfo
.cmsg
->poolp
, &(digd
->digest
), &data
);
215 if (rv
== SECSuccess
)
216 SecCmsDigestContextDestroy(digd
->contentInfo
.digcx
);
217 digd
->contentInfo
.digcx
= NULL
;
224 * SecCmsDigestedDataDecodeAfterEnd - finalize a digestedData.
227 * - check the digests for equality
230 SecCmsDigestedDataDecodeAfterEnd(SecCmsDigestedDataRef digd
)
235 /* did we have digest calculation going on? */
236 if (digd
->cdigest
.Length
!= 0) {
237 /* XXX comparision btw digest & cdigest */