do_tests.pl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. # perl script to run OpenSSL tests
  2. my $base_path = "\\openssl";
  3. my $output_path = "$base_path\\test_out";
  4. my $cert_path = "$base_path\\certs";
  5. my $test_path = "$base_path\\test";
  6. my $app_path = "$base_path\\apps";
  7. my $tmp_cert = "$output_path\\cert.tmp";
  8. my $OpenSSL_config = "$app_path\\openssl.cnf";
  9. my $log_file = "$output_path\\tests.log";
  10. my $pause = 0;
  11. # process the command line args to see if they wanted us to pause
  12. # between executing each command
  13. foreach $i (@ARGV)
  14. {
  15. if ($i =~ /^-p$/)
  16. { $pause=1; }
  17. }
  18. main();
  19. ############################################################################
  20. sub main()
  21. {
  22. # delete all the output files in the output directory
  23. unlink <$output_path\\*.*>;
  24. # open the main log file
  25. open(OUT, ">$log_file") || die "unable to open $log_file\n";
  26. algorithm_tests();
  27. encryption_tests();
  28. pem_tests();
  29. verify_tests();
  30. ca_tests();
  31. ssl_tests();
  32. close(OUT);
  33. print("\nCompleted running tests.\n\n");
  34. print("Check log file for errors: $log_file\n");
  35. }
  36. ############################################################################
  37. sub algorithm_tests
  38. {
  39. my $i;
  40. my $outFile;
  41. my @tests = ( rsa_test, destest, ideatest, bftest, shatest, sha1test,
  42. md5test, dsatest, md2test, mdc2test, rc2test, rc4test, randtest,
  43. dhtest, exptest );
  44. print( "\nRUNNING CRYPTO ALGORITHM TESTS:\n\n");
  45. print( OUT "\n========================================================\n");
  46. print( OUT "CRYPTO ALGORITHM TESTS:\n\n");
  47. foreach $i (@tests)
  48. {
  49. if (-e "$base_path\\$i.nlm")
  50. {
  51. $outFile = "$output_path\\$i.out";
  52. system("$i > $outFile");
  53. log_desc("Test: $i\.nlm:");
  54. log_output("", $outFile );
  55. }
  56. else
  57. {
  58. log_desc("Test: $i\.nlm: file not found");
  59. }
  60. }
  61. }
  62. ############################################################################
  63. sub encryption_tests
  64. {
  65. my $i;
  66. my $outFile;
  67. my @enc_tests = ( "enc", "rc4", "des-cfb", "des-ede-cfb", "des-ede3-cfb",
  68. "des-ofb", "des-ede-ofb", "des-ede3-ofb",
  69. "des-ecb", "des-ede", "des-ede3", "des-cbc",
  70. "des-ede-cbc", "des-ede3-cbc", "idea-ecb", "idea-cfb",
  71. "idea-ofb", "idea-cbc", "rc2-ecb", "rc2-cfb",
  72. "rc2-ofb", "rc2-cbc", "bf-ecb", "bf-cfb",
  73. "bf-ofb", "bf-cbc" );
  74. my $input = "$base_path\\do_tests.pl";
  75. my $cipher = "$output_path\\cipher.out";
  76. my $clear = "$output_path\\clear.out";
  77. print( "\nRUNNING ENCRYPTION & DECRYPTION TESTS:\n\n");
  78. print( OUT "\n========================================================\n");
  79. print( OUT "FILE ENCRYPTION & DECRYPTION TESTS:\n\n");
  80. foreach $i (@enc_tests)
  81. {
  82. log_desc("Testing: $i");
  83. # do encryption
  84. $outFile = "$output_path\\enc.out";
  85. system("openssl2 $i -e -bufsize 113 -k test -in $input -out $cipher > $outFile" );
  86. log_output("Encrypting: $input --> $cipher", $outFile);
  87. # do decryption
  88. $outFile = "$output_path\\dec.out";
  89. system("openssl2 $i -d -bufsize 157 -k test -in $cipher -out $clear > $outFile");
  90. log_output("Decrypting: $cipher --> $clear", $outFile);
  91. # compare files
  92. $x = compare_files( $input, $clear, 1);
  93. if ( $x == 0 )
  94. {
  95. print( "SUCCESS - files match: $input, $clear\n");
  96. print( OUT "SUCCESS - files match: $input, $clear\n");
  97. }
  98. else
  99. {
  100. print( "ERROR: files don't match\n");
  101. print( OUT "ERROR: files don't match\n");
  102. }
  103. do_wait();
  104. # Now do the same encryption but use Base64
  105. # do encryption B64
  106. $outFile = "$output_path\\B64enc.out";
  107. system("openssl2 $i -a -e -bufsize 113 -k test -in $input -out $cipher > $outFile");
  108. log_output("Encrypting(B64): $cipher --> $clear", $outFile);
  109. # do decryption B64
  110. $outFile = "$output_path\\B64dec.out";
  111. system("openssl2 $i -a -d -bufsize 157 -k test -in $cipher -out $clear > $outFile");
  112. log_output("Decrypting(B64): $cipher --> $clear", $outFile);
  113. # compare files
  114. $x = compare_files( $input, $clear, 1);
  115. if ( $x == 0 )
  116. {
  117. print( "SUCCESS - files match: $input, $clear\n");
  118. print( OUT "SUCCESS - files match: $input, $clear\n");
  119. }
  120. else
  121. {
  122. print( "ERROR: files don't match\n");
  123. print( OUT "ERROR: files don't match\n");
  124. }
  125. do_wait();
  126. } # end foreach
  127. # delete the temporary files
  128. unlink($cipher);
  129. unlink($clear);
  130. }
  131. ############################################################################
  132. sub pem_tests
  133. {
  134. my $i;
  135. my $tmp_out;
  136. my $outFile = "$output_path\\pem.out";
  137. my %pem_tests = (
  138. "crl" => "testcrl.pem",
  139. "pkcs7" => "testp7.pem",
  140. "req" => "testreq2.pem",
  141. "rsa" => "testrsa.pem",
  142. "x509" => "testx509.pem",
  143. "x509" => "v3-cert1.pem",
  144. "sess_id" => "testsid.pem" );
  145. print( "\nRUNNING PEM TESTS:\n\n");
  146. print( OUT "\n========================================================\n");
  147. print( OUT "PEM TESTS:\n\n");
  148. foreach $i (keys(%pem_tests))
  149. {
  150. log_desc( "Testing: $i");
  151. my $input = "$test_path\\$pem_tests{$i}";
  152. $tmp_out = "$output_path\\$pem_tests{$i}";
  153. if ($i ne "req" )
  154. {
  155. system("openssl2 $i -in $input -out $tmp_out > $outFile");
  156. log_output( "openssl2 $i -in $input -out $tmp_out", $outFile);
  157. }
  158. else
  159. {
  160. system("openssl2 $i -in $input -out $tmp_out -config $OpenSSL_config > $outFile");
  161. log_output( "openssl2 $i -in $input -out $tmp_out -config $OpenSSL_config", $outFile );
  162. }
  163. $x = compare_files( $input, $tmp_out);
  164. if ( $x == 0 )
  165. {
  166. print( "SUCCESS - files match: $input, $tmp_out\n");
  167. print( OUT "SUCCESS - files match: $input, $tmp_out\n");
  168. }
  169. else
  170. {
  171. print( "ERROR: files don't match\n");
  172. print( OUT "ERROR: files don't match\n");
  173. }
  174. do_wait();
  175. } # end foreach
  176. }
  177. ############################################################################
  178. sub verify_tests
  179. {
  180. my $i;
  181. my $outFile = "$output_path\\verify.out";
  182. my @cert_files = <$cert_path\\*.pem>;
  183. print( "\nRUNNING VERIFY TESTS:\n\n");
  184. print( OUT "\n========================================================\n");
  185. print( OUT "VERIFY TESTS:\n\n");
  186. make_tmp_cert_file();
  187. foreach $i (@cert_files)
  188. {
  189. system("openssl2 verify -CAfile $tmp_cert $i >$outFile");
  190. log_desc("Verifying cert: $i");
  191. log_output("openssl2 verify -CAfile $tmp_cert $i", $outFile);
  192. }
  193. }
  194. ############################################################################
  195. sub ssl_tests
  196. {
  197. my $outFile = "$output_path\\ssl_tst.out";
  198. my($CAcert) = "$output_path\\certCA.ss";
  199. my($Ukey) = "$output_path\\keyU.ss";
  200. my($Ucert) = "$output_path\\certU.ss";
  201. my($ssltest)= "ssltest -key $Ukey -cert $Ucert -c_key $Ukey -c_cert $Ucert -CAfile $CAcert";
  202. print( "\nRUNNING SSL TESTS:\n\n");
  203. print( OUT "\n========================================================\n");
  204. print( OUT "SSL TESTS:\n\n");
  205. system("ssltest -ssl2 >$outFile");
  206. log_desc("Testing sslv2:");
  207. log_output("ssltest -ssl2", $outFile);
  208. system("$ssltest -ssl2 -server_auth >$outFile");
  209. log_desc("Testing sslv2 with server authentication:");
  210. log_output("$ssltest -ssl2 -server_auth", $outFile);
  211. system("$ssltest -ssl2 -client_auth >$outFile");
  212. log_desc("Testing sslv2 with client authentication:");
  213. log_output("$ssltest -ssl2 -client_auth", $outFile);
  214. system("$ssltest -ssl2 -server_auth -client_auth >$outFile");
  215. log_desc("Testing sslv2 with both client and server authentication:");
  216. log_output("$ssltest -ssl2 -server_auth -client_auth", $outFile);
  217. system("ssltest -ssl3 >$outFile");
  218. log_desc("Testing sslv3:");
  219. log_output("ssltest -ssl3", $outFile);
  220. system("$ssltest -ssl3 -server_auth >$outFile");
  221. log_desc("Testing sslv3 with server authentication:");
  222. log_output("$ssltest -ssl3 -server_auth", $outFile);
  223. system("$ssltest -ssl3 -client_auth >$outFile");
  224. log_desc("Testing sslv3 with client authentication:");
  225. log_output("$ssltest -ssl3 -client_auth", $outFile);
  226. system("$ssltest -ssl3 -server_auth -client_auth >$outFile");
  227. log_desc("Testing sslv3 with both client and server authentication:");
  228. log_output("$ssltest -ssl3 -server_auth -client_auth", $outFile);
  229. system("ssltest >$outFile");
  230. log_desc("Testing sslv2/sslv3:");
  231. log_output("ssltest", $outFile);
  232. system("$ssltest -server_auth >$outFile");
  233. log_desc("Testing sslv2/sslv3 with server authentication:");
  234. log_output("$ssltest -server_auth", $outFile);
  235. system("$ssltest -client_auth >$outFile");
  236. log_desc("Testing sslv2/sslv3 with client authentication:");
  237. log_output("$ssltest -client_auth ", $outFile);
  238. system("$ssltest -server_auth -client_auth >$outFile");
  239. log_desc("Testing sslv2/sslv3 with both client and server authentication:");
  240. log_output("$ssltest -server_auth -client_auth", $outFile);
  241. system("ssltest -bio_pair -ssl2 >$outFile");
  242. log_desc("Testing sslv2 via BIO pair:");
  243. log_output("ssltest -bio_pair -ssl2", $outFile);
  244. system("ssltest -bio_pair -dhe1024dsa -v >$outFile");
  245. log_desc("Testing sslv2/sslv3 with 1024 bit DHE via BIO pair:");
  246. log_output("ssltest -bio_pair -dhe1024dsa -v", $outFile);
  247. system("$ssltest -bio_pair -ssl2 -server_auth >$outFile");
  248. log_desc("Testing sslv2 with server authentication via BIO pair:");
  249. log_output("$ssltest -bio_pair -ssl2 -server_auth", $outFile);
  250. system("$ssltest -bio_pair -ssl2 -client_auth >$outFile");
  251. log_desc("Testing sslv2 with client authentication via BIO pair:");
  252. log_output("$ssltest -bio_pair -ssl2 -client_auth", $outFile);
  253. system("$ssltest -bio_pair -ssl2 -server_auth -client_auth >$outFile");
  254. log_desc("Testing sslv2 with both client and server authentication via BIO pair:");
  255. log_output("$ssltest -bio_pair -ssl2 -server_auth -client_auth", $outFile);
  256. system("ssltest -bio_pair -ssl3 >$outFile");
  257. log_desc("Testing sslv3 via BIO pair:");
  258. log_output("ssltest -bio_pair -ssl3", $outFile);
  259. system("$ssltest -bio_pair -ssl3 -server_auth >$outFile");
  260. log_desc("Testing sslv3 with server authentication via BIO pair:");
  261. log_output("$ssltest -bio_pair -ssl3 -server_auth", $outFile);
  262. system("$ssltest -bio_pair -ssl3 -client_auth >$outFile");
  263. log_desc("Testing sslv3 with client authentication via BIO pair:");
  264. log_output("$ssltest -bio_pair -ssl3 -client_auth", $outFile);
  265. system("$ssltest -bio_pair -ssl3 -server_auth -client_auth >$outFile");
  266. log_desc("Testing sslv3 with both client and server authentication via BIO pair:");
  267. log_output("$ssltest -bio_pair -ssl3 -server_auth -client_auth", $outFile);
  268. system("ssltest -bio_pair >$outFile");
  269. log_desc("Testing sslv2/sslv3 via BIO pair:");
  270. log_output("ssltest -bio_pair", $outFile);
  271. system("$ssltest -bio_pair -server_auth >$outFile");
  272. log_desc("Testing sslv2/sslv3 with server authentication via BIO pair:");
  273. log_output("$ssltest -bio_pair -server_auth", $outFile);
  274. system("$ssltest -bio_pair -client_auth >$outFile");
  275. log_desc("Testing sslv2/sslv3 with client authentication via BIO pair:");
  276. log_output("$ssltest -bio_pair -client_auth", $outFile);
  277. system("$ssltest -bio_pair -server_auth -client_auth >$outFile");
  278. log_desc("Testing sslv2/sslv3 with both client and server authentication via BIO pair:");
  279. log_output("$ssltest -bio_pair -server_auth -client_auth", $outFile);
  280. }
  281. ############################################################################
  282. sub ca_tests
  283. {
  284. my $outFile = "$output_path\\ca_tst.out";
  285. my($CAkey) = "$output_path\\keyCA.ss";
  286. my($CAcert) = "$output_path\\certCA.ss";
  287. my($CAserial) = "$output_path\\certCA.srl";
  288. my($CAreq) = "$output_path\\reqCA.ss";
  289. my($CAreq2) = "$output_path\\req2CA.ss";
  290. my($CAconf) = "$test_path\\CAss.cnf";
  291. my($Uconf) = "$test_path\\Uss.cnf";
  292. my($Ukey) = "$output_path\\keyU.ss";
  293. my($Ureq) = "$output_path\\reqU.ss";
  294. my($Ucert) = "$output_path\\certU.ss";
  295. print( "\nRUNNING CA TESTS:\n\n");
  296. print( OUT "\n========================================================\n");
  297. print( OUT "CA TESTS:\n");
  298. system("openssl2 req -config $CAconf -out $CAreq -keyout $CAkey -new >$outFile");
  299. log_desc("Make a certificate request using req:");
  300. log_output("openssl2 req -config $CAconf -out $CAreq -keyout $CAkey -new", $outFile);
  301. system("openssl2 x509 -CAcreateserial -in $CAreq -days 30 -req -out $CAcert -signkey $CAkey >$outFile");
  302. log_desc("Convert the certificate request into a self signed certificate using x509:");
  303. log_output("openssl2 x509 -CAcreateserial -in $CAreq -days 30 -req -out $CAcert -signkey $CAkey", $outFile);
  304. system("openssl2 x509 -in $CAcert -x509toreq -signkey $CAkey -out $CAreq2 >$outFile");
  305. log_desc("Convert a certificate into a certificate request using 'x509':");
  306. log_output("openssl2 x509 -in $CAcert -x509toreq -signkey $CAkey -out $CAreq2", $outFile);
  307. system("openssl2 req -config $OpenSSL_config -verify -in $CAreq -noout >$outFile");
  308. log_output("openssl2 req -config $OpenSSL_config -verify -in $CAreq -noout", $outFile);
  309. system("openssl2 req -config $OpenSSL_config -verify -in $CAreq2 -noout >$outFile");
  310. log_output( "openssl2 req -config $OpenSSL_config -verify -in $CAreq2 -noout", $outFile);
  311. system("openssl2 verify -CAfile $CAcert $CAcert >$outFile");
  312. log_output("openssl2 verify -CAfile $CAcert $CAcert", $outFile);
  313. system("openssl2 req -config $Uconf -out $Ureq -keyout $Ukey -new >$outFile");
  314. log_desc("Make another certificate request using req:");
  315. log_output("openssl2 req -config $Uconf -out $Ureq -keyout $Ukey -new", $outFile);
  316. system("openssl2 x509 -CAcreateserial -in $Ureq -days 30 -req -out $Ucert -CA $CAcert -CAkey $CAkey -CAserial $CAserial >$outFile");
  317. log_desc("Sign certificate request with the just created CA via x509:");
  318. log_output("openssl2 x509 -CAcreateserial -in $Ureq -days 30 -req -out $Ucert -CA $CAcert -CAkey $CAkey -CAserial $CAserial", $outFile);
  319. system("openssl2 verify -CAfile $CAcert $Ucert >$outFile");
  320. log_output("openssl2 verify -CAfile $CAcert $Ucert", $outFile);
  321. system("openssl2 x509 -subject -issuer -startdate -enddate -noout -in $Ucert >$outFile");
  322. log_desc("Certificate details");
  323. log_output("openssl2 x509 -subject -issuer -startdate -enddate -noout -in $Ucert", $outFile);
  324. print(OUT "-- \n");
  325. print(OUT "The generated CA certificate is $CAcert\n");
  326. print(OUT "The generated CA private key is $CAkey\n");
  327. print(OUT "The current CA signing serial number is in $CAserial\n");
  328. print(OUT "The generated user certificate is $Ucert\n");
  329. print(OUT "The generated user private key is $Ukey\n");
  330. print(OUT "--\n");
  331. }
  332. ############################################################################
  333. sub log_output( $ $ )
  334. {
  335. my( $desc, $file ) = @_;
  336. my($error) = 0;
  337. my($key);
  338. my($msg);
  339. if ($desc)
  340. {
  341. print("$desc\n");
  342. print(OUT "$desc\n");
  343. }
  344. # loop waiting for test program to complete
  345. while ( stat($file) == 0)
  346. { print(". "); sleep(1); }
  347. # copy test output to log file
  348. open(IN, "<$file");
  349. while (<IN>)
  350. {
  351. print(OUT $_);
  352. if ( $_ =~ /ERROR/ )
  353. {
  354. $error = 1;
  355. }
  356. }
  357. # close and delete the temporary test output file
  358. close(IN);
  359. unlink($file);
  360. if ( $error == 0 )
  361. {
  362. $msg = "Test Succeeded";
  363. }
  364. else
  365. {
  366. $msg = "Test Failed";
  367. }
  368. print(OUT "$msg\n");
  369. if ($pause)
  370. {
  371. print("$msg - press ENTER to continue...");
  372. $key = getc;
  373. print("\n");
  374. }
  375. # Several of the testing scripts run a loop loading the
  376. # same NLM with different options.
  377. # On slow NetWare machines there appears to be some delay in the
  378. # OS actually unloading the test nlms and the OS complains about.
  379. # the NLM already being loaded. This additional pause is to
  380. # to help provide a little more time for unloading before trying to
  381. # load again.
  382. sleep(1);
  383. }
  384. ############################################################################
  385. sub log_desc( $ )
  386. {
  387. my( $desc ) = @_;
  388. print("\n");
  389. print("$desc\n");
  390. print(OUT "\n");
  391. print(OUT "$desc\n");
  392. print(OUT "======================================\n");
  393. }
  394. ############################################################################
  395. sub compare_files( $ $ $ )
  396. {
  397. my( $file1, $file2, $binary ) = @_;
  398. my( $n1, $n2, $b1, $b2 );
  399. my($ret) = 1;
  400. open(IN0, $file1) || die "\nunable to open $file1\n";
  401. open(IN1, $file2) || die "\nunable to open $file2\n";
  402. if ($binary)
  403. {
  404. binmode IN0;
  405. binmode IN1;
  406. }
  407. for (;;)
  408. {
  409. $n1 = read(IN0, $b1, 512);
  410. $n2 = read(IN1, $b2, 512);
  411. if ($n1 != $n2) {last;}
  412. if ($b1 != $b2) {last;}
  413. if ($n1 == 0)
  414. {
  415. $ret = 0;
  416. last;
  417. }
  418. }
  419. close(IN0);
  420. close(IN1);
  421. return($ret);
  422. }
  423. ############################################################################
  424. sub do_wait()
  425. {
  426. my($key);
  427. if ($pause)
  428. {
  429. print("Press ENTER to continue...");
  430. $key = getc;
  431. print("\n");
  432. }
  433. }
  434. ############################################################################
  435. sub make_tmp_cert_file()
  436. {
  437. my @cert_files = <$cert_path\\*.pem>;
  438. # delete the file if it already exists
  439. unlink($tmp_cert);
  440. open( TMP_CERT, ">$tmp_cert") || die "\nunable to open $tmp_cert\n";
  441. print("building temporary cert file\n");
  442. # create a temporary cert file that contains all the certs
  443. foreach $i (@cert_files)
  444. {
  445. open( IN_CERT, $i ) || die "\nunable to open $i\n";
  446. for(;;)
  447. {
  448. $n = sysread(IN_CERT, $data, 1024);
  449. if ($n == 0)
  450. {
  451. close(IN_CERT);
  452. last;
  453. };
  454. syswrite(TMP_CERT, $data, $n);
  455. }
  456. }
  457. close( TMP_CERT );
  458. }