]> git.saurik.com Git - apple/security.git/blob - libsecurity_cms/lib/CMSUtils.cpp
712d7ed6df35c71d14945eaffac67396bf6184d4
[apple/security.git] / libsecurity_cms / lib / CMSUtils.cpp
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * CMSUtils.cpp - common utility routines for libCMS.
26 * Created 1/12/06 by Doug Mitchell.
27 */
28
29 #include "CMSUtils.h"
30 #include <stdlib.h>
31 #include <string.h>
32 #include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacErrors.h>
33 #include <security_asn1/secerr.h>
34 #include <security_asn1/seccomon.h>
35 #include <Security/SecBase.h>
36
37 /*
38 * Copy a CSSM_DATA, mallocing the result.
39 */
40 void cmsCopyCmsData(
41 const CSSM_DATA *src,
42 CSSM_DATA *dst)
43 {
44 dst->Data = (uint8 *)malloc(src->Length);
45 memmove(dst->Data, src->Data, src->Length);
46 dst->Length = src->Length;
47 }
48
49 /*
50 * Append a CF type, or the contents of an array, to another array.
51 * destination array will be created if necessary.
52 * If srcItemOrArray is not of the type specified in expectedType,
53 * paramErr will be returned.
54 */
55 OSStatus cmsAppendToArray(
56 CFTypeRef srcItemOrArray,
57 CFMutableArrayRef *dstArray,
58 CFTypeID expectedType)
59 {
60 if(srcItemOrArray == NULL) {
61 return noErr;
62 }
63 if(*dstArray == NULL) {
64 *dstArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
65 }
66 CFTypeID inType = CFGetTypeID(srcItemOrArray);
67 if(inType == CFArrayGetTypeID()) {
68 CFArrayRef srcArray = (CFArrayRef)srcItemOrArray;
69 CFRange srcRange = {0, CFArrayGetCount(srcArray)};
70 CFArrayAppendArray(*dstArray, srcArray, srcRange);
71 }
72 else if(inType == expectedType) {
73 CFArrayAppendValue(*dstArray, srcItemOrArray);
74 }
75 else {
76 return paramErr;
77 }
78 return noErr;
79 }
80
81 /*
82 * Munge an OSStatus returned from libsecurity_smime, which may well be an ASN.1 private
83 * error code, to a real OSStatus.
84 */
85 OSStatus cmsRtnToOSStatus(
86 OSStatus smimeRtn, // from libsecurity_smime
87 OSStatus defaultRtn) // use this if we can't map smimeRtn
88 {
89 if(smimeRtn == SECFailure) {
90 /* This is a SECStatus. Try to get detailed error info. */
91 smimeRtn = PORT_GetError();
92 if(smimeRtn == 0) {
93 /* S/MIME just gave us generic error; no further info available; punt. */
94 dprintf("cmsRtnToOSStatus: SECFailure, no status avilable\n");
95 return defaultRtn ? defaultRtn : internalComponentErr;
96 }
97 /* else proceed to map smimeRtn to OSStatus */
98 }
99 if(!IS_SEC_ERROR(smimeRtn)) {
100 /* isn't ASN.1 or S/MIME error; use as is. */
101 return smimeRtn;
102 }
103
104 /* Convert SECErrorCodes to OSStatus */
105 switch(smimeRtn) {
106 case SEC_ERROR_BAD_DER:
107 case SEC_ERROR_BAD_DATA:
108 return errSecUnknownFormat;
109 case SEC_ERROR_NO_MEMORY:
110 return memFullErr;
111 case SEC_ERROR_IO:
112 return ioErr;
113 case SEC_ERROR_OUTPUT_LEN:
114 case SEC_ERROR_INPUT_LEN:
115 case SEC_ERROR_INVALID_ARGS:
116 case SEC_ERROR_INVALID_ALGORITHM:
117 case SEC_ERROR_INVALID_AVA:
118 case SEC_ERROR_INVALID_TIME:
119 return paramErr;
120 case SEC_ERROR_PKCS7_BAD_SIGNATURE:
121 case SEC_ERROR_BAD_SIGNATURE:
122 return CSSMERR_CSP_VERIFY_FAILED;
123 case SEC_ERROR_EXPIRED_CERTIFICATE:
124 case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE:
125 return CSSMERR_TP_CERT_EXPIRED;
126 case SEC_ERROR_REVOKED_CERTIFICATE:
127 return CSSMERR_TP_CERT_REVOKED;
128 case SEC_ERROR_UNKNOWN_ISSUER:
129 case SEC_ERROR_UNTRUSTED_ISSUER:
130 case SEC_ERROR_UNTRUSTED_CERT:
131 return CSSMERR_TP_NOT_TRUSTED;
132 case SEC_ERROR_CERT_USAGES_INVALID:
133 case SEC_ERROR_INADEQUATE_KEY_USAGE:
134 return CSSMERR_CSP_KEY_USAGE_INCORRECT;
135 case SEC_INTERNAL_ONLY:
136 return internalComponentErr;
137 case SEC_ERROR_NO_USER_INTERACTION:
138 return errSecInteractionNotAllowed;
139 case SEC_ERROR_USER_CANCELLED:
140 return userCanceledErr;
141 default:
142 dprintf("cmsRtnToOSStatus: smimeRtn 0x%x\n", smimeRtn);
143 return defaultRtn ? defaultRtn : internalComponentErr;
144 }
145 }