2 * Copyright (c) 2009 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@
26 #include <TargetConditionals.h>
30 #include <CoreFoundation/CoreFoundation.h>
31 #include <Security/SecIdentityPriv.h>
32 #include <Security/SecItem.h>
33 #include <Security/SecCertificateRequest.h>
34 #include <Security/SecInternal.h>
35 #include <utilities/array_size.h>
37 #include <AssertMacros.h>
41 static inline CFMutableArrayRef
maa(CFMutableArrayRef array
, CFTypeRef a
) {
42 CFMutableArrayRef ma
= array
;
44 ma
= CFArrayCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeArrayCallBacks
);
46 CFArrayAppendValue(ma
, a
);
53 CFArrayRef
test_cert_string_to_subject(CFStringRef subject
)
55 CFMutableArrayRef subject_array
= NULL
;
58 if (!CFStringGetCString(subject
, buffer
, sizeof(buffer
), kCFStringEncodingASCII
))
61 char *s
= buffer
, *e
= NULL
;
62 while ( (e
= strchr(s
, ',')) || (e
= strchr(s
, '\0')) ) {
65 if (*e
&& (*(e
-1) == '\\'))
68 while ((k
= strchr(s
, '=')) &&
70 if ( ((k
- s
) > 0) && ((e
- k
) > 1) ) {
71 CFStringRef key
= CFStringCreateWithBytes(kCFAllocatorDefault
, (uint8_t *)s
, k
- s
, kCFStringEncodingASCII
, false);
72 CFStringRef value
= CFStringCreateWithBytes(kCFAllocatorDefault
, (uint8_t *)k
+ 1, e
- k
- 1, kCFStringEncodingASCII
, false);
73 subject_array
= maa(subject_array
, maa(NULL
, maa(maa(NULL
, key
), value
)));
85 static void test_cert_key_usage(CFMutableDictionaryRef extensions_dict
, unsigned int key_usage
)
87 int key_usage_int
= key_usage
;
88 CFNumberRef key_usage_num
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberIntType
, &key_usage_int
);
89 CFDictionarySetValue(extensions_dict
, kSecCertificateKeyUsage
, key_usage_num
);
90 CFRelease(key_usage_num
);
94 static void test_cert_path_length(CFMutableDictionaryRef extensions_dict
, unsigned int path_length
)
96 int path_len_int
= path_length
;
97 CFNumberRef path_len_num
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberIntType
, &path_len_int
);
98 CFDictionarySetValue(extensions_dict
, kSecCSRBasicContraintsPathLen
, path_len_num
);
99 CFRelease(path_len_num
);
103 SecIdentityRef
test_cert_create_root_certificate(CFStringRef subject
, SecKeyRef public_key
, SecKeyRef private_key
)
105 SecCertificateRef ca_cert
= NULL
;
106 SecIdentityRef ca_identity
= NULL
;
107 CFMutableDictionaryRef extensions
= NULL
;
109 CFArrayRef ca_subject
= NULL
;
110 require(ca_subject
= test_cert_string_to_subject(subject
), out
);
111 extensions
= CFDictionaryCreateMutable(kCFAllocatorDefault
, 0, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
112 test_cert_key_usage(extensions
, kSecKeyUsageKeyCertSign
| kSecKeyUsageCRLSign
);
113 test_cert_path_length(extensions
, 0);
114 ca_cert
= SecGenerateSelfSignedCertificate(ca_subject
, extensions
, public_key
, private_key
);
115 if (private_key
&& ca_cert
)
116 ca_identity
= SecIdentityCreate(kCFAllocatorDefault
, ca_cert
, private_key
);
119 CFReleaseSafe(extensions
);
120 CFReleaseSafe(ca_subject
);
121 CFReleaseSafe(ca_cert
);
126 SecCertificateRef
test_cert_issue_certificate(SecIdentityRef ca_identity
,
127 SecKeyRef public_key
, CFStringRef subject
,
128 unsigned int serial_no
, unsigned int key_usage
)
130 SecCertificateRef cert
= NULL
;
131 CFArrayRef cert_subject
= NULL
;
132 CFDataRef serialno
= NULL
;
133 CFMutableDictionaryRef extensions
= NULL
;
135 unsigned int serial
= htonl(serial_no
);
136 unsigned int serial_length
= sizeof(serial
);
137 uint8_t *serial_non_zero
= (uint8_t*)&serial
;
138 while (!*serial_non_zero
&& serial_length
)
139 { serial_non_zero
++; serial_length
--; }
140 serialno
= CFDataCreate(kCFAllocatorDefault
,
141 serial_non_zero
, serial_length
);
142 require(cert_subject
= test_cert_string_to_subject(subject
), out
);
143 //extensions = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
144 //require(extensions, out);
145 //test_cert_key_usage(extensions, key_usage);
147 cert
= SecIdentitySignCertificate(ca_identity
, serialno
,
148 public_key
, cert_subject
, NULL
);
151 CFReleaseSafe(extensions
);
152 CFReleaseSafe(cert_subject
);
153 CFReleaseSafe(serialno
);
159 test_cert_generate_key(uint32_t key_size_in_bits
, CFTypeRef sec_attr_key_type
,
160 SecKeyRef
*private_key
, SecKeyRef
*public_key
)
162 CFNumberRef key_size
= CFNumberCreate(kCFAllocatorDefault
, kCFNumberIntType
, &key_size_in_bits
);
163 const void *keygen_keys
[] = { kSecAttrKeyType
, kSecAttrKeySizeInBits
};
164 const void *keygen_vals
[] = { sec_attr_key_type
, key_size
};
165 CFDictionaryRef parameters
= CFDictionaryCreate(kCFAllocatorDefault
,
166 keygen_keys
, keygen_vals
, array_size(keygen_vals
),
167 &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
170 return SecKeyGeneratePair(parameters
, public_key
, private_key
);
173 #endif /* TARGET_OS_IPHONE */