]> git.saurik.com Git - apple/security.git/blob - libsecurity_smime/lib/cmsutil.c
Security-57740.51.3.tar.gz
[apple/security.git] / libsecurity_smime / lib / cmsutil.c
1 /*
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/
6 *
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.
11 *
12 * The Original Code is the Netscape security libraries.
13 *
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
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
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
31 * GPL.
32 */
33
34 /*
35 * CMS miscellaneous utility functions.
36 */
37
38 #include <Security/SecCmsEncoder.h> /* @@@ Remove this when we move the Encoder method. */
39 #include <Security/SecCmsSignerInfo.h>
40
41 #include "cmslocal.h"
42
43 #include "SecAsn1Item.h"
44 #include "secoid.h"
45 #include "cryptohi.h"
46
47 #include <security_asn1/secasn1.h>
48 #include <security_asn1/secerr.h>
49 #include <security_asn1/secport.h>
50
51 #include <CommonCrypto/CommonDigest.h>
52 #include <Security/SecBase.h>
53
54 /*
55 * SecCmsArraySortByDER - sort array of objects by objects' DER encoding
56 *
57 * make sure that the order of the objects guarantees valid DER (which must be
58 * in lexigraphically ascending order for a SET OF); if reordering is necessary it
59 * will be done in place (in objs).
60 */
61 OSStatus
62 SecCmsArraySortByDER(void **objs, const SecAsn1Template *objtemplate, void **objs2)
63 {
64 PRArenaPool *poolp;
65 int num_objs;
66 SecAsn1Item **enc_objs;
67 OSStatus rv = SECFailure;
68 int i;
69
70 if (objs == NULL) /* already sorted */
71 return SECSuccess;
72
73 num_objs = SecCmsArrayCount((void **)objs);
74 if (num_objs == 0 || num_objs == 1) /* already sorted. */
75 return SECSuccess;
76
77 poolp = PORT_NewArena (1024); /* arena for temporaries */
78 if (poolp == NULL)
79 return SECFailure; /* no memory; nothing we can do... */
80
81 /*
82 * Allocate arrays to hold the individual encodings which we will use
83 * for comparisons and the reordered attributes as they are sorted.
84 */
85 // Security check to prevent under-allocation
86 if (num_objs<0 || num_objs>=(int)((INT_MAX/sizeof(SecAsn1Item *))-1)) {
87 goto loser;
88 }
89 enc_objs = (SecAsn1Item **)PORT_ArenaZAlloc(poolp, (num_objs + 1) * sizeof(SecAsn1Item *));
90 if (enc_objs == NULL)
91 goto loser;
92
93 /* DER encode each individual object. */
94 for (i = 0; i < num_objs; i++) {
95 enc_objs[i] = SEC_ASN1EncodeItem(poolp, NULL, objs[i], objtemplate);
96 if (enc_objs[i] == NULL)
97 goto loser;
98 }
99 enc_objs[num_objs] = NULL;
100
101 /* now compare and sort objs by the order of enc_objs */
102 SecCmsArraySort((void **)enc_objs, SecCmsUtilDERCompare, objs, objs2);
103
104 rv = SECSuccess;
105
106 loser:
107 PORT_FreeArena (poolp, PR_FALSE);
108 return rv;
109 }
110
111 /*
112 * SecCmsUtilDERCompare - for use with SecCmsArraySort to
113 * sort arrays of SecAsn1Items containing DER
114 */
115 int
116 SecCmsUtilDERCompare(void *a, void *b)
117 {
118 SecAsn1Item * der1 = (SecAsn1Item *)a;
119 SecAsn1Item * der2 = (SecAsn1Item *)b;
120
121 /*
122 * Find the lowest (lexigraphically) encoding. One that is
123 * shorter than all the rest is known to be "less" because each
124 * attribute is of the same type (a SEQUENCE) and so thus the
125 * first octet of each is the same, and the second octet is
126 * the length (or the length of the length with the high bit
127 * set, followed by the length, which also works out to always
128 * order the shorter first). Two (or more) that have the
129 * same length need to be compared byte by byte until a mismatch
130 * is found.
131 */
132 if (der1->Length != der2->Length)
133 return (der1->Length < der2->Length) ? -1 : 1;
134
135 #if 1
136 return memcmp(der1->Data, der2->Data, der1->Length);
137 #else
138 size_t j;
139 for (j = 0; j < der1->Length; j++) {
140 if (der1->Data[j] == der2->Data[j])
141 continue;
142 return (der1->Data[j] < der2->Data[j]) ? -1 : 1;
143 }
144 return 0;
145 #endif
146 }
147
148 /*
149 * SecCmsAlgArrayGetIndexByAlgID - find a specific algorithm in an array of
150 * algorithms.
151 *
152 * algorithmArray - array of algorithm IDs
153 * algid - algorithmid of algorithm to pick
154 *
155 * Returns:
156 * An integer containing the index of the algorithm in the array or -1 if
157 * algorithm was not found.
158 */
159 int
160 SecCmsAlgArrayGetIndexByAlgID(SECAlgorithmID **algorithmArray, SECAlgorithmID *algid)
161 {
162 int i;
163
164 if (algorithmArray == NULL || algorithmArray[0] == NULL)
165 return -1;
166
167 for (i = 0; algorithmArray[i] != NULL; i++) {
168 if (SECOID_CompareAlgorithmID(algorithmArray[i], algid) == SECEqual)
169 break; /* bingo */
170 }
171
172 if (algorithmArray[i] == NULL)
173 return -1; /* not found */
174
175 return i;
176 }
177
178 /*
179 * SecCmsAlgArrayGetIndexByAlgTag - find a specific algorithm in an array of
180 * algorithms.
181 *
182 * algorithmArray - array of algorithm IDs
183 * algtag - algorithm tag of algorithm to pick
184 *
185 * Returns:
186 * An integer containing the index of the algorithm in the array or -1 if
187 * algorithm was not found.
188 */
189 int
190 SecCmsAlgArrayGetIndexByAlgTag(SECAlgorithmID **algorithmArray,
191 SECOidTag algtag)
192 {
193 SECOidData *algid;
194 int i = -1;
195
196 if (algorithmArray == NULL || algorithmArray[0] == NULL)
197 return i;
198
199 #ifdef ORDER_N_SQUARED
200 for (i = 0; algorithmArray[i] != NULL; i++) {
201 algid = SECOID_FindOID(&(algorithmArray[i]->algorithm));
202 if (algid->offset == algtag)
203 break; /* bingo */
204 }
205 #else
206 algid = SECOID_FindOIDByTag(algtag);
207 if (!algid)
208 return i;
209 for (i = 0; algorithmArray[i] != NULL; i++) {
210 if (SECITEM_ItemsAreEqual(&algorithmArray[i]->algorithm, &algid->oid))
211 break; /* bingo */
212 }
213 #endif
214
215 if (algorithmArray[i] == NULL)
216 return -1; /* not found */
217
218 return i;
219 }
220
221 void *
222 SecCmsUtilGetHashObjByAlgID(SECAlgorithmID *algid)
223 {
224 SECOidData *oidData = SECOID_FindOID(&(algid->algorithm));
225 if (oidData)
226 {
227 void *digobj = NULL;
228 switch (oidData->offset) {
229 case SEC_OID_SHA1:
230 digobj = calloc(1, sizeof(CC_SHA1_CTX));
231 CC_SHA1_Init(digobj);
232 break;
233 case SEC_OID_MD5:
234 digobj = calloc(1, sizeof(CC_MD5_CTX));
235 CC_MD5_Init(digobj);
236 break;
237 case SEC_OID_SHA224:
238 digobj = calloc(1, sizeof(CC_SHA256_CTX));
239 CC_SHA224_Init(digobj);
240 break;
241 case SEC_OID_SHA256:
242 digobj = calloc(1, sizeof(CC_SHA256_CTX));
243 CC_SHA256_Init(digobj);
244 break;
245 case SEC_OID_SHA384:
246 digobj = calloc(1, sizeof(CC_SHA512_CTX));
247 CC_SHA384_Init(digobj);
248 break;
249 case SEC_OID_SHA512:
250 digobj = calloc(1, sizeof(CC_SHA512_CTX));
251 CC_SHA512_Init(digobj);
252 break;
253 default:
254 break;
255 }
256 return digobj;
257 }
258
259 return 0;
260 }
261
262 /*
263 * XXX I would *really* like to not have to do this, but the current
264 * signing interface gives me little choice.
265 */
266 SECOidTag
267 SecCmsUtilMakeSignatureAlgorithm(SECOidTag hashalg, SECOidTag encalg)
268 {
269 switch (encalg) {
270 case SEC_OID_PKCS1_RSA_ENCRYPTION:
271 switch (hashalg) {
272 case SEC_OID_MD2:
273 return SEC_OID_PKCS1_MD2_WITH_RSA_ENCRYPTION;
274 case SEC_OID_MD5:
275 return SEC_OID_PKCS1_MD5_WITH_RSA_ENCRYPTION;
276 case SEC_OID_SHA1:
277 return SEC_OID_PKCS1_SHA1_WITH_RSA_ENCRYPTION;
278 case SEC_OID_SHA256:
279 return SEC_OID_PKCS1_SHA256_WITH_RSA_ENCRYPTION;
280 case SEC_OID_SHA384:
281 return SEC_OID_PKCS1_SHA384_WITH_RSA_ENCRYPTION;
282 case SEC_OID_SHA512:
283 return SEC_OID_PKCS1_SHA512_WITH_RSA_ENCRYPTION;
284 default:
285 return SEC_OID_UNKNOWN;
286 }
287 case SEC_OID_ANSIX9_DSA_SIGNATURE:
288 case SEC_OID_MISSI_KEA_DSS:
289 case SEC_OID_MISSI_DSS:
290 switch (hashalg) {
291 case SEC_OID_SHA1:
292 return SEC_OID_ANSIX9_DSA_SIGNATURE_WITH_SHA1_DIGEST;
293 default:
294 return SEC_OID_UNKNOWN;
295 }
296 case SEC_OID_EC_PUBLIC_KEY:
297 switch(hashalg) {
298 /*
299 * Note this is only used when signing and verifying signed attributes,
300 * In which case we really do want the combined ECDSA_WithSHA1 alg...
301 */
302 case SEC_OID_SHA1:
303 return SEC_OID_ECDSA_WithSHA1;
304 case SEC_OID_SHA256:
305 return SEC_OID_ECDSA_WITH_SHA256;
306 case SEC_OID_SHA384:
307 return SEC_OID_ECDSA_WITH_SHA384;
308 case SEC_OID_SHA512:
309 return SEC_OID_ECDSA_WITH_SHA512;
310 default:
311 return SEC_OID_UNKNOWN;
312 }
313 default:
314 break;
315 }
316
317 return encalg; /* maybe it is already the right algid */
318 }
319
320 const SecAsn1Template *
321 SecCmsUtilGetTemplateByTypeTag(SECOidTag type)
322 {
323 const SecAsn1Template *template;
324 extern const SecAsn1Template SecCmsSignedDataTemplate[];
325 extern const SecAsn1Template SecCmsEnvelopedDataTemplate[];
326 extern const SecAsn1Template SecCmsEncryptedDataTemplate[];
327 extern const SecAsn1Template SecCmsDigestedDataTemplate[];
328
329 switch (type) {
330 case SEC_OID_PKCS7_SIGNED_DATA:
331 template = SecCmsSignedDataTemplate;
332 break;
333 case SEC_OID_PKCS7_ENVELOPED_DATA:
334 template = SecCmsEnvelopedDataTemplate;
335 break;
336 case SEC_OID_PKCS7_ENCRYPTED_DATA:
337 template = SecCmsEncryptedDataTemplate;
338 break;
339 case SEC_OID_PKCS7_DIGESTED_DATA:
340 template = SecCmsDigestedDataTemplate;
341 break;
342 default:
343 case SEC_OID_PKCS7_DATA:
344 template = NULL;
345 break;
346 }
347 return template;
348 }
349
350 size_t
351 SecCmsUtilGetSizeByTypeTag(SECOidTag type)
352 {
353 size_t size;
354
355 switch (type) {
356 case SEC_OID_PKCS7_SIGNED_DATA:
357 size = sizeof(SecCmsSignedData);
358 break;
359 case SEC_OID_PKCS7_ENVELOPED_DATA:
360 size = sizeof(SecCmsEnvelopedData);
361 break;
362 case SEC_OID_PKCS7_ENCRYPTED_DATA:
363 size = sizeof(SecCmsEncryptedData);
364 break;
365 case SEC_OID_PKCS7_DIGESTED_DATA:
366 size = sizeof(SecCmsDigestedData);
367 break;
368 default:
369 case SEC_OID_PKCS7_DATA:
370 size = 0;
371 break;
372 }
373 return size;
374 }
375
376 SecCmsContentInfoRef
377 SecCmsContentGetContentInfo(void *msg, SECOidTag type)
378 {
379 SecCmsContent c;
380 SecCmsContentInfoRef cinfo;
381
382 if (!msg)
383 return NULL;
384 c.pointer = msg;
385 switch (type) {
386 case SEC_OID_PKCS7_SIGNED_DATA:
387 cinfo = &(c.signedData->contentInfo);
388 break;
389 case SEC_OID_PKCS7_ENVELOPED_DATA:
390 cinfo = &(c.envelopedData->contentInfo);
391 break;
392 case SEC_OID_PKCS7_ENCRYPTED_DATA:
393 cinfo = &(c.encryptedData->contentInfo);
394 break;
395 case SEC_OID_PKCS7_DIGESTED_DATA:
396 cinfo = &(c.digestedData->contentInfo);
397 break;
398 default:
399 cinfo = NULL;
400 }
401 return cinfo;
402 }
403
404 // @@@ Return CFStringRef and do localization.
405 const char *
406 SecCmsUtilVerificationStatusToString(SecCmsVerificationStatus vs)
407 {
408 switch (vs) {
409 case SecCmsVSUnverified: return "Unverified";
410 case SecCmsVSGoodSignature: return "GoodSignature";
411 case SecCmsVSBadSignature: return "BadSignature";
412 case SecCmsVSDigestMismatch: return "DigestMismatch";
413 case SecCmsVSSigningCertNotFound: return "SigningCertNotFound";
414 case SecCmsVSSigningCertNotTrusted: return "SigningCertNotTrusted";
415 case SecCmsVSSignatureAlgorithmUnknown: return "SignatureAlgorithmUnknown";
416 case SecCmsVSSignatureAlgorithmUnsupported: return "SignatureAlgorithmUnsupported";
417 case SecCmsVSMalformedSignature: return "MalformedSignature";
418 case SecCmsVSProcessingError: return "ProcessingError";
419 default: return "Unknown";
420 }
421 }