Skip to content
This repository was archived by the owner on Dec 8, 2017. It is now read-only.

Commit 312bb57

Browse files
committed
Log decrypted response
* add log4j.properties and remove log4j.xml * scrape the logfile for decrypted soap response * update java to set log filename dynamically * fix log4j warning by including lib * uncomment handle_response and clean it up
1 parent 47d537d commit 312bb57

File tree

5 files changed

+47
-78
lines changed

5 files changed

+47
-78
lines changed

lib/log4j.properties

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
log4j.rootCategory=DEBUG, R
2+
log4j.appender.R=org.apache.log4j.RollingFileAppender
3+
log4j.appender.R.File=${logfilename}
4+
log4j.appender.R.MaxFileSize=100KB
5+
log4j.appender.R.MaxBackupIndex=1
6+
log4j.appender.R.layout=org.apache.log4j.PatternLayout
7+
log4j.appender.R.layout.ConversionPattern=[%t:%p] %c: %m%n

lib/log4j.xml

-68
This file was deleted.

src/main/java/DecryptMessage.java

+30-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import javax.xml.parsers.DocumentBuilderFactory;
1414

1515
import java.util.List;
16+
import java.util.Properties;
1617
import java.io.ByteArrayInputStream;
1718
import java.io.InputStream;
1819
import java.io.IOException;
@@ -27,14 +28,16 @@ public static void main(String[] args)
2728
{
2829
try
2930
{
31+
System.setProperty("logfilename", args[2]);
32+
3033
List<String> lines = Files.readAllLines(Paths.get(args[0]), Charset.defaultCharset());
3134
String encrypted_xml = "";
3235
for (String line : lines)
3336
{
3437
encrypted_xml += line;
3538
}
3639

37-
String document = decrypt(encrypted_xml);
40+
String document = decrypt(encrypted_xml, args[1]);
3841
System.out.println(document);
3942
}
4043
catch (Exception e)
@@ -53,12 +56,35 @@ public static Document getSOAPDoc(String document) throws Exception
5356
return doc;
5457
}
5558

56-
public static String decrypt(String encryptedXml) throws Exception {
57-
Crypto crypto = CryptoFactory.getInstance();
59+
public static Crypto getSigningCrypto(String keyfile) throws Exception {
60+
Properties properties = new Properties();
61+
properties.setProperty("org.apache.ws.security.crypto.provider", "org.apache.ws.security.components.crypto.Merlin");
62+
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.file", keyfile);
63+
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", "importkey");
64+
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.private.password", "importkey");
65+
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.alias", "vbms_server_key");
66+
67+
return CryptoFactory.getInstance(properties);
68+
}
69+
70+
public static Crypto getDecryptionCrypto(String keyfile) throws Exception {
71+
Properties properties = new Properties();
72+
properties.setProperty("org.apache.ws.security.crypto.provider", "org.apache.ws.security.components.crypto.Merlin");
73+
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.file", keyfile);
74+
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", "importkey");
75+
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.private.password", "importkey");
76+
77+
return CryptoFactory.getInstance(properties);
78+
}
79+
80+
public static String decrypt(String encryptedXml, String keyfile) throws Exception {
81+
Crypto signCrypto = getSigningCrypto(keyfile);
82+
Crypto deCrypto = getDecryptionCrypto(keyfile);
5883
CallbackHandler handler = new WSSCallbackHandler();
5984
WSSecurityEngine secEngine = new WSSecurityEngine();
85+
6086
Document doc = getSOAPDoc(encryptedXml);
61-
java.util.List<WSSecurityEngineResult> results = secEngine.processSecurityHeader(doc, null, handler, crypto, crypto);
87+
java.util.List<WSSecurityEngineResult> results = secEngine.processSecurityHeader(doc, null, handler, signCrypto, deCrypto);
6288
return XMLUtils.PrettyDocumentToString(doc);
6389
}
6490

src/main/java/UploadDocumentWithAssociations.java

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class UploadDocumentWithAssociations
2727
{
2828
public static void main(String[] args)
2929
{
30+
System.setProperty("logfilename", "../log/upload.log");
31+
3032
Properties properties = new Properties();
3133
properties.setProperty("org.apache.ws.security.crypto.provider", "org.apache.ws.security.components.crypto.Merlin");
3234
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.file", args[1]);

src/send.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def upload_doc(options)
7575
file = prepare_xml(options[:pdf], options[:file_number], options[:received_dt], options[:first_name], options[:middle_name], options[:last_name], options[:exam_name])
7676
encrypted_xml = prepare_upload(file, env)
7777
response = send_document(encrypted_xml, env, options)
78-
#handle_response(response)
78+
puts handle_response(response, env, options)
7979
rescue Exception => e
8080
puts e.backtrace
8181
log(e.backtrace)
@@ -210,23 +210,25 @@ def get_soap(txt)
210210
XML::Parser.string(soap).parse
211211
end
212212

213-
def handle_response(response)
213+
def handle_response(response, env, options)
214214
doc = get_soap(response)
215215
log("Response from VBMS:\n#{doc.to_s}")
216216

217217
soap = "http://schemas.xmlsoap.org/soap/envelope/"
218218
if doc.find_first("//soap:Fault", soap)
219-
$stderr.write("Received error from VBMS:\n#{soap.to_s}\nCheck logfile in #{$logfile}")
219+
$stderr.write("Received error from VBMS:\n#{doc.to_s}\nCheck logfile in #{$logfile}\n")
220220
exit
221221
end
222222

223-
file = write_tempfile(soap.to_s)
223+
file = write_tempfile(doc.to_s)
224224

225225
# now here's the hackiest thing in the world. This command is going to fail,
226226
# because we can't get the signatures to properly get decrypted. So run the
227227
# command, handle the error, and pull the message out of the file >:|
228-
sh "java -classpath '../classes:../lib/*' DecryptMessage", true
228+
fname = rel("../log/#{options[:file_number]}.decrypt.log")
229+
sh "java -classpath '../classes:../lib/*:../lib' DecryptMessage #{file.path} #{env[:keyfile]} '#{fname}'", true
229230

231+
File.read(fname).match(/<soap.*?<\/soap.*?>/)[0]
230232
end
231233

232234
def parse(args)
@@ -286,4 +288,4 @@ def parse(args)
286288
end
287289

288290
options = parse(ARGV)
289-
upload_doc(options)
291+
upload_doc(options)

0 commit comments

Comments
 (0)