]> git.saurik.com Git - apple/security.git/blob - OSX/sec/securityd/Regressions/secd-67-prefixedKeyIDs.m
Security-59306.11.20.tar.gz
[apple/security.git] / OSX / sec / securityd / Regressions / secd-67-prefixedKeyIDs.m
1 //
2 // secd-67-prefixedKeyIDs.c
3 // Security
4 //
5 // Created by Richard Murphy on 11/1/16.
6 //
7 //
8
9 #include <stdio.h>
10 //
11 // secd-66-account-recovery.c
12 // Security
13 //
14 // Created by Richard Murphy on 10/5/16.
15 //
16 //
17
18 #include <stdio.h>
19
20 /*
21 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
22 *
23 * @APPLE_LICENSE_HEADER_START@
24 *
25 * This file contains Original Code and/or Modifications of Original Code
26 * as defined in and that are subject to the Apple Public Source License
27 * Version 2.0 (the 'License'). You may not use this file except in
28 * compliance with the License. Please obtain a copy of the License at
29 * http://www.opensource.apple.com/apsl/ and read it before using this
30 * file.
31 *
32 * The Original Code and all software distributed under the License are
33 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
34 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
35 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
37 * Please see the License for the specific language governing rights and
38 * limitations under the License.
39 *
40 * @APPLE_LICENSE_HEADER_END@
41 */
42
43
44
45
46 #include <Security/SecBase.h>
47 #include <CoreFoundation/CFDictionary.h>
48 #include "SOSKeyedPubKeyIdentifier.h"
49
50 #include <stdlib.h>
51 #include <unistd.h>
52
53 #include "secd_regressions.h"
54 #include "SOSTestDataSource.h"
55
56 #include "SOSRegressionUtilities.h"
57
58 #include <utilities/SecCFWrappers.h>
59 #include <Security/SecKeyPriv.h>
60
61
62
63 #include "SOSAccountTesting.h"
64 static void tests() {
65 CFErrorRef error = NULL;
66 int keySizeInBits = 256;
67 CFNumberRef kzib = CFNumberCreate(NULL, kCFNumberIntType, &keySizeInBits);
68
69 CFDictionaryRef keyattributes = CFDictionaryCreateForCFTypes(kCFAllocatorDefault,
70 kSecAttrKeyType, kSecAttrKeyTypeEC,
71 kSecAttrKeySizeInBits, kzib,
72 NULL);
73 CFReleaseNull(kzib);
74
75
76 SecKeyRef key = SecKeyCreateRandomKey(keyattributes, &error);
77 CFReleaseNull(keyattributes);
78 SecKeyRef pubKey = SecKeyCreatePublicFromPrivate(key);
79 CFDataRef pubKeyData = NULL;
80 SecKeyCopyPublicBytes(pubKey, &pubKeyData);
81 CFStringRef properPref = CFSTR("RK");
82 CFStringRef shortPref = CFSTR("R");
83 CFStringRef longPref = CFSTR("RKR");
84
85 ok(key, "Made private key");
86 ok(pubKey, "Made public key");
87 ok(pubKeyData, "Made public key data");
88
89 CFStringRef pkidseckey = SOSKeyedPubKeyIdentifierCreateWithSecKey(properPref, pubKey);
90 ok(pkidseckey, "made string");
91 CFStringRef pkidseckeyshort = SOSKeyedPubKeyIdentifierCreateWithSecKey(shortPref, pubKey);
92 ok(!pkidseckeyshort, "didn't make string");
93 CFStringRef pkidseckeylong = SOSKeyedPubKeyIdentifierCreateWithSecKey(longPref, pubKey);
94 ok(!pkidseckeylong, "didn't make string");
95
96 ok(SOSKeyedPubKeyIdentifierIsPrefixed(pkidseckey), "properly prefixed string was made");
97 CFStringRef retPref = SOSKeyedPubKeyIdentifierCopyPrefix(pkidseckey);
98 ok(retPref, "got prefix");
99 ok(CFEqualSafe(retPref, properPref), "prefix matches");
100 CFReleaseNull(retPref);
101 CFStringRef retHpub = SOSKeyedPubKeyIdentifierCopyHpub(pkidseckey);
102 ok(retHpub, "got hash of pubkey");
103
104
105 CFStringRef pkiddata = SOSKeyedPubKeyIdentifierCreateWithData(properPref, pubKeyData);
106 ok(pkiddata, "made string");
107 ok(CFEqualSafe(pkiddata, pkidseckey), "strings match");
108
109 //diag("pkiddata %@", pkiddata);
110 //diag("retPref %@", retPref);
111 CFReleaseNull(retHpub);
112 CFReleaseNull(key);
113 CFReleaseNull(pubKey);
114 CFReleaseNull(pubKeyData);
115 CFReleaseNull(pkidseckey);
116 CFReleaseNull(pkidseckeyshort);
117 CFReleaseNull(pkidseckeylong);
118 CFReleaseNull(pkiddata);
119
120
121 }
122
123 int secd_67_prefixedKeyIDs(int argc, char *const *argv) {
124 plan_tests(12);
125
126 tests();
127 return 0;
128 }
129