java.lang.Object
net.i2p.router.crypto.FamilyKeyCrypto
Utilities for creating, storing, retrieving the signing keys for the netdb family feature.
Nested Class Summary
Modifier and TypeClassDescriptionstatic enumOnly STORED_KEY is fully trusted.private static classCached name/sig/result.Field Summary
Modifier and TypeFieldDescriptionprivate final RouterContextThe router context.private final AtomicIntegerConsecutive certificate operation failures.private final StringThe family name we serve.private final Map<String, SigningPublicKey> The family signing keys, by family name.private final LogLogger for the family certificates.private final Map<Hash, FamilyKeyCrypto.Result> The failed verifications, by hash.The hashes of our own family members.private final SigningPrivateKeyOur family signing private key.private final SigningPublicKeyOur family signing public key.private final Map<Hash, FamilyKeyCrypto.Verified> The verified family members, by hash.private static final StringDirectory holding the family certificates.static final StringCertificate file suffix.static final StringCommon name suffix.private static final StringDirectory holding the revocation lists.static final StringCRL file suffix.private static final StringDefault algorithm for the family keys.private static final intDefault family key size, in bits.private static final intDefault time a family certificate stays valid.static final StringKeystore file prefix.static final StringKeystore file suffix.private static final StringDirectory holding the key stores.(package private) static final intBound on the positive verification cache, in entries.static final StringOption key for family certificate.static final StringOption key for family name.static final StringOption key for family signature.static final StringProperty key for family name.static final StringProperty key for key password.static final StringProperty key for keystore password.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprivate voidcleanup()Periodic cleanup to prevent unbounded growth of the two caches.(package private) static int[]cleanupCaches(Map<?, ?> negative, Map<?, ?> verified, int max) Run the periodic cache maintenance.private voidcreateKeyStore(File ks) Call out to keytool to create a new keystore with a keypair in it.private voidexportCert(X509Certificate cert) Save the public key certificate so the clients can get to it.private voidSave the CRL just in case.Get verified members of our family.Get our family name.private SigningPrivateKeygetPrivKey(File ks) Load the private key for this family from the keystore.booleanDo we have a valid family?private SigningPrivateKeyCreate (if necessary) and load the key store, then run.private SigningPublicKeyLoad a public key from a cert.private voidLoad all the certs.static voidCLI entry point for manual family key operations.voidshutdown()Clears the caches.Caller must add family to RI also.verify(RouterInfo ri) Verify the family signature in a RouterInfo.private FamilyKeyCrypto.Resultverify(RouterInfo ri, String name) Verify the family in a RouterInfo, name already retrieved.private voidverifyKeyStore(File ks) Verify the family keystore can be opened and its aliases used.booleanVerify the family in a RouterInfo matches ours and the signature is good.
Field Details
_context
The router context._log
Logger for the family certificates._verified
The verified family members, by hash._knownKeys
The family signing keys, by family name._negativeCache
The failed verifications, by hash._ourFamily
_fname
The family name we serve._privkey
Our family signing private key._pubkey
Our family signing public key._failCount
Consecutive certificate operation failures.PROP_KEYSTORE_PASSWORD
Property key for keystore password.- See Also:
PROP_FAMILY_NAME
Property key for family name.- See Also:
PROP_KEY_PASSWORD
Property key for key password.- See Also:
CERT_SUFFIX
CRL_SUFFIX
KEYSTORE_PREFIX
KEYSTORE_SUFFIX
CN_SUFFIX
DEFAULT_KEY_VALID_DAYS
private static final int DEFAULT_KEY_VALID_DAYSDefault time a family certificate stays valid.- See Also:
DEFAULT_KEY_ALGORITHM
Default algorithm for the family keys. RSA is excluded because its signature would exceed the 255 character limit for a family tag, and EdDSA because the keystore cannot encode or decode it.DEFAULT_KEY_SIZE
private static final int DEFAULT_KEY_SIZEDefault family key size, in bits.KS_DIR
CERT_DIR
Directory holding the family certificates.- See Also:
CRL_DIR
Directory holding the revocation lists.- See Also:
OPT_NAME
OPT_SIG
OPT_KEY
MAX_VERIFIED
static final int MAX_VERIFIEDBound on the positive verification cache, in entries. One entry per router whose family signature we have checked, each holding two strings, so the map has to be bounded the same way the negative cache is. Clearing it only costs a re-verification on the next lookup, so the bound is enforced by a clear rather than by an eviction policy.- See Also:
Constructor Details
FamilyKeyCrypto
For signing and verification.If the context property
netdb.family.nameis set, this can be used for signing, else only for verification.- Parameters:
context- the router context supplying the family name property and certificate directory- Throws:
GeneralSecurityException- if the configured family name is illegal or escapes the certificate directory
Method Details
initialize
Create (if necessary) and load the key store, then run.- Returns:
- the family's private key, read from the key store
- Throws:
GeneralSecurityException- if the key store cannot be opened or the key is missing
shutdown
public void shutdown()Clears the caches.cleanupCaches
Run the periodic cache maintenance. The negative cache is cleared outright, since a miss simply re-checks the router. The positive cache is only cleared once it passes its bound - dropping live entries costs a re-verification of every family router on the next lookup, so while it is inside the bound the entries are worth keeping, and the bound is enforced by a clear rather than by tracking which entry to evict.- Parameters:
negative- the negative cacheverified- the positive cachemax- the bound on the positive cache's entry count- Returns:
- the entries cleared from each cache, negative first
cleanup
private void cleanup()Periodic cleanup to prevent unbounded growth of the two caches. Runs every hour viaSimpleTimer2.sign
Caller must add family to RI also. throws on all errors.- Parameters:
family- non-null, must match that we were initialized with or will throw GSEh- non-null- Returns:
- non-null options to be added to the RI
- Throws:
GeneralSecurityException- on null hash, null or changed family, or signing error
hasFamily
public boolean hasFamily()Do we have a valid family?- Returns:
- whether family is present
getOurFamily
getOurFamilyName
verify
Verify the family signature in a RouterInfo. This requires a family key in the RI, or a certificate file for the family in certificates/family.- Parameters:
ri- the RouterInfo whose family option names the family and whose signature is checked- Returns:
- the verification result
verifyOurFamily
Verify the family in a RouterInfo matches ours and the signature is good. Returns false if we don't have a family and sig, or they don't. Returns false for ourselves.- Parameters:
ri- the RouterInfo whose family option and signature are checked against our own family- Returns:
- true if family matches with good sig
verify
Verify the family in a RouterInfo, name already retrieved.- Parameters:
ri- the router whose family signature is checkedname- the family name already read from the router, compared with the cached one- Returns:
- the verification result, NO_SIG when the router has no signature, otherwise the cached or freshly computed result
verifyKeyStore
Verify the family keystore can be opened and its aliases used. Succeeds if it exists and we have a password, or it was created successfully.- Parameters:
ks- the keystore file to check, created when it does not exist- Throws:
GeneralSecurityException- on keystore error
createKeyStore
Call out to keytool to create a new keystore with a keypair in it. Trying to do this programatically is a nightmare, requiring either BouncyCastle libs or using proprietary Sun libs, and it's a huge mess. If successful, stores the keystore password and key password inrouter.config.- Parameters:
ks- the keystore file to create, together with its key pair- Throws:
GeneralSecurityException- on all errorsIOException- if keytool cannot be run or the files cannot be written
exportCert
Save the public key certificate so the clients can get to it.- Parameters:
cert- the family's certificate to write into the certificate directory
exportCRL
loadCerts
private void loadCerts()Load all the certs.loadCert
Load a public key from a cert.- Parameters:
file- the certificate file to read- Returns:
- null on all errors
getPrivKey
Load the private key for this family from the keystore.- Parameters:
ks- the keystore to read the family key from- Returns:
- non-null, throws on all errors
- Throws:
GeneralSecurityException- if no key password is set, the family key is missing, or the keystore cannot be read
main
CLI entry point for manual family key operations.- Parameters:
args- the keystore file to create, followed by the family name to issue the keys under