Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringDirectory the certificates are kept in.private static final longCertUtil.main() warns about a certificate that expires within this much time from now - six months.private static final intLine length used when writing a certificate.private static final StringDirectory the revocations are kept in.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprivate static intFor use in the build process.private static voidRecords a certificate error for display.static voidexportCert(Certificate cert, OutputStream out) Modified from: http://www.exampledepot.com/egs/java.security.cert/ExportCert.htmlstatic voidexportCRL(X509CRL crl, OutputStream out) Writes a CRL in base64 format.private static voidexportPrivateKey(PrivateKey pk, OutputStream out) Modified from: http://www.exampledepot.com/egs/java.security.cert/ExportCert.htmlstatic voidexportPrivateKey(PrivateKey pk, Certificate[] certs, OutputStream out) Writes the private key and all certs in base64 format.static StringgetIssuerValue(X509Certificate cert, String type) Get a value out of the issuer distinguished name.Get the set of Subject Alternative Names, including DNSNames, RFC822Names, IPv4 and v6 addresses as strings.static StringgetSubjectValue(X509Certificate cert, String type) Get a value out of the subject distinguished name.private static StringgetValue(X500Principal p, String type) Get a value out of a X500Principal.static booleanisRevoked(Certificate cert) Is the certificate revoked? This loads the CRLs from disk.static booleanisRevoked(CertStore store, Certificate cert) Is the certificate revoked?static booleanisRevoked(I2PAppContext ctx, Certificate cert) Is the certificate revoked? This loads the CRLs from disk.static X509CertificateGet the certificate from a X.509 certificate file.static List<X509Certificate> loadCerts(InputStream in) Get one or more certificates from an input stream.private static X509CRLLoad a CRL.static X509CRLloadCRL(InputStream in) Load a CRL.static CertStoreloadCRLs()Load CRLs from standard locations.private static voidLoad CRLs from the directory into the set.static CertStoreloadCRLs(I2PAppContext ctx) Load CRLs from standard locations.static PublicKeyGet the Java public key from a X.509 certificate file.static PrivateKeyGet a single Private Key from an input stream.private static voidlog(I2PAppContext ctx, int level, String msg, Throwable t) Logs a certificate message at a given level.static final voidFor testing purposes.static booleansaveCert(Certificate cert, File file) Write a certificate to a file in base64 format.static booleanWrite a CRL to a file in base64 format.private static voidwritePEM(byte[] buf, String what, OutputStream out) Modified from: http://www.exampledepot.com/egs/java.security.cert/ExportCert.html
Field Details
CERT_DIR
Directory the certificates are kept in.- See Also:
REVOCATION_DIR
Directory the revocations are kept in.- See Also:
LINE_LENGTH
private static final int LINE_LENGTHLine length used when writing a certificate.- See Also:
CHECK
private static final long CHECKCertUtil.main() warns about a certificate that expires within this much time from now - six months.- See Also:
Constructor Details
CertUtil
private CertUtil()Utility class.
Method Details
saveCert
Write a certificate to a file in base64 format.- Parameters:
cert- the certificate to savefile- the file to write to- Returns:
- true if the file was written, false on an encoding or IO error
exportPrivateKey
public static void exportPrivateKey(PrivateKey pk, Certificate[] certs, OutputStream out) throws IOException, GeneralSecurityException Writes the private key and all certs in base64 format. Does NOT close the stream. Throws on all errors.- Parameters:
pk- non-nullcerts- certificate chain, null or empty to export pk onlyout- the output stream to write to- Throws:
InvalidKeyException- if the key does not support encodingCertificateEncodingException- if a cert does not support encodingIOException- if the write failsGeneralSecurityException- on any other security error
exportCert
public static void exportCert(Certificate cert, OutputStream out) throws IOException, CertificateEncodingException Modified from: http://www.exampledepot.com/egs/java.security.cert/ExportCert.htmlWrites a certificate in base64 format. Does NOT close the stream. Throws on all errors.
- Parameters:
cert- the certificate to exportout- the output stream to write to- Throws:
IOException- if the write failsCertificateEncodingException- if the certificate cannot be encoded
exportPrivateKey
private static void exportPrivateKey(PrivateKey pk, OutputStream out) throws IOException, InvalidKeyException Modified from: http://www.exampledepot.com/egs/java.security.cert/ExportCert.htmlWrites a private key in base64 format. Does NOT close the stream. Throws on all errors.
- Parameters:
pk- the private key to exportout- where to write the PEM block, not closed- Throws:
IOException- if the write to out failsInvalidKeyException- if the key does not support encoding
writePEM
Modified from: http://www.exampledepot.com/egs/java.security.cert/ExportCert.htmlWrites data in base64 format. Does NOT close the stream. Throws on all errors.
- Parameters:
buf- the data to base64-encode into the PEM bodywhat- the PEM label, e.g. "PRIVATE KEY", used in the BEGIN and END linesout- where to write the PEM block, not closed- Throws:
IOException- if the write to out fails
getSubjectAlternativeNames
Get the set of Subject Alternative Names, including DNSNames, RFC822Names, IPv4 and v6 addresses as strings.- Parameters:
cert- the certificate to extract names from- Returns:
- non-null, empty on error or none found
getSubjectValue
Get a value out of the subject distinguished name.Warning - unsupported in Android (no javax.naming), returns null.
- Parameters:
cert- certificate whose subject X500Principal is searchedtype- the DN attribute to read, e.g. "CN"- Returns:
- the subject value
getIssuerValue
Get a value out of the issuer distinguished name.Warning - unsupported in Android (no javax.naming), returns null.
- Parameters:
cert- certificate whose issuer X500Principal is searchedtype- the DN attribute to read, e.g. "CN"- Returns:
- the issuer value
getValue
Get a value out of a X500Principal.Warning - unsupported in Android (no javax.naming), returns null.
- Parameters:
p- the X500Principaltype- the DN attribute to read, e.g. "CN"- Returns:
- the attribute value, or null if absent, p is null, or on Android
error
log
Logs a certificate message at a given level.- Parameters:
ctx- the context to log throughlevel- the log levelmsg- a description of what happenedt- the exception, may be null
loadKey
Get the Java public key from a X.509 certificate file. Throws if the certificate is invalid (e.g. expired).This DOES check for revocation.
- Parameters:
kd- the certificate file- Returns:
- non-null, throws on all errors including certificate invalid
- Throws:
IOException- if the file cannot be readGeneralSecurityException- if the certificate is invalid
loadCert
Get the certificate from a X.509 certificate file. Throws if the certificate is invalid (e.g. expired).This does NOT check for revocation. This verifies the signature assuming the cert is self-signed. A non-self-issued (chain) cert that fails self-verification only logs a warning, as of 0.9.54; a self-issued cert with a bad signature throws. We do NOT fetch additional certs or attempt to validate a cert up the chain.
- Parameters:
kd- the certificate file- Returns:
- non-null, throws on all errors including certificate invalid
- Throws:
IOException- if the file cannot be readGeneralSecurityException- if the certificate is invalid
loadPrivateKey
public static PrivateKey loadPrivateKey(InputStream in) throws IOException, GeneralSecurityException Get a single Private Key from an input stream. Does NOT close the stream.- Parameters:
in- the input stream to read from- Returns:
- non-null, non-empty, throws on all errors including certificate invalid
- Throws:
IOException- if the read failsGeneralSecurityException- if the key is invalid
loadCerts
public static List<X509Certificate> loadCerts(InputStream in) throws IOException, GeneralSecurityException Get one or more certificates from an input stream. Throws if any certificate is invalid (e.g. expired). Does NOT close the stream.This does NOT check for revocation.
- Parameters:
in- the input stream to read from- Returns:
- non-null, non-empty, throws on all errors including certificate invalid
- Throws:
IOException- if the read failsGeneralSecurityException- if any certificate is invalid
saveCRL
exportCRL
Writes a CRL in base64 format. Does NOT close the stream. Throws on all errors.- Parameters:
crl- the CRL to exportout- the output stream to write to- Throws:
IOException- if the write failsCRLException- if the crl does not support encoding
isRevoked
Is the certificate revoked? This loads the CRLs from disk. For efficiency, callloadCRLs()and then pass to isRevoked().- Parameters:
cert- the certificate to check- Returns:
- true if revoked
isRevoked
Is the certificate revoked? This loads the CRLs from disk. For efficiency, call loadCRLs() and then pass toisRevoked(Certificate).- Parameters:
ctx- context used to locate and parse the CRLscert- the certificate to check- Returns:
- true if revoked
isRevoked
Is the certificate revoked?- Parameters:
store- the cert store containing CRLscert- the certificate to check- Returns:
- true if revoked
loadCRLs
Load CRLs from standard locations.- Returns:
- non-null, possibly empty
loadCRLs
Load CRLs from standard locations.- Parameters:
ctx- the application context- Returns:
- non-null, possibly empty
loadCRLs
loadCRL
Load a CRL.- Parameters:
file- the CRL file to read- Returns:
- non-null, possibly empty
- Throws:
IOException- if the file cannot be opened or readGeneralSecurityException- if the CRL cannot be parsed
loadCRL
Load a CRL. Does NOT Close the stream.- Parameters:
in- the input stream to read from- Returns:
- non-null
- Throws:
GeneralSecurityException- if the CRL cannot be loaded
main
For testing purposes.- Parameters:
args- a command verb followed by a file path; args[0] selects the operation and args[1] is the file it is applied to
checkAll
For use in the build process.- Parameters:
dir- the data directory holding the certificates and the revocation subdirectory- Returns:
- 0 for success, nonzero for failure