/* * VerifyHandler.java (för 10.2) * Karl-Adam Karlsson * 06-10-11 * * java VerifyHandler som kontrollerar om certifikatet (med den publika nyckeln) * i filen och signaturen i filen verifierar filen * */ import java.security.*; import java.io.*; public class VerifyHandler{ VerifyHandler(String dataPath, String certPath, String sigPath){ verifyData((java.security.cert.Certificate)loadObj(certPath),getInS(dataPath), loadSignature(sigPath)); } /* * Kollar om certifikatet cert och signaturen asig * verifierar datat i ins. * Resultatet av verifieringen skrivs ut i konsolfönstret. * * @param Certificate aCert The certificate to use during verification * @param FileInputStream ins A stream connected to the data to verify * @param Signature aSig A signature to use during verification * */ private void verifyData(java.security.cert.Certificate aCert, FileInputStream ins, byte[] aSig){ Signature anotherSig=null; try{ anotherSig = Signature.getInstance( "SHA1withDSA" ); anotherSig.initVerify(aCert); }catch(InvalidKeyException ink){ ink.printStackTrace(); System.exit(1); }catch(NoSuchAlgorithmException noAl){ noAl.printStackTrace(); System.exit(1); } //update signature fopr verification int aLen; byte[] buffLn = new byte[1024]; try{ while ((aLen = ins.read(buffLn)) != -1) { anotherSig.update( buffLn ); } boolean verification = anotherSig.verify( aSig ); System.out.println("signature verifies: " + verification); }catch(IOException ioe){ ioe.printStackTrace(); System.exit(1); }catch(SignatureException sigEx){ sigEx.printStackTrace(); System.exit(1); } // } /* * loadObj loads and returns a Object from a file named path. * @param path the path to the file to load the Object from * @return Object The loaded Object. */ private Object loadObj(String path){ Object aObj = null; FileInputStream fIn = null; ObjectInputStream obIn = null; try{ fIn = getInS(path); obIn = new ObjectInputStream(fIn); aObj = (Object)obIn.readObject(); }catch(IOException ioe){ ioe.printStackTrace(); System.exit(1); }catch(ClassNotFoundException cNof){ cNof.printStackTrace(); System.exit(1); } return aObj; }//end of loadObj /* * getInS * Returns a FileInputStream connected to a file * @param String fimeName The name of the file to connect the stream to. * @return FileInputStream A stream connected to the file. */ private FileInputStream getInS(String fileName){ FileInputStream inFstream = null; try{ inFstream = new FileInputStream(fileName); }catch(FileNotFoundException fEx){ fEx.printStackTrace();; System.exit(1); } return inFstream; }//end of getInS /* * Loads a signature as byte[] form from the file * signame and returns the byte[] representation. * * @param String sigName The name of the file to load the signature from * @return byte[] A bytearray representation of the signature. */ private byte[] loadSignature(String sigName){ byte[] byteArr = null; try{ FileInputStream sIn = new FileInputStream (sigName); byteArr = new byte[sIn.available ()]; sIn.read (byteArr); sIn.close (); }catch(FileNotFoundException fNf){ System.err.print(fNf); System.exit(1); }catch(IOException iOE){ System.err.print(iOE); System.exit(1); } return byteArr; }//End of loadsignature /* * main * */ public static void main (String[] args) { if(args==null || args.length < 3 ||args.length>3){ System.out.println("Usage: VerifyHandler "); System.out.println("Where is to verify."); System.out.println("Where is the name of certificatefile to use for verification."); System.out.println("Where is the signature to use for verification."); } else if(args.length==3&&args[0]!=null&&args[1]!=null&&args[2]!=null){ new VerifyHandler(args[0],args[1],args[2]); } else if(args[0]==null||args[1]==null||args[2]==null){ System.out.println("Usage: VerifyHandler "); System.out.println("Where is to verify."); System.out.println("Where is the name of certificatefile to use for verification."); System.out.println("Where is the signature to use for verification."); } }//end of main }//End of Class