]> git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/dbVerifyKey/dbVerifyKey.cpp
Security-57031.10.10.tar.gz
[apple/security.git] / SecurityTests / cspxutils / dbVerifyKey / dbVerifyKey.cpp
1 /* Copyright (c) 2004-2005,2008 Apple Inc.
2 *
3 * dbVerifyKey.cpp - verify that specified DB has exactly one key of specified
4 * algorithm, class, and key size - and no other keys.
5 */
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <time.h>
9 #include <strings.h>
10 #include <ctype.h>
11 #include <Security/cssm.h>
12 #include <Security/cssmapple.h>
13 #include "cspwrap.h"
14 #include "common.h"
15 #include "cspdlTesting.h"
16
17
18 static void usage(char **argv)
19 {
20 printf("usage: %s dbFileName alg class keysize [options]\n", argv[0]);
21 printf(" alg : rsa|dsa|dh|ecdsa\n");
22 printf(" class : priv|pub\n");
23 printf("Options:\n");
24 printf(" -q quiet\n");
25 exit(1);
26 }
27
28 static const char *recordTypeStr(
29 CSSM_DB_RECORDTYPE recordType)
30 {
31 static char unk[100];
32
33 switch(recordType) {
34 case CSSM_DL_DB_RECORD_PRIVATE_KEY:
35 return "Private Key";
36 case CSSM_DL_DB_RECORD_PUBLIC_KEY:
37 return "Public Key";
38 case CSSM_DL_DB_RECORD_SYMMETRIC_KEY:
39 return "Symmetric Key";
40 default:
41 sprintf(unk, "**Unknown record type %u\n", (unsigned)recordType);
42 return unk;
43 }
44 }
45
46 /*
47 * Search for specified record type; verify there is exactly one or zero
48 * of them as specified.
49 * Verify key algorthm and key size. Returns nonzero on error.
50 */
51 static int doVerify(
52 CSSM_DL_DB_HANDLE dlDbHand,
53 unsigned numRecords, // zero or one
54 CSSM_DB_RECORDTYPE recordType,
55 uint32 keySize,
56 CSSM_ALGORITHMS keyAlg)
57 {
58 CSSM_QUERY query;
59 CSSM_DB_UNIQUE_RECORD_PTR record = NULL;
60 CSSM_RETURN crtn;
61 CSSM_HANDLE resultHand;
62 CSSM_DB_RECORD_ATTRIBUTE_DATA recordAttrs;
63
64 /* no predicates, all records of specified type, no attrs, get the key */
65 query.RecordType = recordType;
66 query.Conjunctive = CSSM_DB_NONE;
67 query.NumSelectionPredicates = 0;
68 query.QueryLimits.TimeLimit = 0; // FIXME - meaningful?
69 query.QueryLimits.SizeLimit = 1; // FIXME - meaningful?
70 query.QueryFlags = 0; // CSSM_QUERY_RETURN_DATA; // FIXME - used?
71
72 recordAttrs.DataRecordType = recordType;
73 recordAttrs.NumberOfAttributes = 0;
74 recordAttrs.AttributeData = NULL;
75
76 CSSM_DATA recordData = {0, NULL};
77
78 crtn = CSSM_DL_DataGetFirst(dlDbHand,
79 &query,
80 &resultHand,
81 &recordAttrs,
82 &recordData,
83 &record);
84 switch(crtn) {
85 case CSSM_OK:
86 if(numRecords == 0) {
87 printf("***Expected zero records of type %s, found one\n",
88 recordTypeStr(recordType));
89 CSSM_DL_FreeUniqueRecord(dlDbHand, record);
90 CSSM_DL_DataAbortQuery(dlDbHand, resultHand);
91 return 1;
92 }
93 break; // proceed
94 case CSSMERR_DL_ENDOFDATA:
95 if(numRecords == 0) {
96 /* cool */
97 return 0;
98 }
99 printf("**Error: no records of type %s found\n",
100 recordTypeStr(recordType));
101 return 1;
102 default:
103 printError("DataGetFirst", crtn);
104 return 1;
105 }
106
107 CSSM_KEY_PTR theKey = (CSSM_KEY_PTR)recordData.Data;
108 int ourRtn = 0;
109 CSSM_KEYHEADER &hdr = theKey->KeyHeader;
110 if(hdr.AlgorithmId != keyAlg) {
111 printf("***Algorithm mismatch: expect %u, got %u\n",
112 (unsigned)keyAlg, (unsigned)hdr.AlgorithmId);
113 ourRtn++;
114 }
115 if(hdr.LogicalKeySizeInBits != keySize) {
116 printf("***Key Size: expect %u, got %u\n",
117 (unsigned)keySize, (unsigned)hdr.LogicalKeySizeInBits);
118 ourRtn++;
119 }
120 CSSM_DL_FreeUniqueRecord(dlDbHand, record);
121
122 /* see if there are any more */
123 crtn = CSSM_DL_DataGetNext(dlDbHand,
124 resultHand,
125 &recordAttrs,
126 NULL,
127 &record);
128 if(crtn == CSSM_OK) {
129 printf("***More than 1 record of type %s found\n",
130 recordTypeStr(recordType));
131 ourRtn++;
132 CSSM_DL_FreeUniqueRecord(dlDbHand, record);
133 }
134 CSSM_DL_DataAbortQuery(dlDbHand, resultHand);
135 return ourRtn;
136 }
137
138 int main(
139 int argc,
140 char **argv)
141 {
142 int arg;
143 char *argp;
144 char *dbFileName;
145 CSSM_ALGORITHMS keyAlg;
146 CSSM_DB_RECORDTYPE recordType;
147 uint32 keySize;
148 CSSM_DL_DB_HANDLE dlDbHand;
149 CSSM_BOOL quiet = CSSM_FALSE;
150 CSSM_RETURN crtn = CSSM_OK;
151
152 if(argc < 5) {
153 usage(argv);
154 }
155 dbFileName = argv[1];
156
157 /* key algorithm */
158 if(!strcmp(argv[2], "rsa")) {
159 keyAlg = CSSM_ALGID_RSA;
160 }
161 else if(!strcmp(argv[2], "dsa")) {
162 keyAlg = CSSM_ALGID_DSA;
163 }
164 else if(!strcmp(argv[2], "dh")) {
165 keyAlg = CSSM_ALGID_DH;
166 }
167 else if(!strcmp(argv[2], "ecdsa")) {
168 keyAlg = CSSM_ALGID_ECDSA;
169 }
170 else {
171 usage(argv);
172 }
173
174 /* key class */
175 if(!strcmp(argv[3], "priv")) {
176 recordType = CSSM_DL_DB_RECORD_PRIVATE_KEY;
177 }
178 else if(!strcmp(argv[3], "pub")) {
179 recordType = CSSM_DL_DB_RECORD_PUBLIC_KEY;
180 }
181 else {
182 usage(argv);
183 }
184
185 keySize = atoi(argv[4]);
186
187 for(arg=5; arg<argc; arg++) {
188 argp = argv[arg];
189 if(!strcmp(argp, "-q")) {
190 quiet = CSSM_TRUE;
191 }
192 else {
193 usage(argv);
194 }
195 }
196
197 dlDbHand.DLHandle = dlStartup();
198 if(dlDbHand.DLHandle == 0) {
199 exit(1);
200 }
201 crtn = dbCreateOpen(dlDbHand.DLHandle, dbFileName,
202 CSSM_FALSE, CSSM_FALSE, NULL, &dlDbHand.DBHandle);
203 if(crtn) {
204 exit(1);
205 }
206
207 if(doVerify(dlDbHand, 1, recordType, keySize, keyAlg)) {
208 return 1;
209 }
210 if(doVerify(dlDbHand, 0,
211 (recordType == CSSM_DL_DB_RECORD_PRIVATE_KEY) ?
212 CSSM_DL_DB_RECORD_PUBLIC_KEY : CSSM_DL_DB_RECORD_PRIVATE_KEY,
213 keySize, keyAlg)) {
214 return 1;
215 }
216 if(!quiet) {
217 printf("...%s verify succussful\n", recordTypeStr(recordType));
218 }
219 CSSM_DL_DbClose(dlDbHand);
220 CSSM_ModuleDetach(dlDbHand.DLHandle);
221 return 0;
222 }