/* * SignHandler.java (för 10.2) * Karl-Adam Karlsson * 06-10-09 * * java SignHandler * signerar filen med (den privata) nyckeln som finns i under namnet , sparar ett certifikat * som (innehållande den publika nyckeln) i filen och sparar signaturen i filen * (sändaren kan sedan skicka filerna , och till mottagaren för verifiering) * */ import java.security.*; import java.io.*; public class SignHandler{ /* * Konstruktor */ public SignHandler(String kStore,String storePass, String alias, String keyPass, String dataPath, String certName, String sigName){ KeyStore store = loadKeyStore(kStore, storePass); try{ signAndSaveSig(getInS(dataPath), store.getKey(alias,keyPass.toCharArray()), sigName); saveCert(store.getCertificate(alias), certName); }catch(KeyStoreException kex){ kex.printStackTrace(); System.exit(1); }catch(NoSuchAlgorithmException noA){ noA.printStackTrace(); System.exit(1); }catch(UnrecoverableKeyException kk){ kk.printStackTrace(); System.exit(1); } } /* * Saves the public key to a file called * @param Certificate acert The certificate to be saved. * @param String cerName The name of the file to save the key to. * */ private void saveCert(java.security.cert.Certificate aCert, String certName){ File f = new File(certName); FileOutputStream outFile = null; ObjectOutputStream outStream; try{ outFile = new FileOutputStream(certName); outStream = new ObjectOutputStream(outFile); outStream.writeObject(aCert); outStream.close(); } catch(IOException e) { System.err.println("IO Exception"); e.printStackTrace(); System.exit(1); } }//end of saveCert /* * Signs the file referenced by instream with the privat key aKey * And saves the signature to a file named sigName * * @param inStream A instream to the file to sign * @param Key aKey The key to use for signing * @param sigName The Name of the signature file to save signature to. */ private void signAndSaveSig(FileInputStream inStream, Key aKey, String sigName){ File aFile = new File(sigName); Signature aSig = null; byte[] signature = null; try{ aSig = Signature.getInstance( "SHA1withDSA" ); aSig.initSign( (PrivateKey)aKey ); }catch(NoSuchAlgorithmException noAl){ System.err.println(noAl); System.exit(1); }catch(InvalidKeyException inKey){ System.err.println(inKey); System.exit(1); } int aLen; byte[] buffLn = new byte[1024]; try{ while ((aLen = inStream.read(buffLn)) != -1) { aSig.update( buffLn ); } signature = aSig.sign(); saveSig(sigName, signature); inStream.close(); }catch(IOException ioe){ ioe.printStackTrace(); System.exit(1); }catch(SignatureException sigEx){ sigEx.printStackTrace(); System.exit(1); } }//end of file /* * SaveSig saves a Signature to file. * Both Signature to be saved and filename are parameters. * * @param String fileName The file to save to. * @param byte[] aSig The Signature, in bytes, to be saved. * */ private void saveSig(String fileName, byte[] aSig){ File outFile = new File(fileName); FileOutputStream outStream; try{ outStream = new FileOutputStream(fileName); outStream.write(aSig); outStream.close(); } catch(IOException e) { System.err.println("IO Exception"); e.printStackTrace(); System.exit(1); } }//endof saveSig /* * 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 /* * loadKeyStore * Loads a KeyStore Object from the file named * @param keyFileName Name of file to load keypair from * @return KeyStore the loaded keystore. */ private KeyStore loadKeyStore(String keyFileName, String keyPass){ KeyStore aStore = null; System.out.println("Pass: "+keyPass); System.out.println("aschararray: "+keyPass.toCharArray()); try{ aStore = KeyStore.getInstance(KeyStore.getDefaultType()); aStore.load(new FileInputStream(keyFileName), keyPass.toCharArray()); }catch(IOException ioe){ ioe.printStackTrace(); System.exit(1); }catch(NoSuchAlgorithmException alE){ alE.printStackTrace(); System.exit(1); }catch(java.security.cert.CertificateException cex){ cex.printStackTrace(); System.exit(1); }catch(KeyStoreException kex){ kex.printStackTrace(); System.exit(1); } return aStore; }//end of loadKeyPair /* * main * */ public static void main (String[] args) { if(args==null || args.length != 7){ System.out.println("Usage:java SignHandler "); System.out.println("Where is the keystore to use."); System.out.println("Where is the alias specified in the keystore."); System.out.println("Where is the password for this keystore."); System.out.println("Where is the password for the key."); System.out.println("Where is the file to sign."); System.out.println("Where is the filename to save certificate to."); System.out.println("Where is the filename to save signature to."); } else if(args.length==7&&args[0]!=null&&args[1]!=null&&args[2]!=null&&args[3]!=null&&args[4]!=null&&args[5]!=null&&args[6]!=null){ new SignHandler(args[0],args[1],args[2],args[3],args[4],args[5],args[6]); } else if(args[0]==null||args[1]==null||args[2]==null||args[3]==null||args[4]==null||args[5]==null||args[6]==null){ System.out.println("null is NOT a Valid argument."); System.out.println("Usage:java SignHandler "); System.out.println("Where is the keystore to use."); System.out.println("Where is the alias specified in the keystore."); System.out.println("Where is the password for this keystore."); System.out.println("Where is the password for the key."); System.out.println("Where is the file to sign."); System.out.println("Where is the filename to save certificate to."); System.out.println("Where is the filename to save signature to."); } }//end of main }//end of Class