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
41 #include "SecAsn1Item.h"
44 #include <security_asn1/secerr.h>
45 #include <security_asn1/secport.h>
47 #include <CommonCrypto/CommonDigest.h>
49 #include "SecCmsDigestContext.h"
51 /* Return the maximum value between S and T (and U) */
52 #define MAX(S, T) ({__typeof__(S) _max_s = S; __typeof__(T) _max_t = T; _max_s > _max_t ? _max_s : _max_t;})
53 #define MAX_OF_3(S, T, U) ({__typeof__(U) _max_st = MAX(S,T); MAX(_max_st,U);})
55 struct SecCmsDigestContextStr
{
60 SECAlgorithmID
** digestalgs
;
64 * SecCmsDigestContextStartMultiple - start digest calculation using all the
65 * digest algorithms in "digestalgs" in parallel.
67 SecCmsDigestContextRef
68 SecCmsDigestContextStartMultiple(SECAlgorithmID
**digestalgs
)
71 SecCmsDigestContextRef cmsdigcx
;
76 poolp
= PORT_NewArena(1024);
80 digcnt
= (digestalgs
== NULL
) ? 0 : SecCmsArrayCount((void **)digestalgs
);
82 cmsdigcx
= (SecCmsDigestContextRef
)PORT_ArenaAlloc(poolp
, sizeof(struct SecCmsDigestContextStr
));
83 if (cmsdigcx
== NULL
) {
86 cmsdigcx
->poolp
= poolp
;
89 /* Security check to prevent under-allocation */
90 if (digcnt
>= (int)((INT_MAX
/(MAX(sizeof(void *),sizeof(SECAlgorithmID
*))))-1)) {
93 cmsdigcx
->digobjs
= (void**)PORT_ArenaAlloc(poolp
, digcnt
* sizeof(void *));
95 if (cmsdigcx
->digobjs
== NULL
)
98 cmsdigcx
->digestalgs
= (SECAlgorithmID
**)PORT_ArenaZAlloc(poolp
,
99 (digcnt
+ 1) * sizeof(SECAlgorithmID
*));
100 if (cmsdigcx
->digestalgs
== NULL
)
104 cmsdigcx
->digcnt
= 0;
107 * Create a digest object context for each algorithm.
109 for (i
= 0; i
< digcnt
; i
++) {
110 digobj
= SecCmsUtilGetHashObjByAlgID(digestalgs
[i
]);
112 * Skip any algorithm we do not even recognize; obviously,
113 * this could be a problem, but if it is critical then the
114 * result will just be that the signature does not verify.
115 * We do not necessarily want to error out here, because
116 * the particular algorithm may not actually be important,
117 * but we cannot know that until later.
120 cmsdigcx
->digobjs
[cmsdigcx
->digcnt
] = digobj
;
121 cmsdigcx
->digestalgs
[cmsdigcx
->digcnt
] = PORT_ArenaAlloc(poolp
, sizeof(SECAlgorithmID
));
122 if (SECITEM_CopyItem(poolp
,
123 &(cmsdigcx
->digestalgs
[cmsdigcx
->digcnt
]->algorithm
),
124 &(digestalgs
[i
]->algorithm
))
125 || SECITEM_CopyItem(poolp
,
126 &(cmsdigcx
->digestalgs
[cmsdigcx
->digcnt
]->parameters
),
127 &(digestalgs
[i
]->parameters
)))
132 cmsdigcx
->saw_contents
= PR_FALSE
;
138 PORT_FreeArena(poolp
, PR_FALSE
);
144 * SecCmsDigestContextStartSingle - same as SecCmsDigestContextStartMultiple, but
145 * only one algorithm.
147 SecCmsDigestContextRef
148 SecCmsDigestContextStartSingle(SECAlgorithmID
*digestalg
)
150 SECAlgorithmID
*digestalgs
[] = { NULL
, NULL
}; /* fake array */
152 digestalgs
[0] = digestalg
;
153 return SecCmsDigestContextStartMultiple(digestalgs
);
157 * SecCmsDigestContextUpdate - feed more data into the digest machine
160 SecCmsDigestContextUpdate(SecCmsDigestContextRef cmsdigcx
, const unsigned char *data
, size_t len
)
165 dataBuf
.Length
= len
;
166 dataBuf
.Data
= (uint8_t *)data
;
167 cmsdigcx
->saw_contents
= PR_TRUE
;
168 for (i
= 0; i
< cmsdigcx
->digcnt
; i
++) {
169 if (cmsdigcx
->digobjs
[i
]) {
170 /* 64 bits cast: worst case is we truncate the length and we dont hash all the data.
171 This may cause an invalid CMS blob larger than 4GB to be validated. Unlikely, but
172 possible security issue. There is no way to return an error here, but a check at
173 the upper level may happen. */
175 rdar://problem/20642513
176 Let's just die a horrible death rather than have the security issue.
177 CMS blob over 4GB? Oh well.
179 if (len
> UINT32_MAX
) {
183 assert(len
<=UINT32_MAX
); /* Debug check. Correct as long as CC_LONG is uint32_t */
184 switch (SECOID_GetAlgorithmTag(cmsdigcx
->digestalgs
[i
])) {
185 case SEC_OID_SHA1
: CC_SHA1_Update((CC_SHA1_CTX
*)cmsdigcx
->digobjs
[i
], data
, (CC_LONG
)len
); break;
186 case SEC_OID_MD5
: CC_MD5_Update((CC_MD5_CTX
*)cmsdigcx
->digobjs
[i
], data
, (CC_LONG
)len
); break;
187 case SEC_OID_SHA224
: CC_SHA224_Update((CC_SHA256_CTX
*)cmsdigcx
->digobjs
[i
], data
, (CC_LONG
)len
); break;
188 case SEC_OID_SHA256
: CC_SHA256_Update((CC_SHA256_CTX
*)cmsdigcx
->digobjs
[i
], data
, (CC_LONG
)len
); break;
189 case SEC_OID_SHA384
: CC_SHA384_Update((CC_SHA512_CTX
*)cmsdigcx
->digobjs
[i
], data
, (CC_LONG
)len
); break;
190 case SEC_OID_SHA512
: CC_SHA512_Update((CC_SHA512_CTX
*)cmsdigcx
->digobjs
[i
], data
, (CC_LONG
)len
); break;
199 * SecCmsDigestContextCancel - cancel digesting operation
202 SecCmsDigestContextCancel(SecCmsDigestContextRef cmsdigcx
)
206 for (i
= 0; i
< cmsdigcx
->digcnt
; i
++)
207 if (cmsdigcx
->digobjs
[i
])
208 free(cmsdigcx
->digobjs
[i
]);
210 PORT_FreeArena(cmsdigcx
->poolp
, PR_TRUE
);
214 * SecCmsDigestContextDestroy - delete a digesting operation
217 SecCmsDigestContextDestroy(SecCmsDigestContextRef cmsdigcx
)
219 SecCmsDigestContextCancel(cmsdigcx
);
223 * SecCmsDigestContextFinishMultiple - finish the digests
226 SecCmsDigestContextFinishMultiple(SecCmsDigestContextRef cmsdigcx
,
227 SECAlgorithmID
***digestalgsp
,
228 SecAsn1Item
* **digestsp
)
231 SecAsn1Item
**digests
, *digest
;
232 SECAlgorithmID
**digestalgs
;
235 OSStatus rv
= SECFailure
;
237 assert(cmsdigcx
!= NULL
);
238 assert(digestsp
!= NULL
);
239 assert(digestalgsp
!= NULL
);
241 mark
= PORT_ArenaMark (cmsdigcx
->poolp
);
243 /* Security check to prevent under-allocation */
244 if (cmsdigcx
->digcnt
>= (int)((INT_MAX
/(MAX_OF_3(sizeof(SECAlgorithmID
*),sizeof(SecAsn1Item
*),sizeof(SecAsn1Item
))))-1)) {
247 /* allocate digest array & SecAsn1Items on arena */
248 digestalgs
= (SECAlgorithmID
**)PORT_ArenaZAlloc(cmsdigcx
->poolp
, (cmsdigcx
->digcnt
+1) * sizeof(SECAlgorithmID
*));
249 digests
= (SecAsn1Item
* *)PORT_ArenaZAlloc(cmsdigcx
->poolp
, (cmsdigcx
->digcnt
+1) * sizeof(SecAsn1Item
*));
250 digest
= (SecAsn1Item
*)PORT_ArenaZAlloc(cmsdigcx
->poolp
, cmsdigcx
->digcnt
* sizeof(SecAsn1Item
));
251 if (digestalgs
== NULL
|| digests
== NULL
|| digest
== NULL
) {
255 for (i
= 0; i
< cmsdigcx
->digcnt
; i
++, digest
++) {
257 SECOidTag hash_alg
= SECOID_GetAlgorithmTag(cmsdigcx
->digestalgs
[i
]);
261 case SEC_OID_SHA1
: diglength
= CC_SHA1_DIGEST_LENGTH
; break;
262 case SEC_OID_MD5
: diglength
= CC_MD5_DIGEST_LENGTH
; break;
263 case SEC_OID_SHA224
: diglength
= CC_SHA224_DIGEST_LENGTH
; break;
264 case SEC_OID_SHA256
: diglength
= CC_SHA256_DIGEST_LENGTH
; break;
265 case SEC_OID_SHA384
: diglength
= CC_SHA384_DIGEST_LENGTH
; break;
266 case SEC_OID_SHA512
: diglength
= CC_SHA512_DIGEST_LENGTH
; break;
267 default: goto loser
; break;
270 digobj
= cmsdigcx
->digobjs
[i
];
273 digest
->Data
= (unsigned char*)PORT_ArenaAlloc(cmsdigcx
->poolp
, diglength
);
274 if (digest
->Data
== NULL
)
276 digest
->Length
= diglength
;
279 case SEC_OID_SHA1
: CC_SHA1_Final(digest
->Data
, digobj
); break;
280 case SEC_OID_MD5
: CC_MD5_Final(digest
->Data
, digobj
); break;
281 case SEC_OID_SHA224
: CC_SHA224_Final(digest
->Data
, digobj
); break;
282 case SEC_OID_SHA256
: CC_SHA256_Final(digest
->Data
, digobj
); break;
283 case SEC_OID_SHA384
: CC_SHA384_Final(digest
->Data
, digobj
); break;
284 case SEC_OID_SHA512
: CC_SHA512_Final(digest
->Data
, digobj
); break;
285 default: goto loser
; break;
289 digestalgs
[i
] = cmsdigcx
->digestalgs
[i
];
298 digestalgs
[i
] = NULL
;
300 *digestalgsp
= digestalgs
;
306 if (rv
== SECSuccess
)
307 PORT_ArenaUnmark(cmsdigcx
->poolp
, mark
);
309 PORT_ArenaRelease(cmsdigcx
->poolp
, mark
);
312 /* Set things up so SecCmsDigestContextDestroy won't call CSSM_DeleteContext again. */
313 cmsdigcx
->digcnt
= 0;
319 * SecCmsDigestContextFinishSingle - same as SecCmsDigestContextFinishMultiple,
320 * but for one digest.
323 SecCmsDigestContextFinishSingle(SecCmsDigestContextRef cmsdigcx
,
324 SecAsn1Item
* digest
)
326 OSStatus rv
= SECFailure
;
330 /* get the digests into arena, then copy the first digest into poolp */
331 if (SecCmsDigestContextFinishMultiple(cmsdigcx
, &ap
, &dp
) != SECSuccess
)
334 /* Return the first element in the digest array. */