| 1 | package VOMS::Lite::REQ; | ||
| 2 | |||
| 3 | use 5.004; | ||
| 4 | use strict; | ||
| 5 | use VOMS::Lite::CertKeyHelper qw(OIDtoDNattrib digestSign DNattribToOID); | ||
| 6 | use VOMS::Lite::ASN1Helper qw(ASN1OIDtoOID ASN1Index ASN1Wrap ASN1Unwrap ASN1UnwrapHex DecToHex Hex ASN1BitStr OIDtoASN1OID); | ||
| 7 | use VOMS::Lite::X509; | ||
| 8 | use VOMS::Lite::KEY; | ||
| 9 | use Digest::SHA1 qw(sha1_hex); | ||
| 10 | use Digest::MD5 qw(md5); | ||
| 11 | use VOMS::Lite::RSAKey; | ||
| 12 | |||
| 13 | require Exporter; | ||
| 14 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||
| 15 | @ISA = qw(Exporter); | ||
| 16 | |||
| 17 | $VERSION = '0.20'; | ||
| 18 | |||
| 19 | sub Examine { | ||
| 20 | my ($decoded,$dataref)=@_; | ||
| 21 | my %Values=%$dataref; | ||
| 22 | my @ASN1Index=ASN1Index($decoded); | ||
| 23 | |||
| 24 | return ( {Errors=> [ "Unable to parse certificate request" ]} ) if (@ASN1Index==0); | ||
| 25 | |||
| 26 | my ($index,$ignoreuntil)=(0,0); | ||
| 27 | my ($REQInfo,$REQversion,$REQsubject,$REQsubjectPublicKeyInfo,$REQattributes,$REQSignatureAlgorithm,$REQSignatureValue); | ||
| 28 | |||
| 29 | # Drill down into the certificate | ||
| 30 | shift @ASN1Index; # skip the wrapping of the certificate sequence | ||
| 31 | my $REQInfoRef=shift @ASN1Index; #CertificateTBS Sequence | ||
| 32 | if (defined $Values{REQInfo}) { | ||
| 33 | my ($CLASS,$CONSTRUCTED,$TAG,$HEADSTART,$HEADLEN,$CHUNKLEN) = @$REQInfoRef; | ||
| 34 | $Values{REQInfo}=substr($decoded,$HEADSTART,($HEADLEN+$CHUNKLEN)); | ||
| 35 | } | ||
| 36 | |||
| 37 | # Extract the main components of the certificate | ||
| 38 | foreach (@ASN1Index) { | ||
| 39 | my ($CLASS,$CONSTRUCTED,$TAG,$HEADSTART,$HEADLEN,$CHUNKLEN) = @$_; | ||
| 40 | if ( $HEADSTART < $ignoreuntil ) { next; } | ||
| 41 | else { | ||
| 42 | if ($index==0 && $TAG==2) {$REQversion = substr($decoded,$HEADSTART,($HEADLEN+$CHUNKLEN));$index++;} | ||
| 43 | elsif ($index==1 && $TAG==16) {$REQsubject = substr($decoded,$HEADSTART,($HEADLEN+$CHUNKLEN));$index++;} | ||
| 44 | elsif ($index==2 && $TAG==16) {$REQsubjectPublicKeyInfo = substr($decoded,$HEADSTART,($HEADLEN+$CHUNKLEN));$index++;} | ||
| 45 | elsif ($index==3 && $CLASS==2 && $TAG==0) {$REQattributes = substr($decoded,$HEADSTART,($HEADLEN+$CHUNKLEN));} | ||
| 46 | elsif ($index==3 && $TAG==16) {$REQSignatureAlgorithm = substr($decoded,$HEADSTART,($HEADLEN+$CHUNKLEN));$index++;} | ||
| 47 | elsif ($index==4 && $TAG==3) {$REQSignatureValue = substr($decoded,$HEADSTART,($HEADLEN+$CHUNKLEN));} | ||
| 48 | $ignoreuntil=$HEADSTART+$HEADLEN+$CHUNKLEN; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | if ($index != 4) {return undef;} #Failed to read certificate request | ||
| 52 | |||
| 53 | #Standard | ||
| 54 | if (defined $Values{REQversion}) {$Values{REQversion}=$REQversion;} | ||
| 55 | if (defined $Values{REQsubject}) {$Values{REQsubject}=$REQsubject;} | ||
| 56 | if (defined $Values{REQsubjectPublicKeyInfo}) {$Values{REQsubjectPublicKeyInfo}=$REQsubjectPublicKeyInfo;} | ||
| 57 | if (defined $Values{REQattributes}) {$Values{REQattributes}=$REQattributes;} | ||
| 58 | if (defined $Values{REQSignatureAlgorithm}) {$Values{REQSignatureAlgorithm}=$REQSignatureAlgorithm;} | ||
| 59 | if (defined $Values{REQSignatureValue}) {$Values{REQSignatureValue}=$REQSignatureValue;} | ||
| 60 | |||
| 61 | ################## | ||
| 62 | # Helpers -- Deeper parsing of certificate | ||
| 63 | |||
| 64 | # The String Repersentation of the subject and issuer DNs | ||
| 65 | if (defined $Values{SubjectDN}) { | ||
| 66 | my @ASN1SubjectDNIndex=ASN1Index($REQsubject); | ||
| 67 | shift @ASN1SubjectDNIndex; | ||
| 68 | $Values{SubjectDN}=""; | ||
| 69 | while (@ASN1SubjectDNIndex) { | ||
| 70 | my ($CLASS,$CONSTRUCTED,$TAG,$HEADSTART,$HEADLEN,$CHUNKLEN)=(0,0,0,0,0); | ||
| 71 | until ($TAG == 6 ) { ($CLASS,$CONSTRUCTED,$TAG,$HEADSTART,$HEADLEN,$CHUNKLEN) = @{shift @ASN1SubjectDNIndex}; } | ||
| 72 | my $OID=substr($REQsubject,($HEADSTART+$HEADLEN),$CHUNKLEN); | ||
| 73 | ($CLASS,$CONSTRUCTED,$TAG,$HEADSTART,$HEADLEN,$CHUNKLEN) = @{shift @ASN1SubjectDNIndex}; | ||
| 74 | my $Value=substr($REQsubject,($HEADSTART+$HEADLEN),$CHUNKLEN); | ||
| 75 | $Values{SubjectDN}.="/".OIDtoDNattrib(ASN1OIDtoOID($OID))."=$Value"; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | # Public key and Modulus | ||
| 80 | if ( defined $Values{KeypublicExponent} || defined $Values{Keymodulus} ) { | ||
| 81 | my ($OID,$modexpbitstr); | ||
| 82 | my @KeyIndex=ASN1Index($REQsubjectPublicKeyInfo); | ||
| 83 | foreach (@KeyIndex) { | ||
| 84 | my ($CLASS,$CONSTRUCTED,$TAG,$HEADSTART,$HEADLEN,$CHUNKLEN) = @$_; | ||
| 85 | if ( ! $CONSTRUCTED ) { | ||
| 86 | $OID=Hex(substr($REQsubjectPublicKeyInfo,($HEADSTART+$HEADLEN),$CHUNKLEN)) if ( $TAG == 6 ); | ||
| 87 | $modexpbitstr=substr($REQsubjectPublicKeyInfo,($HEADSTART+$HEADLEN),$CHUNKLEN) if ( $TAG == 3 ); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | if ( $OID eq "2a864886f70d010101" ) { | ||
| 91 | $modexpbitstr=~ s/.//; # BS always encoding 8 bit bytes | ||
| 92 | my @KeyIndex2=ASN1Index($modexpbitstr); | ||
| 93 | shift @KeyIndex2; | ||
| 94 | my ($CLASS,$CONSTRUCTED,$TAG,$HEADSTART,$HEADLEN,$CHUNKLEN) = @{shift @KeyIndex2}; | ||
| 95 | $Values{Keymodulus}=substr($modexpbitstr,($HEADSTART+$HEADLEN),$CHUNKLEN); | ||
| 96 | ($CLASS,$CONSTRUCTED,$TAG,$HEADSTART,$HEADLEN,$CHUNKLEN) = @{shift @KeyIndex2}; | ||
| 97 | $Values{KeypublicExponent}=substr($modexpbitstr,($HEADSTART+$HEADLEN),$CHUNKLEN); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | # Signature Value | ||
| 102 | if (defined $Values{SignatureValue} || defined $Values{SignatureType}) { | ||
| 103 | $Values{SignatureValue}=substr(ASN1Unwrap($REQSignatureValue),1); | ||
| 104 | my $HexREQsignature=Hex($REQSignatureAlgorithm); | ||
| 105 | if ( $HexREQsignature eq "300d06092a864886f70d0101040500" ) { $Values{SignatureType}="md5WithRSA"; } | ||
| 106 | elsif ( $HexREQsignature eq "300d06092a864886f70d0101050500" ) { $Values{SignatureType}="sha1WithRSA"; } | ||
| 107 | elsif ( $HexREQsignature eq "300d06092a864886f70d0101030500" ) { $Values{SignatureType}="md4WithRSA"; } | ||
| 108 | elsif ( $HexREQsignature eq "300d06092a864886f70d0101020500" ) { $Values{SignatureType}="md2WithRSA"; } | ||
| 109 | else { $Values{SignatureType}="unrecognised"; } | ||
| 110 | } | ||
| 111 | return (\%Values); | ||
| 112 | } | ||
| 113 | |||
| 114 | #################################################### | ||
| 115 | |||
| 116 | |||
| 117 | sub Create { | ||
| 118 | |||
| 119 | # Load in Context | ||
| 120 | my %context = %{ shift() }; | ||
| 121 | |||
| 122 | # Create error and warning arrays | ||
| 123 | my @Errors; | ||
| 124 | my @Warnings; | ||
| 125 | |||
| 126 | # Get request time; | ||
| 127 | my $now=time(); | ||
| 128 | |||
| 129 | # Check for required input values | ||
| 130 | if ( ! defined $context{'DN'} && ! defined $context{'Cert'} ) { push @Errors, "REQ: Distinguished Name not supplied"; } | ||
| 131 | if ( defined $context{'DN'} && defined $context{'Cert'} ) { push @Errors, "REQ: Two methods specified for DN"; } | ||
| 132 | |||
| 133 | # Bail if there isn't enough information | ||
| 134 | if ( @Errors > 0 ) { return { Errors => \@Errors} ; } | ||
| 135 | |||
| 136 | # Check input values | ||
| 137 | if ( defined $context{'DN'} && ref($context{'DN'}) ne "ARRAY" ) { push @Errors, "REQ: DN Must be a reference to an array of distinguished name component strings."; } | ||
| 138 | if ( defined $context{'Bits'} && $context{'Bits'} !~ /^(512|1024|2048|4096)$/ ) { push @Errors, "REQ: Key size can only be 512, 1024, 2048 or 4096."; } | ||
| 139 | elsif ( ! defined $context{'Bits'} ) { $context{'Bits'} = 1024; } | ||
| 140 | my $Verbosity=((defined $context{'Quiet'})?0:1); | ||
| 141 | |||
| 142 | # Bail if inputs are not the right format | ||
| 143 | if ( @Errors > 0 ) { return { Errors => \@Errors} ; } | ||
| 144 | |||
| 145 | # Load input data into local variables | ||
| 146 | my ($CertInfoRef,$KeyInfoRef)=(undef,undef); | ||
| 147 | my %CI; | ||
| 148 | my %KI; | ||
| 149 | if ( defined $context{'Cert'} ) { | ||
| 150 | $CertInfoRef = (($context{'Cert'} =~ /^(\060.+)$/s) ? VOMS::Lite::X509::Examine($&, {X509subject=>"", SubjectDN=>""}) : undef); | ||
| 151 | if ( defined $CertInfoRef ) { %CI=%$CertInfoRef; } else { push @Errors, "REQ: Unable to parse CA certificate."; } | ||
| 152 | } | ||
| 153 | if ( defined $context{'Key'} ) { | ||
| 154 | $KeyInfoRef = (($context{'Key'} =~ /^(\060.+)$/s) ? VOMS::Lite::KEY::Examine($&, {Keymodulus=>"", KeyprivateExponent=>"", KeypublicExponent=>""}) : undef); | ||
| 155 | if ( defined $KeyInfoRef ) { %KI=%$KeyInfoRef; } else { push @Errors, "REQ: Unable to parse CA key."; } | ||
| 156 | } | ||
| 157 | |||
| 158 | # Bail if there is a certificate Parse error | ||
| 159 | if ( @Errors > 0 ) { return { Errors => \@Errors} ; } | ||
| 160 | |||
| 161 | # Check for unknown options | ||
| 162 | foreach (keys %context) { if ( ! /^(Quiet|DN|Bits|Cert|Key|subjectAltName)$/ ) {push @Errors, "REQ: $_ is an invalid option.";}} | ||
| 163 | |||
| 164 | # Bail if any recognised options are invalid | ||
| 165 | if ( @Errors > 0 ) { return { Errors => \@Errors} ; } | ||
| 166 | |||
| 167 | ################################################################### | ||
| 168 | # Do not edit below these lines (unless there's a bug of course!) # | ||
| 169 | ################################################################### | ||
| 170 | |||
| 171 | my $host=0; | ||
| 172 | |||
| 173 | # Check and parse the DN array referenced | ||
| 174 | if ( defined $context{'DN'} ) { | ||
| 175 | $CI{'X509subject'}=""; | ||
| 176 | foreach (@{ $context{'DN'} }) { | ||
| 177 | my ($attrib,$value)=split(/=/,$_,2); # Splits attribute and value | ||
| 178 | my $OID = DNattribToOID($attrib); # Convert Attribute to dot representation e.g. CN -> 2.5.4.3 | ||
| 179 | if ( defined $OID ) { | ||
| 180 | my $STRtype; | ||
| 181 | if ( $value =~ /^[a-zA-Z0-9 \x22()+,.\/:?-]*$/ ) { $STRtype="13"; } # Printable String | ||
| 182 | elsif ( $value =~ /^[\x00\x07-\x0f\x11-\x14\x18-\x1b\x20-\x23\x25-\x7d\x7f]*$/ ) { $STRtype="16"; } #IA5 String | ||
| 183 | else { push @Errors, "REQ: Can't find an apropriate encoding for $attrib+$value."; } | ||
| 184 | if ( defined $STRtype ) { $CI{'X509subject'} .= ASN1Wrap("31",ASN1Wrap("30",ASN1Wrap("06",Hex(OIDtoASN1OID($OID))).ASN1Wrap($STRtype,Hex($value)))) }; | ||
| 185 | if ( $OID eq '2.5.4.3' && $value =~ /^[a-zA-Z0-9]+\.[a-zA-Z0-9]+$/ ) { $host = 1;} # detect that this is a host name for later use | ||
| 186 | } | ||
| 187 | else { push @Errors, "REQ: unknown Attribute: $attrib"; } | ||
| 188 | } | ||
| 189 | if ( $CI{'X509subject'} eq "" ) { push @Errors, "REQ: No Attributes in Distunguished Name"; } ; | ||
| 190 | $CI{'X509subject'}=ASN1Wrap("30",$CI{'X509subject'}); # The DN in an apropriate X.509 ASN1 structure. | ||
| 191 | $CI{'X509subject'}=~ s/(..)/pack('C',hex($&))/ge; | ||
| 192 | } | ||
| 193 | elsif ( ! defined $CI{'X509subject'} ) { push @Errors, "REQ: Unable to obtain Subject of certificate supplied"; } | ||
| 194 | else { if ( $CI{'SubjectDN'} =~ /\/CN=[a-zA-Z0-9]+\.[a-zA-Z0-9]+/ ) {$host = 1;} } # detect that this is a host name for later use | ||
| 195 | |||
| 196 | # Bail if DN is bad | ||
| 197 | if ( @Errors > 0 ) { return { Errors => \@Errors} ; } | ||
| 198 | |||
| 199 | ############################### | ||
| 200 | #OK Let's create an REQ Cred!# | ||
| 201 | ############################### | ||
| 202 | |||
| 203 | my $Keymodulus; | ||
| 204 | my $KeyprivateExponent; | ||
| 205 | my $KeypublicExponent; | ||
| 206 | my $Privatekey; | ||
| 207 | if ( ! defined $context{'Key'} ) { # Need to generate a key pair | ||
| 208 | |||
| 209 | # Generate Key Pair | ||
| 210 | # my $keyref = VOMS::Lite::Key::GenRSAKey( { Bits => 512, Verbose => (defined $context{'Quiet'})?undef:"y" } ); | ||
| 211 | my $keyref = VOMS::Lite::RSAKey::Create( { Bits => $context{'Bits'}, Verbose => (defined $context{'Quiet'})?undef:"y" } ); | ||
| 212 | if ( ! defined $keyref ) { return { Errors => [ "REQ: Key Generation Failure" ] } ; } | ||
| 213 | my %key = %{ $keyref }; | ||
| 214 | if ( defined $key{'Error'} ) { return { Errors => [ "REQ: Error in Key Generation ".$key{'Error'} ] } ; } | ||
| 215 | |||
| 216 | ###Private Key##################################################### | ||
| 217 | |||
| 218 | # Keyversion Keymodulus KeypublicExponent KeyprivateExponent | ||
| 219 | # Keyprime1 Keyprime2 Keyexponent1 Keyexponent2 Keycoefficient | ||
| 220 | |||
| 221 | my $Keyversion = "020100"; | ||
| 222 | $Keymodulus = ASN1Wrap("02",DecToHex($key{Modulus})); | ||
| 223 | $KeypublicExponent = ASN1Wrap("02",DecToHex($key{PublicExponent})); | ||
| 224 | $KeyprivateExponent = ASN1Wrap("02",DecToHex($key{PrivateExponent})); | ||
| 225 | my $Keyprime1 = ASN1Wrap("02",DecToHex($key{Prime1})); | ||
| 226 | my $Keyprime2 = ASN1Wrap("02",DecToHex($key{Prime2})); | ||
| 227 | my $Keyexponent1 = ASN1Wrap("02",DecToHex($key{Exponent1})); | ||
| 228 | my $Keyexponent2 = ASN1Wrap("02",DecToHex($key{Exponent2})); | ||
| 229 | my $Keycoefficient = ASN1Wrap("02",DecToHex($key{Iqmp})); | ||
| 230 | |||
| 231 | $Privatekey=ASN1Wrap("30",$Keyversion.$Keymodulus.$KeypublicExponent.$KeyprivateExponent. | ||
| 232 | $Keyprime1.$Keyprime2.$Keyexponent1.$Keyexponent2.$Keycoefficient); | ||
| 233 | |||
| 234 | $KI{Keymodulus} = DecToHex(${ $keyref }{Modulus}); | ||
| 235 | $KI{KeyprivateExponent} = DecToHex(${ $keyref }{PrivateExponent}); | ||
| 236 | $KI{Keymodulus} =~ s/(..)/pack("C",hex($&))/ge; | ||
| 237 | $KI{KeyprivateExponent} =~ s/(..)/pack("C",hex($&))/ge; | ||
| 238 | } | ||
| 239 | else { | ||
| 240 | #foreach (keys %KI) { print "$_ -> ".Hex($KI{$_})."\n"; } | ||
| 241 | # $Keymodulus = ASN1Wrap("02",ASN1Unwrap($KI{Keymodulus})); | ||
| 242 | # $KeypublicExponent = ASN1Wrap("02",ASN1Unwrap($KI{KeypublicExponent})); | ||
| 243 | # $KeyprivateExponent = ASN1Wrap("02",ASN1Unwrap($KI{KeyprivateExponent})); | ||
| 244 | $Keymodulus = ASN1Wrap("02",Hex($KI{Keymodulus})); | ||
| 245 | $KeypublicExponent = ASN1Wrap("02",Hex($KI{KeypublicExponent})); | ||
| 246 | $KeyprivateExponent = ASN1Wrap("02",Hex($KI{KeyprivateExponent})); | ||
| 247 | $Privatekey = $context{'Key'}; | ||
| 248 | } | ||
| 249 | |||
| 250 | ###Request Bits###################################################### | ||
| 251 | # TBSRequest: | ||
| 252 | # REQversion REQsubjext REQsubjectPublicKeyInfo REQext | ||
| 253 | |||
| 254 | #### Certificate Version #### (x509 v3) | ||
| 255 | my $REQversion = "020100"; | ||
| 256 | |||
| 257 | #### Subject #### | ||
| 258 | my $REQsubject=Hex($CI{'X509subject'}); | ||
| 259 | |||
| 260 | #### Public Key (RSA) #### | ||
| 261 | my $PubKeyChunk=ASN1Wrap("30",$Keymodulus.$KeypublicExponent); | ||
| 262 | my $REQsubjectPublicKeyInfo=ASN1Wrap("30",ASN1Wrap("30","06092a864886f70d0101010500").ASN1Wrap("03",ASN1BitStr($PubKeyChunk))); | ||
| 263 | |||
| 264 | #### Something Else #### | ||
| 265 | my $REQExt="a000"; | ||
| 266 | |||
| 267 | # 1.2.840.113549.1.9.14 Request extension | ||
| 268 | # a0 [ SEQ [ OID [ 1.2.840.113549.1.9.14 ] SET [ SEQ [ SEQ [ OID [ 2.5.29.17 ] $SubjectAltName ] ] ] ] ] | ||
| 269 | # Check and parse the SubjectAltName array referenced | ||
| 270 | my $SubjectAltNameExt=""; | ||
| 271 | if ( defined $context{'subjectAltName'} ) { | ||
| 272 | my $SubjectAltName=""; | ||
| 273 | foreach (@{ $context{'subjectAltName'} }) { | ||
| 274 | if ( /^otherName=/ ) { push @Errors, "X509: otherName not supported"; } | ||
| 275 | elsif ( /^rfc822Name=([\x00\x07-\x0f\x11-\x14\x18-\x1b\x20-\x23\x25-\x7d\x7f]*)$/ ) | ||
| 276 | { $SubjectAltName.=ASN1Wrap("81",Hex($1)); } | ||
| 277 | elsif ( /^dNSName=([\x00\x07-\x0f\x11-\x14\x18-\x1b\x20-\x23\x25-\x7d\x7f]*)$/ ) | ||
| 278 | { $SubjectAltName.=ASN1Wrap("82",Hex($1)); | ||
| 279 | $host=1; | ||
| 280 | } | ||
| 281 | elsif ( /^x400Address=/ ) { push @Errors, "X509: x400Address not supported"; } | ||
| 282 | elsif ( /^directoryName=(30[0-9a-f]*)$/ ) { $SubjectAltName.=ASN1Wrap("84",$1); } | ||
| 283 | elsif ( /^directoryName=(\060.*)$/ ) { $SubjectAltName.=ASN1Wrap("84",Hex($1)); } | ||
| 284 | elsif ( /^ediPartyName=/ ) { push @Errors, "X509: ediPartyName not supported"; } | ||
| 285 | elsif ( /^uniformResourceIdentifier=([\x00\x07-\x0f\x11-\x14\x18-\x1b\x20-\x23\x25-\x7d\x7f]*)$/ ) | ||
| 286 | { $SubjectAltName.=ASN1Wrap("86",Hex($1)); } | ||
| 287 | elsif ( /^IPAddress=(.{4})$/ ) { $SubjectAltName.=ASN1Wrap("87",$1."ffffffff"); | ||
| 288 | push @Warnings, "X509: Assuming IPv4Address has /32 Mask"; } | ||
| 289 | elsif ( /^IPAddress=([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ ) | ||
| 290 | { $SubjectAltName.=ASN1Wrap("87",Hex(chr($1).chr($2).chr($3).chr($4))."ffffffff"); | ||
| 291 | push @Warnings, "X509: Assuming IPv4Address has /32 Mask";} | ||
| 292 | elsif ( /^IPAddress=([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)\/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/ ) | ||
| 293 | { $SubjectAltName.=ASN1Wrap("87",Hex(chr($1).chr($2).chr($3).chr($4).chr($5).chr($6).chr($7).chr($8)));} | ||
| 294 | elsif ( /^IPAddress=(.{8})$/ ) { $SubjectAltName.=ASN1Wrap("87",Hex($1)); } | ||
| 295 | elsif ( /^IPAddress=(.{16})$/ ) { $SubjectAltName.=ASN1Wrap("87",Hex($1).("\xff" x 16)); | ||
| 296 | push @Warnings, "X509: Assuming IPv6Address has /128 Mask"; } | ||
| 297 | elsif ( /^registeredID=/ ) { push @Errors, "X509: registeredID not supported"; } | ||
| 298 | elsif ( /^(rfc822Name|dNSName|directoryName|uniformResourceIdentifier|IPAddress)=/ ) | ||
| 299 | { push @Errors, "X509: Bad data for $1 subjectAlternitiveName"; } | ||
| 300 | elsif ( /^([^=]+)=/ ) { push @Errors, "X509: unknown generalName $1 for subjectAlternitiveName"; } | ||
| 301 | else { push @Errors, "X509: malformed SubjectAltName entry"; } | ||
| 302 | } | ||
| 303 | $SubjectAltName=ASN1Wrap("04",ASN1Wrap("30",$SubjectAltName)); | ||
| 304 | $SubjectAltNameExt=ASN1Wrap("30","0603551d11".$SubjectAltName); | ||
| 305 | } | ||
| 306 | |||
| 307 | my $BasicConstraints='30090603551d1304023000'; #CA False | ||
| 308 | my $KU='300b0603551d0f0404030204b0'; #Digital Siganture, Key Encypherment, Digital Encryption | ||
| 309 | my $EKU; | ||
| 310 | if ( $host ) { $EKU='301d0603551d250416301406082b0601050507030106082b06010505070302';} | ||
| 311 | else { $EKU='30130603551d25040c300a06082b06010505070302'; } | ||
| 312 | |||
| 313 | $REQExt=ASN1Wrap("a0", ASN1Wrap("30", "06092a864886f70d01090e".ASN1Wrap("31", ASN1Wrap("30",$BasicConstraints.$KU.$EKU.$SubjectAltNameExt )))); | ||
| 314 | |||
| 315 | #### The whole chunck of certificate to be signed #### | ||
| 316 | my $TBSRequest=ASN1Wrap("30",$REQversion.$REQsubject.$REQsubjectPublicKeyInfo.$REQExt); | ||
| 317 | |||
| 318 | ###Signature Bits##################################################### | ||
| 319 | # REQsignatureAlgorithm REQsignature | ||
| 320 | |||
| 321 | # Make Checksum and RSA sign it | ||
| 322 | # my $REQsignature="300d06092a864886f70d0101040500"; #SEQ(OID:md5WithRSAEncryption NULL) | ||
| 323 | my $REQsignature="300d06092a864886f70d0101050500"; #SEQ(OID:SHA1WithSHA1Encryption NULL) | ||
| 324 | my $BinaryTBSRequest = $TBSRequest; | ||
| 325 | $BinaryTBSRequest =~ s/(..)/pack('C',hex($&))/ge; | ||
| 326 | my $RSAsignedDigest = digestSign("sha1WithRSA",$BinaryTBSRequest,Hex($KI{KeyprivateExponent}),Hex($KI{Keymodulus})); | ||
| 327 | my $Signature = ASN1Wrap("03",ASN1BitStr($RSAsignedDigest)); #(Always n*8 bits for MDnRSA and SHA1RSA) | ||
| 328 | |||
| 329 | ###Wrap it all up Public Bits and Signature############################ | ||
| 330 | # TBSCertificate X509signatureAlgorithm X509signature | ||
| 331 | |||
| 332 | my $REQ = ASN1Wrap("30",$TBSRequest.$REQsignature.$Signature); | ||
| 333 | |||
| 334 | ###Write out the Cert and Key files#################################### | ||
| 335 | |||
| 336 | $REQ=~s/(..)/pack('C',hex($&))/ge; | ||
| 337 | $Privatekey=~s/(..)/pack('C',hex($&))/ge; | ||
| 338 | |||
| 339 | return { Req=>$REQ, Key=>$Privatekey, Warnings=>\@Warnings }; | ||
| 340 | } | ||
| 341 | |||
| 342 | 1; | ||
| 343 | |||
| 344 | __END__ |