2 * Copyright (c) 2006-2010 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * SecFramework.c - generic non API class specific functions
29 #include "SecFrameworkP.h"
31 #include <CoreFoundation/CFBundle.h>
32 #include <CoreFoundation/CFURLAccess.h>
34 #include "SecRandomP.h"
36 #include <CommonCrypto/CommonDigest.h>
37 #include <Security/SecAsn1Coder.h>
38 #include <Security/oidsalg.h>
40 #include <sys/types.h>
45 #include <CoreFoundation/CFBundlePriv.h>
47 #include <utilities/debugging.h>
49 /* Security.framework's bundle id. */
50 static CFStringRef kSecFrameworkBundleID
= CFSTR("com.apple.Security");
52 /* Security framework's own bundle used for localized string lookups. */
53 static CFBundleRef kSecFrameworkBundle
;
54 static pthread_once_t kSecFrameworkBundleLookup
= PTHREAD_ONCE_INIT
;
57 // copied from SecAsn1Coder.c
59 bool SecAsn1OidCompare(const SecAsn1Oid
*oid1
, const SecAsn1Oid
*oid2
) {
62 if (oid1
->Length
!= oid2
->Length
)
64 return !memcmp(oid1
->Data
, oid2
->Data
, oid1
->Length
);
68 static void SecFrameworkBundleLookup(void) {
69 // figure out the path to our executable
73 // make a file URL from the returned string
74 CFURLRef urlRef
= CFURLCreateFromFileSystemRepresentation(NULL
, (const UInt8
*) info
.dli_fname
, strlen(info
.dli_fname
), false);
75 kSecFrameworkBundle
= _CFBundleCreateWithExecutableURLIfLooksLikeBundle(NULL
, urlRef
);
78 if (kSecFrameworkBundle
)
79 CFRetain(kSecFrameworkBundle
);
82 CFStringRef
SecFrameworkCopyLocalizedString(CFStringRef key
,
83 CFStringRef tableName
) {
84 pthread_once(&kSecFrameworkBundleLookup
, SecFrameworkBundleLookup
);
85 if (kSecFrameworkBundle
) {
86 return CFBundleCopyLocalizedString(kSecFrameworkBundle
, key
, key
,
94 CFURLRef
SecFrameworkCopyResourceURL(CFStringRef resourceName
,
95 CFStringRef resourceType
, CFStringRef subDirName
) {
97 pthread_once(&kSecFrameworkBundleLookup
, SecFrameworkBundleLookup
);
98 if (kSecFrameworkBundle
) {
99 url
= CFBundleCopyResourceURL(kSecFrameworkBundle
, resourceName
,
100 resourceType
, subDirName
);
102 secdebug("SecFramework", "resource: %@.%@ in %@ not found", resourceName
,
103 resourceType
, subDirName
);
111 CFDataRef
SecFrameworkCopyResourceContents(CFStringRef resourceName
,
112 CFStringRef resourceType
, CFStringRef subDirName
) {
113 CFURLRef url
= SecFrameworkCopyResourceURL(resourceName
, resourceType
,
115 CFDataRef data
= NULL
;
118 if (!CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault
,
119 url
, &data
, NULL
, NULL
, &error
)) {
120 secdebug("SecFramework", "read: %d", (int)error
);
128 /* Return the SHA1 digest of a chunk of data as newly allocated CFDataRef. */
129 CFDataRef
SecSHA1DigestCreate(CFAllocatorRef allocator
,
130 const UInt8
*data
, CFIndex length
) {
131 CFMutableDataRef digest
= CFDataCreateMutable(allocator
,
132 CC_SHA1_DIGEST_LENGTH
);
133 CFDataSetLength(digest
, CC_SHA1_DIGEST_LENGTH
);
134 CC_SHA1(data
, (CC_LONG
)length
, CFDataGetMutableBytePtr(digest
));
139 CFDataRef
SecDigestCreate(CFAllocatorRef allocator
,
140 const SecAsn1Oid
*algorithm
, const SecAsn1Item
*params
,
141 const UInt8
*data
, CFIndex length
) {
142 unsigned char *(*digestFcn
)(const void *data
, CC_LONG len
, unsigned char *md
);
145 if (SecAsn1OidCompare(algorithm
, &CSSMOID_SHA1
)) {
147 digestLen
= CC_SHA1_DIGEST_LENGTH
;
148 } else if (SecAsn1OidCompare(algorithm
, &CSSMOID_SHA224
)) {
149 digestFcn
= CC_SHA224
;
150 digestLen
= CC_SHA224_DIGEST_LENGTH
;
151 } else if (SecAsn1OidCompare(algorithm
, &CSSMOID_SHA256
)) {
152 digestFcn
= CC_SHA256
;
153 digestLen
= CC_SHA256_DIGEST_LENGTH
;
154 } else if (SecAsn1OidCompare(algorithm
, &CSSMOID_SHA384
)) {
155 digestFcn
= CC_SHA384
;
156 digestLen
= CC_SHA384_DIGEST_LENGTH
;
157 } else if (SecAsn1OidCompare(algorithm
, &CSSMOID_SHA512
)) {
158 digestFcn
= CC_SHA512
;
159 digestLen
= CC_SHA512_DIGEST_LENGTH
;
164 CFMutableDataRef digest
= CFDataCreateMutable(allocator
, digestLen
);
165 CFDataSetLength(digest
, digestLen
);
166 digestFcn(data
, length
, CFDataGetMutableBytePtr(digest
));
173 /* Default random ref for /dev/random. */
174 const SecRandomRef kSecRandomDefault
= NULL
;
176 /* File descriptor for "/dev/random". */
177 static int kSecRandomFD
;
178 static pthread_once_t kSecDevRandomOpen
= PTHREAD_ONCE_INIT
;
180 static void SecDevRandomOpen(void) {
181 kSecRandomFD
= open("/dev/random", O_RDONLY
);
184 int SecRandomCopyBytes(SecRandomRef rnd
, size_t count
, uint8_t *bytes
) {
185 if (rnd
!= kSecRandomDefault
)
187 pthread_once(&kSecDevRandomOpen
, SecDevRandomOpen
);
188 if (kSecRandomFD
< 0)
191 ssize_t bytes_read
= read(kSecRandomFD
, bytes
, count
);
192 if (bytes_read
== -1) {
197 if (bytes_read
== 0) {
206 #include <CommonCrypto/CommonDigest.h>
209 /* FIPS rng declarations. */
210 typedef struct __SecRandom
*SecRandomRef
;
211 SecRandomRef
SecRandomCreate(CFIndex randomAlg
, CFIndex seedLength
,
213 void SecRandomCopyBytes(SecRandomRef randomref
, CFIndex numBytes
, UInt8
*outBytes
);
215 /* FIPS Rng implementation. */
222 SecRandomRef
SecRandomCreate(CFIndex randomAlg
, CFIndex seedLength
,
224 SecRandomRef result
= (SecRandomRef
)malloc(sizeof(struct __SecRandom
));
225 CC_SHA1_Init(&result
->sha1
);
226 memset(result
->block
+ 20, 0, 44);
227 result
->bytesLeft
= 0;
230 /* Digest the seed and put it into output. */
231 CC_SHA1(seed
, seedLength
, result
->block
);
233 /* Seed 20 bytes from "/dev/srandom". */
234 int fd
= open("/dev/srandom", O_RDONLY
);
238 if (read(fd
, result
->block
, 20) != 20)
244 CC_SHA1_Update(&result
->sha1
, result
->block
, 64);
253 void SecRandomCopyBytes(SecRandomRef randomref
, CFIndex numBytes
,
255 while (numBytes
> 0) {
256 if (!randomref
->bytesLeft
) {
257 CC_SHA1_Update(&randomref
->sha1
, randomref
->block
, 64);
258 OSWriteBigInt32(randomref
->block
, 0, randomref
->sha1
.h0
);
259 OSWriteBigInt32(randomref
->block
, 4, randomref
->sha1
.h1
);
260 OSWriteBigInt32(randomref
->block
, 8, randomref
->sha1
.h2
);
261 OSWriteBigInt32(randomref
->block
, 12, randomref
->sha1
.h3
);
262 OSWriteBigInt32(randomref
->block
, 16, randomref
->sha1
.h4
);
263 randomref
->bytesLeft
= 20;
265 CFIndex outLength
= (numBytes
> randomref
->bytesLeft
?
266 randomref
->bytesLeft
: numBytes
);
267 memcpy(outBytes
, randomref
->block
+ 20 - randomref
->bytesLeft
,
269 randomref
->bytesLeft
-= outLength
;
270 outBytes
+= outLength
;
271 numBytes
-= outLength
;