script/cert-req.pl untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 3 Podpod n/a totaltotal 0.0% 0 / 159 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2  
3 use VOMS::Lite::PEMHelper qw(readCert readAC readPrivateKey writeCert writeKey);
4 use VOMS::Lite::ASN1Helper qw(ASN1Wrap ASN1Unwrap DecToHex Hex ASN1BitStr);
5 use VOMS::Lite::REQ;
6  
7 my $name=$0;
8 my $lname=length($name);
9 $name =~ s#.*/##g;
10 my $usage = "Usage: $name [ -cert /path/to/cert.pem ]\n".
11 " " x ($lname+6). "[ -key /path/to/request signers/key.pem ]\n".
12 " " x ($lname+6). "[ -out /path/to/save/req ]\n".
13 " " x ($lname+6). "[ -keyout /path/to/save/key ]\n".
14 " " x ($lname+6). "[ -passout password-to-encrypt-output-key ]\n".
15 " " x ($lname+6). "[ -pass password-to-decrypt-cert-key ]\n".
16 " " x ($lname+6). "[ -bits N (any of {512|1024|2048|4096} default 1024) ]\n".
17 " " x ($lname+8). "[ -host dns.of.host.for.alt.name ]\n".
18 " " x ($lname). "The following will be processsed in the order they are encountered\n".
19 " " x ($lname+6). "[ -C countryName ]\n".
20 " " x ($lname+6). "[ -O Organisation ]\n".
21 " " x ($lname+6). "[ -OU OrganisationUnit ]\n".
22 " " x ($lname+6). "[ -L Location ]\n".
23 " " x ($lname+6). "[ -DC DomainComponent ]\n".
24 " " x ($lname+6). "[ -ST StateOrProvince ]\n".
25 " " x ($lname+6). "[ -UID UserID ]\n".
26 " " x ($lname+6). "[ -Email emailAddress ]\n".
27 " " x ($lname+6). "[ -CN CommonName ]\n";
28  
29 my %Input;
30 my $outfile;
31 my $outkeyfile;
32 my $lifetime=8544;
33 my $passout;
34 my $pass;
35 my $Key;
36  
37 while ($_=shift @ARGV) {
38 if ( /^--?cert$/ ) {
39 my $Cert=shift @ARGV;
40 die "$& requires an argument\n$usage" if ( ! defined $Cert );
41 die "cannot open CA certificate file $Cert\n$usage" if ( ! -r $Cert );
42 my @decodedCERTS=readCert($Cert);
43 $Input{'Cert'}=$decodedCERTS[0];
44 }
45 elsif ( /^--?key$/ ) {
46 $Key=shift @ARGV;
47 die "$_ requires an argument\n$usage" if ( ! defined $Key );
48 die "cannot open key file $Key\n$usage" if ( ! -r $Key );
49 }
50 elsif ( /^--?out$/ ) {
51 $outfile=shift @ARGV;
52 die "$_ requires an argument\n$usage" if ( ! defined $outfile );
53 }
54 elsif ( /^--?keyout$/ ) {
55 $outkeyfile=shift @ARGV;
56 die "$_ requires an argument\n$usage" if ( ! defined $outkeyfile );
57 }
58 elsif ( /^--?bits$/ ) {
59 my $bits=shift @ARGV;
60 die "$_ requires an argument\n$usage" if ( ! defined $bits );
61 die "$_ requires a valus of 512, 1024, 2048 or 4096.\n$usage" if ( $bits !~ /^(512|1024|2048|4096)$/ );
62 $Input{'Bits'}=$bits;
63 }
64 elsif ( /^--?host$/ ) {
65 $host=shift @ARGV;
66 die "$_ requires an argument" if ( ! defined $host );
67 }
68 elsif ( /^--?passout$/ ) {
69 $passout=shift @ARGV;
70 die "$_ requires an argument\n$usage" if ( ! defined $passout );
71 }
72 elsif ( /^--?pass$/ ) {
73 $pass=shift @ARGV;
74 die "$_ requires an argument\n$usage" if ( ! defined $pass );
75 }
76 elsif ( /^--?(C|O|OU|L|DC|ST|UID|Email|CN)$/ ) {
77 my $value=shift @ARGV;
78 die "$_ requires an argument\n$usage" if ( ! defined $value );
79 push @DN,"$1=$value";
80 if ( $1 eq "Email" ) { $email=$value; }
81 }
82 else { die "Unrecognised option \"$_\"\n$usage"; }
83 }
84  
85 $lifetime *= 3600 * 24 * 356; # 1 year
86 if ( ! defined $outfile ) { die "Certificate output file not specified.\n$usage"; }
87 if ( ! defined $outkeyfile && ! defined $Key) { die "Key output file not specified.\n$usage"; }
88  
89 if ( @DN == 0 && ! defined $Input{'Cert'} ) { die "No DN attributes specified.\n$usage"; }
90 if ( @DN != 0 ) { $Input{'DN'}=\@DN; }
91  
92 if ( defined $Key ) { $Input{'Key'}=readPrivateKey($Key,$pass); }
93 if ( defined $host ) { $Input{'subjectAltName'}=["dNSName=$host"]; }
94 elsif ( defined $email ) { $Input{'subjectAltName'}=["rfc822Name=$email"]; }
95  
96  
97  
98 my %Output = %{ VOMS::Lite::REQ::Create(\%Input) };
99  
100 if ( ! defined $Output{Req} || ! defined $Output{Key} ) {
101 foreach ( @{ $Output{Errors} } ) { print "Error: $_\n"; }
102 die "Failed to create X509 Certificate";
103 }
104  
105 foreach ( @{ $Output{Warnings} } ) { print "Warning: $_\n"; }
106  
107 writeCert($outfile, $Output{'Req'}, "CERTIFICATE REQUEST"); ### Needs it's type changing
108 if ( ! defined $Key) {
109 writeKey($outkeyfile, $Output{'Key'}, $passout);
110 }
111  
112 __END__