Class TlsUtils

java.lang.Object
org.bouncycastle.tls.TlsUtils

public class TlsUtils extends Object
Some helper functions for the TLS API.
  • Field Details

    • 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
    • EMPTY_STRINGS

      public static final String[] EMPTY_STRINGS
  • Constructor Details

    • TlsUtils

      public TlsUtils()
  • Method Details

    • checkUint8

      public static void checkUint8(short i) throws IOException
      Throws:
      IOException
    • checkUint8

      public static void checkUint8(int i) throws IOException
      Throws:
      IOException
    • checkUint8

      public static void checkUint8(long i) throws IOException
      Throws:
      IOException
    • checkUint16

      public static void checkUint16(int i) throws IOException
      Throws:
      IOException
    • checkUint16

      public static void checkUint16(long i) throws IOException
      Throws:
      IOException
    • checkUint24

      public static void checkUint24(int i) throws IOException
      Throws:
      IOException
    • checkUint24

      public static void checkUint24(long i) throws IOException
      Throws:
      IOException
    • checkUint32

      public static void checkUint32(long i) throws IOException
      Throws:
      IOException
    • checkUint48

      public static void checkUint48(long i) throws IOException
      Throws:
      IOException
    • checkUint64

      public static void checkUint64(long i) throws IOException
      Throws:
      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)
    • isTLSv10

      public static boolean isTLSv10(ProtocolVersion version)
    • isTLSv10

      public static boolean isTLSv10(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)
    • isTLSv13

      public static boolean isTLSv13(ProtocolVersion version)
    • isTLSv13

      public static boolean isTLSv13(TlsContext context)
    • writeUint8

      public static void writeUint8(short i, OutputStream output) throws IOException
      Throws:
      IOException
    • writeUint8

      public static void writeUint8(int i, OutputStream output) throws IOException
      Throws:
      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, OutputStream output) throws IOException
      Throws:
      IOException
    • writeUint16

      public static void writeUint16(int i, byte[] buf, int offset)
    • writeUint24

      public static void writeUint24(int i, OutputStream output) throws IOException
      Throws:
      IOException
    • writeUint24

      public static void writeUint24(int i, byte[] buf, int offset)
    • writeUint32

      public static void writeUint32(long i, OutputStream output) throws IOException
      Throws:
      IOException
    • writeUint32

      public static void writeUint32(long i, byte[] buf, int offset)
    • writeUint48

      public static void writeUint48(long i, OutputStream output) throws IOException
      Throws:
      IOException
    • writeUint48

      public static void writeUint48(long i, byte[] buf, int offset)
    • writeUint64

      public static void writeUint64(long i, OutputStream output) throws IOException
      Throws:
      IOException
    • writeUint64

      public static void writeUint64(long i, byte[] buf, int offset)
    • writeOpaque8

      public static void writeOpaque8(byte[] buf, OutputStream output) throws IOException
      Throws:
      IOException
    • writeOpaque8

      public static void writeOpaque8(byte[] data, byte[] buf, int off) throws IOException
      Throws:
      IOException
    • writeOpaque16

      public static void writeOpaque16(byte[] buf, OutputStream output) throws IOException
      Throws:
      IOException
    • writeOpaque16

      public static void writeOpaque16(byte[] data, byte[] buf, int off) throws IOException
      Throws:
      IOException
    • writeOpaque24

      public static void writeOpaque24(byte[] buf, OutputStream output) throws IOException
      Throws:
      IOException
    • writeOpaque24

      public static void writeOpaque24(byte[] data, byte[] buf, int off) throws IOException
      Throws:
      IOException
    • writeUint8Array

      public static void writeUint8Array(short[] uints, OutputStream output) throws IOException
      Throws:
      IOException
    • writeUint8Array

      public static void writeUint8Array(short[] uints, byte[] buf, int offset) throws IOException
      Throws:
      IOException
    • writeUint8ArrayWithUint8Length

      public static void writeUint8ArrayWithUint8Length(short[] uints, OutputStream output) throws IOException
      Throws:
      IOException
    • writeUint8ArrayWithUint8Length

      public static void writeUint8ArrayWithUint8Length(short[] uints, byte[] buf, int offset) throws IOException
      Throws:
      IOException
    • writeUint16Array

      public static void writeUint16Array(int[] uints, OutputStream output) throws IOException
      Throws:
      IOException
    • writeUint16Array

      public static void writeUint16Array(int[] uints, byte[] buf, int offset) throws IOException
      Throws:
      IOException
    • writeUint16ArrayWithUint8Length

      public static void writeUint16ArrayWithUint8Length(int[] uints, byte[] buf, int offset) throws IOException
      Throws:
      IOException
    • writeUint16ArrayWithUint16Length

      public static void writeUint16ArrayWithUint16Length(int[] uints, OutputStream output) throws IOException
      Throws:
      IOException
    • writeUint16ArrayWithUint16Length

      public static void writeUint16ArrayWithUint16Length(int[] uints, byte[] buf, int offset) throws IOException
      Throws:
      IOException
    • decodeOpaque8

      public static byte[] decodeOpaque8(byte[] buf) throws IOException
      Throws:
      IOException
    • decodeOpaque8

      public static byte[] decodeOpaque8(byte[] buf, int minLength) throws IOException
      Throws:
      IOException
    • decodeOpaque16

      public static byte[] decodeOpaque16(byte[] buf) throws IOException
      Throws:
      IOException
    • decodeOpaque16

      public static byte[] decodeOpaque16(byte[] buf, int minLength) throws IOException
      Throws:
      IOException
    • decodeUint8

      public static short decodeUint8(byte[] buf) throws IOException
      Throws:
      IOException
    • decodeUint8ArrayWithUint8Length

      public static short[] decodeUint8ArrayWithUint8Length(byte[] buf) throws IOException
      Throws:
      IOException
    • decodeUint16

      public static int decodeUint16(byte[] buf) throws IOException
      Throws:
      IOException
    • decodeUint16ArrayWithUint8Length

      public static int[] decodeUint16ArrayWithUint8Length(byte[] buf) throws IOException
      Throws:
      IOException
    • decodeUint32

      public static long decodeUint32(byte[] buf) throws IOException
      Throws:
      IOException
    • encodeOpaque8

      public static byte[] encodeOpaque8(byte[] buf) throws IOException
      Throws:
      IOException
    • encodeOpaque16

      public static byte[] encodeOpaque16(byte[] buf) throws IOException
      Throws:
      IOException
    • encodeOpaque24

      public static byte[] encodeOpaque24(byte[] buf) throws IOException
      Throws:
      IOException
    • encodeUint8

      public static byte[] encodeUint8(short uint) throws IOException
      Throws:
      IOException
    • encodeUint8ArrayWithUint8Length

      public static byte[] encodeUint8ArrayWithUint8Length(short[] uints) throws IOException
      Throws:
      IOException
    • encodeUint16

      public static byte[] encodeUint16(int uint) throws IOException
      Throws:
      IOException
    • encodeUint16ArrayWithUint8Length

      public static byte[] encodeUint16ArrayWithUint8Length(int[] uints) throws IOException
      Throws:
      IOException
    • encodeUint16ArrayWithUint16Length

      public static byte[] encodeUint16ArrayWithUint16Length(int[] uints) throws IOException
      Throws:
      IOException
    • encodeUint24

      public static byte[] encodeUint24(int uint) throws IOException
      Throws:
      IOException
    • encodeUint32

      public static byte[] encodeUint32(long uint) throws IOException
      Throws:
      IOException
    • encodeVersion

      public static byte[] encodeVersion(ProtocolVersion version) throws IOException
      Throws:
      IOException
    • readInt32

      public static int readInt32(byte[] buf, int offset)
    • readUint8

      public static short readUint8(InputStream input) throws IOException
      Throws:
      IOException
    • readUint8

      public static short readUint8(byte[] buf, int offset)
    • readUint16

      public static int readUint16(InputStream input) throws IOException
      Throws:
      IOException
    • readUint16

      public static int readUint16(byte[] buf, int offset)
    • readUint24

      public static int readUint24(InputStream input) throws IOException
      Throws:
      IOException
    • readUint24

      public static int readUint24(byte[] buf, int offset)
    • readUint32

      public static long readUint32(InputStream input) throws IOException
      Throws:
      IOException
    • readUint32

      public static long readUint32(byte[] buf, int offset)
    • readUint48

      public static long readUint48(InputStream input) throws IOException
      Throws:
      IOException
    • readUint48

      public static long readUint48(byte[] buf, int offset)
    • readAllOrNothing

      public static byte[] readAllOrNothing(int length, InputStream input) throws IOException
      Throws:
      IOException
    • readFully

      public static byte[] readFully(int length, InputStream input) throws IOException
      Throws:
      IOException
    • readFully

      public static void readFully(byte[] buf, InputStream input) throws IOException
      Throws:
      IOException
    • readOpaque8

      public static byte[] readOpaque8(InputStream input) throws IOException
      Throws:
      IOException
    • readOpaque8

      public static byte[] readOpaque8(InputStream input, int minLength) throws IOException
      Throws:
      IOException
    • readOpaque8

      public static byte[] readOpaque8(InputStream input, int minLength, int maxLength) throws IOException
      Throws:
      IOException
    • readOpaque16

      public static byte[] readOpaque16(InputStream input) throws IOException
      Throws:
      IOException
    • readOpaque16

      public static byte[] readOpaque16(InputStream input, int minLength) throws IOException
      Throws:
      IOException
    • readOpaque24

      public static byte[] readOpaque24(InputStream input) throws IOException
      Throws:
      IOException
    • readOpaque24

      public static byte[] readOpaque24(InputStream input, int minLength) throws IOException
      Throws:
      IOException
    • readUint8Array

      public static short[] readUint8Array(int count, InputStream input) throws IOException
      Throws:
      IOException
    • readUint8ArrayWithUint8Length

      public static short[] readUint8ArrayWithUint8Length(InputStream input, int minLength) throws IOException
      Throws:
      IOException
    • readUint16Array

      public static int[] readUint16Array(int count, InputStream input) throws IOException
      Throws:
      IOException
    • readVersion

      public static ProtocolVersion readVersion(byte[] buf, int offset)
    • readVersion

      public static ProtocolVersion readVersion(InputStream input) throws IOException
      Throws:
      IOException
    • readASN1Object

      public static org.bouncycastle.asn1.ASN1Primitive readASN1Object(byte[] encoding) throws IOException
      Throws:
      IOException
    • readDERObject

      public static org.bouncycastle.asn1.ASN1Primitive readDERObject(byte[] encoding) throws IOException
      Deprecated.
      Will be removed. Use readASN1Object in combination with requireDEREncoding instead
      Throws:
      IOException
    • requireDEREncoding

      public static void requireDEREncoding(org.bouncycastle.asn1.ASN1Object asn1, byte[] encoding) throws IOException
      Throws:
      IOException
    • writeGMTUnixTime

      public static void writeGMTUnixTime(byte[] buf, int offset)
    • writeVersion

      public static void writeVersion(ProtocolVersion version, OutputStream output) throws IOException
      Throws:
      IOException
    • writeVersion

      public static void writeVersion(ProtocolVersion version, byte[] buf, int offset)
    • addIfSupported

      public static void addIfSupported(Vector supportedAlgs, TlsCrypto crypto, SignatureAndHashAlgorithm alg)
    • addIfSupported

      public static void addIfSupported(Vector supportedGroups, TlsCrypto crypto, int namedGroup)
    • addIfSupported

      public static void addIfSupported(Vector supportedGroups, TlsCrypto crypto, int[] namedGroups)
    • addToSet

      public static boolean addToSet(Vector s, int i)
    • getDefaultDSSSignatureAlgorithms

      public static Vector getDefaultDSSSignatureAlgorithms()
    • getDefaultECDSASignatureAlgorithms

      public static Vector getDefaultECDSASignatureAlgorithms()
    • getDefaultRSASignatureAlgorithms

      public static Vector getDefaultRSASignatureAlgorithms()
    • getDefaultSignatureAlgorithm

      public static SignatureAndHashAlgorithm getDefaultSignatureAlgorithm(short signatureAlgorithm)
    • getDefaultSignatureAlgorithms

      public static Vector getDefaultSignatureAlgorithms(short signatureAlgorithm)
    • getDefaultSupportedSignatureAlgorithms

      public static Vector getDefaultSupportedSignatureAlgorithms(TlsContext context)
    • getSupportedSignatureAlgorithms

      public static Vector getSupportedSignatureAlgorithms(TlsContext context, Vector candidates)
    • getSignatureAndHashAlgorithm

      public static SignatureAndHashAlgorithm getSignatureAndHashAlgorithm(TlsContext context, TlsCredentialedSigner signerCredentials) throws IOException
      Deprecated.
      Will be removed
      Throws:
      IOException
    • getExtensionData

      public static byte[] getExtensionData(Hashtable extensions, Integer extensionType)
    • hasExpectedEmptyExtensionData

      public static boolean hasExpectedEmptyExtensionData(Hashtable extensions, Integer extensionType, short alertDescription) throws IOException
      Throws:
      IOException
    • importSession

      public static TlsSession importSession(byte[] sessionID, SessionParameters sessionParameters)
    • isNullOrContainsNull

      public static boolean isNullOrContainsNull(Object[] array)
    • isNullOrEmpty

      public static boolean isNullOrEmpty(byte[] array)
    • isNullOrEmpty

      public static boolean isNullOrEmpty(short[] array)
    • isNullOrEmpty

      public static boolean isNullOrEmpty(int[] array)
    • isNullOrEmpty

      public static boolean isNullOrEmpty(Object[] array)
    • isNullOrEmpty

      public static boolean isNullOrEmpty(String s)
    • isNullOrEmpty

      public static boolean isNullOrEmpty(Vector v)
    • isSignatureAlgorithmsExtensionAllowed

      public static boolean isSignatureAlgorithmsExtensionAllowed(ProtocolVersion version)
    • getLegacyClientCertType

      public static short getLegacyClientCertType(short signatureAlgorithm)
    • getLegacySignatureAlgorithmClient

      public static short getLegacySignatureAlgorithmClient(short clientCertificateType)
    • getLegacySignatureAlgorithmClientCert

      public static short getLegacySignatureAlgorithmClientCert(short clientCertificateType)
    • getLegacySignatureAlgorithmServer

      public static short getLegacySignatureAlgorithmServer(int keyExchangeAlgorithm)
    • getLegacySignatureAlgorithmServerCert

      public static short getLegacySignatureAlgorithmServerCert(int keyExchangeAlgorithm)
    • getLegacySupportedSignatureAlgorithms

      public static Vector getLegacySupportedSignatureAlgorithms()
    • encodeSupportedSignatureAlgorithms

      public static void encodeSupportedSignatureAlgorithms(Vector supportedSignatureAlgorithms, OutputStream output) throws IOException
      Throws:
      IOException
    • parseSupportedSignatureAlgorithms

      public static Vector parseSupportedSignatureAlgorithms(InputStream input) throws IOException
      Throws:
      IOException
    • verifySupportedSignatureAlgorithm

      public static void verifySupportedSignatureAlgorithm(Vector supportedSignatureAlgorithms, SignatureAndHashAlgorithm signatureAlgorithm) throws IOException
      Throws:
      IOException
    • containsSignatureAlgorithm

      public static boolean containsSignatureAlgorithm(Vector supportedSignatureAlgorithms, SignatureAndHashAlgorithm signatureAlgorithm) throws IOException
      Throws:
      IOException
    • containsAnySignatureAlgorithm

      public static boolean containsAnySignatureAlgorithm(Vector supportedSignatureAlgorithms, short signatureAlgorithm)
    • PRF

      public static TlsSecret PRF(SecurityParameters securityParameters, TlsSecret secret, String asciiLabel, byte[] seed, int length)
    • PRF

      public static TlsSecret PRF(TlsContext context, TlsSecret secret, String asciiLabel, byte[] seed, int length)
    • clone

      public static byte[] clone(byte[] data)
    • clone

      public static String[] clone(String[] s)
    • constantTimeAreEqual

      public static boolean constantTimeAreEqual(int len, byte[] a, int aOff, byte[] b, int bOff)
    • copyOfRangeExact

      public static byte[] copyOfRangeExact(byte[] original, int from, int to)
    • calculateExporterSeed

      public static byte[] calculateExporterSeed(SecurityParameters securityParameters, byte[] context)
    • getOIDForHashAlgorithm

      public static org.bouncycastle.asn1.ASN1ObjectIdentifier getOIDForHashAlgorithm(short hashAlgorithm)
    • hasSigningCapability

      public static boolean hasSigningCapability(short clientCertificateType)
    • vectorOfOne

      public static Vector vectorOfOne(Object obj)
    • getCipherType

      public static int getCipherType(int cipherSuite)
    • getEncryptionAlgorithm

      public static int getEncryptionAlgorithm(int cipherSuite)
    • getEncryptionAlgorithmType

      public static int getEncryptionAlgorithmType(int encryptionAlgorithm)
    • getKeyExchangeAlgorithm

      public static int getKeyExchangeAlgorithm(int cipherSuite)
    • getKeyExchangeAlgorithms

      public static Vector getKeyExchangeAlgorithms(int[] cipherSuites)
    • getMACAlgorithm

      public static int getMACAlgorithm(int cipherSuite)
    • getMinimumVersion

      public static ProtocolVersion getMinimumVersion(int cipherSuite)
    • getNamedGroupRoles

      public static Vector getNamedGroupRoles(int[] cipherSuites)
    • getNamedGroupRoles

      public static Vector getNamedGroupRoles(Vector keyExchangeAlgorithms)
    • isAEADCipherSuite

      public static boolean isAEADCipherSuite(int cipherSuite) throws IOException
      Throws:
      IOException
    • isBlockCipherSuite

      public static boolean isBlockCipherSuite(int cipherSuite) throws IOException
      Throws:
      IOException
    • isStreamCipherSuite

      public static boolean isStreamCipherSuite(int cipherSuite) throws IOException
      Throws:
      IOException
    • isValidCipherSuiteForSignatureAlgorithms

      public static boolean isValidCipherSuiteForSignatureAlgorithms(int cipherSuite, Vector sigAlgs)
      Returns:
      Whether a server can select the specified cipher suite given the available signature algorithms for ServerKeyExchange.
    • isValidCipherSuiteForVersion

      public static boolean isValidCipherSuiteForVersion(int cipherSuite, ProtocolVersion version)
    • isValidSignatureSchemeForServerKeyExchange

      public static boolean isValidSignatureSchemeForServerKeyExchange(int signatureScheme, int keyExchangeAlgorithm)
    • isValidVersionForCipherSuite

      public static boolean isValidVersionForCipherSuite(int cipherSuite, ProtocolVersion version)
    • chooseSignatureAndHashAlgorithm

      public static SignatureAndHashAlgorithm chooseSignatureAndHashAlgorithm(TlsContext context, Vector sigHashAlgs, short signatureAlgorithm) throws IOException
      Throws:
      IOException
    • chooseSignatureAndHashAlgorithm

      public static SignatureAndHashAlgorithm chooseSignatureAndHashAlgorithm(ProtocolVersion negotiatedVersion, Vector sigHashAlgs, short signatureAlgorithm) throws IOException
      Throws:
      IOException
    • getUsableSignatureAlgorithms

      public static Vector getUsableSignatureAlgorithms(Vector sigHashAlgs)
    • getCommonCipherSuite13

      public static int getCommonCipherSuite13(ProtocolVersion negotiatedVersion, int[] peerCipherSuites, int[] localCipherSuites, boolean useLocalOrder)
    • getCommonCipherSuites

      public static int[] getCommonCipherSuites(int[] peerCipherSuites, int[] localCipherSuites, boolean useLocalOrder)
    • getSupportedCipherSuites

      public static int[] getSupportedCipherSuites(TlsCrypto crypto, int[] suites)
    • getSupportedCipherSuites

      public static int[] getSupportedCipherSuites(TlsCrypto crypto, int[] suites, int suitesCount)
    • getSupportedCipherSuites

      public static int[] getSupportedCipherSuites(TlsCrypto crypto, int[] suites, int suitesOff, int suitesCount)
    • isSupportedCipherSuite

      public static boolean isSupportedCipherSuite(TlsCrypto crypto, int cipherSuite)
    • isSupportedKeyExchange

      public static boolean isSupportedKeyExchange(TlsCrypto crypto, int keyExchangeAlgorithm)
    • checkPeerSigAlgs

      public static void checkPeerSigAlgs(TlsContext context, TlsCertificate[] peerCertPath) throws IOException
      Check the signature algorithm for certificates in the peer's CertPath as specified in RFC 5246 7.4.2, 7.4.4, 7.4.6 and similar rules for earlier TLS versions. The supplied CertPath should include the trust anchor (its signature algorithm isn't checked, but in the general case checking a certificate requires the issuer certificate).
      Throws:
      IOException - if any certificate in the CertPath (excepting the trust anchor) has a signature algorithm that is not one of the locally supported signature algorithms.
    • containsNonAscii

      public static boolean containsNonAscii(byte[] bs)
    • containsNonAscii

      public static boolean containsNonAscii(String s)
    • generateEncryptedPreMasterSecret

      public static TlsSecret generateEncryptedPreMasterSecret(TlsContext context, TlsEncryptor encryptor, OutputStream output) throws IOException
      Generate a pre_master_secret and send it encrypted to the server.
      Throws:
      IOException