2 * Copyright (c) 2009,2012-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@
24 #include "si-64-ossl-cms/attached_no_data_signed_data.h"
25 #include "si-64-ossl-cms/attached_signed_data.h"
26 #include "si-64-ossl-cms/detached_content.h"
27 #include "si-64-ossl-cms/detached_signed_data.h"
28 #include "si-64-ossl-cms/signer.h"
29 #include "si-64-ossl-cms/privkey.h"
31 #include <CoreFoundation/CoreFoundation.h>
32 #include <Security/SecCMS.h>
33 #include <Security/SecRSAKey.h>
34 #include <Security/SecCertificatePriv.h>
35 #include <Security/SecIdentityPriv.h>
36 #include <Security/SecItem.h>
37 #include <utilities/SecCFWrappers.h>
40 #include <AssertMacros.h>
42 #include "shared_regressions.h"
45 openssl req -new -newkey rsa:512 -x509 -nodes -subj "/O=foo/CN=bar" -out signer.pem
46 echo -n "hoi joh" > detached_content
47 openssl smime -sign -outform der -signer signer.pem -in detached_content -inkey privkey.pem -out detached_signed_data.der
48 openssl smime -nodetach -sign -outform der -signer test.pem -in detached_content -inkey privkey.pem -out attached_signed_data.der
49 openssl smime -nodetach -sign -outform der -signer test.pem -inkey privkey.pem -out attached_no_data_signed_data.der < /dev/null
51 xxd -i detached_content > detached_content.h
52 xxd -i attached_no_data_signed_data.der > attached_no_data_signed_data.h
53 xxd -i attached_signed_data.der > attached_signed_data.h
54 xxd -i detached_signed_data.der > detached_signed_data.h
56 openssl x509 -in test.pem -outform der -out signer.der
57 xxd -i signer.der > signer.h
64 35 9: OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
68 48 NDEF: OCTET STRING {
70 50 7: OCTET STRING 'hoi joh'
80 41 9: OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
84 54 7: OCTET STRING 'hoi joh'
93 35 9: OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
100 41 9: OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
108 35 9: OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
112 48 NDEF: OCTET STRING {
123 41 9: OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
128 : Error: Object has zero length.
136 __unused
static inline void write_data(const char * path
, CFDataRef data
)
138 int data_file
= open(path
, O_CREAT
|O_WRONLY
|O_TRUNC
, 0644);
139 write(data_file
, CFDataGetBytePtr(data
), CFDataGetLength(data
));
143 static void tests(void)
145 CFDataRef attached_signed_data
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, attached_signed_data_der
, attached_signed_data_der_len
, kCFAllocatorNull
);
146 CFDataRef detached_signed_data
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, detached_signed_data_der
, detached_signed_data_der_len
, kCFAllocatorNull
);
147 CFDataRef attached_no_data_signed_data
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, attached_no_data_signed_data_der
, attached_no_data_signed_data_der_len
, kCFAllocatorNull
);
148 CFDataRef detached_data
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, detached_content
, detached_content_len
, kCFAllocatorNull
);
149 CFDataRef no_data
= CFDataCreate(kCFAllocatorDefault
, NULL
, 0);
150 SecPolicyRef policy
= SecPolicyCreateBasicX509();
151 SecTrustRef trust
= NULL
;
153 ok_status(SecCMSVerifyCopyDataAndAttributes(attached_signed_data
, NULL
, policy
, &trust
, NULL
, NULL
), "verify attached data");
154 CFReleaseNull(trust
);
155 ok_status(SecCMSVerifyCopyDataAndAttributes(detached_signed_data
, detached_data
, policy
, &trust
, NULL
, NULL
), "verify detached data");
156 CFReleaseNull(trust
);
158 /* iOS supports empty data */
159 ok_status(SecCMSVerifyCopyDataAndAttributes(attached_no_data_signed_data
, NULL
, policy
, &trust
, NULL
, NULL
), "verify attached no data");
162 is_status(SecCMSVerifyCopyDataAndAttributes(attached_no_data_signed_data
, NULL
, policy
, &trust
, NULL
, NULL
), errSecAuthFailed
, "verify attached no data");
164 CFReleaseNull(trust
);
165 ok_status(SecCMSVerifyCopyDataAndAttributes(attached_no_data_signed_data
, no_data
, policy
, &trust
, NULL
, NULL
), "verify attached no data");
166 CFReleaseNull(trust
);
169 SecCertificateRef cert
= NULL
;
170 SecKeyRef privKey
= NULL
;
171 SecIdentityRef identity
= NULL
;
173 isnt(cert
= SecCertificateCreateWithBytes(NULL
, signer_der
, signer_der_len
), NULL
, "create certificate");
174 CFDataRef keyData
= CFDataCreate(NULL
, privkey_der
, privkey_der_len
);
175 CFMutableDictionaryRef keyAttrs
= CFDictionaryCreateMutable(NULL
, 2, &kCFTypeDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
176 CFDictionaryAddValue(keyAttrs
, kSecAttrKeyType
, kSecAttrKeyTypeRSA
);
177 CFDictionaryAddValue(keyAttrs
, kSecAttrKeyClass
, kSecAttrKeyClassPrivate
);
178 isnt(privKey
= SecKeyCreateWithData(keyData
, keyAttrs
, NULL
), NULL
, "Create private key");
179 isnt(identity
= SecIdentityCreate(NULL
, cert
, privKey
), NULL
, "create identity");
180 CFReleaseSafe(privKey
);
181 CFReleaseNull(keyData
);
182 CFReleaseNull(keyAttrs
);
184 CFMutableDataRef cms_data
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
185 ok_status(SecCMSCreateSignedData(identity
, detached_data
, NULL
, NULL
, cms_data
), "create attached data");
186 //write_data("/var/tmp/attached", cms_data);
187 CFDataSetLength(cms_data
, 0);
188 CFDictionaryRef detached_cms_dict
= CFDictionaryCreate(kCFAllocatorDefault
, (const void **)&kSecCMSSignDetached
, (const void **)&kCFBooleanTrue
, 1, NULL
, NULL
);
189 ok_status(SecCMSCreateSignedData(identity
, detached_data
, detached_cms_dict
, NULL
, cms_data
), "create attached data");
190 CFReleaseNull(detached_cms_dict
);
191 //write_data("/var/tmp/detached", cms_data);
192 CFDataSetLength(cms_data
, 0);
194 /* iOS supports empty data */
195 ok_status(SecCMSCreateSignedData(identity
, NULL
, NULL
, NULL
, cms_data
), "create attached data");
198 is_status(SecCMSCreateSignedData(identity
, NULL
, NULL
, NULL
, cms_data
), errSecParam
, "create attached data");
201 //write_data("/var/tmp/empty_attached", cms_data);
203 CFReleaseSafe(cms_data
);
205 CFReleaseNull(identity
);
206 CFReleaseSafe(attached_signed_data
);
207 CFReleaseSafe(detached_signed_data
);
208 CFReleaseSafe(attached_no_data_signed_data
);
209 CFReleaseSafe(detached_data
);
210 CFReleaseSafe(no_data
);
211 CFReleaseSafe(policy
);
214 int si_64_ossl_cms(int argc
, char *const *argv
)