]> git.saurik.com Git - apple/security.git/blob - libsecurity_cms/lib/CMSUtils.cpp
Security-55471.tar.gz
[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 <security_asn1/secerr.h>
33 #include <security_asn1/seccomon.h>
34 #include <Security/SecBase.h>
35
36 /*
37 * Copy a CSSM_DATA, mallocing the result.
38 */
39 void cmsCopyCmsData(
40 const CSSM_DATA *src,
41 CSSM_DATA *dst)
42 {
43 dst->Data = (uint8 *)malloc(src->Length);
44 memmove(dst->Data, src->Data, src->Length);
45 dst->Length = src->Length;
46 }
47
48 /*
49 * Append a CF type, or the contents of an array, to another array.
50 * destination array will be created if necessary.
51 * If srcItemOrArray is not of the type specified in expectedType,
52 * errSecParam will be returned.
53 */
54 OSStatus cmsAppendToArray(
55 CFTypeRef srcItemOrArray,
56 CFMutableArrayRef *dstArray,
57 CFTypeID expectedType)
58 {
59 if(srcItemOrArray == NULL) {
60 return errSecSuccess;
61 }
62 if(*dstArray == NULL) {
63 *dstArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
64 }
65 CFTypeID inType = CFGetTypeID(srcItemOrArray);
66 if(inType == CFArrayGetTypeID()) {
67 CFArrayRef srcArray = (CFArrayRef)srcItemOrArray;
68 CFRange srcRange = {0, CFArrayGetCount(srcArray)};
69 CFArrayAppendArray(*dstArray, srcArray, srcRange);
70 }
71 else if(inType == expectedType) {
72 CFArrayAppendValue(*dstArray, srcItemOrArray);
73 }
74 else {
75 return errSecParam;
76 }
77 return errSecSuccess;
78 }
79
80 /*
81 * Munge an OSStatus returned from libsecurity_smime, which may well be an ASN.1 private
82 * error code, to a real OSStatus.
83 */
84 OSStatus cmsRtnToOSStatus(
85 OSStatus smimeRtn, // from libsecurity_smime
86 OSStatus defaultRtn) // use this if we can't map smimeRtn
87 {
88 if(smimeRtn == SECFailure) {
89 /* This is a SECStatus. Try to get detailed error info. */
90 smimeRtn = PORT_GetError();
91 if(smimeRtn == 0) {
92 /* S/MIME just gave us generic error; no further info available; punt. */
93 dprintf("cmsRtnToOSStatus: SECFailure, no status avilable\n");
94 return defaultRtn ? defaultRtn : errSecInternalComponent;
95 }
96 /* else proceed to map smimeRtn to OSStatus */
97 }
98 if(!IS_SEC_ERROR(smimeRtn)) {
99 /* isn't ASN.1 or S/MIME error; use as is. */
100 return smimeRtn;
101 }
102
103 /* Convert SECErrorCodes to OSStatus */
104 switch(smimeRtn) {
105 case SEC_ERROR_BAD_DER:
106 case SEC_ERROR_BAD_DATA:
107 return errSecUnknownFormat;
108 case SEC_ERROR_NO_MEMORY:
109 return errSecAllocate;
110 case SEC_ERROR_IO:
111 return errSecIO;
112 case SEC_ERROR_OUTPUT_LEN:
113 case SEC_ERROR_INPUT_LEN:
114 case SEC_ERROR_INVALID_ARGS:
115 case SEC_ERROR_INVALID_ALGORITHM:
116 case SEC_ERROR_INVALID_AVA:
117 case SEC_ERROR_INVALID_TIME:
118 return errSecParam;
119 case SEC_ERROR_PKCS7_BAD_SIGNATURE:
120 case SEC_ERROR_BAD_SIGNATURE:
121 return CSSMERR_CSP_VERIFY_FAILED;
122 case SEC_ERROR_EXPIRED_CERTIFICATE:
123 case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE:
124 return CSSMERR_TP_CERT_EXPIRED;
125 case SEC_ERROR_REVOKED_CERTIFICATE:
126 return CSSMERR_TP_CERT_REVOKED;
127 case SEC_ERROR_UNKNOWN_ISSUER:
128 case SEC_ERROR_UNTRUSTED_ISSUER:
129 case SEC_ERROR_UNTRUSTED_CERT:
130 return CSSMERR_TP_NOT_TRUSTED;
131 case SEC_ERROR_CERT_USAGES_INVALID:
132 case SEC_ERROR_INADEQUATE_KEY_USAGE:
133 return CSSMERR_CSP_KEY_USAGE_INCORRECT;
134 case SEC_INTERNAL_ONLY:
135 return errSecInternalComponent;
136 case SEC_ERROR_NO_USER_INTERACTION:
137 return errSecInteractionNotAllowed;
138 case SEC_ERROR_USER_CANCELLED:
139 return errSecUserCanceled;
140 default:
141 dprintf("cmsRtnToOSStatus: smimeRtn 0x%x\n", smimeRtn);
142 return defaultRtn ? defaultRtn : errSecInternalComponent;
143 }
144 }