]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_keychain/regressions/kc-10-item-add-certificate.c
Security-57740.51.3.tar.gz
[apple/security.git] / OSX / libsecurity_keychain / regressions / kc-10-item-add-certificate.c
1 /*
2 * Copyright (c) 2005-2007,2009,2011 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 * 01DL_CreateReleation.c
24 */
25
26 #include <Security/cssmapi.h>
27 #include <Security/SecCertificate.h>
28 #include <Security/SecKeychain.h>
29 #include <Security/SecKeychainItem.h>
30
31 #include "keychain_regressions.h"
32 #include "kc-helpers.h"
33
34 /* Cert File Name: keybank_v3.101.cer */
35 static const uint8 keybank_der_bytes[] =
36 {
37 0x30, 0x82, 0x03, 0x86, 0x30, 0x82, 0x02, 0xef,
38 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x10, 0x78,
39 0xee, 0x48, 0xde, 0x18, 0x5b, 0x20, 0x71, 0xc9,
40 0xc9, 0xc3, 0xb5, 0x1d, 0x7b, 0xdd, 0xc1, 0x30,
41 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
42 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x5f,
43 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
44 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x17, 0x30,
45 0x15, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0e,
46 0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6e,
47 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x31, 0x37,
48 0x30, 0x35, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13,
49 0x2e, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x20, 0x33,
50 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20,
51 0x50, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79, 0x20,
52 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63,
53 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x41, 0x75,
54 0x74, 0x68, 0x6f, 0x72, 0x69, 0x74, 0x79, 0x30,
55 0x1e, 0x17, 0x0d, 0x39, 0x37, 0x30, 0x34, 0x31,
56 0x37, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a,
57 0x17, 0x0d, 0x31, 0x31, 0x31, 0x30, 0x32, 0x34,
58 0x32, 0x33, 0x35, 0x39, 0x35, 0x39, 0x5a, 0x30,
59 0x81, 0xba, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
60 0x55, 0x04, 0x0a, 0x13, 0x16, 0x56, 0x65, 0x72,
61 0x69, 0x53, 0x69, 0x67, 0x6e, 0x20, 0x54, 0x72,
62 0x75, 0x73, 0x74, 0x20, 0x4e, 0x65, 0x74, 0x77,
63 0x6f, 0x72, 0x6b, 0x31, 0x17, 0x30, 0x15, 0x06,
64 0x03, 0x55, 0x04, 0x0b, 0x13, 0x0e, 0x56, 0x65,
65 0x72, 0x69, 0x53, 0x69, 0x67, 0x6e, 0x2c, 0x20,
66 0x49, 0x6e, 0x63, 0x2e, 0x31, 0x33, 0x30, 0x31,
67 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x2a, 0x56,
68 0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6e, 0x20,
69 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x74,
70 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x20, 0x53, 0x65,
71 0x72, 0x76, 0x65, 0x72, 0x20, 0x43, 0x41, 0x20,
72 0x2d, 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x20,
73 0x33, 0x31, 0x49, 0x30, 0x47, 0x06, 0x03, 0x55,
74 0x04, 0x0b, 0x13, 0x40, 0x77, 0x77, 0x77, 0x2e,
75 0x76, 0x65, 0x72, 0x69, 0x73, 0x69, 0x67, 0x6e,
76 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x43, 0x50, 0x53,
77 0x20, 0x49, 0x6e, 0x63, 0x6f, 0x72, 0x70, 0x2e,
78 0x62, 0x79, 0x20, 0x52, 0x65, 0x66, 0x2e, 0x20,
79 0x4c, 0x49, 0x41, 0x42, 0x49, 0x4c, 0x49, 0x54,
80 0x59, 0x20, 0x4c, 0x54, 0x44, 0x2e, 0x28, 0x63,
81 0x29, 0x39, 0x37, 0x20, 0x56, 0x65, 0x72, 0x69,
82 0x53, 0x69, 0x67, 0x6e, 0x30, 0x81, 0x9f, 0x30,
83 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
84 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81,
85 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81,
86 0x00, 0xd8, 0x82, 0x80, 0xe8, 0xd6, 0x19, 0x02,
87 0x7d, 0x1f, 0x85, 0x18, 0x39, 0x25, 0xa2, 0x65,
88 0x2b, 0xe1, 0xbf, 0xd4, 0x05, 0xd3, 0xbc, 0xe6,
89 0x36, 0x3b, 0xaa, 0xf0, 0x4c, 0x6c, 0x5b, 0xb6,
90 0xe7, 0xaa, 0x3c, 0x73, 0x45, 0x55, 0xb2, 0xf1,
91 0xbd, 0xea, 0x97, 0x42, 0xed, 0x9a, 0x34, 0x0a,
92 0x15, 0xd4, 0xa9, 0x5c, 0xf5, 0x40, 0x25, 0xdd,
93 0xd9, 0x07, 0xc1, 0x32, 0xb2, 0x75, 0x6c, 0xc4,
94 0xca, 0xbb, 0xa3, 0xfe, 0x56, 0x27, 0x71, 0x43,
95 0xaa, 0x63, 0xf5, 0x30, 0x3e, 0x93, 0x28, 0xe5,
96 0xfa, 0xf1, 0x09, 0x3b, 0xf3, 0xb7, 0x4d, 0x4e,
97 0x39, 0xf7, 0x5c, 0x49, 0x5a, 0xb8, 0xc1, 0x1d,
98 0xd3, 0xb2, 0x8a, 0xfe, 0x70, 0x30, 0x95, 0x42,
99 0xcb, 0xfe, 0x2b, 0x51, 0x8b, 0x5a, 0x3c, 0x3a,
100 0xf9, 0x22, 0x4f, 0x90, 0xb2, 0x02, 0xa7, 0x53,
101 0x9c, 0x4f, 0x34, 0xe7, 0xab, 0x04, 0xb2, 0x7b,
102 0x6f, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x81,
103 0xe6, 0x30, 0x81, 0xe3, 0x30, 0x0f, 0x06, 0x03,
104 0x55, 0x1d, 0x13, 0x04, 0x08, 0x30, 0x06, 0x01,
105 0x01, 0xff, 0x02, 0x01, 0x00, 0x30, 0x44, 0x06,
106 0x03, 0x55, 0x1d, 0x20, 0x04, 0x3d, 0x30, 0x3b,
107 0x30, 0x39, 0x06, 0x0b, 0x60, 0x86, 0x48, 0x01,
108 0x86, 0xf8, 0x45, 0x01, 0x07, 0x01, 0x01, 0x30,
109 0x2a, 0x30, 0x28, 0x06, 0x08, 0x2b, 0x06, 0x01,
110 0x05, 0x05, 0x07, 0x02, 0x01, 0x16, 0x1c, 0x68,
111 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77,
112 0x77, 0x77, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x73,
113 0x69, 0x67, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
114 0x43, 0x50, 0x53, 0x30, 0x34, 0x06, 0x03, 0x55,
115 0x1d, 0x1f, 0x04, 0x2d, 0x30, 0x2b, 0x30, 0x29,
116 0xa0, 0x27, 0xa0, 0x25, 0x86, 0x23, 0x68, 0x74,
117 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x63, 0x72, 0x6c,
118 0x2e, 0x76, 0x65, 0x72, 0x69, 0x73, 0x69, 0x67,
119 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x70, 0x63,
120 0x61, 0x33, 0x2d, 0x67, 0x32, 0x2e, 0x63, 0x72,
121 0x6c, 0x30, 0x34, 0x06, 0x03, 0x55, 0x1d, 0x25,
122 0x04, 0x2d, 0x30, 0x2b, 0x06, 0x08, 0x2b, 0x06,
123 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08,
124 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
125 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8,
126 0x42, 0x04, 0x01, 0x06, 0x0a, 0x60, 0x86, 0x48,
127 0x01, 0x86, 0xf8, 0x45, 0x01, 0x08, 0x01, 0x30,
128 0x0b, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x04, 0x04,
129 0x03, 0x02, 0x01, 0x06, 0x30, 0x11, 0x06, 0x09,
130 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x01,
131 0x01, 0x04, 0x04, 0x03, 0x02, 0x01, 0x06, 0x30,
132 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
133 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x81,
134 0x81, 0x00, 0x23, 0x5d, 0xee, 0xa6, 0x24, 0x05,
135 0xfd, 0x76, 0xd3, 0x6a, 0x1a, 0xd6, 0xba, 0x46,
136 0x06, 0xaa, 0x6a, 0x0f, 0x03, 0x90, 0x66, 0xb2,
137 0xb0, 0xa6, 0xc2, 0x9e, 0xc9, 0x1e, 0xa3, 0x55,
138 0x53, 0xaf, 0x3e, 0x45, 0xfd, 0xdc, 0x8c, 0x27,
139 0xdd, 0x53, 0x38, 0x09, 0xbb, 0x7c, 0x4b, 0x2b,
140 0xba, 0x95, 0x4a, 0xfe, 0x70, 0x4e, 0x1b, 0x69,
141 0xd6, 0x3c, 0xf7, 0x4f, 0x07, 0xc5, 0xf2, 0x17,
142 0x5a, 0x4c, 0xa2, 0x8f, 0xac, 0x0b, 0x8a, 0x06,
143 0xdb, 0xb9, 0xd4, 0x6b, 0xc5, 0x1d, 0x58, 0xda,
144 0x17, 0x52, 0xe3, 0x21, 0xf1, 0xd2, 0xd7, 0x5a,
145 0xd5, 0xe5, 0xab, 0x59, 0x7b, 0x21, 0x7a, 0x86,
146 0x6a, 0xd4, 0xfe, 0x17, 0x11, 0x3a, 0x53, 0x0d,
147 0x9c, 0x60, 0xa0, 0x4a, 0xd9, 0x5e, 0xe4, 0x1d,
148 0x0c, 0x29, 0xaa, 0x13, 0x07, 0x65, 0x86, 0x1f,
149 0xbf, 0xb4, 0xc9, 0x82, 0x53, 0x9c, 0x2c, 0x02,
150 0x8f, 0x23
151 };
152 static const CSSM_DATA keybank_der =
153 {
154 sizeof(keybank_der_bytes),
155 (uint8 *)keybank_der_bytes
156 };
157
158 static void
159 certTests(SecKeychainRef keychain)
160 {
161 SecCertificateRef certificate = NULL;
162 ok_status(SecCertificateCreateFromData(&keybank_der,
163 CSSM_CERT_X_509v3, CSSM_CERT_ENCODING_DER, &certificate),
164 "SecCertificateCreateFromData");
165 ok(certificate, "certificate non NULL");
166 ok_status(SecCertificateAddToKeychain(certificate, keychain),
167 "SecCertificateAddToKeychain fails unless 4039735 is fixed");
168
169 is_status(SecCertificateAddToKeychain(certificate, keychain),
170 errSecDuplicateItem, "SecCertificateAddToKeychain twice");
171
172 SecCertificateRef certificate2 = NULL;
173 ok_status(SecCertificateCreateFromData(&keybank_der,
174 CSSM_CERT_X_509v3, CSSM_CERT_ENCODING_DER, &certificate2),
175 "SecCertificateCreateFromData");
176 is_status(SecCertificateAddToKeychain(certificate2, keychain),
177 errSecDuplicateItem, "SecCertificateAddToKeychain twice");
178 is(CFGetRetainCount(certificate2), 1, "certificate2 retain count is 1");
179 CFRelease(certificate2);
180
181 SecKeychainRef certKeychain = NULL;
182 ok_status(SecKeychainItemCopyKeychain((SecKeychainItemRef)certificate,
183 &certKeychain), "SecKeychainItemCopyKeychain");
184 is((intptr_t)keychain, (intptr_t)certKeychain, "cert's keychain is keychain");
185 CFRelease(certKeychain);
186
187 is(CFGetRetainCount(certificate), 1, "certificate retain count is 1");
188 CFRelease(certificate);
189
190 ok_status(SecKeychainDelete(keychain), "SecKeychainDelete");
191 cmp_ok(CFGetRetainCount(keychain), >=, 1, "keychain retain count is 1");
192 CFRelease(keychain);
193 }
194
195 int
196 kc_10_item_add_certificate(int argc, char * const *argv)
197 {
198 plan_tests(33);
199
200 /* Test with autocommit on. */
201 SecKeychainRef keychain = createNewKeychain("test.keychain", "test");
202 ok(keychain, "keychain non NULL");
203 certTests(keychain);
204
205 /* Test with autocommit off. */
206 keychain = NULL;
207 keychain = createNewKeychain("test2.keychain", "test");
208 ok(keychain, "keychain non NULL");
209 CSSM_DL_DB_HANDLE cspdl_dldb = {};
210 ok_status(SecKeychainGetDLDBHandle(keychain, &cspdl_dldb),
211 "SecKeychainGetDLDBHandle");
212 CSSM_DL_DB_HANDLE dldb = {};
213 ok_status(CSSM_DL_PassThrough(cspdl_dldb, CSSM_APPLECSPDL_DB_GET_HANDLE,
214 NULL, (void **)&dldb), "get dl handle");
215 ok(dldb.DLHandle, "dldb.DLHandle non 0");
216 ok(dldb.DBHandle, "dldb.DBHandle non 0");
217 ok_status(CSSM_DL_PassThrough(dldb, CSSM_APPLEFILEDL_TOGGLE_AUTOCOMMIT,
218 (const void *)FALSE, NULL), "autocommit off");
219 certTests(keychain);
220
221 deleteTestFiles();
222 return 0;
223 }