| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use VOMS::Lite::PEMHelper qw(readCert readPrivateKey writeCert writeKey); | ||
| 4 | use VOMS::Lite::ASN1Helper qw(ASN1Wrap ASN1Unwrap DecToHex Hex ASN1BitStr); | ||
| 5 | use VOMS::Lite::X509; | ||
| 6 | |||
| 7 | my $name=$0; | ||
| 8 | $name =~ s#.*/##g; | ||
| 9 | my $lname=length($name); | ||
| 10 | my $usage = "Usage: $name [ -cacert /path/to/cacert.pem ]\n". | ||
| 11 | " " x ($lname+8). "[ -cakey /path/to/cacert's/key.pem ]\n". | ||
| 12 | " " x ($lname+8). "[ -out /path/to/save/cert ]\n". | ||
| 13 | " " x ($lname+8). "[ -outkey /path/to/save/key ]\n". | ||
| 14 | " " x ($lname+8). "[ -passout password-to-encrypt-output-key ]\n". | ||
| 15 | " " x ($lname+8). "[ -capass password-to-decrypt-cakey ]\n". | ||
| 16 | " " x ($lname+8). "[ -lifetime N (hours, default 8544 hours (365 days)) ]\n". | ||
| 17 | " " x ($lname+8). "[ -serial N ]\n". | ||
| 18 | " " x ($lname+8). "[ -host dns.of.host.for.alt.name ]\n". | ||
| 19 | " " x ($lname+8). "[ -bits N (any of {512|1024|2048|4096} default 1024) ]\n". | ||
| 20 | " " x ($lname+8). "[ -CA ]\n". | ||
| 21 | " ". "The following will be processsed in the order they are encountered\n". | ||
| 22 | " " x ($lname+8). "[ -C countryName ]\n". | ||
| 23 | " " x ($lname+8). "[ -O Organisation ]\n". | ||
| 24 | " " x ($lname+8). "[ -OU OrganisationUnit ]\n". | ||
| 25 | " " x ($lname+8). "[ -L Location ]\n". | ||
| 26 | " " x ($lname+8). "[ -DC DomainComponent ]\n". | ||
| 27 | " " x ($lname+8). "[ -ST StateOrProvince ] (Not recommended)\n". | ||
| 28 | " " x ($lname+8). "[ -UID UserID ] (Not recommended)\n". | ||
| 29 | " " x ($lname+8). "[ -Email emailAddress ]\n". | ||
| 30 | " " x ($lname+8). "[ -CN CommonName ]\n"; | ||
| 31 | |||
| 32 | my %Input; | ||
| 33 | my $outfile; | ||
| 34 | my $outkeyfile; | ||
| 35 | my $CA; | ||
| 36 | my $lifetime=8544; | ||
| 37 | my $passout; | ||
| 38 | my $capass; | ||
| 39 | my $CAKey; | ||
| 40 | my $host; | ||
| 41 | my $email; | ||
| 42 | |||
| 43 | while ($_=shift @ARGV) { | ||
| 44 | if ( /^--?cacert$/ ) { | ||
| 45 | my $CACert=shift @ARGV; | ||
| 46 | die "$& requires an argument" if ( ! defined $CACert ); | ||
| 47 | die "cannot open CA certificate file $CACert" if ( ! -r $CACert ); | ||
| 48 | my @decodedCERTS=readCert($CACert); | ||
| 49 | $Input{'CACert'}=$decodedCERTS[0]; | ||
| 50 | } | ||
| 51 | elsif ( /^--?cakey$/ ) { | ||
| 52 | $CAKey=shift @ARGV; | ||
| 53 | die "$_ requires an argument" if ( ! defined $CAKey ); | ||
| 54 | die "cannot open CA key file $CAKey" if ( ! -r $CAKey ); | ||
| 55 | } | ||
| 56 | elsif ( /^--?out$/ ) { | ||
| 57 | $outfile=shift @ARGV; | ||
| 58 | die "$_ requires an argument" if ( ! defined $outfile ); | ||
| 59 | } | ||
| 60 | elsif ( /^--?outkey$/ ) { | ||
| 61 | $outkeyfile=shift @ARGV; | ||
| 62 | die "$_ requires an argument" if ( ! defined $outkeyfile ); | ||
| 63 | } | ||
| 64 | elsif ( /^--?lifetime$/ ) { | ||
| 65 | $lifetime=shift @ARGV; | ||
| 66 | die "$_ requires an argument" if ( ! defined $lifetime ); | ||
| 67 | die "$_ requires a positive numeric integer argument." if ( $lifetime !~ /^[0-9]+$/ ); | ||
| 68 | } | ||
| 69 | elsif ( /^--?serial$/ ) { | ||
| 70 | my $serial=shift @ARGV; | ||
| 71 | die "$_ requires an argument" if ( ! defined $serial ); | ||
| 72 | die "$_ requires a positive numeric integer argument." if ( $serial !~ /^[0-9]+$/ ); | ||
| 73 | $Input{'Serial'}=$serial; | ||
| 74 | } | ||
| 75 | elsif ( /^--?bits$/ ) { | ||
| 76 | my $bits=shift @ARGV; | ||
| 77 | die "$_ requires an argument" if ( ! defined $bits ); | ||
| 78 | die "$_ requires a valus of 512, 1024, 2048 or 4096." if ( $bits !~ /^(512|1024|2048|4096)$/ ); | ||
| 79 | $Input{'Bits'}=$bits; | ||
| 80 | } | ||
| 81 | elsif ( /^--?host$/ ) { | ||
| 82 | $host=shift @ARGV; | ||
| 83 | die "$_ requires an argument" if ( ! defined $host ); | ||
| 84 | } | ||
| 85 | elsif ( /^--?passout$/ ) { | ||
| 86 | $passout=shift @ARGV; | ||
| 87 | die "$_ requires an argument" if ( ! defined $passout ); | ||
| 88 | } | ||
| 89 | elsif ( /^--?capass$/ ) { | ||
| 90 | $capass=shift @ARGV; | ||
| 91 | die "$_ requires an argument" if ( ! defined $capass ); | ||
| 92 | } | ||
| 93 | elsif ( /^--?CA$/ ) { | ||
| 94 | $CA="True"; | ||
| 95 | $Input{'CA'}=$CA; | ||
| 96 | } | ||
| 97 | elsif ( /^--?EEC$/ ) { | ||
| 98 | $CA="False"; | ||
| 99 | $Input{'CA'}=$CA; | ||
| 100 | } | ||
| 101 | elsif ( /^--?(C|O|OU|L|DC|ST|UID|Email|CN)$/ ) { | ||
| 102 | my $value=shift @ARGV; | ||
| 103 | die "$_ requires an argument" if ( ! defined $value ); | ||
| 104 | push @DN,"$1=$value"; | ||
| 105 | if ( $1 eq "Email" ) { $email=$value; } | ||
| 106 | } | ||
| 107 | else { die "Unrecognised option \"$_\"\n$usage"; } | ||
| 108 | } | ||
| 109 | |||
| 110 | $lifetime *= 3600; # Seconde -> hours | ||
| 111 | if ( ! defined $outfile ) { die "Certificate output file not specified."; } | ||
| 112 | if ( ! defined $outkeyfile ) { die "Key output file not specified."; } | ||
| 113 | if ( ! defined $Input{'Serial'} ) { die "Serial number not specified."; } | ||
| 114 | if ( @DN == 0 ) { die "No DN attributes specified."; } | ||
| 115 | |||
| 116 | if ( defined $CAKey ) { $Input{'CAKey'}=readPrivateKey($CAKey,$capass); } | ||
| 117 | $Input{'Lifetime'}=$lifetime; | ||
| 118 | $Input{'DN'}=\@DN; | ||
| 119 | if ( defined $host ) { $Input{'subjectAltName'}=["dNSName=$host"]; } | ||
| 120 | elsif ( defined $email ) { $Input{'subjectAltName'}=["rfc822Name=$email"]; } | ||
| 121 | |||
| 122 | my %Output = %{ VOMS::Lite::X509::Create(\%Input) }; | ||
| 123 | |||
| 124 | if ( ! defined $Output{Cert} || ! defined $Output{Key} ) { | ||
| 125 | foreach ( @{ $Output{Errors} } ) { print "Error: $_\n"; } | ||
| 126 | die "Failed to create X509 Certificate"; | ||
| 127 | } | ||
| 128 | |||
| 129 | foreach ( @{ $Output{Warnings} } ) { print "Warning: $_\n"; } | ||
| 130 | |||
| 131 | writeCert($outfile, $Output{'Cert'}); | ||
| 132 | writeKey($outkeyfile, $Output{'Key'}, $passout); | ||
| 133 | |||
| 134 | __END__ |