* Key Manager: Hopefully avoid some races at startup
http://forum.i2p/viewtopic.php?t=4424
This commit is contained in:
@ -105,11 +105,15 @@ public class KeyManager {
|
|||||||
_leaseSetKeys.put(dest.calculateHash(), keys);
|
_leaseSetKeys.put(dest.calculateHash(), keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wait one second, as this will get called 4 times in quick succession
|
||||||
|
* There is still a race here though, if a key is set while the sync job is running
|
||||||
|
*/
|
||||||
private void queueWrite() {
|
private void queueWrite() {
|
||||||
Clock cl = _context.clock();
|
Clock cl = _context.clock();
|
||||||
JobQueue q = _context.jobQueue();
|
JobQueue q = _context.jobQueue();
|
||||||
if ( (cl == null) || (q == null) ) return;
|
if ( (cl == null) || (q == null) ) return;
|
||||||
_synchronizeJob.getTiming().setStartAfter(cl.now());
|
_synchronizeJob.getTiming().setStartAfter(cl.now() + 1000);
|
||||||
q.addJob(_synchronizeJob);
|
q.addJob(_synchronizeJob);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,33 +163,55 @@ public class KeyManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void syncPrivateKey(File keyDir) {
|
private synchronized void syncPrivateKey(File keyDir) {
|
||||||
File keyFile = new File(keyDir, KeyManager.KEYFILE_PRIVATE_ENC);
|
DataStructure ds;
|
||||||
|
File keyFile = new File(keyDir, KEYFILE_PRIVATE_ENC);
|
||||||
boolean exists = (_privateKey != null);
|
boolean exists = (_privateKey != null);
|
||||||
if (!exists)
|
if (exists)
|
||||||
_privateKey = new PrivateKey();
|
ds = _privateKey;
|
||||||
_privateKey = (PrivateKey)syncKey(keyFile, _privateKey, exists);
|
else
|
||||||
|
ds = new PrivateKey();
|
||||||
|
DataStructure readin = syncKey(keyFile, ds, exists);
|
||||||
|
if (readin != null && !exists)
|
||||||
|
_privateKey = (PrivateKey) readin;
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void syncPublicKey(File keyDir) {
|
private synchronized void syncPublicKey(File keyDir) {
|
||||||
File keyFile = new File(keyDir, KeyManager.KEYFILE_PUBLIC_ENC);
|
DataStructure ds;
|
||||||
|
File keyFile = new File(keyDir, KEYFILE_PUBLIC_ENC);
|
||||||
boolean exists = (_publicKey != null);
|
boolean exists = (_publicKey != null);
|
||||||
if (!exists)
|
if (exists)
|
||||||
_publicKey = new PublicKey();
|
ds = _publicKey;
|
||||||
_publicKey = (PublicKey)syncKey(keyFile, _publicKey, exists);
|
else
|
||||||
|
ds = new PublicKey();
|
||||||
|
DataStructure readin = syncKey(keyFile, ds, exists);
|
||||||
|
if (readin != null && !exists)
|
||||||
|
_publicKey = (PublicKey) readin;
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void syncSigningKey(File keyDir) {
|
private synchronized void syncSigningKey(File keyDir) {
|
||||||
File keyFile = new File(keyDir, KeyManager.KEYFILE_PRIVATE_SIGNING);
|
DataStructure ds;
|
||||||
|
File keyFile = new File(keyDir, KEYFILE_PRIVATE_SIGNING);
|
||||||
boolean exists = (_signingPrivateKey != null);
|
boolean exists = (_signingPrivateKey != null);
|
||||||
if (!exists)
|
if (exists)
|
||||||
_signingPrivateKey = new SigningPrivateKey();
|
ds = _signingPrivateKey;
|
||||||
_signingPrivateKey = (SigningPrivateKey)syncKey(keyFile, _signingPrivateKey, exists);
|
else
|
||||||
|
ds = new SigningPrivateKey();
|
||||||
|
DataStructure readin = syncKey(keyFile, ds, exists);
|
||||||
|
if (readin != null && !exists)
|
||||||
|
_signingPrivateKey = (SigningPrivateKey) readin;
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void syncVerificationKey(File keyDir) {
|
private synchronized void syncVerificationKey(File keyDir) {
|
||||||
File keyFile = new File(keyDir, KeyManager.KEYFILE_PUBLIC_SIGNING);
|
DataStructure ds;
|
||||||
|
File keyFile = new File(keyDir, KEYFILE_PUBLIC_SIGNING);
|
||||||
boolean exists = (_signingPublicKey != null);
|
boolean exists = (_signingPublicKey != null);
|
||||||
if (!exists)
|
if (exists)
|
||||||
_signingPublicKey = new SigningPublicKey();
|
ds = _signingPublicKey;
|
||||||
_signingPublicKey = (SigningPublicKey)syncKey(keyFile, _signingPublicKey, exists);
|
else
|
||||||
|
ds = new SigningPublicKey();
|
||||||
|
DataStructure readin = syncKey(keyFile, ds, exists);
|
||||||
|
if (readin != null && !exists)
|
||||||
|
_signingPublicKey = (SigningPublicKey) readin;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DataStructure syncKey(File keyFile, DataStructure structure, boolean exists) {
|
private DataStructure syncKey(File keyFile, DataStructure structure, boolean exists) {
|
||||||
|
Reference in New Issue
Block a user