]> git.saurik.com Git - apple/security.git/blob - regressions/test/testcert.c
Security-55471.tar.gz
[apple/security.git] / regressions / test / testcert.c
1 /*
2 * Copyright (c) 2009 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 * testcert.c
24 */
25
26 #include <TargetConditionals.h>
27
28 #if TARGET_OS_IPHONE
29
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>
36
37 #include <AssertMacros.h>
38
39 #include "testcert.h"
40
41 static inline CFMutableArrayRef maa(CFMutableArrayRef array, CFTypeRef a) {
42 CFMutableArrayRef ma = array;
43 if (!ma)
44 ma = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
45 if (ma) {
46 CFArrayAppendValue(ma, a);
47 CFRelease(a);
48 }
49 return ma;
50 }
51
52
53 CFArrayRef test_cert_string_to_subject(CFStringRef subject)
54 {
55 CFMutableArrayRef subject_array = NULL;
56 char buffer[1024];
57
58 if (!CFStringGetCString(subject, buffer, sizeof(buffer), kCFStringEncodingASCII))
59 goto out;
60
61 char *s = buffer, *e = NULL;
62 while ( (e = strchr(s, ',')) || (e = strchr(s, '\0')) ) {
63 if (s == e)
64 break;
65 if (*e && (*(e-1) == '\\'))
66 continue;
67 char *k;
68 while ((k = strchr(s, '=')) &&
69 (*(k-1) == '\\'));
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)));
74 }
75 if (*e == '\0')
76 break;
77 s = e + 1;
78 }
79
80 out:
81 return subject_array;
82 }
83
84
85 static void test_cert_key_usage(CFMutableDictionaryRef extensions_dict, unsigned int key_usage)
86 {
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);
91 }
92
93
94 static void test_cert_path_length(CFMutableDictionaryRef extensions_dict, unsigned int path_length)
95 {
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);
100 }
101
102
103 SecIdentityRef test_cert_create_root_certificate(CFStringRef subject, SecKeyRef public_key, SecKeyRef private_key)
104 {
105 SecCertificateRef ca_cert = NULL;
106 SecIdentityRef ca_identity = NULL;
107 CFMutableDictionaryRef extensions = NULL;
108
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);
117
118 out:
119 CFReleaseSafe(extensions);
120 CFReleaseSafe(ca_subject);
121 CFReleaseSafe(ca_cert);
122
123 return ca_identity;
124 }
125
126 SecCertificateRef test_cert_issue_certificate(SecIdentityRef ca_identity,
127 SecKeyRef public_key, CFStringRef subject,
128 unsigned int serial_no, unsigned int key_usage)
129 {
130 SecCertificateRef cert = NULL;
131 CFArrayRef cert_subject = NULL;
132 CFDataRef serialno = NULL;
133 CFMutableDictionaryRef extensions = NULL;
134
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);
146
147 cert = SecIdentitySignCertificate(ca_identity, serialno,
148 public_key, cert_subject, NULL);
149
150 out:
151 CFReleaseSafe(extensions);
152 CFReleaseSafe(cert_subject);
153 CFReleaseSafe(serialno);
154
155 return cert;
156 }
157
158 OSStatus
159 test_cert_generate_key(uint32_t key_size_in_bits, CFTypeRef sec_attr_key_type,
160 SecKeyRef *private_key, SecKeyRef *public_key)
161 {
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);
168 CFRelease(key_size);
169
170 return SecKeyGeneratePair(parameters, public_key, private_key);
171 }
172
173 #endif /* TARGET_OS_IPHONE */