cms-test.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. # test/cms-test.pl
  2. # Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  3. # project.
  4. #
  5. # ====================================================================
  6. # Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions
  10. # are met:
  11. #
  12. # 1. Redistributions of source code must retain the above copyright
  13. # notice, this list of conditions and the following disclaimer.
  14. #
  15. # 2. Redistributions in binary form must reproduce the above copyright
  16. # notice, this list of conditions and the following disclaimer in
  17. # the documentation and/or other materials provided with the
  18. # distribution.
  19. #
  20. # 3. All advertising materials mentioning features or use of this
  21. # software must display the following acknowledgment:
  22. # "This product includes software developed by the OpenSSL Project
  23. # for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. #
  25. # 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. # endorse or promote products derived from this software without
  27. # prior written permission. For written permission, please contact
  28. # licensing@OpenSSL.org.
  29. #
  30. # 5. Products derived from this software may not be called "OpenSSL"
  31. # nor may "OpenSSL" appear in their names without prior written
  32. # permission of the OpenSSL Project.
  33. #
  34. # 6. Redistributions of any form whatsoever must retain the following
  35. # acknowledgment:
  36. # "This product includes software developed by the OpenSSL Project
  37. # for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. #
  39. # THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. # OF THE POSSIBILITY OF SUCH DAMAGE.
  51. # ====================================================================
  52. # CMS, PKCS7 consistency test script. Run extensive tests on
  53. # OpenSSL PKCS#7 and CMS implementations.
  54. my $ossl_path;
  55. my $redir = " 2> cms.err > cms.out";
  56. # Make VMS work
  57. if ( $^O eq "VMS" && -f "OSSLX:openssl.exe" ) {
  58. $ossl_path = "pipe mcr OSSLX:openssl";
  59. }
  60. # Make MSYS work
  61. elsif ( $^O eq "MSWin32" && -f "../apps/openssl.exe" ) {
  62. $ossl_path = "cmd /c ..\\apps\\openssl";
  63. }
  64. elsif ( -f "../apps/openssl$ENV{EXE_EXT}" ) {
  65. $ossl_path = "../util/shlib_wrap.sh ../apps/openssl";
  66. }
  67. elsif ( -f "..\\out32dll\\openssl.exe" ) {
  68. $ossl_path = "..\\out32dll\\openssl.exe";
  69. }
  70. elsif ( -f "..\\out32\\openssl.exe" ) {
  71. $ossl_path = "..\\out32\\openssl.exe";
  72. }
  73. else {
  74. die "Can't find OpenSSL executable";
  75. }
  76. my $pk7cmd = "$ossl_path smime ";
  77. my $cmscmd = "$ossl_path cms ";
  78. my $smdir = "smime-certs";
  79. my $halt_err = 1;
  80. my $badcmd = 0;
  81. my $ossl8 = `$ossl_path version -v` =~ /0\.9\.8/;
  82. my @smime_pkcs7_tests = (
  83. [
  84. "signed content DER format, RSA key",
  85. "-sign -in smcont.txt -outform \"DER\" -nodetach"
  86. . " -certfile $smdir/smroot.pem"
  87. . " -signer $smdir/smrsa1.pem -out test.cms",
  88. "-verify -in test.cms -inform \"DER\" "
  89. . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
  90. ],
  91. [
  92. "signed detached content DER format, RSA key",
  93. "-sign -in smcont.txt -outform \"DER\""
  94. . " -signer $smdir/smrsa1.pem -out test.cms",
  95. "-verify -in test.cms -inform \"DER\" "
  96. . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
  97. ],
  98. [
  99. "signed content test streaming BER format, RSA",
  100. "-sign -in smcont.txt -outform \"DER\" -nodetach"
  101. . " -stream -signer $smdir/smrsa1.pem -out test.cms",
  102. "-verify -in test.cms -inform \"DER\" "
  103. . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
  104. ],
  105. [
  106. "signed content DER format, DSA key",
  107. "-sign -in smcont.txt -outform \"DER\" -nodetach"
  108. . " -signer $smdir/smdsa1.pem -out test.cms",
  109. "-verify -in test.cms -inform \"DER\" "
  110. . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
  111. ],
  112. [
  113. "signed detached content DER format, DSA key",
  114. "-sign -in smcont.txt -outform \"DER\""
  115. . " -signer $smdir/smdsa1.pem -out test.cms",
  116. "-verify -in test.cms -inform \"DER\" "
  117. . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
  118. ],
  119. [
  120. "signed detached content DER format, add RSA signer",
  121. "-resign -inform \"DER\" -in test.cms -outform \"DER\""
  122. . " -signer $smdir/smrsa1.pem -out test2.cms",
  123. "-verify -in test2.cms -inform \"DER\" "
  124. . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt -content smcont.txt"
  125. ],
  126. [
  127. "signed content test streaming BER format, DSA key",
  128. "-sign -in smcont.txt -outform \"DER\" -nodetach"
  129. . " -stream -signer $smdir/smdsa1.pem -out test.cms",
  130. "-verify -in test.cms -inform \"DER\" "
  131. . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
  132. ],
  133. [
  134. "signed content test streaming BER format, 2 DSA and 2 RSA keys",
  135. "-sign -in smcont.txt -outform \"DER\" -nodetach"
  136. . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
  137. . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
  138. . " -stream -out test.cms",
  139. "-verify -in test.cms -inform \"DER\" "
  140. . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
  141. ],
  142. [
  143. "signed content test streaming BER format, 2 DSA and 2 RSA keys, no attributes",
  144. "-sign -in smcont.txt -outform \"DER\" -noattr -nodetach"
  145. . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
  146. . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
  147. . " -stream -out test.cms",
  148. "-verify -in test.cms -inform \"DER\" "
  149. . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
  150. ],
  151. [
  152. "signed content test streaming S/MIME format, 2 DSA and 2 RSA keys",
  153. "-sign -in smcont.txt -nodetach"
  154. . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
  155. . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
  156. . " -stream -out test.cms",
  157. "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
  158. ],
  159. [
  160. "signed content test streaming multipart S/MIME format, 2 DSA and 2 RSA keys",
  161. "-sign -in smcont.txt"
  162. . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
  163. . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
  164. . " -stream -out test.cms",
  165. "-verify -in test.cms " . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
  166. ],
  167. [
  168. "enveloped content test streaming S/MIME format, 3 recipients",
  169. "-encrypt -in smcont.txt"
  170. . " -stream -out test.cms"
  171. . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
  172. "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
  173. ],
  174. [
  175. "enveloped content test streaming S/MIME format, 3 recipients, 3rd used",
  176. "-encrypt -in smcont.txt"
  177. . " -stream -out test.cms"
  178. . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
  179. "-decrypt -recip $smdir/smrsa3.pem -in test.cms -out smtst.txt"
  180. ],
  181. [
  182. "enveloped content test streaming S/MIME format, 3 recipients, key only used",
  183. "-encrypt -in smcont.txt"
  184. . " -stream -out test.cms"
  185. . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
  186. "-decrypt -inkey $smdir/smrsa3.pem -in test.cms -out smtst.txt"
  187. ],
  188. [
  189. "enveloped content test streaming S/MIME format, AES-256 cipher, 3 recipients",
  190. "-encrypt -in smcont.txt"
  191. . " -aes256 -stream -out test.cms"
  192. . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
  193. "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
  194. ],
  195. );
  196. my @smime_cms_tests = (
  197. [
  198. "signed content test streaming BER format, 2 DSA and 2 RSA keys, keyid",
  199. "-sign -in smcont.txt -outform \"DER\" -nodetach -keyid"
  200. . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
  201. . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
  202. . " -stream -out test.cms",
  203. "-verify -in test.cms -inform \"DER\" "
  204. . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
  205. ],
  206. [
  207. "signed content test streaming PEM format, 2 DSA and 2 RSA keys",
  208. "-sign -in smcont.txt -outform PEM -nodetach"
  209. . " -signer $smdir/smrsa1.pem -signer $smdir/smrsa2.pem"
  210. . " -signer $smdir/smdsa1.pem -signer $smdir/smdsa2.pem"
  211. . " -stream -out test.cms",
  212. "-verify -in test.cms -inform PEM "
  213. . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
  214. ],
  215. [
  216. "signed content MIME format, RSA key, signed receipt request",
  217. "-sign -in smcont.txt -signer $smdir/smrsa1.pem -nodetach"
  218. . " -receipt_request_to test\@openssl.org -receipt_request_all"
  219. . " -out test.cms",
  220. "-verify -in test.cms "
  221. . " \"-CAfile\" $smdir/smroot.pem -out smtst.txt"
  222. ],
  223. [
  224. "signed receipt MIME format, RSA key",
  225. "-sign_receipt -in test.cms"
  226. . " -signer $smdir/smrsa2.pem"
  227. . " -out test2.cms",
  228. "-verify_receipt test2.cms -in test.cms"
  229. . " \"-CAfile\" $smdir/smroot.pem"
  230. ],
  231. [
  232. "enveloped content test streaming S/MIME format, 3 recipients, keyid",
  233. "-encrypt -in smcont.txt"
  234. . " -stream -out test.cms -keyid"
  235. . " $smdir/smrsa1.pem $smdir/smrsa2.pem $smdir/smrsa3.pem ",
  236. "-decrypt -recip $smdir/smrsa1.pem -in test.cms -out smtst.txt"
  237. ],
  238. [
  239. "enveloped content test streaming PEM format, KEK",
  240. "-encrypt -in smcont.txt -outform PEM -aes128"
  241. . " -stream -out test.cms "
  242. . " -secretkey 000102030405060708090A0B0C0D0E0F "
  243. . " -secretkeyid C0FEE0",
  244. "-decrypt -in test.cms -out smtst.txt -inform PEM"
  245. . " -secretkey 000102030405060708090A0B0C0D0E0F "
  246. . " -secretkeyid C0FEE0"
  247. ],
  248. [
  249. "enveloped content test streaming PEM format, KEK, key only",
  250. "-encrypt -in smcont.txt -outform PEM -aes128"
  251. . " -stream -out test.cms "
  252. . " -secretkey 000102030405060708090A0B0C0D0E0F "
  253. . " -secretkeyid C0FEE0",
  254. "-decrypt -in test.cms -out smtst.txt -inform PEM"
  255. . " -secretkey 000102030405060708090A0B0C0D0E0F "
  256. ],
  257. [
  258. "data content test streaming PEM format",
  259. "-data_create -in smcont.txt -outform PEM -nodetach"
  260. . " -stream -out test.cms",
  261. "-data_out -in test.cms -inform PEM -out smtst.txt"
  262. ],
  263. [
  264. "encrypted content test streaming PEM format, 128 bit RC2 key",
  265. "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
  266. . " -rc2 -secretkey 000102030405060708090A0B0C0D0E0F"
  267. . " -stream -out test.cms",
  268. "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
  269. . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
  270. ],
  271. [
  272. "encrypted content test streaming PEM format, 40 bit RC2 key",
  273. "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
  274. . " -rc2 -secretkey 0001020304"
  275. . " -stream -out test.cms",
  276. "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
  277. . " -secretkey 0001020304 -out smtst.txt"
  278. ],
  279. [
  280. "encrypted content test streaming PEM format, triple DES key",
  281. "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
  282. . " -des3 -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
  283. . " -stream -out test.cms",
  284. "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
  285. . " -secretkey 000102030405060708090A0B0C0D0E0F1011121314151617"
  286. . " -out smtst.txt"
  287. ],
  288. [
  289. "encrypted content test streaming PEM format, 128 bit AES key",
  290. "\"-EncryptedData_encrypt\" -in smcont.txt -outform PEM"
  291. . " -aes128 -secretkey 000102030405060708090A0B0C0D0E0F"
  292. . " -stream -out test.cms",
  293. "\"-EncryptedData_decrypt\" -in test.cms -inform PEM "
  294. . " -secretkey 000102030405060708090A0B0C0D0E0F -out smtst.txt"
  295. ],
  296. );
  297. my @smime_cms_comp_tests = (
  298. [
  299. "compressed content test streaming PEM format",
  300. "-compress -in smcont.txt -outform PEM -nodetach"
  301. . " -stream -out test.cms",
  302. "-uncompress -in test.cms -inform PEM -out smtst.txt"
  303. ]
  304. );
  305. print "CMS => PKCS#7 compatibility tests\n";
  306. run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $pk7cmd );
  307. print "CMS <= PKCS#7 compatibility tests\n";
  308. run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $pk7cmd, $cmscmd );
  309. print "CMS <=> CMS consistency tests\n";
  310. run_smime_tests( \$badcmd, \@smime_pkcs7_tests, $cmscmd, $cmscmd );
  311. run_smime_tests( \$badcmd, \@smime_cms_tests, $cmscmd, $cmscmd );
  312. if ( `$ossl_path version -f` =~ /ZLIB/ ) {
  313. run_smime_tests( \$badcmd, \@smime_cms_comp_tests, $cmscmd, $cmscmd );
  314. }
  315. else {
  316. print "Zlib not supported: compression tests skipped\n";
  317. }
  318. print "Running modified tests for OpenSSL 0.9.8 cms backport\n" if($ossl8);
  319. if ($badcmd) {
  320. print "$badcmd TESTS FAILED!!\n";
  321. }
  322. else {
  323. print "ALL TESTS SUCCESSFUL.\n";
  324. }
  325. unlink "test.cms";
  326. unlink "test2.cms";
  327. unlink "smtst.txt";
  328. unlink "cms.out";
  329. unlink "cms.err";
  330. sub run_smime_tests {
  331. my ( $rv, $aref, $scmd, $vcmd ) = @_;
  332. foreach $smtst (@$aref) {
  333. my ( $tnam, $rscmd, $rvcmd ) = @$smtst;
  334. if ($ossl8)
  335. {
  336. # Skip smime resign: 0.9.8 smime doesn't support -resign
  337. next if ($scmd =~ /smime/ && $rscmd =~ /-resign/);
  338. # Disable streaming: option not supported in 0.9.8
  339. $tnam =~ s/streaming//;
  340. $rscmd =~ s/-stream//;
  341. $rvcmd =~ s/-stream//;
  342. }
  343. system("$scmd$rscmd$redir");
  344. if ($?) {
  345. print "$tnam: generation error\n";
  346. $$rv++;
  347. exit 1 if $halt_err;
  348. next;
  349. }
  350. system("$vcmd$rvcmd$redir");
  351. if ($?) {
  352. print "$tnam: verify error\n";
  353. $$rv++;
  354. exit 1 if $halt_err;
  355. next;
  356. }
  357. if (!cmp_files("smtst.txt", "smcont.txt")) {
  358. print "$tnam: content verify error\n";
  359. $$rv++;
  360. exit 1 if $halt_err;
  361. next;
  362. }
  363. print "$tnam: OK\n";
  364. }
  365. }
  366. sub cmp_files {
  367. use FileHandle;
  368. my ( $f1, $f2 ) = @_;
  369. my $fp1 = FileHandle->new();
  370. my $fp2 = FileHandle->new();
  371. my ( $rd1, $rd2 );
  372. if ( !open( $fp1, "<$f1" ) ) {
  373. print STDERR "Can't Open file $f1\n";
  374. return 0;
  375. }
  376. if ( !open( $fp2, "<$f2" ) ) {
  377. print STDERR "Can't Open file $f2\n";
  378. return 0;
  379. }
  380. binmode $fp1;
  381. binmode $fp2;
  382. my $ret = 0;
  383. for ( ; ; ) {
  384. $n1 = sysread $fp1, $rd1, 4096;
  385. $n2 = sysread $fp2, $rd2, 4096;
  386. last if ( $n1 != $n2 );
  387. last if ( $rd1 ne $rd2 );
  388. if ( $n1 == 0 ) {
  389. $ret = 1;
  390. last;
  391. }
  392. }
  393. close $fp1;
  394. close $fp2;
  395. return $ret;
  396. }