]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/secitem/si-30-keychain-upgrade.c
Security-57740.60.18.tar.gz
[apple/security.git] / OSX / sec / Security / Regressions / secitem / si-30-keychain-upgrade.c
1 /*
2 * Copyright (c) 2008,2010,2012-2014 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
24
25 #include <CoreFoundation/CoreFoundation.h>
26 #include <Security/SecBase.h>
27 #include <Security/SecItem.h>
28 #include <Security/SecItemPriv.h>
29 #include <Security/SecInternal.h>
30 #include <utilities/SecCFWrappers.h>
31
32 #include <stdlib.h>
33 #include <sys/stat.h>
34 #include <sqlite3.h>
35
36 #include "Security_regressions.h"
37 #include <securityd/SecItemServer.h>
38
39 /* TODO: This test needs to be updated. It was originally created to test upgrades from DB prior to the introduction of versionning, circa 2008.
40 We don't support upgrading from that old of keychain, but this test should be upgraded to test upgrades from v5 to v6 keychain, or more current
41 */
42
43 const char *create_db_sql =
44 "BEGIN TRANSACTION;"
45 "CREATE TABLE genp(cdat REAL,mdat REAL,desc BLOB,icmt BLOB,crtr INTEGER,type INTEGER,scrp INTEGER,labl BLOB,alis BLOB,invi INTEGER,nega INTEGER,cusi INTEGER,prot BLOB,acct BLOB NOT NULL DEFAULT '',svce BLOB NOT NULL DEFAULT '',gena BLOB,data BLOB,PRIMARY KEY(acct,svce));"
46 "INSERT INTO \"genp\" VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'4087574952','EnhancedVoicemail',NULL,X'34F32095A0ED6F32637629114439CE38E6FF39ADB591E761D20ED23F9FACF639258DA4F12454FD4D0189C0D39AAA9227');"
47 "INSERT INTO \"genp\" VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'TlalocNet','AirPort',NULL,X'52E24441994D93D18F344DDF6A7F1F6EC43A63BCEB5F89B02FEBEEAAE108BB4933EAE73A0FB615F693C70BCFBCF034BE74BDF0280ECBEB357EEFA3B7EF03060B');"
48 "INSERT INTO \"genp\" VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'weasels','AirPort',NULL,X'3FAD49851913382FBC92C9EB90D90D82A74B1DABB5F726648898B2FA2FBA405AA0B9D95D9837BBFF0F9B7C29954973249AA066F9F8AA68D79552970C687A7DA6');"
49 "CREATE TABLE inet(cdat REAL,mdat REAL,desc BLOB,icmt BLOB,crtr INTEGER,type INTEGER,scrp INTEGER,labl BLOB,alis BLOB,invi INTEGER,nega INTEGER,cusi INTEGER,prot BLOB,acct BLOB NOT NULL DEFAULT '',sdmn BLOB NOT NULL DEFAULT '',srvr BLOB NOT NULL DEFAULT '',ptcl INTEGER NOT NULL DEFAULT 0,atyp BLOB NOT NULL DEFAULT '',port INTEGER NOT NULL DEFAULT 0,path BLOB NOT NULL DEFAULT '',data BLOB,PRIMARY KEY(acct,sdmn,srvr,ptcl,atyp,port,path));"
50 "INSERT INTO \"inet\" VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'mb.7766@gmail.com','','imap.gmail.com','imap','',143,'',X'0029D7AFBF0000E0E386C8654070569B2DF1D7DC2D641AA29223297EC9E8AD86ED91CA6DEE3D2DA0FABD8F05DE5A7AD4CC46B134A211472B6DE50595EACAC149');"
51 "INSERT INTO \"inet\" VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'brouwer','','phonehome.apple.com','imap','',143,'',X'BB373BAE840427C5E1247540ADA559AB14DF3788906B786498A8E1CFF4B4C596634E4A4C7F9C55EA1B646163AFCDADA8');"
52 "INSERT INTO \"inet\" VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'mb.7766@gmail.com','','smtp.gmail.com','smtp','',25,'',X'042C08A4AECD3957822F531A602734F07B89DABA3BA6629ECEFE10E264C12635F83EFBB1707C6B39FB20CCE0200D8997B690FBB0B92911BFE9B2D1E05B1CD5F5');"
53 "INSERT INTO \"inet\" VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'brouwer','','phonehome.apple.com','smtp','',25,'',X'25B0775265ADC808B8AFB2F2602C44B13F5ECC1F04B1D5E6EAE1B803446F3A817CCF8401416FE673CE366E25FACF5C55');"
54 "CREATE TABLE cert(ctyp INTEGER NOT NULL DEFAULT 0,cenc INTEGER,labl BLOB,alis BLOB,subj BLOB,issr BLOB NOT NULL DEFAULT '',slnr BLOB NOT NULL DEFAULT '',skid BLOB,pkhh BLOB,data BLOB,PRIMARY KEY(ctyp,issr,slnr));"
55 "CREATE TABLE keys(kcls INTEGER NOT NULL DEFAULT 0,labl BLOB,alis BLOB,perm INTEGER,priv INTEGER,modi INTEGER,klbl BLOB NOT NULL DEFAULT '',atag BLOB NOT NULL DEFAULT '',crtr INTEGER NOT NULL DEFAULT 0,type INTEGER NOT NULL DEFAULT 0,bsiz INTEGER NOT NULL DEFAULT 0,esiz INTEGER NOT NULL DEFAULT 0,sdat REAL NOT NULL DEFAULT 0,edat REAL NOT NULL DEFAULT 0,sens INTEGER,asen INTEGER,extr INTEGER,next INTEGER,encr INTEGER,decr INTEGER,drve INTEGER,sign INTEGER,vrfy INTEGER,snrc INTEGER,vyrc INTEGER,wrap INTEGER,unwp INTEGER,data BLOB,PRIMARY KEY(kcls,klbl,atag,crtr,type,bsiz,esiz,sdat,edat));"
56 "CREATE INDEX ialis ON cert(alis);"
57 "CREATE INDEX isubj ON cert(subj);"
58 "CREATE INDEX iskid ON cert(skid);"
59 "CREATE INDEX ipkhh ON cert(pkhh);"
60 "CREATE INDEX ikcls ON keys(kcls);"
61 "CREATE INDEX iklbl ON keys(klbl);"
62 "CREATE INDEX iencr ON keys(encr);"
63 "CREATE INDEX idecr ON keys(decr);"
64 "CREATE INDEX idrve ON keys(drve);"
65 "CREATE INDEX isign ON keys(sign);"
66 "CREATE INDEX ivrfy ON keys(vrfy);"
67 "CREATE INDEX iwrap ON keys(wrap);"
68 "CREATE INDEX iunwp ON keys(unwp);"
69 "COMMIT;";
70
71 #ifdef NO_SERVER
72 static void ensureKeychainExists(void) {
73 CFDictionaryRef query = CFDictionaryCreateForCFTypes(0, kSecClass,kSecClassInternetPassword, NULL);
74 CFTypeRef results = NULL;
75 is_status(SecItemCopyMatching(query, &results), errSecItemNotFound, "expected nothing got %@", results);
76 CFReleaseNull(query);
77 CFReleaseNull(results);
78 }
79 #endif
80
81 /* Test basic add delete update copy matching stuff. */
82 static void tests(void)
83 {
84 #ifndef NO_SERVER
85 plan_skip_all("No testing against server.");
86 #else
87 char *keychain_name;
88
89 ensureKeychainExists();
90
91 CFStringRef dbPath = __SecKeychainCopyPath();
92 keychain_name = CFStringToCString(dbPath);
93 CFRelease(dbPath);
94
95 /* delete the keychain file, and let sqllite recreate it */
96 ok_unix(unlink(keychain_name), "delete keychain file");
97
98 sqlite3 *db;
99 is(sqlite3_open(keychain_name, &db), SQLITE_OK, "create keychain");
100 is(sqlite3_exec(db, create_db_sql, NULL, NULL, NULL), SQLITE_OK,
101 "populate keychain");
102 free(keychain_name);
103
104 SecKeychainDbReset(NULL);
105
106 int v_eighty = 80;
107 CFNumberRef eighty = CFNumberCreate(NULL, kCFNumberSInt32Type, &v_eighty);
108 const char *v_data = "test";
109 CFDataRef pwdata = CFDataCreate(NULL, (UInt8 *)v_data, strlen(v_data));
110 CFMutableDictionaryRef query = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
111 CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);
112 CFDictionaryAddValue(query, kSecAttrServer, CFSTR("members.spamcop.net"));
113 CFDictionaryAddValue(query, kSecAttrAccount, CFSTR("smith"));
114 CFDictionaryAddValue(query, kSecAttrPort, eighty);
115 CFDictionaryAddValue(query, kSecAttrProtocol, kSecAttrProtocolHTTP);
116 CFDictionaryAddValue(query, kSecAttrAuthenticationType, kSecAttrAuthenticationTypeDefault);
117 CFDictionaryAddValue(query, kSecValueData, pwdata);
118 ok_status(SecItemAdd(query, NULL), "add internet password");
119 is_status(SecItemAdd(query, NULL), errSecDuplicateItem,
120 "add internet password again");
121
122 ok_status(SecItemCopyMatching(query, NULL), "Found the item we added");
123
124 ok_status(SecItemDelete(query), "Deleted the item we added");
125
126 CFReleaseSafe(eighty);
127 CFReleaseSafe(pwdata);
128 CFReleaseSafe(query);
129 #endif
130 }
131
132 int si_30_keychain_upgrade(int argc, char *const *argv)
133 {
134 plan_tests(8);
135
136 tests();
137
138 return 0;
139 }