#!/usr/bin/perl # # Copyright (C) 1998-2016, OFFIS e.V. # All rights reserved. See COPYRIGHT file for details. # # This software and supporting documentation were developed by # # OFFIS e.V. # R&D Division Health # Escherweg 2 # D-26121 Oldenburg, Germany # # # Module: dcmtls # # Author: Marco Eichelberg # # Purpose: # This script simplifies the set-up and operation of a Certification # Authority by means of OpenSSL, for use with the TLS-enhanced # DCMTK DICOM Tools. # $const_openssl="openssl"; $const_ca_key="cakey.pem"; $const_ca_cert="cacert.pem"; $const_ca_config="openssl.cnf"; $const_ca_seed="randseed.bin"; $numCommands = 0; for ($i=0; $i<=$#ARGV; $i++) { $_ = $ARGV[$i]; if (/^-/) { $options{$_} = $ARGV[++$i]; } else { $command[$numCommands++] = $_; } } if ($numCommands < 1) { &usageAndExit(); } if ($command[0] eq 'newca') { if ($numCommands != 2) { &usageAndExit(); } &createNewCA($command[1]); exit($?); } elsif ($command[0] eq 'mkcert') { if ($numCommands != 4) { &usageAndExit(); } &createNewCertificate($command[1], $command[2], $command[3]); exit($?); } else { &usageAndExit(); } exit(0); # # usageAndExit() # print usage string and terminate. # sub usageAndExit { print <${ca_directory}/serial"; print OUT "01\n"; close OUT; open OUT, ">${ca_directory}/index.txt"; close OUT; &createNewConfigFile($ca_directory); system ("$const_openssl rand -out ${ca_directory}/private/$const_ca_seed 1024"); if ($type eq 'dsa') { system ("$const_openssl dsaparam -out ${ca_directory}/private/dsaparam.pem $bits"); } else { } system ("$const_openssl req -config ${ca_directory}/$const_ca_config $keytype -x509 -keyout ${ca_directory}/private/$const_ca_key -out ${ca_directory}/$const_ca_cert $days"); system ("cp ${ca_directory}/$const_ca_cert ${ca_directory}/certs/00.pem"); system ("cd ${ca_directory}/certs; ln -s 00.pem `openssl x509 -hash -noout -in 00.pem`.0"); return $?; } # # createNewCertificate(string cadir, string certfile, string certkey) # creates a new key pair consisting of a private key file and a certificate file # signed by the CA. # @param cadir directory of the CA # @param certfile certificate file to be written # @param certkey private key file to be written # sub createNewCertificate { local($ca_directory, $certfile, $certkey) = @_; local($days) = $options{'-days'}; if ($days ne '') { $days = "-days $days"; } local($ca_certificate) = $options{'-cacert'}; local($bits) = $options{'-bits'}; if ($bits == 0) { $bits = 2048; } local($type) = $options{'-type'}; local($encryption); local($des) = $options{'-des'}; local($pkcs12) = $options{'-pkcs12'}; local($pkcs12name) = $options{'-pkcs12name'}; if ($pkcs12name eq '') { $pkcs12name = "OpenSSL generated DCMTK Certificate"; } if ($des eq 'no') { $encryption = '-nodes'; } local($keytype); if ($type eq 'dsa') { $keytype = "-newkey dsa:${ca_directory}/private/tempdh.pem"; } else { $keytype = "-newkey rsa:$bits"; } if (! -d "$ca_directory") { die "error: $ca_directory not found, bailing out."; } if ($type eq 'dsa') { system ("$const_openssl dsaparam -out ${ca_directory}/private/tempdh.pem $bits"); } system ("$const_openssl req -config ${ca_directory}/$const_ca_config $encryption $keytype -keyout $certkey -out ${ca_directory}/private/tempreq.pem"); system ("$const_openssl ca -config ${ca_directory}/$const_ca_config -policy policy_anything -in ${ca_directory}/private/tempreq.pem -out $certfile $days"); if ($type eq 'dsa') { unlink "${ca_directory}/private/tempdh.pem"; } unlink "${ca_directory}/private/tempreq.pem"; @newfiles = `cd ${ca_directory}/newcerts; ls *.pem`; foreach (@newfiles) { chop; system("mv ${ca_directory}/newcerts/$_ ${ca_directory}/certs"); system("cd ${ca_directory}/certs; ln -s $_ `$const_openssl x509 -hash -noout -in $_`.0"); } if ($pkcs12 ne '') { system ("$const_openssl pkcs12 -in $certfile -inkey $certkey -certfile ${ca_directory}/$const_ca_cert -out $pkcs12 -export -name \"$pkcs12name\""); } return $?; } # # createNewConfigFile(string directory) # creates new default configuration file named $const_ca_config in CA directory. # @param directory CA directory. # sub createNewConfigFile { local($ca_directory) = @_; open OUT, ">${ca_directory}/$const_ca_config"; ##################################################################### print OUT <