cryptography - Securing a private key on disk using symmetric encryption in Java -
i have distributed application polls commands central server , content of commands "signed" private key public key deployed along application @ each remote site. each command signed private key , signature verified before command executed.
here how generate public/private key pairs in java. (i realize, should doing more 1024)
keypairgenerator keygen = keypairgenerator.getinstance("dsa", "sun"); securerandom random = securerandom.getinstance("sha1prng", "sun"); keygen.initialize(1024, random); keypair pair = keygen.generatekeypair();
the new requirement in addition sending simple relatively safe commands, want send commands instruct our software download , execute installer update itself. has potential open big hole if not done correctly. "execute update installer" command signed , include md5 of executable downloaded , run. so, signature on command must correct (and include md5) , calculated md5 on downloaded file need correct, before execution occur. should take care of attack vectors can think of. any others should concerned about?
so, directing attention securing private key on server these commands originate. if private key obtained, game over. how should secure key on disk?
my thought use symmetric encryption passphrase secure private key.
my current solution follows:
char[] passphrase; // actual passphrase privatekey privatekeyobj; // actual private key byte[] privatekeybytes = privatekeyobj.getencoded(); byte[] salt = new byte[8]; new securerandom().nextbytes(salt); secretkeyfactory factory = secretkeyfactory.getinstance("pbkdf2withhmacsha1"); keyspec spec = new pbekeyspec(passphrase, salt, 1024, 256); secretkey tmp = factory.generatesecret(spec); secretkey secret = new secretkeyspec(tmp.getencoded(), "aes"); cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding"); cipher.init(cipher.encrypt_mode, secret); algorithmparameters params = cipher.getparameters(); byte[] iv = params.getparameterspec(ivparameterspec.class).getiv(); byte[] ciphertext = cipher.dofinal(privatekeybytes); fileoutputstream outputfilestream = new fileoutputstream(outputfile); outputfilestream.write(salt); outputfilestream.write(iv); outputfilestream.write(ciphertext); outputfilestream.close();
(exceptions removed clarity)
this randomly generates salt, uses passphrase come key , stores resulting salt, iv , cipher text in file ready decryption.
however, feel missing here. should somehow include type of mac on key know valid description? maybe simple putting 5 or 6 bytes of known text before private key? bad passphrase right results in bad padding exception, read might not case , bad passphrases decode , result in junk. feel need guard against this.
can let me know if on right track here , provide feedback. important secure possible..
including hash or hmac reasonable, given want know if mistyped passphrase. otherwise, solution looks reasonable me.
one other thing can store securely possible: take offline. put copies on couple of usb keys, store 1 somewhere safe hand, , backup in safety deposit box or lawyer. it's heard secret key if it's physically inaccessible.
Comments
Post a Comment