Package org.bouncycastle.crypto.tls
Class TlsUtils
- java.lang.Object
-
- org.bouncycastle.crypto.tls.TlsUtils
-
public class TlsUtils extends java.lang.Object
Some helper functions for MicroTLS.
-
-
Field Summary
Fields Modifier and Type Field Description static byte[]
EMPTY_BYTES
static int[]
EMPTY_INTS
static long[]
EMPTY_LONGS
static short[]
EMPTY_SHORTS
static java.lang.Integer
EXT_signature_algorithms
-
Constructor Summary
Constructors Constructor Description TlsUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addSignatureAlgorithmsExtension(java.util.Hashtable extensions, java.util.Vector supportedSignatureAlgorithms)
Add a 'signature_algorithms' extension to existing extensions.static void
checkUint16(int i)
static void
checkUint16(long i)
static void
checkUint24(int i)
static void
checkUint24(long i)
static void
checkUint32(long i)
static void
checkUint48(long i)
static void
checkUint64(long i)
static void
checkUint8(int i)
static void
checkUint8(long i)
static void
checkUint8(short i)
static Digest
cloneHash(short hashAlgorithm, Digest hash)
static Digest
clonePRFHash(int prfAlgorithm, Digest hash)
static Digest
createHash(short hashAlgorithm)
static Digest
createHash(SignatureAndHashAlgorithm signatureAndHashAlgorithm)
static Digest
createPRFHash(int prfAlgorithm)
static byte[]
createSignatureAlgorithmsExtension(java.util.Vector supportedSignatureAlgorithms)
Create a 'signature_algorithms' extension value.static TlsSigner
createTlsSigner(short clientCertificateType)
static byte[]
encodeOpaque8(byte[] buf)
static void
encodeSupportedSignatureAlgorithms(java.util.Vector supportedSignatureAlgorithms, boolean allowAnonymous, java.io.OutputStream output)
static byte[]
encodeUint16ArrayWithUint16Length(int[] uints)
static byte[]
encodeUint8ArrayWithUint8Length(short[] uints)
static java.util.Vector
getAllSignatureAlgorithms()
static int
getCipherType(int ciphersuite)
static java.util.Vector
getDefaultDSSSignatureAlgorithms()
static java.util.Vector
getDefaultECDSASignatureAlgorithms()
static java.util.Vector
getDefaultRSASignatureAlgorithms()
static java.util.Vector
getDefaultSupportedSignatureAlgorithms()
static int
getEncryptionAlgorithm(int ciphersuite)
static byte[]
getExtensionData(java.util.Hashtable extensions, java.lang.Integer extensionType)
static short
getHashAlgorithmForPRFAlgorithm(int prfAlgorithm)
static int
getKeyExchangeAlgorithm(int ciphersuite)
static int
getMACAlgorithm(int ciphersuite)
static ProtocolVersion
getMinimumVersion(int ciphersuite)
static ASN1ObjectIdentifier
getOIDForHashAlgorithm(short hashAlgorithm)
static java.util.Vector
getSignatureAlgorithmsExtension(java.util.Hashtable extensions)
Get a 'signature_algorithms' extension from extensions.static SignatureAndHashAlgorithm
getSignatureAndHashAlgorithm(TlsContext context, TlsSignerCredentials signerCredentials)
static java.util.Vector
getUsableSignatureAlgorithms(java.util.Vector sigHashAlgs)
static boolean
hasExpectedEmptyExtensionData(java.util.Hashtable extensions, java.lang.Integer extensionType, short alertDescription)
static boolean
hasSigningCapability(short clientCertificateType)
static TlsSession
importSession(byte[] sessionID, SessionParameters sessionParameters)
static boolean
isAEADCipherSuite(int ciphersuite)
static boolean
isBlockCipherSuite(int ciphersuite)
static boolean
isSignatureAlgorithmsExtensionAllowed(ProtocolVersion clientVersion)
static boolean
isSSL(TlsContext context)
static boolean
isStreamCipherSuite(int ciphersuite)
static boolean
isTLSv11(ProtocolVersion version)
static boolean
isTLSv11(TlsContext context)
static boolean
isTLSv12(ProtocolVersion version)
static boolean
isTLSv12(TlsContext context)
static boolean
isValidCipherSuiteForSignatureAlgorithms(int cipherSuite, java.util.Vector sigAlgs)
static boolean
isValidCipherSuiteForVersion(int cipherSuite, ProtocolVersion serverVersion)
static boolean
isValidUint16(int i)
static boolean
isValidUint16(long i)
static boolean
isValidUint24(int i)
static boolean
isValidUint24(long i)
static boolean
isValidUint32(long i)
static boolean
isValidUint48(long i)
static boolean
isValidUint64(long i)
static boolean
isValidUint8(int i)
static boolean
isValidUint8(long i)
static boolean
isValidUint8(short i)
static java.util.Vector
parseSupportedSignatureAlgorithms(boolean allowAnonymous, java.io.InputStream input)
static byte[]
PRF(TlsContext context, byte[] secret, java.lang.String asciiLabel, byte[] seed, int size)
static byte[]
PRF_legacy(byte[] secret, java.lang.String asciiLabel, byte[] seed, int size)
static byte[]
readAllOrNothing(int length, java.io.InputStream input)
static ASN1Primitive
readASN1Object(byte[] encoding)
static ASN1Primitive
readDERObject(byte[] encoding)
static void
readFully(byte[] buf, java.io.InputStream input)
static byte[]
readFully(int length, java.io.InputStream input)
static byte[]
readOpaque16(java.io.InputStream input)
static byte[]
readOpaque24(java.io.InputStream input)
static byte[]
readOpaque8(java.io.InputStream input)
static java.util.Vector
readSignatureAlgorithmsExtension(byte[] extensionData)
Read 'signature_algorithms' extension data.static int
readUint16(byte[] buf, int offset)
static int
readUint16(java.io.InputStream input)
static int[]
readUint16Array(int count, java.io.InputStream input)
static int
readUint24(byte[] buf, int offset)
static int
readUint24(java.io.InputStream input)
static long
readUint32(byte[] buf, int offset)
static long
readUint32(java.io.InputStream input)
static long
readUint48(byte[] buf, int offset)
static long
readUint48(java.io.InputStream input)
static short
readUint8(byte[] buf, int offset)
static short
readUint8(java.io.InputStream input)
static short[]
readUint8Array(int count, java.io.InputStream input)
static ProtocolVersion
readVersion(byte[] buf, int offset)
static ProtocolVersion
readVersion(java.io.InputStream input)
static int
readVersionRaw(byte[] buf, int offset)
static int
readVersionRaw(java.io.InputStream input)
static void
verifySupportedSignatureAlgorithm(java.util.Vector supportedSignatureAlgorithms, SignatureAndHashAlgorithm signatureAlgorithm)
static void
writeGMTUnixTime(byte[] buf, int offset)
static void
writeOpaque16(byte[] buf, java.io.OutputStream output)
static void
writeOpaque24(byte[] buf, java.io.OutputStream output)
static void
writeOpaque8(byte[] buf, java.io.OutputStream output)
static void
writeUint16(int i, byte[] buf, int offset)
static void
writeUint16(int i, java.io.OutputStream output)
static void
writeUint16Array(int[] uints, byte[] buf, int offset)
static void
writeUint16Array(int[] uints, java.io.OutputStream output)
static void
writeUint16ArrayWithUint16Length(int[] uints, byte[] buf, int offset)
static void
writeUint16ArrayWithUint16Length(int[] uints, java.io.OutputStream output)
static void
writeUint24(int i, byte[] buf, int offset)
static void
writeUint24(int i, java.io.OutputStream output)
static void
writeUint32(long i, byte[] buf, int offset)
static void
writeUint32(long i, java.io.OutputStream output)
static void
writeUint48(long i, byte[] buf, int offset)
static void
writeUint48(long i, java.io.OutputStream output)
static void
writeUint64(long i, byte[] buf, int offset)
static void
writeUint64(long i, java.io.OutputStream output)
static void
writeUint8(int i, byte[] buf, int offset)
static void
writeUint8(int i, java.io.OutputStream output)
static void
writeUint8(short i, byte[] buf, int offset)
static void
writeUint8(short i, java.io.OutputStream output)
static void
writeUint8Array(short[] uints, byte[] buf, int offset)
static void
writeUint8Array(short[] uints, java.io.OutputStream output)
static void
writeUint8ArrayWithUint8Length(short[] uints, byte[] buf, int offset)
static void
writeUint8ArrayWithUint8Length(short[] uints, java.io.OutputStream output)
static void
writeVersion(ProtocolVersion version, byte[] buf, int offset)
static void
writeVersion(ProtocolVersion version, java.io.OutputStream output)
-
-
-
Field Detail
-
EMPTY_BYTES
public static final byte[] EMPTY_BYTES
-
EMPTY_SHORTS
public static final short[] EMPTY_SHORTS
-
EMPTY_INTS
public static final int[] EMPTY_INTS
-
EMPTY_LONGS
public static final long[] EMPTY_LONGS
-
EXT_signature_algorithms
public static final java.lang.Integer EXT_signature_algorithms
-
-
Method Detail
-
checkUint8
public static void checkUint8(short i) throws java.io.IOException
- Throws:
java.io.IOException
-
checkUint8
public static void checkUint8(int i) throws java.io.IOException
- Throws:
java.io.IOException
-
checkUint8
public static void checkUint8(long i) throws java.io.IOException
- Throws:
java.io.IOException
-
checkUint16
public static void checkUint16(int i) throws java.io.IOException
- Throws:
java.io.IOException
-
checkUint16
public static void checkUint16(long i) throws java.io.IOException
- Throws:
java.io.IOException
-
checkUint24
public static void checkUint24(int i) throws java.io.IOException
- Throws:
java.io.IOException
-
checkUint24
public static void checkUint24(long i) throws java.io.IOException
- Throws:
java.io.IOException
-
checkUint32
public static void checkUint32(long i) throws java.io.IOException
- Throws:
java.io.IOException
-
checkUint48
public static void checkUint48(long i) throws java.io.IOException
- Throws:
java.io.IOException
-
checkUint64
public static void checkUint64(long i) throws java.io.IOException
- Throws:
java.io.IOException
-
isValidUint8
public static boolean isValidUint8(short i)
-
isValidUint8
public static boolean isValidUint8(int i)
-
isValidUint8
public static boolean isValidUint8(long i)
-
isValidUint16
public static boolean isValidUint16(int i)
-
isValidUint16
public static boolean isValidUint16(long i)
-
isValidUint24
public static boolean isValidUint24(int i)
-
isValidUint24
public static boolean isValidUint24(long i)
-
isValidUint32
public static boolean isValidUint32(long i)
-
isValidUint48
public static boolean isValidUint48(long i)
-
isValidUint64
public static boolean isValidUint64(long i)
-
isSSL
public static boolean isSSL(TlsContext context)
-
isTLSv11
public static boolean isTLSv11(ProtocolVersion version)
-
isTLSv11
public static boolean isTLSv11(TlsContext context)
-
isTLSv12
public static boolean isTLSv12(ProtocolVersion version)
-
isTLSv12
public static boolean isTLSv12(TlsContext context)
-
writeUint8
public static void writeUint8(short i, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint8
public static void writeUint8(int i, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint8
public static void writeUint8(short i, byte[] buf, int offset)
-
writeUint8
public static void writeUint8(int i, byte[] buf, int offset)
-
writeUint16
public static void writeUint16(int i, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint16
public static void writeUint16(int i, byte[] buf, int offset)
-
writeUint24
public static void writeUint24(int i, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint24
public static void writeUint24(int i, byte[] buf, int offset)
-
writeUint32
public static void writeUint32(long i, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint32
public static void writeUint32(long i, byte[] buf, int offset)
-
writeUint48
public static void writeUint48(long i, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint48
public static void writeUint48(long i, byte[] buf, int offset)
-
writeUint64
public static void writeUint64(long i, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint64
public static void writeUint64(long i, byte[] buf, int offset)
-
writeOpaque8
public static void writeOpaque8(byte[] buf, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeOpaque16
public static void writeOpaque16(byte[] buf, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeOpaque24
public static void writeOpaque24(byte[] buf, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint8Array
public static void writeUint8Array(short[] uints, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint8Array
public static void writeUint8Array(short[] uints, byte[] buf, int offset) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint8ArrayWithUint8Length
public static void writeUint8ArrayWithUint8Length(short[] uints, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint8ArrayWithUint8Length
public static void writeUint8ArrayWithUint8Length(short[] uints, byte[] buf, int offset) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint16Array
public static void writeUint16Array(int[] uints, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint16Array
public static void writeUint16Array(int[] uints, byte[] buf, int offset) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint16ArrayWithUint16Length
public static void writeUint16ArrayWithUint16Length(int[] uints, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeUint16ArrayWithUint16Length
public static void writeUint16ArrayWithUint16Length(int[] uints, byte[] buf, int offset) throws java.io.IOException
- Throws:
java.io.IOException
-
encodeOpaque8
public static byte[] encodeOpaque8(byte[] buf) throws java.io.IOException
- Throws:
java.io.IOException
-
encodeUint8ArrayWithUint8Length
public static byte[] encodeUint8ArrayWithUint8Length(short[] uints) throws java.io.IOException
- Throws:
java.io.IOException
-
encodeUint16ArrayWithUint16Length
public static byte[] encodeUint16ArrayWithUint16Length(int[] uints) throws java.io.IOException
- Throws:
java.io.IOException
-
readUint8
public static short readUint8(java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readUint8
public static short readUint8(byte[] buf, int offset)
-
readUint16
public static int readUint16(java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readUint16
public static int readUint16(byte[] buf, int offset)
-
readUint24
public static int readUint24(java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readUint24
public static int readUint24(byte[] buf, int offset)
-
readUint32
public static long readUint32(java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readUint32
public static long readUint32(byte[] buf, int offset)
-
readUint48
public static long readUint48(java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readUint48
public static long readUint48(byte[] buf, int offset)
-
readAllOrNothing
public static byte[] readAllOrNothing(int length, java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readFully
public static byte[] readFully(int length, java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readFully
public static void readFully(byte[] buf, java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readOpaque8
public static byte[] readOpaque8(java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readOpaque16
public static byte[] readOpaque16(java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readOpaque24
public static byte[] readOpaque24(java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readUint8Array
public static short[] readUint8Array(int count, java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readUint16Array
public static int[] readUint16Array(int count, java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readVersion
public static ProtocolVersion readVersion(byte[] buf, int offset) throws java.io.IOException
- Throws:
java.io.IOException
-
readVersion
public static ProtocolVersion readVersion(java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readVersionRaw
public static int readVersionRaw(byte[] buf, int offset) throws java.io.IOException
- Throws:
java.io.IOException
-
readVersionRaw
public static int readVersionRaw(java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
readASN1Object
public static ASN1Primitive readASN1Object(byte[] encoding) throws java.io.IOException
- Throws:
java.io.IOException
-
readDERObject
public static ASN1Primitive readDERObject(byte[] encoding) throws java.io.IOException
- Throws:
java.io.IOException
-
writeGMTUnixTime
public static void writeGMTUnixTime(byte[] buf, int offset)
-
writeVersion
public static void writeVersion(ProtocolVersion version, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
writeVersion
public static void writeVersion(ProtocolVersion version, byte[] buf, int offset)
-
getAllSignatureAlgorithms
public static java.util.Vector getAllSignatureAlgorithms()
-
getDefaultDSSSignatureAlgorithms
public static java.util.Vector getDefaultDSSSignatureAlgorithms()
-
getDefaultECDSASignatureAlgorithms
public static java.util.Vector getDefaultECDSASignatureAlgorithms()
-
getDefaultRSASignatureAlgorithms
public static java.util.Vector getDefaultRSASignatureAlgorithms()
-
getDefaultSupportedSignatureAlgorithms
public static java.util.Vector getDefaultSupportedSignatureAlgorithms()
-
getSignatureAndHashAlgorithm
public static SignatureAndHashAlgorithm getSignatureAndHashAlgorithm(TlsContext context, TlsSignerCredentials signerCredentials) throws java.io.IOException
- Throws:
java.io.IOException
-
getExtensionData
public static byte[] getExtensionData(java.util.Hashtable extensions, java.lang.Integer extensionType)
-
hasExpectedEmptyExtensionData
public static boolean hasExpectedEmptyExtensionData(java.util.Hashtable extensions, java.lang.Integer extensionType, short alertDescription) throws java.io.IOException
- Throws:
java.io.IOException
-
importSession
public static TlsSession importSession(byte[] sessionID, SessionParameters sessionParameters)
-
isSignatureAlgorithmsExtensionAllowed
public static boolean isSignatureAlgorithmsExtensionAllowed(ProtocolVersion clientVersion)
-
addSignatureAlgorithmsExtension
public static void addSignatureAlgorithmsExtension(java.util.Hashtable extensions, java.util.Vector supportedSignatureAlgorithms) throws java.io.IOException
Add a 'signature_algorithms' extension to existing extensions.- Parameters:
extensions
- AHashtable
to add the extension to.supportedSignatureAlgorithms
-Vector
containing at least 1SignatureAndHashAlgorithm
.- Throws:
java.io.IOException
-
getSignatureAlgorithmsExtension
public static java.util.Vector getSignatureAlgorithmsExtension(java.util.Hashtable extensions) throws java.io.IOException
Get a 'signature_algorithms' extension from extensions.- Parameters:
extensions
- AHashtable
to get the extension from, if it is present.- Returns:
- A
Vector
containing at least 1SignatureAndHashAlgorithm
, or null. - Throws:
java.io.IOException
-
createSignatureAlgorithmsExtension
public static byte[] createSignatureAlgorithmsExtension(java.util.Vector supportedSignatureAlgorithms) throws java.io.IOException
Create a 'signature_algorithms' extension value.- Parameters:
supportedSignatureAlgorithms
- AVector
containing at least 1SignatureAndHashAlgorithm
.- Returns:
- A byte array suitable for use as an extension value.
- Throws:
java.io.IOException
-
readSignatureAlgorithmsExtension
public static java.util.Vector readSignatureAlgorithmsExtension(byte[] extensionData) throws java.io.IOException
Read 'signature_algorithms' extension data.- Parameters:
extensionData
- The extension data.- Returns:
- A
Vector
containing at least 1SignatureAndHashAlgorithm
. - Throws:
java.io.IOException
-
encodeSupportedSignatureAlgorithms
public static void encodeSupportedSignatureAlgorithms(java.util.Vector supportedSignatureAlgorithms, boolean allowAnonymous, java.io.OutputStream output) throws java.io.IOException
- Throws:
java.io.IOException
-
parseSupportedSignatureAlgorithms
public static java.util.Vector parseSupportedSignatureAlgorithms(boolean allowAnonymous, java.io.InputStream input) throws java.io.IOException
- Throws:
java.io.IOException
-
verifySupportedSignatureAlgorithm
public static void verifySupportedSignatureAlgorithm(java.util.Vector supportedSignatureAlgorithms, SignatureAndHashAlgorithm signatureAlgorithm) throws java.io.IOException
- Throws:
java.io.IOException
-
PRF
public static byte[] PRF(TlsContext context, byte[] secret, java.lang.String asciiLabel, byte[] seed, int size)
-
PRF_legacy
public static byte[] PRF_legacy(byte[] secret, java.lang.String asciiLabel, byte[] seed, int size)
-
createHash
public static Digest createHash(short hashAlgorithm)
-
createHash
public static Digest createHash(SignatureAndHashAlgorithm signatureAndHashAlgorithm)
-
createPRFHash
public static Digest createPRFHash(int prfAlgorithm)
-
getHashAlgorithmForPRFAlgorithm
public static short getHashAlgorithmForPRFAlgorithm(int prfAlgorithm)
-
getOIDForHashAlgorithm
public static ASN1ObjectIdentifier getOIDForHashAlgorithm(short hashAlgorithm)
-
hasSigningCapability
public static boolean hasSigningCapability(short clientCertificateType)
-
createTlsSigner
public static TlsSigner createTlsSigner(short clientCertificateType)
-
getCipherType
public static int getCipherType(int ciphersuite) throws java.io.IOException
- Throws:
java.io.IOException
-
getEncryptionAlgorithm
public static int getEncryptionAlgorithm(int ciphersuite) throws java.io.IOException
- Throws:
java.io.IOException
-
getKeyExchangeAlgorithm
public static int getKeyExchangeAlgorithm(int ciphersuite) throws java.io.IOException
- Throws:
java.io.IOException
-
getMACAlgorithm
public static int getMACAlgorithm(int ciphersuite) throws java.io.IOException
- Throws:
java.io.IOException
-
getMinimumVersion
public static ProtocolVersion getMinimumVersion(int ciphersuite)
-
isAEADCipherSuite
public static boolean isAEADCipherSuite(int ciphersuite) throws java.io.IOException
- Throws:
java.io.IOException
-
isBlockCipherSuite
public static boolean isBlockCipherSuite(int ciphersuite) throws java.io.IOException
- Throws:
java.io.IOException
-
isStreamCipherSuite
public static boolean isStreamCipherSuite(int ciphersuite) throws java.io.IOException
- Throws:
java.io.IOException
-
isValidCipherSuiteForSignatureAlgorithms
public static boolean isValidCipherSuiteForSignatureAlgorithms(int cipherSuite, java.util.Vector sigAlgs)
-
isValidCipherSuiteForVersion
public static boolean isValidCipherSuiteForVersion(int cipherSuite, ProtocolVersion serverVersion)
-
getUsableSignatureAlgorithms
public static java.util.Vector getUsableSignatureAlgorithms(java.util.Vector sigHashAlgs)
-
-