]> git.saurik.com Git - apple/security.git/blob - OSX/regressions/test/testcert.c
Security-59306.41.2.tar.gz
[apple/security.git] / OSX / regressions / test / testcert.c
1 /*
2 * Copyright (c) 2009,2012-2017 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 #include <CoreFoundation/CoreFoundation.h>
29 #include <Security/SecIdentityPriv.h>
30 #include <Security/SecItem.h>
31 #include <Security/SecCertificateRequest.h>
32 #include <Security/SecInternal.h>
33 #include <utilities/array_size.h>
34
35 #include <AssertMacros.h>
36
37 #include "testcert.h"
38
39 static inline CF_RETURNS_RETAINED CFMutableArrayRef maa(CFMutableArrayRef array CF_CONSUMED, CFTypeRef a CF_CONSUMED) {
40 CFMutableArrayRef ma = array;
41 if (!ma)
42 ma = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
43 if (ma) {
44 CFArrayAppendValue(ma, a);
45 }
46 CFRelease(a);
47 return ma;
48 }
49
50
51 CF_RETURNS_RETAINED
52 CFArrayRef test_cert_string_to_subject(CFStringRef subject)
53 {
54 CFMutableArrayRef subject_array = NULL;
55 char buffer[1024];
56
57 if (!CFStringGetCString(subject, buffer, sizeof(buffer), kCFStringEncodingASCII))
58 goto out;
59
60 char *s = buffer, *e = NULL;
61 while ( (e = strchr(s, ',')) || (e = strchr(s, '\0')) ) {
62 if (s == e)
63 break;
64 if (*e && (*(e-1) == '\\'))
65 continue;
66 char *k;
67 while ((k = strchr(s, '=')) &&
68 (*(k-1) == '\\'));
69 if ( ((k - s) > 0) && ((e - k) > 1) ) {
70 CFStringRef key = CFStringCreateWithBytes(kCFAllocatorDefault, (uint8_t *)s, k - s, kCFStringEncodingASCII, false);
71 CFStringRef value = CFStringCreateWithBytes(kCFAllocatorDefault, (uint8_t *)k + 1, e - k - 1, kCFStringEncodingASCII, false);
72 subject_array = maa(subject_array, maa(NULL, maa(maa(NULL, key), value)));
73 }
74 if (*e == '\0')
75 break;
76 s = e + 1;
77 }
78
79 out:
80 return subject_array;
81 }
82
83
84 static void test_cert_key_usage(CFMutableDictionaryRef extensions_dict, unsigned int key_usage)
85 {
86 int key_usage_int = key_usage;
87 CFNumberRef key_usage_num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &key_usage_int);
88 CFDictionarySetValue(extensions_dict, kSecCertificateKeyUsage, key_usage_num);
89 CFRelease(key_usage_num);
90 }
91
92
93 static void test_cert_path_length(CFMutableDictionaryRef extensions_dict, unsigned int path_length)
94 {
95 int path_len_int = path_length;
96 CFNumberRef path_len_num = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &path_len_int);
97 CFDictionarySetValue(extensions_dict, kSecCSRBasicContraintsPathLen, path_len_num);
98 CFRelease(path_len_num);
99 }
100
101
102 SecIdentityRef test_cert_create_root_certificate(CFStringRef subject, SecKeyRef public_key, SecKeyRef private_key)
103 {
104 SecCertificateRef ca_cert = NULL;
105 SecIdentityRef ca_identity = NULL;
106 CFMutableDictionaryRef extensions = NULL;
107
108 CFArrayRef ca_subject = NULL;
109 require(ca_subject = test_cert_string_to_subject(subject), out);
110 extensions = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
111 test_cert_key_usage(extensions, kSecKeyUsageKeyCertSign | kSecKeyUsageCRLSign);
112 test_cert_path_length(extensions, 0);
113 ca_cert = SecGenerateSelfSignedCertificate(ca_subject, extensions, public_key, private_key);
114 if (private_key && ca_cert)
115 ca_identity = SecIdentityCreate(kCFAllocatorDefault, ca_cert, private_key);
116
117 out:
118 CFReleaseSafe(extensions);
119 CFReleaseSafe(ca_subject);
120 CFReleaseSafe(ca_cert);
121
122 return ca_identity;
123 }
124
125 SecCertificateRef test_cert_issue_certificate(SecIdentityRef ca_identity,
126 SecKeyRef public_key, CFStringRef subject,
127 unsigned int serial_no, unsigned int key_usage)
128 {
129 SecCertificateRef cert = NULL;
130 CFArrayRef cert_subject = NULL;
131 CFDataRef serialno = NULL;
132 CFMutableDictionaryRef extensions = NULL;
133
134 unsigned int serial = htonl(serial_no);
135 unsigned int serial_length = sizeof(serial);
136 uint8_t *serial_non_zero = (uint8_t*)&serial;
137 while (!*serial_non_zero && serial_length)
138 { serial_non_zero++; serial_length--; }
139 serialno = CFDataCreate(kCFAllocatorDefault,
140 serial_non_zero, serial_length);
141 require(cert_subject = test_cert_string_to_subject(subject), out);
142 //extensions = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
143 //require(extensions, out);
144 //test_cert_key_usage(extensions, key_usage);
145
146 cert = SecIdentitySignCertificate(ca_identity, serialno,
147 public_key, cert_subject, NULL);
148
149 out:
150 CFReleaseSafe(extensions);
151 CFReleaseSafe(cert_subject);
152 CFReleaseSafe(serialno);
153
154 return cert;
155 }
156
157 OSStatus
158 test_cert_generate_key(uint32_t key_size_in_bits, CFTypeRef sec_attr_key_type,
159 SecKeyRef *private_key, SecKeyRef *public_key)
160 {
161 CFNumberRef key_size = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &key_size_in_bits);
162 const void *keygen_keys[] = { kSecAttrKeyType, kSecAttrKeySizeInBits };
163 const void *keygen_vals[] = { sec_attr_key_type, key_size };
164 CFDictionaryRef parameters = CFDictionaryCreate(kCFAllocatorDefault,
165 keygen_keys, keygen_vals, array_size(keygen_vals),
166 &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
167 CFRelease(key_size);
168
169 return SecKeyGeneratePair(parameters, public_key, private_key);
170 }