]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/lib/SecFrameworkP.c
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / lib / SecFrameworkP.c
1 /*
2 * Copyright (c) 2006-2015 Apple 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 * SecFramework.c - generic non API class specific functions
26 */
27
28
29 #include "SecFrameworkP.h"
30 #include <pthread.h>
31 #include <CoreFoundation/CFBundle.h>
32 #include <CoreFoundation/CFURLAccess.h>
33 #if 0
34 #include "SecRandomP.h"
35 #endif
36 #include <CommonCrypto/CommonDigest.h>
37 #include <Security/SecAsn1Coder.h>
38 #include <Security/oidsalg.h>
39 #include <fcntl.h>
40 #include <sys/types.h>
41 #include <unistd.h>
42 #include <errno.h>
43 #include <dlfcn.h>
44 #include <string.h>
45 #include <CoreFoundation/CFBundlePriv.h>
46
47 #include <utilities/debugging.h>
48
49 /* Security framework's own bundle used for localized string lookups. */
50 static CFBundleRef kSecFrameworkBundle;
51 static pthread_once_t kSecFrameworkBundleLookup = PTHREAD_ONCE_INIT;
52
53 static void SecFrameworkBundleLookup(void) {
54 // figure out the path to our executable
55 Dl_info info;
56 dladdr("", &info);
57
58 // make a file URL from the returned string
59 CFURLRef urlRef = CFURLCreateFromFileSystemRepresentation(NULL, (const UInt8*) info.dli_fname, strlen(info.dli_fname), false);
60 kSecFrameworkBundle = _CFBundleCreateWithExecutableURLIfLooksLikeBundle(NULL, urlRef);
61 CFRelease(urlRef);
62
63 if (kSecFrameworkBundle)
64 CFRetain(kSecFrameworkBundle);
65 }
66
67 CFStringRef SecFrameworkCopyLocalizedString(CFStringRef key,
68 CFStringRef tableName) {
69 pthread_once(&kSecFrameworkBundleLookup, SecFrameworkBundleLookup);
70 if (kSecFrameworkBundle) {
71 return CFBundleCopyLocalizedString(kSecFrameworkBundle, key, key,
72 tableName);
73 }
74
75 CFRetain(key);
76 return key;
77 }
78
79 CFURLRef SecFrameworkCopyResourceURL(CFStringRef resourceName,
80 CFStringRef resourceType, CFStringRef subDirName) {
81 CFURLRef url = NULL;
82 pthread_once(&kSecFrameworkBundleLookup, SecFrameworkBundleLookup);
83 if (kSecFrameworkBundle) {
84 url = CFBundleCopyResourceURL(kSecFrameworkBundle, resourceName,
85 resourceType, subDirName);
86 if (!url) {
87 secinfo("SecFramework", "resource: %@.%@ in %@ not found", resourceName,
88 resourceType, subDirName);
89 }
90 }
91
92 return url;
93 }
94
95
96 CFDataRef SecFrameworkCopyResourceContents(CFStringRef resourceName,
97 CFStringRef resourceType, CFStringRef subDirName) {
98 CFURLRef url = SecFrameworkCopyResourceURL(resourceName, resourceType,
99 subDirName);
100 CFDataRef data = NULL;
101 if (url) {
102 SInt32 error;
103 if (!CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault,
104 url, &data, NULL, NULL, &error)) {
105 secinfo("SecFramework", "read: %d", (int)error);
106 }
107 CFRelease(url);
108 }
109
110 return data;
111 }
112
113 /* Return the SHA1 digest of a chunk of data as newly allocated CFDataRef. */
114 CFDataRef SecSHA1DigestCreate(CFAllocatorRef allocator,
115 const UInt8 *data, CFIndex length) {
116 CFMutableDataRef digest = CFDataCreateMutable(allocator,
117 CC_SHA1_DIGEST_LENGTH);
118 CFDataSetLength(digest, CC_SHA1_DIGEST_LENGTH);
119 CC_SHA1(data, (CC_LONG)length, CFDataGetMutableBytePtr(digest));
120 return digest;
121 }
122
123 #if 0
124 CFDataRef SecDigestCreate(CFAllocatorRef allocator,
125 const SecAsn1Oid *algorithm, const SecAsn1Item *params,
126 const UInt8 *data, CFIndex length) {
127 unsigned char *(*digestFcn)(const void *data, CC_LONG len, unsigned char *md);
128 CFIndex digestLen;
129
130 if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA1)) {
131 digestFcn = CC_SHA1;
132 digestLen = CC_SHA1_DIGEST_LENGTH;
133 } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA224)) {
134 digestFcn = CC_SHA224;
135 digestLen = CC_SHA224_DIGEST_LENGTH;
136 } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA256)) {
137 digestFcn = CC_SHA256;
138 digestLen = CC_SHA256_DIGEST_LENGTH;
139 } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA384)) {
140 digestFcn = CC_SHA384;
141 digestLen = CC_SHA384_DIGEST_LENGTH;
142 } else if (SecAsn1OidCompare(algorithm, &CSSMOID_SHA512)) {
143 digestFcn = CC_SHA512;
144 digestLen = CC_SHA512_DIGEST_LENGTH;
145 } else {
146 return NULL;
147 }
148
149 CFMutableDataRef digest = CFDataCreateMutable(allocator, digestLen);
150 CFDataSetLength(digest, digestLen);
151 digestFcn(data, length, CFDataGetMutableBytePtr(digest));
152 return digest;
153 }
154 #endif