| 1 | # Copyright (c) 2003, Cornell University | ||
| 2 | # See the file COPYING for the status of this software | ||
| 3 | |||
| 4 | package SOAP::Clean::Security; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use SOAP::Clean::Internal; | ||
| 10 | use SOAP::Clean::XML; | ||
| 11 | |||
| 12 | BEGIN { | ||
| 13 | use Exporter (); | ||
| 14 | our (@ISA, @EXPORT); | ||
| 15 | |||
| 16 | @ISA = qw(Exporter); | ||
| 17 | @EXPORT = qw( | ||
| 18 | ); | ||
| 19 | }; | ||
| 20 | |||
| 21 | ######################################################################## | ||
| 22 | |||
| 23 | #Digitally signing a document | ||
| 24 | sub digsign { | ||
| 25 | my ($doc,$keyfile,$certfile,$sigapp) = @_; | ||
| 26 | my $in_tmp = "/tmp/tmpdsig.tmpl"; | ||
| 27 | my $in_tmp2 = "/tmp/tmpdsig2.tmpl"; | ||
| 28 | open DOCIN, "> $in_tmp" || die("SOAP-ENV:Server - Error! Line ".__LINE__); | ||
| 29 | print DOCIN "$doc"; | ||
| 30 | close DOCIN || | ||
| 31 | die("SOAP-ENV:Server - Error! Line ".__LINE__); | ||
| 32 | system ("$sigapp sign --privkey $keyfile,$certfile $in_tmp > $in_tmp2"); | ||
| 33 | my $newdoc = docinsert($in_tmp2); | ||
| 34 | return $newdoc; | ||
| 35 | } | ||
| 36 | |||
| 37 | ######################################################################## | ||
| 38 | |||
| 39 | #Verifying a signed document | ||
| 40 | sub digverify { | ||
| 41 | my ($doc,$certfile,$sigapp) = @_; | ||
| 42 | my $in_tmp = "/tmp/tmpdsigsrv.tmpl"; | ||
| 43 | my $in_tmp2 = "/tmp/dsiganswer.tmpl"; | ||
| 44 | open DOCIN, "> $in_tmp" || die("SOAP-ENV:Server - Error! Line ".__LINE__); | ||
| 45 | print DOCIN "$doc"; | ||
| 46 | close DOCIN || | ||
| 47 | die("SOAP-ENV:Server - Error! Line ".__LINE__); | ||
| 48 | system ("$sigapp verify --trusted $certfile $in_tmp > $in_tmp2"); | ||
| 49 | open ANS, "< $in_tmp2"; | ||
| 50 | |||
| 51 | my $newdoc = <ANS>; | ||
| 52 | return $newdoc; | ||
| 53 | |||
| 54 | |||
| 55 | } | ||
| 56 | |||
| 57 | ######################################################################## | ||
| 58 | |||
| 59 | # Take a document $d. Encrypt its body, !!! in place !!! | ||
| 60 | sub encrypt_body { | ||
| 61 | my ($d,$privkeyenc,$pubkeyenc,$enctmpl,$appl) = @_; | ||
| 62 | |||
| 63 | my $envelope = xml_get_child($d,$SOAP_ENV,'Envelope'); | ||
| 64 | |||
| 65 | # fixme: Which of these are really needed? | ||
| 66 | $envelope->setAttribute("xmlns:wsse",$wsse); | ||
| 67 | $envelope->setAttribute("xmlns:xenc",$xenc); | ||
| 68 | $envelope->setAttribute("xmlns:ds",$ds); | ||
| 69 | |||
| 70 | my $body = xml_get_child($envelope,$SOAP_ENV,'Body'); | ||
| 71 | |||
| 72 | ##first encrypt the body | ||
| 73 | my $encrypted_body = encrypt(xml_to_string($body), | ||
| 74 | $privkeyenc, | ||
| 75 | $pubkeyenc, | ||
| 76 | $enctmpl, | ||
| 77 | $appl); | ||
| 78 | # now add the wsse:Security tag to make sure that we adapt to | ||
| 79 | # WS-Security standard | ||
| 80 | my $new_body = element("wsse:Security",namespace("wsse",$wsse), | ||
| 81 | $encrypted_body); | ||
| 82 | $envelope->replaceChild($body,$new_body); | ||
| 83 | } | ||
| 84 | |||
| 85 | ######################################################################## | ||
| 86 | |||
| 87 | sub encrypt { | ||
| 88 | my ($doc,$privkey,$pubkey,$enctmpl,$sigapp) = @_; | ||
| 89 | my $in_tmp = "/tmp/tmpenc.tmpl"; | ||
| 90 | my $in_tmp2 = "/tmp/encanswer.tmpl"; | ||
| 91 | #my $in_tmp = tmpnam(); | ||
| 92 | #my $in_tmp2 = tmpnam(); | ||
| 93 | open DOCIN, "> $in_tmp" || die("SOAP-ENV:Server - Error! Line ".__LINE__); | ||
| 94 | print DOCIN "$doc"; | ||
| 95 | close DOCIN || | ||
| 96 | die("SOAP-ENV:Server - Error! Line ".__LINE__); | ||
| 97 | system ("$sigapp encrypt --session-key-des3 --pubkey $pubkey --privkey $privkey --binary $in_tmp $enctmpl > $in_tmp2"); | ||
| 98 | #return; | ||
| 99 | |||
| 100 | my $newdoc = docinsert($in_tmp2); | ||
| 101 | unlink($in_tmp,$in_tmp2); | ||
| 102 | |||
| 103 | return $newdoc; | ||
| 104 | } | ||
| 105 | |||
| 106 | ######################################################################## | ||
| 107 | |||
| 108 | ######################################################################## | ||
| 109 | |||
| 110 | sub decrypt { | ||
| 111 | my ($doc,$privkey,$pubkey,$sigapp,$env_in) = @_; | ||
| 112 | my %env = %{$env_in}; | ||
| 113 | my $in_tmp = "/tmp/tmpdec.tmpl"; | ||
| 114 | my $in_tmp2 = "/tmp/decanswer.tmpl"; | ||
| 115 | open DOCIN, "> $in_tmp" || die("SOAP-ENV:Server - Error! Line ".__LINE__); | ||
| 116 | print DOCIN "$doc"; | ||
| 117 | close DOCIN || | ||
| 118 | die("SOAP-ENV:Server - Error! Line ".__LINE__); | ||
| 119 | open ANS, "> $in_tmp2" || die("SOAP-ENV:Server - Error! Line ".__LINE__); | ||
| 120 | ##Here we need to insert a dummy tag with all the namespaces to do this correctly | ||
| 121 | print ANS "<SOAP-ENV:Body "; | ||
| 122 | my $keys; | ||
| 123 | foreach $keys (keys %env){ | ||
| 124 | my $el = $env{$keys}; | ||
| 125 | $keys =~ s/^(.*):$/$1/; | ||
| 126 | print ANS " xmlns:$keys=\"$el\""; | ||
| 127 | } | ||
| 128 | print ANS ">"; | ||
| 129 | close ANS || die("SOAP-ENV:Server - Error! Line ".__LINE__); | ||
| 130 | system ("$sigapp decrypt --privkey $privkey --pubkey $pubkey $in_tmp >> $in_tmp2"); | ||
| 131 | open ANS, ">> $in_tmp2" || die("SOAP-ENV:Server - Error! Line ".__LINE__); | ||
| 132 | |||
| 133 | print ANS "</SOAP-ENV:Body>\n"; | ||
| 134 | close ANS || die("SOAP-ENV:Server - Error! Line ".__LINE__); | ||
| 135 | |||
| 136 | my $newdoc = docinsert($in_tmp2); | ||
| 137 | return $newdoc; | ||
| 138 | } | ||
| 139 | |||
| 140 | ######################################################################## | ||
| 141 | |||
| 142 | sub verify_envelope { | ||
| 143 | my ($server,$d) = @_; | ||
| 144 | |||
| 145 | |||
| 146 | defined($server->{dsig}) || | ||
| 147 | die("Error! file \"".__FILE__."\", line ".__LINE__); | ||
| 148 | |||
| 149 | my $verification = | ||
| 150 | digverify(xml_to_string($d),$server->{cert},$server->{appl}); | ||
| 151 | $verification eq "OK\n" || | ||
| 152 | die("Error, your signature is a fraud!"); | ||
| 153 | } | ||
| 154 | |||
| 155 | |||
| 156 | ######################################################################## | ||
| 157 | |||
| 158 | sub decrypt_body { | ||
| 159 | |||
| 160 | my ($server,$d) = @_; | ||
| 161 | |||
| 162 | |||
| 163 | defined($server->{enc}) || | ||
| 164 | die("Error! file \"".__FILE__."\", line ".__LINE__); | ||
| 165 | |||
| 166 | |||
| 167 | my ($envelope,$namespaces) = | ||
| 168 | destruct_children | ||
| 169 | ($d,{},$SOAP_ENV,'Envelope'); | ||
| 170 | |||
| 171 | my ($body,$body_namespaces) = | ||
| 172 | destruct_children($envelope,$namespaces, | ||
| 173 | $SOAP_ENV,'Body'); | ||
| 174 | |||
| 175 | #destruct the children to get the wsse:Security tag out | ||
| 176 | my ($wsse,$wsse_namespaces) = | ||
| 177 | destruct_children($body,$body_namespaces, | ||
| 178 | $wsse,'Security'); | ||
| 179 | |||
| 180 | #Inside the wsse:Security tag is the xenc:EncryptedData tag | ||
| 181 | #Destruct the wsse:security node to get data | ||
| 182 | my ($encr,$encr_namespaces) = | ||
| 183 | destruct_children($wsse,$wsse_namespaces, | ||
| 184 | $xenc,'EncryptedData'); | ||
| 185 | |||
| 186 | ##decrypt it | ||
| 187 | my $new_body = decrypt(xml_to_string($encr),$server->{privkeyenc}, | ||
| 188 | $server->{pubkeyenc},$server->{appl}, | ||
| 189 | $encr_namespaces); | ||
| 190 | |||
| 191 | $envelope->replaceChild($body,$new_body); | ||
| 192 | } | ||
| 193 | |||
| 194 | ######################################################################## | ||
| 195 | |||
| 196 | 1; |