java.lang.Object
net.i2p.crypto.TrustedUpdate
Handles signing and verification of I2P update files. Supports multiple signature types and content formats for secure software distribution.
Field Summary
Modifier and TypeFieldDescriptionprivate final I2PAppContextThe app context.private final LogThe log for update verification.private StringThe version of the verified update.private final Map<SigningPublicKey, String> The trusted keys, by signing key, named by signer.The built-in trusted keys.private static final StringBase64 of the default trusted signing key, zzz's key.static final intBytes preceding the version in an update file: a signature followed by a 16-byte version.private static final intKey size in base64 bytes (padding + key data, 172).private static final StringConfig key of the trusted update keys.private static final intBytes in the version field.Constructor Summary
ConstructorsConstructorDescriptionTrusted update verifier using the default global context.TrustedUpdate(I2PAppContext context) Trusted update verifier using the given context.Method Summary
Modifier and TypeMethodDescriptionfinal booleanDuplicate keys or names rejected, except that duplicate empty names are allowed.private static final booleangenKeysCLI(String publicKeyFile, String privateKeyFile) Generate signing key pair files via CLI.getKeys()Returns the signing keys whose releases this router accepts, in an unmodifiable view.Fetches the trusted keys for the current instance.static StringgetVersionString(File signedFile) Reads the version string from a signed update file.static StringgetVersionString(InputStream inputStream) Reads the version string from an input stream.booleanDo we know about the following key?booleanisUpdatedVersion(String currentVersion, File signedFile) Verifies that the version of the given signed update file is newer thancurrentVersion.static voidParses command line arguments when this class is used from the command line.migrateFile(File signedFile, File outputFile) Extract the file.migrateVerified(String currentVersion, File signedFile, File outputFile) Verifies the signature of a signed update file, and if it's valid and the file's version is newer than the given current version, migrates the data out ofsignedFileand intooutputFile.static final booleanneedsUpdate(String currentVersion, String newVersion) Checks if the given version is newer than the given current version.Version in the .sud file, valid only after callingmigrateVerified(String, File, File).private static final voidShows the command line usage.private static final booleanshowVersionCLI(String signedFile) Display version string from a signed file via CLI.Uses the given private key to sign the given input file along with its version string using DSA.sign(String inputFile, String signedFile, SigningPrivateKey signingPrivateKey, String version) Uses the givenSigningPrivateKeyto sign the given input file along with its version string using DSA.private static final booleanSign an input file via CLI.booleanVerifies the DSA signature of a signed update file.booleanverify(File signedFile, SigningPublicKey signingPublicKey) Verifies the DSA signature of a signed update file.booleanVerifies the DSA signature of a signed update file.verifyAndGetSigner(File signedFile) Verifies the DSA signature of a signed update file.private static final booleanverifySigCLI(String signedFile) Verify signature on a signed file via CLI.private static final booleanverifyUpdateCLI(String signedFile) Check whether a signed file is a newer version via CLI.private static final booleanverifyVersionCLI(String signedFile) True if there's no version mismatch.private booleanverifyVersionMatch(File signedFile) Verify the version in the sud header matches the version in the zip comment (and that the embedded file is a zip file at all)isUpdatedVersion(String, File)must be called first to set _newVersion.
Field Details
DEFAULT_TRUSTED_KEY
Base64 of the default trusted signing key, zzz's key.- See Also:
VERSION_BYTES
HEADER_BYTES
public static final int HEADER_BYTESBytes preceding the version in an update file: a signature followed by a 16-byte version.PROP_TRUSTED_KEYS
Config key of the trusted update keys.- See Also:
_context
The app context._log
The log for update verification._trustedKeys
The trusted keys, by signing key, named by signer._newVersion
The version of the verified update.KEYSIZE_B64_BYTES
private static final int KEYSIZE_B64_BYTESKey size in base64 bytes (padding + key data, 172).DEFAULT_KEYS
Constructor Details
TrustedUpdate
public TrustedUpdate()Trusted update verifier using the default global context.TrustedUpdate
Trusted update verifier using the given context.- Parameters:
context- An instance ofI2PAppContext.
Method Details
getKeys
Returns the signing keys whose releases this router accepts, in an unmodifiable view.- Returns:
- the trusted signing public keys, each mapped to its owner name, which may be empty when the key was supplied without one
addKey
haveKey
Do we know about the following key?- Parameters:
key- the base64 signing key to look up; any other length returns false- Returns:
- true if the key is trusted
main
Parses command line arguments when this class is used from the command line. Exits 1 on failure so this can be used in scripts.- Parameters:
args- Command line parameters.
needsUpdate
Checks if the given version is newer than the given current version.- Parameters:
currentVersion- The current version.newVersion- The version to test.- Returns:
trueif the given version is newer than the current version, otherwisefalse.
genKeysCLI
Generate signing key pair files via CLI.- Parameters:
publicKeyFile- path of the public key file to create, not overwritten if it existsprivateKeyFile- path of the private key file to create, not overwritten if it exists- Returns:
- true if key files were generated successfully
showUsageCLI
private static final void showUsageCLI()Shows the command line usage.showVersionCLI
Display version string from a signed file via CLI.- Parameters:
signedFile- path of the signed file to read the version from- Returns:
- true if version string was extracted from the file
signCLI
private static final boolean signCLI(String inputFile, String signedFile, String privateKeyFile, String version) Sign an input file via CLI.- Parameters:
inputFile- path of the file to signsignedFile- path of the signed output file to writeprivateKeyFile- path of the signing private key file to readversion- version string to embed in the signed file- Returns:
- true if the file was signed successfully
verifySigCLI
Verify signature on a signed file via CLI.- Parameters:
signedFile- path of the signed file whose signature is checked- Returns:
- true if the signature is valid
verifyUpdateCLI
Check whether a signed file is a newer version via CLI.- Parameters:
signedFile- path of the signed file whose version is compared with the running CoreVersion- Returns:
- true if the signed file is a newer version
verifyVersionCLI
True if there's no version mismatch.- Parameters:
signedFile- path of the signed file whose header and zip comment versions are compared- Returns:
- true if there's no version mismatch
getTrustedKeysString
Fetches the trusted keys for the current instance. We could sort it but don't bother.- Returns:
- A
Stringcontaining the trusted keys, delimited by CR LF line breaks.
getVersionString
getVersionString
Reads the version string from an input stream.- Parameters:
inputStream- containing at least 56 bytes- Returns:
- The version string read, or an empty string if no version string is present.
newVersion
Version in the .sud file, valid only after callingmigrateVerified(String, File, File).- Returns:
- the new version string
isUpdatedVersion
Verifies that the version of the given signed update file is newer thancurrentVersion.- Parameters:
currentVersion- The current version to check against.signedFile- The signed update file.- Returns:
trueif the signed update file's version is newer than the current version, otherwisefalse.
migrateVerified
Verifies the signature of a signed update file, and if it's valid and the file's version is newer than the given current version, migrates the data out ofsignedFileand intooutputFile.As of 0.8.8, the embedded file must be a zip file with a standard zip header and a UTF-8 zip file comment matching the version in the sud header. This prevents spoofing the version, since the sud signature does NOT cover the version in the header. (We do this for sud/su2 files but not plugin xpi2p files - don't use this method for plugin files).
- Parameters:
currentVersion- The current version to check against.signedFile- A signed update file.outputFile- The file to write the verified data to.- Returns:
nullif the signature and version were valid and the data was moved, and an errorStringotherwise.
verifyVersionMatch
Verify the version in the sud header matches the version in the zip comment (and that the embedded file is a zip file at all)isUpdatedVersion(String, File)must be called first to set _newVersion.- Parameters:
signedFile- the signed update file, its zip comment is compared against the version read earlier- Returns:
- true if matches
migrateFile
Extract the file. Skips and ignores the signature and version. No verification.- Parameters:
signedFile- A signed update file.outputFile- The file to write the verified data to.- Returns:
nullif the data was moved, and an errorStringotherwise.
sign
Uses the given private key to sign the given input file along with its version string using DSA. The output will be a signed update file where the first 40 bytes are the resulting DSA signature, the next 16 bytes are the input file's version string encoded in UTF-8 (padded with trailing0hcharacters if necessary), and the remaining bytes are the raw bytes of the input file.- Parameters:
inputFile- The file to be signed.signedFile- The signed update file to write.privateKeyFile- The name of the file containing the private key to signinputFilewith.version- The version string of the input file. If this is longer than 16 characters it will be truncated.- Returns:
- An instance of
Signature, ornullif there was an error.
sign
public Signature sign(String inputFile, String signedFile, SigningPrivateKey signingPrivateKey, String version) Uses the givenSigningPrivateKeyto sign the given input file along with its version string using DSA. The output will be a signed update file where the first 40 bytes are the resulting DSA signature, the next 16 bytes are the input file's version string encoded in UTF-8 (padded with trailing0hcharacters if necessary), and the remaining bytes are the raw bytes of the input file.- Parameters:
inputFile- The file to be signed.signedFile- The signed update file to write.signingPrivateKey- An instance ofSigningPrivateKeyto signinputFilewith.version- The version string of the input file. If this is longer than 16 characters it will be truncated.- Returns:
- An instance of
Signature, ornullif there was an error.
verify
Verifies the DSA signature of a signed update file.- Parameters:
signedFile- The signed update file to check.- Returns:
trueif the file has a valid signature, otherwisefalse.
verifyAndGetSigner
verify
Verifies the DSA signature of a signed update file.- Parameters:
signedFile- The signed update file to check.publicKeyFile- A file containing the public key to use for verification.- Returns:
trueif the file has a valid signature, otherwisefalse.
verify
Verifies the DSA signature of a signed update file.- Parameters:
signedFile- The signed update file to check.signingPublicKey- An instance ofSigningPublicKeyto use for verification.- Returns:
trueif the file has a valid signature, otherwisefalse.