2 * Copyright (c) 2016 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 #import <Foundation/Foundation.h>
27 #import <Security/SecKeyPriv.h>
28 #import <Security/oidsalg.h>
30 #include "shared_regressions.h"
32 static NSData *decryptAndUnpad(SecKeyRef privateKey, SecKeyAlgorithm algorithm, NSData *ciphertext, NSError **error) {
33 NSData *plaintext = CFBridgingRelease(SecKeyCreateDecryptedData(privateKey, algorithm, (CFDataRef)ciphertext, (void *)error));
34 if (plaintext != nil && [(__bridge id)algorithm isEqual:(id)kSecKeyAlgorithmRSAEncryptionRaw]) {
35 NSRange range = NSMakeRange(0, plaintext.length);
36 while (((const UInt8 *)plaintext.bytes)[range.location] == 0x00 && range.location < plaintext.length) {
40 plaintext = [plaintext subdataWithRange:range];
45 static void test_encrypt_run(SecKeyRef privateKey, SecKeyRef publicKey, SecKeyRef iosPrivateKey, SecKeyRef iosPublicKey, SecKeyAlgorithm algorithm) {
46 NSData *original = [NSData dataWithBytes:"encrypt" length:7], *plaintext;
50 NSData *ciphertext = CFBridgingRelease(SecKeyCreateEncryptedData(publicKey, algorithm, (CFDataRef)original, (void *)&error));
51 ok(ciphertext != nil, "RSA encrypt (native) succeeded (error: %@, key %@)", error, publicKey);
54 NSData *iosCiphertext = CFBridgingRelease(SecKeyCreateEncryptedData(iosPublicKey, algorithm, (CFDataRef)original, (void *)&error));
55 ok(iosCiphertext != nil, "RSA encrypt (native) succeeded (error: %@, key %@)", error, iosPublicKey);
58 plaintext = decryptAndUnpad(privateKey, algorithm, ciphertext, &error);
59 ok(plaintext != nil, "RSA decrypt (native) succeeded (error: %@, key %@)", error, privateKey);
60 ok([plaintext isEqual:original], "(native -> native) plaintext equals original (%@ : %@)", original, plaintext);
63 plaintext = decryptAndUnpad(privateKey, algorithm, iosCiphertext, &error);
64 ok(plaintext != nil, "RSA decrypt (native) succeeded (error: %@, key %@)", error, privateKey);
65 ok([plaintext isEqual:original], "(ios -> native) plaintext equals original (%@ : %@)", original, plaintext);
68 plaintext = decryptAndUnpad(iosPrivateKey, algorithm, ciphertext, &error);
69 ok(plaintext != nil, "RSA decrypt (ios) succeeded (error: %@, key %@)", error, privateKey);
70 ok([plaintext isEqual:original], "(native -> ios) plaintext equals original (%@ : %@)", original, plaintext);
73 plaintext = decryptAndUnpad(iosPrivateKey, algorithm, iosCiphertext, &error);
74 ok(plaintext != nil, "RSA decrypt (ios) succeeded (error: %@, key %@)", error, privateKey);
75 ok([plaintext isEqual:original], "(ios -> ios) plaintext equals original (%@ : %@)", original, plaintext);
77 static const int TestCountEncryptRun = 10;
79 static void test_encrypt_keypair_run(int keySizeInBits, NSArray *algorithms, NSArray *failAlgorithms) {
81 NSDictionary *params = @{(id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA, (id)kSecAttrKeySizeInBits: @(keySizeInBits)};
84 id privateKey = CFBridgingRelease(SecKeyCreateRandomKey((CFDictionaryRef)params, (void *)&error));
85 ok(privateKey != nil, "generate private key (error %@)", error);
87 id publicKey = CFBridgingRelease(SecKeyCopyPublicKey((SecKeyRef)privateKey));
88 ok(publicKey != nil, "get public key");
90 NSData *data = CFBridgingRelease(SecKeyCopyExternalRepresentation((SecKeyRef)privateKey, NULL));
91 NSDictionary *attrs = CFBridgingRelease(SecKeyCopyAttributes((SecKeyRef)privateKey));
93 id iosPrivateKey = CFBridgingRelease(SecKeyCreateWithData((CFDataRef)data, (CFDictionaryRef)attrs, (void *)&error));
94 ok(iosPrivateKey != nil, "get private key created from data");
96 data = CFBridgingRelease(SecKeyCopyExternalRepresentation((SecKeyRef)publicKey, NULL));
97 attrs = CFBridgingRelease(SecKeyCopyAttributes((SecKeyRef)publicKey));
99 id iosPublicKey = CFBridgingRelease(SecKeyCreateWithData((CFDataRef)data, (CFDictionaryRef)attrs, (void *)&error));
100 ok(iosPublicKey != nil, "get public key created from data");
102 for (id algorithm in algorithms) {
103 test_encrypt_run((__bridge SecKeyRef)privateKey, (__bridge SecKeyRef)publicKey,
104 (__bridge SecKeyRef)iosPrivateKey, (__bridge SecKeyRef)iosPublicKey,
105 (__bridge SecKeyAlgorithm)algorithm);
108 for (id algorithm in failAlgorithms) {
110 NSData *data = CFBridgingRelease(SecKeyCreateEncryptedData((SecKeyRef)publicKey, (SecKeyAlgorithm)algorithm, (CFDataRef)[NSData data], (void *)&error));
111 ok(data == nil && error.code == errSecParam, "incorrect algorithm refused");
114 static const int TestCountEncryptKeypairRun = 4;
116 static void test_encryption() {
117 test_encrypt_keypair_run(1024,
119 (id)kSecKeyAlgorithmRSAEncryptionRaw,
120 (id)kSecKeyAlgorithmRSAEncryptionPKCS1,
121 (id)kSecKeyAlgorithmRSAEncryptionOAEPSHA1,
122 (id)kSecKeyAlgorithmRSAEncryptionOAEPSHA224,
123 (id)kSecKeyAlgorithmRSAEncryptionOAEPSHA256,
124 (id)kSecKeyAlgorithmRSAEncryptionOAEPSHA384,
127 (id)kSecKeyAlgorithmRSAEncryptionOAEPSHA512,
130 test_encrypt_keypair_run(2048,
132 (id)kSecKeyAlgorithmRSAEncryptionRaw,
133 (id)kSecKeyAlgorithmRSAEncryptionPKCS1,
134 (id)kSecKeyAlgorithmRSAEncryptionOAEPSHA1,
135 (id)kSecKeyAlgorithmRSAEncryptionOAEPSHA224,
136 (id)kSecKeyAlgorithmRSAEncryptionOAEPSHA256,
137 (id)kSecKeyAlgorithmRSAEncryptionOAEPSHA384,
138 (id)kSecKeyAlgorithmRSAEncryptionOAEPSHA512,
143 static const int TestCountEncryption =
144 TestCountEncryptKeypairRun + (TestCountEncryptRun * 6) + (1 * 1) +
145 TestCountEncryptKeypairRun + (TestCountEncryptRun * 7) + (1 * 0);
147 static void test_bad_input(NSInteger keySizeInBits, NSInteger inputSize, SecKeyAlgorithm algorithm) {
149 NSDictionary *params = @{(id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA, (id)kSecAttrKeySizeInBits: @(keySizeInBits)};
152 id privateKey = CFBridgingRelease(SecKeyCreateRandomKey((CFDictionaryRef)params, (void *)&error));
153 ok(privateKey != nil, "generate private key (error %@)", error);
154 id publicKey = CFBridgingRelease(SecKeyCopyPublicKey((SecKeyRef)privateKey));
156 NSData *input, *output;
159 input = [NSMutableData dataWithLength:inputSize];
160 output = CFBridgingRelease(SecKeyCreateEncryptedData((SecKeyRef)publicKey, algorithm, (CFDataRef)input, (void *)&error));
161 ok(output, "encryption succeeds at the border size %d (key=%dbytes, %@)", (int)input.length, (int)keySizeInBits / 8, algorithm);
162 is((NSInteger)output.length, keySizeInBits / 8, "Unexpected output block size");
164 input = [NSMutableData dataWithLength:inputSize + 1];
165 output = CFBridgingRelease(SecKeyCreateEncryptedData((SecKeyRef)publicKey, algorithm, (CFDataRef)input, (void *)&error));
166 ok(output == nil, "encryption did not fail for border size %d (key=%dbytes, output=%dbytes, %@)", (int)input.length, (int)keySizeInBits / 8, (int)output.length, algorithm);
167 is_status((OSStatus)error.code, errSecParam, "Fails with errSecParam for too long input (%@)", algorithm);
169 static const int TestCountBadInputSizeStep = 5;
171 static void test_bad_input_size() {
172 test_bad_input(1024, 128, kSecKeyAlgorithmRSAEncryptionRaw);
173 test_bad_input(2048, 256, kSecKeyAlgorithmRSAEncryptionRaw);
174 test_bad_input(1024, 128 - 11, kSecKeyAlgorithmRSAEncryptionPKCS1);
175 test_bad_input(2048, 256 - 11, kSecKeyAlgorithmRSAEncryptionPKCS1);
176 test_bad_input(1024, 128 - 42, kSecKeyAlgorithmRSAEncryptionOAEPSHA1);
177 test_bad_input(2048, 256 - 42, kSecKeyAlgorithmRSAEncryptionOAEPSHA1);
178 test_bad_input(1024, 128 - 66, kSecKeyAlgorithmRSAEncryptionOAEPSHA256);
179 test_bad_input(2048, 256 - 66, kSecKeyAlgorithmRSAEncryptionOAEPSHA256);
181 static const int TestCountBadInputSize = TestCountBadInputSizeStep * 8;
183 static void test_bad_signature() {
184 NSDictionary *params = @{ (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA, (id)kSecAttrKeySizeInBits: @2048 };
186 id privateKey = CFBridgingRelease(SecKeyCreateRandomKey((CFDictionaryRef)params, (void *)&error));
187 ok(privateKey, "Generate RSA-2048 temporary key, err %@", error);
188 id publicKey = CFBridgingRelease(SecKeyCopyPublicKey((SecKeyRef)privateKey));
189 ok(publicKey, "Get public key from private key");
192 SecKeyAlgorithm algorithm = kSecKeyAlgorithmRSASignatureDigestPKCS1v15SHA1;
194 SecKeyAlgorithm algorithm = kSecKeyAlgorithmRSASignatureMessagePKCS1v15SHA1;
197 char digest[20] = "digest";
198 NSData *digestData = [NSData dataWithBytes:digest length:sizeof(digest)];
199 NSData *signature = CFBridgingRelease(SecKeyCreateSignature((SecKeyRef)privateKey, algorithm, (CFDataRef)digestData, (void *)&error));
200 ok(signature, "Sign digest, err %@", error);
202 bool result = SecKeyVerifySignature((SecKeyRef)publicKey, algorithm, (CFDataRef)digestData, (CFDataRef)signature, (void *)&error);
203 ok(result, "Verify signature, err %@", error);
205 OSStatus status = SecKeyRawVerify((SecKeyRef)publicKey, kSecPaddingPKCS1SHA1, (const uint8_t *)digest, sizeof(digest), signature.bytes, signature.length);
206 ok_status(status, "Raw verify correct signature");
208 status = SecKeyRawVerify((SecKeyRef)publicKey, kSecPaddingPKCS1SHA1, (const uint8_t *)digest, sizeof(digest), (void * _Nonnull)NULL, 0);
209 is_status(status, errSSLCrypto, "NULL signature failure");
211 const SecAsn1AlgId algId = { .algorithm = CSSMOID_SHA1WithRSA };
212 signature = CFBridgingRelease(SecKeyCreateSignature((SecKeyRef)privateKey, kSecKeyAlgorithmRSASignatureMessagePKCS1v15SHA1, (CFDataRef)digestData, (void *)&error));
213 ok(signature, "Sign message, err %@", error);
215 status = SecKeyDigestAndVerify((__bridge SecKeyRef)publicKey, &algId, (const uint8_t *)digest, sizeof(digest), signature.bytes, signature.length);
216 ok_status(status, "Raw verify correct signature");
218 status = SecKeyDigestAndVerify((__bridge SecKeyRef)publicKey, &algId, (const uint8_t *)digest, sizeof(digest), (void * _Nonnull)NULL, 0);
219 is_status(status, errSSLCrypto, "NULL signature failure");
221 signature = CFBridgingRelease(SecKeyCreateSignature((SecKeyRef)privateKey, kSecKeyAlgorithmRSASignatureDigestPKCS1v15SHA1, (CFDataRef)digestData, (void *)&error));
222 ok(signature, "Sign message, err %@", error);
224 status = SecKeyVerifyDigest((__bridge SecKeyRef)publicKey, &algId, (const uint8_t *)digest, sizeof(digest), signature.bytes, signature.length);
225 ok_status(status, "Raw verify correct signature");
227 status = SecKeyVerifyDigest((__bridge SecKeyRef)publicKey, &algId, (const uint8_t *)digest, sizeof(digest), (void * _Nonnull)NULL, 0);
228 is_status(status, errSSLCrypto, "NULL signature failure");
230 static const int TestCountBadSignature = 12;
232 static const int TestCount =
233 TestCountEncryption +
234 TestCountBadInputSize +
235 TestCountBadSignature;
237 int si_44_seckey_rsa(int argc, char *const *argv) {
238 plan_tests(TestCount);
242 test_bad_input_size();
243 test_bad_signature();