2 * Copyright (c) 2005-2007,2009,2011 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@
23 * 01DL_CreateReleation.c
26 #include <Security/cssmapi.h>
27 #include <Security/SecCertificate.h>
28 #include <Security/SecKeychain.h>
29 #include <Security/SecKeychainItem.h>
30 #include <Security/SecKeychainPriv.h>
36 /* Cert File Name: keybank_v3.101.cer */
37 static const uint8 keybank_der_bytes
[] =
39 0x30, 0x82, 0x03, 0x86, 0x30, 0x82, 0x02, 0xef,
40 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x10, 0x78,
41 0xee, 0x48, 0xde, 0x18, 0x5b, 0x20, 0x71, 0xc9,
42 0xc9, 0xc3, 0xb5, 0x1d, 0x7b, 0xdd, 0xc1, 0x30,
43 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
44 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x5f,
45 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
46 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x17, 0x30,
47 0x15, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13, 0x0e,
48 0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6e,
49 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e, 0x31, 0x37,
50 0x30, 0x35, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13,
51 0x2e, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x20, 0x33,
52 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69, 0x63, 0x20,
53 0x50, 0x72, 0x69, 0x6d, 0x61, 0x72, 0x79, 0x20,
54 0x43, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63,
55 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x41, 0x75,
56 0x74, 0x68, 0x6f, 0x72, 0x69, 0x74, 0x79, 0x30,
57 0x1e, 0x17, 0x0d, 0x39, 0x37, 0x30, 0x34, 0x31,
58 0x37, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a,
59 0x17, 0x0d, 0x31, 0x31, 0x31, 0x30, 0x32, 0x34,
60 0x32, 0x33, 0x35, 0x39, 0x35, 0x39, 0x5a, 0x30,
61 0x81, 0xba, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
62 0x55, 0x04, 0x0a, 0x13, 0x16, 0x56, 0x65, 0x72,
63 0x69, 0x53, 0x69, 0x67, 0x6e, 0x20, 0x54, 0x72,
64 0x75, 0x73, 0x74, 0x20, 0x4e, 0x65, 0x74, 0x77,
65 0x6f, 0x72, 0x6b, 0x31, 0x17, 0x30, 0x15, 0x06,
66 0x03, 0x55, 0x04, 0x0b, 0x13, 0x0e, 0x56, 0x65,
67 0x72, 0x69, 0x53, 0x69, 0x67, 0x6e, 0x2c, 0x20,
68 0x49, 0x6e, 0x63, 0x2e, 0x31, 0x33, 0x30, 0x31,
69 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x2a, 0x56,
70 0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6e, 0x20,
71 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x74,
72 0x69, 0x6f, 0x6e, 0x61, 0x6c, 0x20, 0x53, 0x65,
73 0x72, 0x76, 0x65, 0x72, 0x20, 0x43, 0x41, 0x20,
74 0x2d, 0x20, 0x43, 0x6c, 0x61, 0x73, 0x73, 0x20,
75 0x33, 0x31, 0x49, 0x30, 0x47, 0x06, 0x03, 0x55,
76 0x04, 0x0b, 0x13, 0x40, 0x77, 0x77, 0x77, 0x2e,
77 0x76, 0x65, 0x72, 0x69, 0x73, 0x69, 0x67, 0x6e,
78 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x43, 0x50, 0x53,
79 0x20, 0x49, 0x6e, 0x63, 0x6f, 0x72, 0x70, 0x2e,
80 0x62, 0x79, 0x20, 0x52, 0x65, 0x66, 0x2e, 0x20,
81 0x4c, 0x49, 0x41, 0x42, 0x49, 0x4c, 0x49, 0x54,
82 0x59, 0x20, 0x4c, 0x54, 0x44, 0x2e, 0x28, 0x63,
83 0x29, 0x39, 0x37, 0x20, 0x56, 0x65, 0x72, 0x69,
84 0x53, 0x69, 0x67, 0x6e, 0x30, 0x81, 0x9f, 0x30,
85 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
86 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x81,
87 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81,
88 0x00, 0xd8, 0x82, 0x80, 0xe8, 0xd6, 0x19, 0x02,
89 0x7d, 0x1f, 0x85, 0x18, 0x39, 0x25, 0xa2, 0x65,
90 0x2b, 0xe1, 0xbf, 0xd4, 0x05, 0xd3, 0xbc, 0xe6,
91 0x36, 0x3b, 0xaa, 0xf0, 0x4c, 0x6c, 0x5b, 0xb6,
92 0xe7, 0xaa, 0x3c, 0x73, 0x45, 0x55, 0xb2, 0xf1,
93 0xbd, 0xea, 0x97, 0x42, 0xed, 0x9a, 0x34, 0x0a,
94 0x15, 0xd4, 0xa9, 0x5c, 0xf5, 0x40, 0x25, 0xdd,
95 0xd9, 0x07, 0xc1, 0x32, 0xb2, 0x75, 0x6c, 0xc4,
96 0xca, 0xbb, 0xa3, 0xfe, 0x56, 0x27, 0x71, 0x43,
97 0xaa, 0x63, 0xf5, 0x30, 0x3e, 0x93, 0x28, 0xe5,
98 0xfa, 0xf1, 0x09, 0x3b, 0xf3, 0xb7, 0x4d, 0x4e,
99 0x39, 0xf7, 0x5c, 0x49, 0x5a, 0xb8, 0xc1, 0x1d,
100 0xd3, 0xb2, 0x8a, 0xfe, 0x70, 0x30, 0x95, 0x42,
101 0xcb, 0xfe, 0x2b, 0x51, 0x8b, 0x5a, 0x3c, 0x3a,
102 0xf9, 0x22, 0x4f, 0x90, 0xb2, 0x02, 0xa7, 0x53,
103 0x9c, 0x4f, 0x34, 0xe7, 0xab, 0x04, 0xb2, 0x7b,
104 0x6f, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x81,
105 0xe6, 0x30, 0x81, 0xe3, 0x30, 0x0f, 0x06, 0x03,
106 0x55, 0x1d, 0x13, 0x04, 0x08, 0x30, 0x06, 0x01,
107 0x01, 0xff, 0x02, 0x01, 0x00, 0x30, 0x44, 0x06,
108 0x03, 0x55, 0x1d, 0x20, 0x04, 0x3d, 0x30, 0x3b,
109 0x30, 0x39, 0x06, 0x0b, 0x60, 0x86, 0x48, 0x01,
110 0x86, 0xf8, 0x45, 0x01, 0x07, 0x01, 0x01, 0x30,
111 0x2a, 0x30, 0x28, 0x06, 0x08, 0x2b, 0x06, 0x01,
112 0x05, 0x05, 0x07, 0x02, 0x01, 0x16, 0x1c, 0x68,
113 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f, 0x2f, 0x77,
114 0x77, 0x77, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x73,
115 0x69, 0x67, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
116 0x43, 0x50, 0x53, 0x30, 0x34, 0x06, 0x03, 0x55,
117 0x1d, 0x1f, 0x04, 0x2d, 0x30, 0x2b, 0x30, 0x29,
118 0xa0, 0x27, 0xa0, 0x25, 0x86, 0x23, 0x68, 0x74,
119 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x63, 0x72, 0x6c,
120 0x2e, 0x76, 0x65, 0x72, 0x69, 0x73, 0x69, 0x67,
121 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x70, 0x63,
122 0x61, 0x33, 0x2d, 0x67, 0x32, 0x2e, 0x63, 0x72,
123 0x6c, 0x30, 0x34, 0x06, 0x03, 0x55, 0x1d, 0x25,
124 0x04, 0x2d, 0x30, 0x2b, 0x06, 0x08, 0x2b, 0x06,
125 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08,
126 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
127 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8,
128 0x42, 0x04, 0x01, 0x06, 0x0a, 0x60, 0x86, 0x48,
129 0x01, 0x86, 0xf8, 0x45, 0x01, 0x08, 0x01, 0x30,
130 0x0b, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x04, 0x04,
131 0x03, 0x02, 0x01, 0x06, 0x30, 0x11, 0x06, 0x09,
132 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x01,
133 0x01, 0x04, 0x04, 0x03, 0x02, 0x01, 0x06, 0x30,
134 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
135 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x81,
136 0x81, 0x00, 0x23, 0x5d, 0xee, 0xa6, 0x24, 0x05,
137 0xfd, 0x76, 0xd3, 0x6a, 0x1a, 0xd6, 0xba, 0x46,
138 0x06, 0xaa, 0x6a, 0x0f, 0x03, 0x90, 0x66, 0xb2,
139 0xb0, 0xa6, 0xc2, 0x9e, 0xc9, 0x1e, 0xa3, 0x55,
140 0x53, 0xaf, 0x3e, 0x45, 0xfd, 0xdc, 0x8c, 0x27,
141 0xdd, 0x53, 0x38, 0x09, 0xbb, 0x7c, 0x4b, 0x2b,
142 0xba, 0x95, 0x4a, 0xfe, 0x70, 0x4e, 0x1b, 0x69,
143 0xd6, 0x3c, 0xf7, 0x4f, 0x07, 0xc5, 0xf2, 0x17,
144 0x5a, 0x4c, 0xa2, 0x8f, 0xac, 0x0b, 0x8a, 0x06,
145 0xdb, 0xb9, 0xd4, 0x6b, 0xc5, 0x1d, 0x58, 0xda,
146 0x17, 0x52, 0xe3, 0x21, 0xf1, 0xd2, 0xd7, 0x5a,
147 0xd5, 0xe5, 0xab, 0x59, 0x7b, 0x21, 0x7a, 0x86,
148 0x6a, 0xd4, 0xfe, 0x17, 0x11, 0x3a, 0x53, 0x0d,
149 0x9c, 0x60, 0xa0, 0x4a, 0xd9, 0x5e, 0xe4, 0x1d,
150 0x0c, 0x29, 0xaa, 0x13, 0x07, 0x65, 0x86, 0x1f,
151 0xbf, 0xb4, 0xc9, 0x82, 0x53, 0x9c, 0x2c, 0x02,
154 static const CSSM_DATA keybank_der
=
156 sizeof(keybank_der_bytes
),
157 (uint8
*)keybank_der_bytes
161 certTests(SecKeychainRef keychain
)
163 SecCertificateRef certificate
= NULL
;
164 ok_status(SecCertificateCreateFromData(&keybank_der
,
165 CSSM_CERT_X_509v3
, CSSM_CERT_ENCODING_DER
, &certificate
),
166 "SecCertificateCreateFromData");
167 ok(certificate
, "certificate non NULL");
168 ok_status(SecCertificateAddToKeychain(certificate
, keychain
),
169 "SecCertificateAddToKeychain fails unless 4039735 is fixed");
171 is_status(SecCertificateAddToKeychain(certificate
, keychain
),
172 errSecDuplicateItem
, "SecCertificateAddToKeychain twice");
174 SecCertificateRef certificate2
= NULL
;
175 ok_status(SecCertificateCreateFromData(&keybank_der
,
176 CSSM_CERT_X_509v3
, CSSM_CERT_ENCODING_DER
, &certificate2
),
177 "SecCertificateCreateFromData");
178 is_status(SecCertificateAddToKeychain(certificate2
, keychain
),
179 errSecDuplicateItem
, "SecCertificateAddToKeychain twice");
180 is(CFGetRetainCount(certificate2
), 1, "certificate2 retain count is 1"); // 1 because the above test fails, therefore it's not owned by a keychain
181 CFRelease(certificate2
);
183 SecKeychainRef certKeychain
= NULL
;
184 ok_status(SecKeychainItemCopyKeychain((SecKeychainItemRef
)certificate
,
185 &certKeychain
), "SecKeychainItemCopyKeychain");
186 is((intptr_t)keychain
, (intptr_t)certKeychain
, "cert's keychain is keychain");
187 CFRelease(certKeychain
);
189 is(CFGetRetainCount(certificate
), 1, "certificate retain count is 1");
190 CFRelease(certificate
);
192 ok_status(SecKeychainDelete(keychain
), "SecKeychainDelete");
193 is(CFGetRetainCount(keychain
), 1, "keychain retain count is 1");
199 0 /* NumberOfRecordTypes */,
203 CSSM_TRUE
/* IsLocal */,
204 NULL
, /* AccessPath - URL, dir path, etc. */
209 main(int argc
, char * const *argv
)
211 int guid_alt
= argc
> 1 && !strcmp(argv
[1], "-g");
212 /* {2cb56191-ee6f-432d-a377-853d3c6b949e} */
213 CSSM_GUID s3dl_guid
=
215 0x2cb56191, 0xee6f, 0x432d,
216 { 0xa3, 0x77, 0x85, 0x3d, 0x3c, 0x6b, 0x94, 0x9e }
218 const CSSM_GUID
*guid
= guid_alt
? & s3dl_guid
: &gGuidAppleFileDL
;
222 CSSM_DL_DB_HANDLE dldb
= {};
223 ok(cssm_attach(guid
, &dldb
.DLHandle
), "cssm_attach");
225 if (!tests_begin(argc
, argv
))
228 const char *dbname
= "test.keychain";
229 ok_status(CSSM_DL_DbCreate(dldb
.DLHandle
, dbname
, NULL
/* DbLocation */,
231 CSSM_DB_ACCESS_READ
| CSSM_DB_ACCESS_WRITE
,
232 NULL
/* CredAndAclEntry */,
233 NULL
/* &openParameters */,
235 "CSSM_DL_DbCreate pure dl");
236 ok_status(CSSM_DL_DbClose(dldb
), "close db");
237 ok(cssm_detach(guid
, dldb
.DLHandle
), "cssm_detach");
239 SecKeychainRef keychain
= NULL
;
240 ok_status(SecKeychainOpenWithGuid(guid
, 0, CSSM_SERVICE_DL
, dbname
,
241 NULL
, &keychain
), "Open pure dl keychain");
242 ok(keychain
, "keychain non NULL");
246 return !tests_end(1);