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 <utilities/SecCFWrappers.h>
39 #include <AssertMacros.h>
41 #include "Security_regressions.h"
44 openssl req -new -newkey rsa:512 -x509 -nodes -subj "/O=foo/CN=bar" -out signer.pem
45 echo -n "hoi joh" > detached_content
46 openssl smime -sign -outform der -signer signer.pem -in detached_content -inkey privkey.pem -out detached_signed_data.der
47 openssl smime -nodetach -sign -outform der -signer test.pem -in detached_content -inkey privkey.pem -out attached_signed_data.der
48 openssl smime -nodetach -sign -outform der -signer test.pem -inkey privkey.pem -out attached_no_data_signed_data.der < /dev/null
50 xxd -i detached_content > detached_content.h
51 xxd -i attached_no_data_signed_data.der > attached_no_data_signed_data.h
52 xxd -i attached_signed_data.der > attached_signed_data.h
53 xxd -i detached_signed_data.der > detached_signed_data.h
55 openssl x509 -in test.pem -outform der -out signer.der
56 xxd -i signer.der > signer.h
63 35 9: OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
67 48 NDEF: OCTET STRING {
69 50 7: OCTET STRING 'hoi joh'
79 41 9: OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
83 54 7: OCTET STRING 'hoi joh'
92 35 9: OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
99 41 9: OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
107 35 9: OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
111 48 NDEF: OCTET STRING {
122 41 9: OBJECT IDENTIFIER data (1 2 840 113549 1 7 1)
127 : Error: Object has zero length.
135 __unused
static inline void write_data(const char * path
, CFDataRef data
)
137 int data_file
= open(path
, O_CREAT
|O_WRONLY
|O_TRUNC
, 0644);
138 write(data_file
, CFDataGetBytePtr(data
), CFDataGetLength(data
));
142 static void tests(void)
144 CFDataRef attached_signed_data
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, attached_signed_data_der
, attached_signed_data_der_len
, kCFAllocatorNull
);
145 CFDataRef detached_signed_data
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, detached_signed_data_der
, detached_signed_data_der_len
, kCFAllocatorNull
);
146 CFDataRef attached_no_data_signed_data
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, attached_no_data_signed_data_der
, attached_no_data_signed_data_der_len
, kCFAllocatorNull
);
147 CFDataRef detached_data
= CFDataCreateWithBytesNoCopy(kCFAllocatorDefault
, detached_content
, detached_content_len
, kCFAllocatorNull
);
148 CFDataRef no_data
= CFDataCreate(kCFAllocatorDefault
, NULL
, 0);
149 SecPolicyRef policy
= SecPolicyCreateBasicX509();
150 SecTrustRef trust
= NULL
;
152 ok_status(SecCMSVerifyCopyDataAndAttributes(attached_signed_data
, NULL
, policy
, &trust
, NULL
, NULL
), "verify attached data");
154 ok_status(SecCMSVerifyCopyDataAndAttributes(detached_signed_data
, detached_data
, policy
, &trust
, NULL
, NULL
), "verify detached data");
156 ok_status(SecCMSVerifyCopyDataAndAttributes(attached_no_data_signed_data
, NULL
, policy
, &trust
, NULL
, NULL
), "verify attached no data");
158 ok_status(SecCMSVerifyCopyDataAndAttributes(attached_no_data_signed_data
, no_data
, policy
, &trust
, NULL
, NULL
), "verify attached no data");
162 SecCertificateRef cert
= NULL
;
163 SecKeyRef privKey
= NULL
;
164 SecIdentityRef identity
= NULL
;
166 isnt(cert
= SecCertificateCreateWithBytes(NULL
, signer_der
, signer_der_len
), NULL
, "create certificate");
167 isnt(privKey
= SecKeyCreateRSAPrivateKey(NULL
, privkey_der
, privkey_der_len
, kSecKeyEncodingPkcs1
), NULL
, "create private key");
168 isnt(identity
= SecIdentityCreate(NULL
, cert
, privKey
), NULL
, "create identity");
169 CFReleaseSafe(privKey
);
171 CFMutableDataRef cms_data
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
172 ok_status(SecCMSCreateSignedData(identity
, detached_data
, NULL
, NULL
, cms_data
), "create attached data");
173 //write_data("/var/tmp/attached", cms_data);
174 CFDataSetLength(cms_data
, 0);
175 CFDictionaryRef detached_cms_dict
= CFDictionaryCreate(kCFAllocatorDefault
, (const void **)&kSecCMSSignDetached
, (const void **)&kCFBooleanTrue
, 1, NULL
, NULL
);
176 ok_status(SecCMSCreateSignedData(identity
, detached_data
, detached_cms_dict
, NULL
, cms_data
), "create attached data");
177 CFRelease(detached_cms_dict
);
178 //write_data("/var/tmp/detached", cms_data);
179 CFDataSetLength(cms_data
, 0);
180 ok_status(SecCMSCreateSignedData(identity
, NULL
, NULL
, NULL
, cms_data
), "create attached data");
181 //write_data("/var/tmp/empty_attached", cms_data);
183 CFReleaseSafe(cms_data
);
185 CFReleaseNull(identity
);
186 CFRelease(attached_signed_data
);
187 CFRelease(detached_signed_data
);
188 CFRelease(attached_no_data_signed_data
);
189 CFRelease(detached_data
);
194 int si_64_ossl_cms(int argc
, char *const *argv
)