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
43 #include <security_asn1/secerr.h>
44 #include <Security/cssmapi.h>
46 #include <Security/SecCmsDigestContext.h>
49 struct SecCmsDigestContextStr
{
52 CSSM_CC_HANDLE
* digobjs
;
56 * SecCmsDigestContextStartMultiple - start digest calculation using all the
57 * digest algorithms in "digestalgs" in parallel.
59 SecCmsDigestContextRef
60 SecCmsDigestContextStartMultiple(SECAlgorithmID
**digestalgs
)
62 SecCmsDigestContextRef cmsdigcx
;
63 CSSM_CC_HANDLE digobj
;
67 digcnt
= (digestalgs
== NULL
) ? 0 : SecCmsArrayCount((void **)digestalgs
);
69 cmsdigcx
= (SecCmsDigestContextRef
)PORT_Alloc(sizeof(struct SecCmsDigestContextStr
));
74 cmsdigcx
->digobjs
= (CSSM_CC_HANDLE
*)PORT_Alloc(digcnt
* sizeof(CSSM_CC_HANDLE
));
75 if (cmsdigcx
->digobjs
== NULL
)
82 * Create a digest object context for each algorithm.
84 for (i
= 0; i
< digcnt
; i
++) {
85 digobj
= SecCmsUtilGetHashObjByAlgID(digestalgs
[i
]);
87 * Skip any algorithm we do not even recognize; obviously,
88 * this could be a problem, but if it is critical then the
89 * result will just be that the signature does not verify.
90 * We do not necessarily want to error out here, because
91 * the particular algorithm may not actually be important,
92 * but we cannot know that until later.
97 result
= CSSM_DigestDataInit(digobj
);
98 if (result
!= CSSM_OK
)
104 cmsdigcx
->digobjs
[cmsdigcx
->digcnt
] = digobj
;
108 cmsdigcx
->saw_contents
= PR_FALSE
;
114 if (cmsdigcx
->digobjs
)
115 PORT_Free(cmsdigcx
->digobjs
);
121 * SecCmsDigestContextStartSingle - same as SecCmsDigestContextStartMultiple, but
122 * only one algorithm.
124 SecCmsDigestContextRef
125 SecCmsDigestContextStartSingle(SECAlgorithmID
*digestalg
)
127 SECAlgorithmID
*digestalgs
[] = { NULL
, NULL
}; /* fake array */
129 digestalgs
[0] = digestalg
;
130 return SecCmsDigestContextStartMultiple(digestalgs
);
134 * SecCmsDigestContextUpdate - feed more data into the digest machine
137 SecCmsDigestContextUpdate(SecCmsDigestContextRef cmsdigcx
, const unsigned char *data
, size_t len
)
142 dataBuf
.Length
= len
;
143 dataBuf
.Data
= (uint8
*)data
;
144 cmsdigcx
->saw_contents
= PR_TRUE
;
145 for (i
= 0; i
< cmsdigcx
->digcnt
; i
++)
146 if (cmsdigcx
->digobjs
[i
])
147 CSSM_DigestDataUpdate(cmsdigcx
->digobjs
[i
], &dataBuf
, 1);
151 * SecCmsDigestContextCancel - cancel digesting operation
154 SecCmsDigestContextCancel(SecCmsDigestContextRef cmsdigcx
)
158 for (i
= 0; i
< cmsdigcx
->digcnt
; i
++)
159 if (cmsdigcx
->digobjs
[i
])
160 CSSM_DeleteContext(cmsdigcx
->digobjs
[i
]);
164 * SecCmsDigestContextFinishMultiple - finish the digests and put them
165 * into an array of CSSM_DATAs (allocated on poolp)
168 SecCmsDigestContextFinishMultiple(SecCmsDigestContextRef cmsdigcx
, SecArenaPoolRef poolp
,
169 CSSM_DATA_PTR
**digestsp
)
171 CSSM_CC_HANDLE digobj
;
172 CSSM_DATA_PTR
*digests
, digest
;
175 OSStatus rv
= SECFailure
;
177 /* no contents? do not update digests */
178 if (digestsp
== NULL
|| !cmsdigcx
->saw_contents
) {
179 for (i
= 0; i
< cmsdigcx
->digcnt
; i
++)
180 if (cmsdigcx
->digobjs
[i
])
181 CSSM_DeleteContext(cmsdigcx
->digobjs
[i
]);
188 mark
= PORT_ArenaMark ((PLArenaPool
*)poolp
);
190 /* allocate digest array & CSSM_DATAs on arena */
191 digests
= (CSSM_DATA_PTR
*)PORT_ArenaAlloc((PLArenaPool
*)poolp
, (cmsdigcx
->digcnt
+1) * sizeof(CSSM_DATA_PTR
));
192 digest
= (CSSM_DATA_PTR
)PORT_ArenaZAlloc((PLArenaPool
*)poolp
, cmsdigcx
->digcnt
* sizeof(CSSM_DATA
));
193 if (digests
== NULL
|| digest
== NULL
) {
197 for (i
= 0; i
< cmsdigcx
->digcnt
; i
++, digest
++) {
198 digobj
= cmsdigcx
->digobjs
[i
];
199 CSSM_QUERY_SIZE_DATA dataSize
;
200 rv
= CSSM_QuerySize(digobj
, CSSM_FALSE
, 1, &dataSize
);
206 int diglength
= dataSize
.SizeOutputBlock
;
210 digest
->Data
= (unsigned char*)PORT_ArenaAlloc((PLArenaPool
*)poolp
, diglength
);
211 if (digest
->Data
== NULL
)
213 digest
->Length
= diglength
;
214 rv
= CSSM_DigestDataFinal(digobj
, digest
);
220 CSSM_DeleteContext(digobj
);
236 if (rv
== SECSuccess
)
237 PORT_ArenaUnmark((PLArenaPool
*)poolp
, mark
);
239 PORT_ArenaRelease((PLArenaPool
*)poolp
, mark
);
242 if (cmsdigcx
->digcnt
> 0) {
243 PORT_Free(cmsdigcx
->digobjs
);
251 * SecCmsDigestContextFinishSingle - same as SecCmsDigestContextFinishMultiple,
252 * but for one digest.
255 SecCmsDigestContextFinishSingle(SecCmsDigestContextRef cmsdigcx
, SecArenaPoolRef poolp
,
256 CSSM_DATA_PTR digest
)
258 OSStatus rv
= SECFailure
;
260 PLArenaPool
*arena
= NULL
;
262 if ((arena
= PORT_NewArena(1024)) == NULL
)
265 /* get the digests into arena, then copy the first digest into poolp */
266 if (SecCmsDigestContextFinishMultiple(cmsdigcx
, (SecArenaPoolRef
)arena
, &dp
) != SECSuccess
)
269 /* now copy it into poolp */
270 if (SECITEM_CopyItem((PLArenaPool
*)poolp
, digest
, dp
[0]) != SECSuccess
)
277 PORT_FreeArena(arena
, PR_FALSE
);