2 * Copyright (c) 2006-2014 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@
25 * SecItemSchema.c - CoreFoundation-based constants and functions for
26 access to Security items (certificates, keys, identities, and
30 #include "SecItemSchema.h"
31 #include <securityd/SecDbKeychainItem.h>
34 // MARK Keychain version 6 schema
36 #define __FLAGS(ARG, ...) SECDBFLAGS(__VA_ARGS__)
37 #define SECDBFLAGS(ARG, ...) __FLAGS_##ARG | __FLAGS(__VA_ARGS__)
39 #define SecDbFlags(P,L,I,S,A,D,R,C,H,B,Z,E,N,U,V) (__FLAGS_##P|__FLAGS_##L|__FLAGS_##I|__FLAGS_##S|__FLAGS_##A|__FLAGS_##D|__FLAGS_##R|__FLAGS_##C|__FLAGS_##H|__FLAGS_##B|__FLAGS_##Z|__FLAGS_##E|__FLAGS_##N|__FLAGS_##U|__FLAGS_##V)
42 #define __FLAGS_P kSecDbPrimaryKeyFlag
43 #define __FLAGS_L kSecDbInFlag
44 #define __FLAGS_I kSecDbIndexFlag
45 #define __FLAGS_S kSecDbSHA1ValueInFlag
46 #define __FLAGS_A kSecDbReturnAttrFlag
47 #define __FLAGS_D kSecDbReturnDataFlag
48 #define __FLAGS_R kSecDbReturnRefFlag
49 #define __FLAGS_C kSecDbInCryptoDataFlag
50 #define __FLAGS_H kSecDbInHashFlag
51 #define __FLAGS_B kSecDbInBackupFlag
52 #define __FLAGS_Z kSecDbDefault0Flag
53 #define __FLAGS_E kSecDbDefaultEmptyFlag
54 #define __FLAGS_N kSecDbNotNullFlag
55 #define __FLAGS_U kSecDbInAuthenticatedDataFlag
56 #define __FLAGS_V0 kSecDbSyncPrimaryKeyV0
57 #define __FLAGS_V2 (kSecDbSyncPrimaryKeyV0 | kSecDbSyncPrimaryKeyV2)
59 // ,-------------- P : Part of primary key
60 // / ,------------- L : Stored in local database
61 // / / ,------------ I : Attribute wants an index in the database
62 // / / / ,----------- S : SHA1 hashed attribute value in database (implies L)
63 // / / / / ,---------- A : Returned to client as attribute in queries
64 // / / / / / ,--------- D : Returned to client as data in queries
65 // / / / / / / ,-------- R : Returned to client as ref/persistent ref in queries
66 // / / / / / / / ,------- C : Part of encrypted blob
67 // / / / / / / / / ,------ H : Attribute is part of item SHA1 hash (Implied by C)
68 // / / / / / / / / / ,----- B : Attribute is part of iTunes/iCloud backup bag
69 // / / / / / / / / / / ,---- Z : Attribute has a default value of 0
70 // / / / / / / / / / / / ,--- E : Attribute has a default value of "" or empty data
71 // / / / / / / / / / / / / ,-- N : Attribute must have a value
72 // / / / / / / / / / / / / / ,- U : Attribute is stored in authenticated, but not necessarily encrypted data
73 // / / / / / / / / / / / / / / - S : Sync primpary key version
74 // / / / / / / / / / / / / / / /
75 // | | | | | | | | | | | | | | |
76 // common to all | | | | | | | | | | | | | | |
77 SECDB_ATTR(v6rowid
, "rowid", RowId
, SecDbFlags( ,L
, , , , ,R
, , ,B
, , , , , ), NULL
, NULL
);
78 SECDB_ATTR(v6cdat
, "cdat", CreationDate
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), SecDbKeychainItemCopyCurrentDate
, NULL
);
79 SECDB_ATTR(v6mdat
, "mdat",ModificationDate
,SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), SecDbKeychainItemCopyCurrentDate
, NULL
);
80 SECDB_ATTR(v6labl
, "labl", Blob
, SecDbFlags( ,L
, ,S
,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
81 SECDB_ATTR(v6data
, "data", EncryptedData
, SecDbFlags( ,L
, , , , , , , ,B
, , , , , ), SecDbKeychainItemCopyEncryptedData
, NULL
);
82 SECDB_ATTR(v6agrp
, "agrp", String
, SecDbFlags(P
,L
, , ,A
, , , ,H
, , , ,N
,U
,V0
), NULL
, NULL
);
83 SECDB_ATTR(v6pdmn
, "pdmn", Access
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
84 SECDB_ATTR(v6sync
, "sync", Sync
, SecDbFlags(P
,L
,I
, ,A
, , , ,H
, ,Z
, ,N
,U
,V0
), NULL
, NULL
);
85 SECDB_ATTR(v6tomb
, "tomb", Tomb
, SecDbFlags( ,L
, , , , , , ,H
, ,Z
, ,N
,U
, ), NULL
, NULL
);
86 SECDB_ATTR(v6sha1
, "sha1", SHA1
, SecDbFlags( ,L
,I
, ,A
, ,R
, , , , , , , , ), SecDbKeychainItemCopySHA1
, NULL
);
87 SECDB_ATTR(v6accc
, "accc", AccessControl
, SecDbFlags( , , , ,A
, , , , , , , , , , ), NULL
, NULL
);
88 SECDB_ATTR(v6v_Data
, "v_Data", Data
, SecDbFlags( , , , , ,D
, ,C
,H
, , , , , , ), NULL
, NULL
);
89 SECDB_ATTR(v6v_pk
, "v_pk", PrimaryKey
, SecDbFlags( , , , , , , , , , , , , , , ), SecDbKeychainItemCopyPrimaryKey
, NULL
);
90 SECDB_ATTR(v7vwht
, "vwht", String
, SecDbFlags(P
,L
, , ,A
, , , ,H
, , , , ,U
,V2
), NULL
, NULL
);
91 SECDB_ATTR(v7tkid
, "tkid", String
, SecDbFlags(P
,L
, , ,A
, , , ,H
, , , , ,U
,V2
), NULL
, NULL
);
92 SECDB_ATTR(v7utomb
, "u_Tomb", UTomb
, SecDbFlags( , , , , , , , , , , , , , , ), NULL
, NULL
);
93 SECDB_ATTR(v8musr
, "musr", UUID
, SecDbFlags(P
,L
,I
, , , , , , , , , ,N
,U
, ), NULL
, NULL
);
94 // genp and inet and keys | | | | | | | | | | | | | | |
95 SECDB_ATTR(v6crtr
, "crtr", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
96 SECDB_ATTR(v6alis
, "alis", Blob
, SecDbFlags( ,L
, ,S
,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
97 // genp and inet | | | | | | | | | | | | | | |
98 SECDB_ATTR(v6desc
, "desc", Blob
, SecDbFlags( ,L
, ,S
,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
99 SECDB_ATTR(v6icmt
, "icmt", Blob
, SecDbFlags( ,L
, ,S
,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
100 SECDB_ATTR(v6type
, "type", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
101 SECDB_ATTR(v6invi
, "invi", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
102 SECDB_ATTR(v6nega
, "nega", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
103 SECDB_ATTR(v6cusi
, "cusi", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
104 SECDB_ATTR(v6prot
, "prot", Blob
, SecDbFlags( ,L
, ,S
,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
105 SECDB_ATTR(v6scrp
, "scrp", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
106 SECDB_ATTR(v6acct
, "acct", Blob
, SecDbFlags(P
,L
, ,S
,A
, , ,C
,H
, , ,E
,N
, ,V0
), NULL
, NULL
);
107 // genp only | | | | | | | | | | | | | | |
108 SECDB_ATTR(v6svce
, "svce", Blob
, SecDbFlags(P
,L
, ,S
,A
, , ,C
,H
, , ,E
,N
, ,V0
), NULL
, NULL
);
109 SECDB_ATTR(v6gena
, "gena", Blob
, SecDbFlags( ,L
, ,S
,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
110 // inet only | | | | | | | | | | | | | | |
111 SECDB_ATTR(v6sdmn
, "sdmn", Blob
, SecDbFlags(P
,L
, ,S
,A
, , ,C
,H
, , ,E
,N
, ,V0
), NULL
, NULL
);
112 SECDB_ATTR(v6srvr
, "srvr", Blob
, SecDbFlags(P
,L
, ,S
,A
, , ,C
,H
, , ,E
,N
, ,V0
), NULL
, NULL
);
113 SECDB_ATTR(v6ptcl
, "ptcl", Number
, SecDbFlags(P
,L
, , ,A
, , ,C
,H
, ,Z
, ,N
, ,V0
), NULL
, NULL
);
114 SECDB_ATTR(v6atyp
, "atyp", Blob
, SecDbFlags(P
,L
, ,S
,A
, , ,C
,H
, , ,E
,N
, ,V0
), NULL
, NULL
);
115 SECDB_ATTR(v6port
, "port", Number
, SecDbFlags(P
,L
, , ,A
, , ,C
,H
, ,Z
, ,N
, ,V0
), NULL
, NULL
);
116 SECDB_ATTR(v6path
, "path", Blob
, SecDbFlags(P
,L
, ,S
,A
, , ,C
,H
, , ,E
,N
, ,V0
), NULL
, NULL
);
117 // cert only | | | | | | | | | | | | | | |
118 SECDB_ATTR(v6ctyp
, "ctyp", Number
, SecDbFlags(P
,L
, , ,A
, , ,C
,H
, ,Z
, ,N
, ,V0
), NULL
, NULL
);
119 SECDB_ATTR(v6cenc
, "cenc", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
120 SECDB_ATTR(v6subj
, "subj", Data
, SecDbFlags( ,L
,I
,S
,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
121 SECDB_ATTR(v6issr
, "issr", Data
, SecDbFlags(P
,L
, ,S
,A
, , ,C
,H
, , ,E
,N
, ,V0
), NULL
, NULL
);
122 SECDB_ATTR(v6slnr
, "slnr", Data
, SecDbFlags(P
,L
, ,S
,A
, , ,C
,H
, , ,E
,N
, ,V0
), NULL
, NULL
);
123 SECDB_ATTR(v6skid
, "skid", Data
, SecDbFlags( ,L
,I
,S
,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
124 SECDB_ATTR(v6pkhh
, "pkhh", Data
, SecDbFlags( ,L
,I
, ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
125 // cert attributes that share names with common ones but have different flags
126 SECDB_ATTR(v6certalis
, "alis", Blob
, SecDbFlags( ,L
,I
,S
,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
127 // keys only | | | | | | | | | | | | | | |
128 SECDB_ATTR(v6kcls
, "kcls", Number
, SecDbFlags(P
,L
,I
,S
,A
, , ,C
,H
, ,Z
, ,N
, ,V0
), NULL
, NULL
);
129 SECDB_ATTR(v6perm
, "perm", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
130 SECDB_ATTR(v6priv
, "priv", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
131 SECDB_ATTR(v6modi
, "modi", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
132 SECDB_ATTR(v6klbl
, "klbl", Data
, SecDbFlags(P
,L
,I
, ,A
, , ,C
,H
, , ,E
,N
, ,V0
), NULL
, NULL
);
133 SECDB_ATTR(v6atag
, "atag", Blob
, SecDbFlags(P
,L
, ,S
,A
, , ,C
,H
, , ,E
,N
, ,V0
), NULL
, NULL
);
134 SECDB_ATTR(v6bsiz
, "bsiz", Number
, SecDbFlags(P
,L
, , ,A
, , ,C
,H
, ,Z
, ,N
, ,V0
), NULL
, NULL
);
135 SECDB_ATTR(v6esiz
, "esiz", Number
, SecDbFlags(P
,L
, , ,A
, , ,C
,H
, ,Z
, ,N
, ,V0
), NULL
, NULL
);
136 SECDB_ATTR(v6sdat
, "sdat", Date
, SecDbFlags(P
,L
, , ,A
, , ,C
,H
, ,Z
, ,N
, ,V0
), NULL
, NULL
);
137 SECDB_ATTR(v6edat
, "edat", Date
, SecDbFlags(P
,L
, , ,A
, , ,C
,H
, ,Z
, ,N
, ,V0
), NULL
, NULL
);
138 SECDB_ATTR(v6sens
, "sens", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
139 SECDB_ATTR(v6asen
, "asen", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
140 SECDB_ATTR(v6extr
, "extr", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
141 SECDB_ATTR(v6next
, "next", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
142 SECDB_ATTR(v6encr
, "encr", Number
, SecDbFlags( ,L
,I
, ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
143 SECDB_ATTR(v6decr
, "decr", Number
, SecDbFlags( ,L
,I
, ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
144 SECDB_ATTR(v6drve
, "drve", Number
, SecDbFlags( ,L
,I
, ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
145 SECDB_ATTR(v6sign
, "sign", Number
, SecDbFlags( ,L
,I
, ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
146 SECDB_ATTR(v6vrfy
, "vrfy", Number
, SecDbFlags( ,L
,I
, ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
147 SECDB_ATTR(v6snrc
, "snrc", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
148 SECDB_ATTR(v6vyrc
, "vyrc", Number
, SecDbFlags( ,L
, , ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
149 SECDB_ATTR(v6wrap
, "wrap", Number
, SecDbFlags( ,L
,I
, ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
150 SECDB_ATTR(v6unwp
, "unwp", Number
, SecDbFlags( ,L
,I
, ,A
, , ,C
,H
, , , , , , ), NULL
, NULL
);
151 // keys attributes that share names with common ones but have different flags
152 SECDB_ATTR(v6keytype
, "type", Number
, SecDbFlags(P
,L
, , ,A
, , ,C
,H
, ,Z
, ,N
, ,V0
), NULL
, NULL
);
153 SECDB_ATTR(v6keycrtr
, "crtr", Number
, SecDbFlags(P
,L
, , ,A
, , ,C
,H
, ,Z
, ,N
, ,V0
), NULL
, NULL
);
154 // | | | | | | | | | | | | | | |
155 SECDB_ATTR(v6version
, "version", Number
, SecDbFlags(P
,L
, , , , , , , , , , ,N
, , ), NULL
, NULL
);
157 const SecDbClass genp_class
= {
158 .name
= CFSTR("genp"),
194 const SecDbClass inet_class
= {
195 .name
= CFSTR("inet"),
235 const SecDbClass cert_class
= {
236 .name
= CFSTR("cert"),
267 const SecDbClass keys_class
= {
268 .name
= CFSTR("keys"),
317 const SecDbClass tversion_class
= {
318 .name
= CFSTR("tversion"),
325 /* An identity which is really a cert + a key, so all cert and keys attrs are
327 const SecDbClass identity_class
= {
328 .name
= CFSTR("idnt"),
334 /* Version 9 (iOS 9.3 and OSX 10.11.5) database schema
335 * Same contents as v8 tables; table names changed to force upgrade
336 * and correct default values in table. */
337 const SecDbSchema v9_schema
= {
349 // Version 8 (Internal release iOS 9.3 and OSX 10.11.5) database schema
350 const SecDbClass v8genp_class
= {
351 .name
= CFSTR("genp8"),
387 const SecDbClass v8inet_class
= {
388 .name
= CFSTR("inet8"),
428 const SecDbClass v8cert_class
= {
429 .name
= CFSTR("cert8"),
460 const SecDbClass v8keys_class
= {
461 .name
= CFSTR("keys8"),
510 const SecDbSchema v8_schema
= {
522 // Version 7 (iOS 9 and OSX 10.11) database schema
523 const SecDbClass v7genp_class
= {
524 .name
= CFSTR("genp7"),
559 const SecDbClass v7inet_class
= {
560 .name
= CFSTR("inet7"),
599 const SecDbClass v7cert_class
= {
600 .name
= CFSTR("cert7"),
630 const SecDbClass v7keys_class
= {
631 .name
= CFSTR("keys7"),
680 const SecDbSchema v7_schema
= {
693 // Version 6 (iOS 7 and OSX 10.9) database schema
694 static const SecDbClass v6genp_class
= {
695 .name
= CFSTR("genp6"),
727 static const SecDbClass v6inet_class
= {
728 .name
= CFSTR("inet6"),
764 static const SecDbClass v6cert_class
= {
765 .name
= CFSTR("cert6"),
792 static const SecDbClass v6keys_class
= {
793 .name
= CFSTR("keys6"),
838 static const SecDbSchema v6_schema
= {
851 // Version 5 (iOS 5 & iOS 6) database schema.
852 static const SecDbClass v5genp_class
= {
853 .name
= CFSTR("genp5"),
880 static const SecDbClass v5inet_class
= {
881 .name
= CFSTR("inet5"),
912 static const SecDbClass v5cert_class
= {
913 .name
= CFSTR("cert5"),
935 static const SecDbClass v5keys_class
= {
936 .name
= CFSTR("keys5"),
976 static const SecDbSchema v5_schema
= {
988 const SecDbSchema
*kc_schemas
[] = {