public class SSecurity
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
protected static java.lang.String |
DEFAULT_SIGNATURE_HASH
Default hash used for signatures.
|
protected static boolean |
ENTROPY_FALLBACK_WARNING_DONE
Flag if the fallback warning has been issued before.
|
protected static IEntropySource |
ENTROPY_SOURCE
Entropy source for seeding CSPRNGS.
|
static boolean |
PARANOID_PRNG
Flag if the paranoid/hedged-mode PRNG should be used (much slower, but guarded against single-point failures).
|
protected static java.security.SecureRandom |
SECURE_RANDOM
Common secure random number source.
|
protected static boolean |
TEST_ENTROPY_FALLBACK
Enable this to test the seeding fallback, do not change, used by tests only.
|
Constructor and Description |
---|
SSecurity() |
Modifier and Type | Method and Description |
---|---|
protected static byte[] |
asn1ToBytes(ASN1Object obj)
Shorthand for converting ANS1Objects to bytes.
|
static boolean |
checkEntity(X509CertificateHolder cert,
java.lang.String entityname)
Check whether a certificate belongs to an entity,
either as common name or as alt name.
|
static Tuple2<java.lang.String,java.lang.String> |
createCertificate(java.lang.String issuercert,
java.lang.String issuerkey,
java.lang.String subjectdn,
java.lang.String scheme,
java.lang.String schemeconf,
java.lang.String hashalg,
int strength,
int daysvalid)
Generates a certificate that allows signing / authentication.
|
protected static Tuple2<java.lang.String,java.lang.String> |
createCertificateBySpecification(java.lang.String issuercert,
java.lang.String issuerkey,
X500Name subject,
java.lang.String sigalg,
java.lang.String schemeconf,
java.lang.String digalg,
int strength,
int daysvalid,
Extension... extensions)
Creates a certificate using the given specification.
|
static Tuple2<java.lang.String,java.lang.String> |
createIntermediateCaCertificate(java.lang.String issuercert,
java.lang.String issuerkey,
java.lang.String subjectdn,
int pathlen,
java.lang.String scheme,
java.lang.String schemeconf,
java.lang.String hashalg,
int strength,
int daysvalid)
Generates a certificate for an intermediate CA.
|
protected static AsymmetricCipherKeyPair |
createKeyPair(java.lang.String alg,
java.lang.String algconf,
int strength)
Generate a key pair.
|
static Tuple2<java.lang.String,java.lang.String> |
createRootCaCertificate(java.lang.String subjectdn,
int pathlen,
java.lang.String scheme,
java.lang.String schemeconf,
java.lang.String hashalg,
int strength,
int daysvalid)
Generates a certificate for a root CA.
|
static Tuple2<java.lang.String,java.lang.String> |
createSelfSignedCertificate(java.lang.String subjectdn,
java.lang.String scheme,
java.lang.String schemeconf,
java.lang.String hashalg,
int strength,
int daysvalid)
Generates a self-signed certificate that allows signing / authentication.
|
static PemKeyPair |
createTestCACert()
Creates a random CA certificate for testing.
|
static PemKeyPair |
createTestCert(PemKeyPair ca)
Creates a random certificate for testing.
|
protected static java.security.SecureRandom |
generateParanoidSecureRandom()
Generates a secure PRNG.
|
protected static java.security.SecureRandom |
generateSecureRandom()
Generates a fast secure PRNG.
|
static java.lang.String |
getCertSigAlg(java.lang.String cert)
Gets the signatures algorithm supported by the key provided by a certificate.
|
static java.lang.String |
getCertSigAlg(X509CertificateHolder cert)
Gets the signatures algorithm supported by the key provided by a certificate.
|
protected static CertPathValidation[] |
getChainValidationRules()
Gets the certificate chain validation rules.
|
static java.lang.String |
getCommonName(X500Name name)
Returns the subject ID of a certificate.
|
protected static ContentVerifier |
getDefaultVerifier(X509CertificateHolder cert)
Gets a verifier based on a certificate to identify the algorithm.
|
static IEntropySource |
getEntropySource()
Gets a secure entropy source from OS or otherwise.
|
static java.security.SecureRandom |
getSecureRandom()
Gets access to the common secure PRNG.
|
static java.lang.String |
getSigAlg(SubjectPublicKeyInfo spki)
Gets the signatures algorithm supported by the key.
|
protected static ContentSigner |
getSigner(java.lang.String algospec,
PrivateKeyInfo pki)
Gets a signer based on a private key to identify the algorithm.
|
protected static ContentVerifierProvider |
getVerifierProvider(java.lang.Object keyinfo)
Gets a verifier provider based on a certificate to identify the algorithm.
|
static boolean |
isCaCertificate(java.lang.String cert)
Tests if a certificate is a CA certificate.
|
static java.util.List<X509CertificateHolder> |
readCertificateChainFromPEM(java.lang.String pem)
Reads a certificate chain.
|
static X509CertificateHolder |
readCertificateFromPEM(java.lang.String pem)
Read a certificate from a PEM-encoded string.
|
static PrivateKeyInfo |
readPrivateKeyFromPEM(java.lang.String pem)
Reads a private key from a PEM string.
|
static byte[] |
signWithPEM(byte[] msghash,
java.io.InputStream pemcert,
java.io.InputStream pemkey)
Sign using a PEM-encoded X.509 certificate/key.
|
static boolean |
verifyWithPEM(byte[] msghash,
byte[] token,
java.lang.String signingcert,
java.util.LinkedHashSet<X509CertificateHolder> trustchain)
Verify using a PEM-encoded X.509 certificate/key.
|
static java.lang.String |
writeCertificateAsPEM(X509CertificateHolder cert)
Writes a certificate as PEM-encoded string.
|
static byte[] |
xor(byte[] op1result,
byte[] op2)
XORs two byte arrays.
|
protected static final java.lang.String DEFAULT_SIGNATURE_HASH
public static boolean PARANOID_PRNG
protected static volatile java.security.SecureRandom SECURE_RANDOM
protected static volatile IEntropySource ENTROPY_SOURCE
protected static boolean ENTROPY_FALLBACK_WARNING_DONE
protected static boolean TEST_ENTROPY_FALLBACK
public static final java.security.SecureRandom getSecureRandom()
public static IEntropySource getEntropySource()
public static final byte[] signWithPEM(byte[] msghash, java.io.InputStream pemcert, java.io.InputStream pemkey)
msghash
- The message hash.pemcert
- The PEM certificate.pemkey
- The PEM key.public static final boolean verifyWithPEM(byte[] msghash, byte[] token, java.lang.String signingcert, java.util.LinkedHashSet<X509CertificateHolder> trustchain)
msghash
- The message hash.token
- The authentication token.signingcert
- The signing certificate.trustedpemcert
- The PEM certificate trust anchor.public static final Tuple2<java.lang.String,java.lang.String> createSelfSignedCertificate(java.lang.String subjectdn, java.lang.String scheme, java.lang.String schemeconf, java.lang.String hashalg, int strength, int daysvalid)
subjectdn
- The CA subject identifier.scheme
- Signature scheme to use, e.g. RSA, DSA, ECDSA.hashalg
- Hash algorithm to use.strength
- Strength of the key.daysvalid
- Number of days valid.public static final Tuple2<java.lang.String,java.lang.String> createCertificate(java.lang.String issuercert, java.lang.String issuerkey, java.lang.String subjectdn, java.lang.String scheme, java.lang.String schemeconf, java.lang.String hashalg, int strength, int daysvalid)
issuercert
- Certificate of the parent CA.issuerkey
- Key of the parent CA.subjectdn
- The CA subject identifier.scheme
- Signature scheme to use, e.g. RSA, DSA, ECDSA.hashalg
- Hash algorithm to use.strength
- Strength of the key.daysvalid
- Number of days valid.public static final Tuple2<java.lang.String,java.lang.String> createIntermediateCaCertificate(java.lang.String issuercert, java.lang.String issuerkey, java.lang.String subjectdn, int pathlen, java.lang.String scheme, java.lang.String schemeconf, java.lang.String hashalg, int strength, int daysvalid)
issuercert
- Certificate of the parent CA.issuerkey
- Key of the parent CA.subjectdn
- The CA subject identifier.pathlen
- Allowed path length for the intermediate CA (0 = no intermediate CA certificate children).scheme
- Signature scheme to use, e.g. RSA, DSA, ECDSA.hashalg
- Hash algorithm to use.strength
- Strength of the key.daysvalid
- Number of days valid.public static final Tuple2<java.lang.String,java.lang.String> createRootCaCertificate(java.lang.String subjectdn, int pathlen, java.lang.String scheme, java.lang.String schemeconf, java.lang.String hashalg, int strength, int daysvalid)
subjectdn
- The CA subject identifier.scheme
- Signature scheme to use, e.g. RSA, DSA, ECDSA.hashalg
- Hash algorithm to use.strength
- Strength of the key.daysvalid
- Number of days valid.public static final PemKeyPair createTestCACert()
public static final PemKeyPair createTestCert(PemKeyPair ca)
public static final byte[] xor(byte[] op1result, byte[] op2)
op1result
- First array and output array.op2
- Second array.public static final X509CertificateHolder readCertificateFromPEM(java.lang.String pem)
pem
- The PEM-encoded string.public static final java.util.List<X509CertificateHolder> readCertificateChainFromPEM(java.lang.String pem)
pem
- PEM of the chain.public static java.lang.String getCommonName(X500Name name)
cert
- The certificate.public static final boolean checkEntity(X509CertificateHolder cert, java.lang.String entityname)
cert
- The certificate.entityname
- The entity name.public static final java.lang.String writeCertificateAsPEM(X509CertificateHolder cert)
cert
- The certificate.public static final PrivateKeyInfo readPrivateKeyFromPEM(java.lang.String pem)
pem
- The PEM-encoded string.public static final boolean isCaCertificate(java.lang.String cert)
cert
- The certificate.public static final java.lang.String getCertSigAlg(java.lang.String cert)
cert
- The certificate.public static final java.lang.String getCertSigAlg(X509CertificateHolder cert)
cert
- The certificate.public static final java.lang.String getSigAlg(SubjectPublicKeyInfo spki)
spki
- The subject key info.protected static final CertPathValidation[] getChainValidationRules()
protected static final java.security.SecureRandom generateSecureRandom()
protected static final java.security.SecureRandom generateParanoidSecureRandom()
protected static final Tuple2<java.lang.String,java.lang.String> createCertificateBySpecification(java.lang.String issuercert, java.lang.String issuerkey, X500Name subject, java.lang.String sigalg, java.lang.String schemeconf, java.lang.String digalg, int strength, int daysvalid, Extension... extensions)
issuercert
- Certificate of the issuer (CA).issuerkey
- Key of the issuer (CA).subject
- Subject of the certificate.sigalg
- Signature scheme / certificate key algorithm to use, e.g. RSA, DSA, ECDSA.schemeconf
- Additional scheme configuration, may be null.digalg
- Hash algorithm to use for certificate signature.strength
- Strength of the key.daysvalid
- Number of days valid.extensions
- Certificate extensions.protected static final AsymmetricCipherKeyPair createKeyPair(java.lang.String alg, java.lang.String algconf, int strength)
alg
- Algorithm to use, e.g. RSA, DSA, ECDSA.strength
- Strength of the key pair.protected static final ContentSigner getSigner(java.lang.String algospec, PrivateKeyInfo pki)
pki
- The private key.protected static final ContentVerifier getDefaultVerifier(X509CertificateHolder cert)
cert
- The certificate.protected static final ContentVerifierProvider getVerifierProvider(java.lang.Object keyinfo)
keyinfo
- The certificate or key info.protected static final byte[] asn1ToBytes(ASN1Object obj)
obj
- The object.