]> git.saurik.com Git - apple/security.git/blob - OSX/sec/Security/Regressions/secitem/si-69-keydesc.c
Security-57740.60.18.tar.gz
[apple/security.git] / OSX / sec / Security / Regressions / secitem / si-69-keydesc.c
1 /*
2 * Copyright (c) 2013-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 <Security/SecCMS.h>
26 #include <Security/SecItem.h>
27 #include <Security/SecKey.h>
28 #include <Security/SecItemPriv.h>
29 #include <Security/SecInternal.h>
30 #include <Security/SecPolicyPriv.h>
31 #include <Security/SecBasePriv.h>
32 #include <Security/SecRSAKey.h>
33 #include <Security/SecECKey.h>
34 #include <Security/SecCertificatePriv.h>
35 #include <Security/SecIdentityPriv.h>
36 #include <Security/SecCertificateRequest.h>
37
38 #include <CommonCrypto/CommonDigest.h>
39 #include <CommonCrypto/CommonDigestSPI.h>
40
41 #include <corecrypto/ccec.h>
42
43 #include <CoreFoundation/CoreFoundation.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include "AssertMacros.h"
47 #include "Security_regressions.h"
48
49
50 unsigned char rsaPubKey[] = {
51 0x30, 0x68, 0x02, 0x61, 0x00, 0xc0, 0x63, 0x42, 0xb4, 0xf0, 0x6f, 0x2c, 0xda, 0x71, 0xef, 0x9d, 0x9d, 0x3e, 0x3e, 0x93, 0xc9, 0xd4, 0x2e, 0xe5, 0x62, 0x32, 0x6a, 0xbb, 0xeb, 0x34, 0x57, 0xeb, 0x8a, 0xf8, 0x62, 0xf9, 0xc0, 0x1c, 0x14, 0x54, 0x99, 0x04, 0x6b, 0x19, 0x92, 0xdd, 0xa3, 0x7d, 0x8c, 0x86, 0x48, 0xc9, 0xa6, 0x03, 0x63, 0xf8, 0xab, 0x9c, 0xb9, 0x2b, 0x49, 0x4c, 0x53, 0xb1, 0x39, 0xf9, 0x4a, 0xbc, 0x8b, 0xfc, 0xea, 0x93, 0xb6, 0x2e, 0x41, 0x1f, 0x21, 0x50, 0xca, 0xf7, 0x99, 0xc7, 0x77, 0x73, 0x03, 0x14, 0x58, 0x51, 0x1a, 0xa7, 0x68, 0xe5, 0x8d, 0x68, 0x6b, 0xca, 0x33, 0x4f, 0xcc, 0x6b, 0x41, 0x02, 0x03, 0x01, 0x00, 0x01
52 };
53
54 unsigned char ecPubKey[] = {
55 0x04, 0x01, 0x41, 0x34, 0x87, 0xfd, 0xe1, 0x51, 0x5d, 0x29, 0x12, 0x07, 0xc2, 0x57, 0x54, 0x19, 0xd2, 0xd9, 0x18, 0x95, 0x07, 0x17, 0x8a, 0xf7, 0x2d, 0x2b, 0xf9, 0xbc, 0xe6, 0x1b, 0xe7, 0x81, 0x35, 0x13, 0x5f, 0x1d, 0xfa, 0xed, 0x7e, 0x70, 0x2b, 0xcd, 0x01, 0xa0, 0xaa, 0x7f, 0xe4, 0x0f, 0x4e, 0x19, 0x56, 0xb0, 0x15, 0xfb, 0xd8, 0xc9, 0xe7, 0x48, 0xcf, 0xc7, 0x5e, 0xe8, 0xcc, 0x74, 0x34, 0x61, 0xa5, 0x01, 0x02, 0x67, 0x03, 0x16, 0xce, 0x3d, 0x31, 0x37, 0x9c, 0x0b, 0x03, 0x65, 0x94, 0xaa, 0xd0, 0x1d, 0xa9, 0x5a, 0xe3, 0x0a, 0xf9, 0x82, 0xef, 0x43, 0x75, 0x5b, 0x46, 0x52, 0x6c, 0x0a, 0x02, 0x3f, 0xc3, 0xd3, 0x42, 0x0d, 0xa7, 0x90, 0x8c, 0x4b, 0x15, 0x88, 0x89, 0x24, 0xed, 0x91, 0x0a, 0xa1, 0x20, 0x0d, 0x82, 0xed, 0x87, 0x8c, 0x98, 0x8e, 0xbe, 0xbc, 0xa3, 0xa7, 0xca, 0x50, 0x2d, 0x71, 0x73
56 };
57
58 const char *rsaKeyDescription = "<SecKeyRef algorithm id: 1, key type: RSAPublicKey, version: 4, block size: 768 bits, exponent: {hex: 10001, decimal: 65537}, modulus: C06342B4F06F2CDA71EF9D9D3E3E93C9D42EE562326ABBEB3457EB8AF862F9C01C145499046B1992DDA37D8C8648C9A60363F8AB9CB92B494C53B139F94ABC8BFCEA93B62E411F2150CAF799C77773031458511AA768E58D686BCA334FCC6B41";
59
60 const char *ecKeyDescription = "<SecKeyRef curve type: kSecECCurveSecp521r1, algorithm id: 3, key type: ECPublicKey, version: 4, block size: 528 bits, y: 0102670316CE3D31379C0B036594AAD01DA95AE30AF982EF43755B46526C0A023FC3D3420DA7908C4B15888924ED910AA1200D82ED878C988EBEBCA3A7CA502D7173, x: 01413487FDE1515D291207C2575419D2D9189507178AF72D2BF9BCE61BE78135135F1DFAED7E702BCD01A0AA7FE40F4E1956B015FBD8C9E748CFC75EE8CC743461A5";
61
62 static void testECKeyDesc() {
63
64 SecKeyRef pubKey = NULL;
65 CFStringRef pubRef = NULL;
66 long pubLength = 0;
67
68 pubKey = SecKeyCreateECPublicKey(kCFAllocatorDefault, ecPubKey, sizeof(ecPubKey), kSecKeyEncodingBytes);
69 require_quiet( pubKey, fail);
70
71 pubRef = CFCopyDescription(pubKey);
72 require_quiet(pubRef, fail);
73
74 pubLength = CFStringGetLength(pubRef)+1;
75 char *publicDescription = (char*)malloc(pubLength);
76
77 if(false == CFStringGetCString(pubRef, publicDescription, pubLength, kCFStringEncodingUTF8))
78 {
79 free(publicDescription);
80 goto fail;
81 }
82
83 ok_status(strncmp(ecKeyDescription, publicDescription, strlen(ecKeyDescription)-17), "ec key description");
84 free(publicDescription);
85
86 fail:
87 CFReleaseSafe(pubRef);
88 CFReleaseSafe(pubKey);
89
90 }
91
92 /*
93 * tests the description for
94 * RSA keypairs
95 */
96
97
98 static void testRSAKeyDesc()
99 {
100 SecKeyRef pubKey = NULL;
101 CFStringRef pubRef = NULL;
102 long pubLength = 0;
103
104 pubKey = SecKeyCreateRSAPublicKey(kCFAllocatorDefault, rsaPubKey, sizeof(rsaPubKey), kSecKeyEncodingBytes);
105 require_quiet( pubKey, fail);
106
107 pubRef = CFCopyDescription(pubKey);
108 require_quiet(pubRef, fail);
109
110 pubLength = CFStringGetLength(pubRef)+1;
111 char *publicDescription = (char*)malloc(pubLength);
112 require_quiet(publicDescription != NULL, fail);
113
114 if(false == CFStringGetCString(pubRef, publicDescription, pubLength, kCFStringEncodingUTF8))
115 {
116 free(publicDescription);
117 goto fail;
118 }
119
120 ok_status(strncmp(rsaKeyDescription, publicDescription, strlen(rsaKeyDescription)-17), "rsa key descriptions don't match: %s %s", rsaKeyDescription, publicDescription);
121 free(publicDescription);
122
123 fail:
124 CFReleaseSafe(pubRef);
125 CFReleaseSafe(pubKey);
126
127 }
128
129 static void tests(void)
130 {
131 //test rsa public key description
132 testRSAKeyDesc();
133
134 //test ec public key description
135 testECKeyDesc();
136
137 }
138
139 int si_69_keydesc(int argc, char *const *argv)
140 {
141
142 plan_tests(2);
143
144 tests();
145
146 return 0;
147 }