java.lang.Object
net.i2p.crypto.SU3File
I2P update file format handler for .su3 files. Supports reading, writing, and verification of signed update packages.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static enumThe ContentType is the trust domain for the content.Field Summary
Modifier and TypeFieldDescriptionprivate FileCertificate file used for verification.private longLength of the content, in bytes.private SU3File.ContentTypeThe content type of the file.private final I2PAppContextThe app context.private final FileThe SU3 file being read.private intThe content type code, -1 until read.private booleanTrue once the header has been verified.private intLength of the signature, in bytes.private StringThe signer name from the header.private intLength of the signer field, in bytes.private PublicKeyThe signer's public key.private SigTypeThe signature type of the file.private booleanWhether to verify the signature.private StringThe file's version string.private intLength of the version field, in bytes.private static final Map<Integer, SU3File.ContentType> Content types indexed by code.static final intThe archive holds a blocklist.static final intThe archive holds a news feed.static final intThe archive holds a plugin.static final intThe archive holds reseed data.static final intThe archive holds a router.static final intThe content type has not been determined.private static final SU3File.ContentTypeType assumed when the code is unknown.private static final intSignature type assumed when none is set.private static final intThe SU3 file version we write.static final StringThe magic string every SU3 file starts with.private static final byte[]Magic bytes at the start of the file.private static final intShortest version string accepted.static final intA macOS disk image.static final intA Windows executable.static final intA file whose contents are HTML.static final intA gzipped plain text file.static final intA file whose contents are XML.static final intA gzipped XML file.static final intThe file type is advisory and is application-dependent.private static final intOffset where the version string starts.Constructor Summary
Method Summary
Modifier and TypeMethodDescriptionprivate static final booleanbulkSignCLI(String stype, String ctype, String dir, String privateKeyFile, String version, String signerName, String kspass) Zip, xml, and xml.gz only.private static StringString listing available signature types.private static final booleanextractCLI(String signedFile, String outFile, boolean verifySig, String pkFile) If null, will use a name derived from signedFile.private static final booleangenKeysCLI(String stype, String publicKeyFile, String privateKeyFile, String crlFile, String alias, String kspass) May be null; non-null to save.private static final booleangenKeysCLI(SigType type, String publicKeyFile, String privateKeyFile, String crlFile, String alias, String kspass) Writes Java-encoded keys (X.509 for public and PKCS#8 for private).private intReturns the offset where the content starts.intThe ContentType is the trust domain for the content.intThe file type is advisory and is application-dependent.This does not check the signature, but it will fail if the signer is unknown, unless setVerifySignature(false) has been called.This does not check the signature, but it will fail if the signer is unknown, unless setVerifySignature(false) has been called.This does not check the signature, but it will fail if the signer is unknown, unless setVerifySignature(false) has been called.private static PublicKeyFor the -k CLI option.static voidParses command line arguments when this class is used from the command line.private static SU3File.ContentTypeparseContentType(String ctype) Resolve a content type from its number or its name.private voidsetPublicKeyCertificate(File certFile) Use this X.509 cert file for verification instead of $I2P/certificates/content_type/foo_at_mail.i2p.voidsetVerifySignature(boolean shouldVerify) Choose whether the signature is verified.private static final voidPrints the command line usage.private static final booleanshowVersionCLI(String signedFile) Display version from a signed SU3 file via CLI.private static final booleansignCLI(String stype, String ctype, String ftype, String inputFile, String signedFile, String privateKeyFile, String version, String signerName, String keypw, String kspass) Whether the operation succeeded.private static final booleansignCLI(SigType type, SU3File.ContentType ctype, int ftype, String inputFile, String signedFile, String privateKeyFile, String version, String signerName, String keypw, String kspass) Whether the operation succeeded.private static voidskip(InputStream in, int cnt) Skip the given number of bytes.booleanverify()One-pass verify.booleanverifyAndMigrate(File migrateTo) One-pass verify and extract the content.voidThis does not check the signature, but it will fail if the signer is unknown, unless setVerifySignature(false) has been called.private voidThrows if verify fails.private static final booleanverifySigCLI(String signedFile, String pkFile) Verify signature on a signed file via CLI.voidwrite(File content, int fileType, int contentType, String version, String signer, PrivateKey privkey, SigType sigType) One-pass wrap and sign the content.
Field Details
_context
The app context._file
The SU3 file being read._version
The file's version string._versionLength
private int _versionLengthLength of the version field, in bytes._signer
The signer name from the header._signatureLength
private int _signatureLengthLength of the signature, in bytes._signerLength
private int _signerLengthLength of the signer field, in bytes._fileType
private int _fileTypeThe content type code, -1 until read._contentType
The content type of the file._contentLength
private long _contentLengthLength of the content, in bytes._signerPubkey
The signer's public key._headerVerified
private boolean _headerVerifiedTrue once the header has been verified._sigType
The signature type of the file._verifySignature
private boolean _verifySignatureWhether to verify the signature._certFile
Certificate file used for verification.MAGIC
The magic string every SU3 file starts with.- See Also:
MAGIC_BYTES
private static final byte[] MAGIC_BYTESMagic bytes at the start of the file.FILE_VERSION
MIN_VERSION_BYTES
private static final int MIN_VERSION_BYTESShortest version string accepted.- See Also:
VERSION_OFFSET
private static final int VERSION_OFFSETOffset where the version string starts.- See Also:
TYPE_ZIP
public static final int TYPE_ZIPThe file type is advisory and is application-dependent. The following values are defined but any value 0-255 is allowed.- See Also:
TYPE_XML
TYPE_HTML
TYPE_XML_GZ
TYPE_TXT_GZ
TYPE_DMG
TYPE_EXE
CONTENT_UNKNOWN
public static final int CONTENT_UNKNOWNThe content type has not been determined.- See Also:
CONTENT_ROUTER
CONTENT_PLUGIN
CONTENT_RESEED
CONTENT_NEWS
CONTENT_BLOCKLIST
public static final int CONTENT_BLOCKLISTThe archive holds a blocklist.- See Also:
BY_CODE
Content types indexed by code.DEFAULT_CONTENT_TYPE
Type assumed when the code is unknown.DEFAULT_SIG_CODE
private static final int DEFAULT_SIG_CODESignature type assumed when none is set.- See Also:
Constructor Details
SU3File
SU3File
SU3File
SU3 file from the given file, using the given context.- Parameters:
context- the application contextfile- the su3 file
Method Details
setVerifySignature
public void setVerifySignature(boolean shouldVerify) Choose whether the signature is verified.- Parameters:
shouldVerify- true to verify the signature, the default
setPublicKeyCertificate
Use this X.509 cert file for verification instead of $I2P/certificates/content_type/foo_at_mail.i2p.- Parameters:
certFile- the X.509 certificate file to verify the signature against
getVersionString
This does not check the signature, but it will fail if the signer is unknown, unless setVerifySignature(false) has been called.- Returns:
- the version string
- Throws:
IOException- if the header cannot be read
getSignerString
This does not check the signature, but it will fail if the signer is unknown, unless setVerifySignature(false) has been called.- Returns:
- the signer string
- Throws:
IOException- if the header cannot be read
getSigType
This does not check the signature, but it will fail if the signer is unknown, unless setVerifySignature(false) has been called.- Returns:
- null if unknown
- Throws:
IOException- if the header cannot be read
getContentType
The ContentType is the trust domain for the content. The signer and signature will be checked with the trusted certificates for that type.This does not check the signature, but it will fail if the signer is unknown, unless setVerifySignature(false) has been called.
- Returns:
- -1 if unknown
- Throws:
IOException- if the header cannot be read
getFileType
The file type is advisory and is application-dependent. The following values are defined but any value 0-255 is allowed.This does not check the signature, but it will fail if the signer is unknown, unless setVerifySignature(false) has been called.
- Returns:
- 0-255 or -1 if unknown
- Throws:
IOException- if the header cannot be read
verifyHeader
This does not check the signature, but it will fail if the signer is unknown, unless setVerifySignature(false) has been called.- Throws:
IOException- if the header cannot be read or the signer is unknown
verifyHeader
Throws if verify fails.- Parameters:
in- the stream positioned at the start of the su3 file- Throws:
IOException- if the magic bytes, file version, or a length field is invalid, or the signer is unknownDataFormatException- not thrown by the current implementation
skip
Skip the given number of bytes.- Parameters:
in- the stream to read fromcnt- the number of bytes to skip- Throws:
IOException- if the stream ends before cnt bytes are read
getContentOffset
Returns the offset where the content starts.- Returns:
- the content offset
- Throws:
IOException- if the header is invalid
verify
One-pass verify.- Returns:
- true if signature is good
- Throws:
IOException- on any format error
verifyAndMigrate
One-pass verify and extract the content. Recommend extracting to a temp location as the sig is not checked until after extraction. This will delete the file if the sig does not verify.- Parameters:
migrateTo- the output file, probably in zip format. Null for verify only.- Returns:
- true if signature is good
- Throws:
IOException- on any format error
write
public void write(File content, int fileType, int contentType, String version, String signer, PrivateKey privkey, SigType sigType) throws IOException One-pass wrap and sign the content. Writes to the file specified in the constructor.- Parameters:
content- the input file, probably in zip formatfileType- 0-255, 0 for zipcontentType- 0-255version- 1-255 bytes when converted to UTF-8signer- ID of the public key, 1-255 bytes when converted to UTF-8privkey- the signing key, whose public half is the signer IDsigType- the digest and signature scheme to sign with- Throws:
IOException- if the content cannot be read or the file cannot be written
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.
showUsageCLI
private static final void showUsageCLI()Prints the command line usage.dumpTypes
String listing available signature types.- Returns:
- the list of type names for the -t option
parseContentType
Resolve a content type from its number or its name.- Parameters:
ctype- number or name- Returns:
- null if not found
showVersionCLI
Display version from a signed SU3 file via CLI.- Parameters:
signedFile- path of the signed SU3 file to display- Returns:
- true if version was displayed successfully
bulkSignCLI
private static final boolean bulkSignCLI(String stype, String ctype, String dir, String privateKeyFile, String version, String signerName, String kspass) Zip, xml, and xml.gz only.- Parameters:
stype- signature type name or code, null for the defaultctype- content type name or code, null for unknowndir- directory containing the .zip, .xml, and .xml.gz files to signprivateKeyFile- keystore file containing the private signing keyversion- the version string written into the su3 headersignerName- key alias in the keystore, also written as the signer stringkspass- the keystore password- Returns:
- true on success
signCLI
private static final boolean signCLI(String stype, String ctype, String ftype, String inputFile, String signedFile, String privateKeyFile, String version, String signerName, String keypw, String kspass) Whether the operation succeeded.- Parameters:
stype- signature type name or code, null for the defaultctype- content type name or code, null for unknownftype- file type name (ZIP, XML, HTML, XML_GZ, DMG, EXE) or code 0-255, null for ZIPinputFile- path of the unsigned input filesignedFile- path of the su3 file to writeprivateKeyFile- keystore file containing the private signing keyversion- the version string written into the su3 headersignerName- key alias in the keystore, also written as the signer stringkeypw- private key password, prompted for if under 6 characterskspass- the keystore password- Returns:
- true on success
signCLI
private static final boolean signCLI(SigType type, SU3File.ContentType ctype, int ftype, String inputFile, String signedFile, String privateKeyFile, String version, String signerName, String keypw, String kspass) Whether the operation succeeded.- Parameters:
type- signature type, overridden by the actual type of the private keyctype- content type recorded in the su3 headerftype- numeric file type, 0-255inputFile- path of the unsigned input filesignedFile- path of the su3 file to writeprivateKeyFile- keystore file containing the private signing keyversion- the version string written into the su3 headersignerName- key alias in the keystore, also written as the signer stringkeypw- private key password, prompted for if under 6 characterskspass- the keystore password- Returns:
- true on success
verifySigCLI
extractCLI
private static final boolean extractCLI(String signedFile, String outFile, boolean verifySig, String pkFile) If null, will use a name derived from signedFile.- Parameters:
signedFile- path of the signed SU3 file to extractoutFile- if null, will use a name derived from signedFileverifySig- true to verify the signature before extractingpkFile- X.509 cert file to verify against, null to use the key rings- Returns:
- true on success
genKeysCLI
private static final boolean genKeysCLI(String stype, String publicKeyFile, String privateKeyFile, String crlFile, String alias, String kspass) May be null; non-null to save.- Parameters:
stype- signature type name or code, null for the defaultpublicKeyFile- path of the X.509 certificate file to write, must not already existprivateKeyFile- keystore file to store the new private key incrlFile- may be null; non-null to savealias- key name and certificate subject, prompted for if emptykspass- the keystore password- Returns:
- true on success
genKeysCLI
private static final boolean genKeysCLI(SigType type, String publicKeyFile, String privateKeyFile, String crlFile, String alias, String kspass) Writes Java-encoded keys (X.509 for public and PKCS#8 for private).- Parameters:
type- signature type for the new key pairpublicKeyFile- path of the X.509 certificate file to write, must not already existprivateKeyFile- keystore file to store the new private key incrlFile- may be null; non-null to savealias- key name and certificate subject, prompted for if emptykspass- the keystore password- Returns:
- true on success
loadKey
For the -k CLI option.- Parameters:
kd- the X.509 certificate file to load the public key from- Returns:
- non-null, throws IOE on all errors
- Throws:
IOException- if the file cannot be read or does not contain a valid certificate