Package org.red5.server.net.rtmp
Class RTMPHandshake
java.lang.Object
org.red5.server.net.rtmp.RTMPHandshake
- All Implemented Interfaces:
IHandshake
- Direct Known Subclasses:
InboundHandshake
,OutboundHandshake
Generates and validates the RTMP handshake response for Flash Players. Client versions equal to or greater than Flash 9,0,124,0 require a
nonzero value as the fifth byte of the handshake request.
- Author:
- Jacinto Shy II (jacinto.m.shy@ieee.org), Steven Zimmer (stevenlzimmer@gmail.com), Gavriloaie Eugen-Andrei, Ari-Pekka Viitanen, Paul Gregoire, Tiago Jacobs
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected int
protected org.bouncycastle.crypto.engines.BlowfishEngine
protected static final byte[][]
Blowfish keys for RTMPE (RTMP type 0x09) - 16 x 24protected Cipher
protected Cipher
protected static final BigInteger
ConstantDH_BASE
protected static final BigInteger
ConstantDH_MODULUS
protected static final byte[]
"Second Oakley Default Group" from RFC2409, section 6.2.protected static final int
ConstantDIGEST_LENGTH=32
protected boolean
static final byte[]
ConstantGENUINE_FMS_KEY
static final byte[]
ConstantGENUINE_FP_KEY
static final String[]
ConstantHANDSHAKE_TYPES
protected byte[]
protected byte
protected byte[]
protected static final int
ConstantKEY_LENGTH=128
protected KeyAgreement
protected org.slf4j.Logger
protected byte[]
protected static final Random
Constantrandom
protected int
protected byte[]
protected org.bouncycastle.crypto.engines.XTEAEngine
protected static final byte[][]
XTEA keys for RTMPE (RTMP type 0x08) - 16 x 16 (little endian) -
Constructor Summary
ConstructorsConstructorDescriptionConstructor for RTMPHandshake.RTMPHandshake
(byte handshakeType) Constructor for RTMPHandshake. -
Method Summary
Modifier and TypeMethodDescriptionvoid
calculateDigest
(int digestPos, byte[] handshakeMessage, int handshakeOffset, byte[] key, int keyLen, byte[] digest, int digestOffset) Calculates the digest given the its offset in the handshake data.void
calculateHMAC_SHA256
(byte[] message, int messageOffset, int messageLen, byte[] key, int keyLen, byte[] digest, int digestOffset) Calculates an HMAC SHA256 hash into the digest at the given offset.void
calculateSwfVerification
(byte[] handshakeMessage, byte[] swfHash, int swfSize) Calculates the swf verification token.protected abstract void
Create the initial bytes for a request / response.protected KeyPair
Creates a Diffie-Hellman key pair.void
getBlowfishSignature
(byte[] array, int offset, int keyId) RTMPE type 9 uses Blowfish on the regular signature http://en.wikipedia.org/wiki/Blowfish_(cipher)Returns the contained handshake bytes.Gets the DH offset in the handshake bytes array based on validation scheme Generates DH keypair Adds public key to handshake bytesint
getDHOffset
(int algorithm, byte[] handshake, int bufferOffset) Returns the DH offset from an array of bytes.protected int
getDHOffset1
(byte[] handshake, int bufferOffset) Returns the DH byte offset.protected int
getDHOffset2
(byte[] handshake, int bufferOffset) Returns the DH byte offset.int
getDigestOffset
(int algorithm, byte[] handshake, int bufferOffset) Returns the digest offset using current validation scheme.protected int
getDigestOffset1
(byte[] handshake, int bufferOffset) Returns a digest byte offset.protected int
getDigestOffset2
(byte[] handshake, int bufferOffset) Returns a digest byte offset.byte
Returns the handshake type.protected byte[]
getPublicKey
(KeyPair keyPair) Returns the public key for a given key pair.protected byte[]
getSharedSecret
(byte[] publicKeyBytes, KeyAgreement agreement) Determines the validation scheme for given input.byte[]
Returns the SWF verification bytes.void
getXteaSignature
(byte[] array, int offset, int keyId) RTMPE type 8 uses XTEA on the regular signature http://en.wikipedia.org/wiki/XTEAprotected void
initBlowfishEncryption
(int keyId) initBlowfishEncryption.protected void
initRC4Encryption
(byte[] sharedSecret) Prepare the ciphers.protected void
initXteaEncryption
(int keyId) initXteaEncryption.void
setHandshakeType
(byte handshakeType) Sets the handshake type.boolean
Whether or not encryptions is in use.abstract boolean
validate
(byte[] handshake) Determines the validation scheme for given input.static final boolean
validHandshakeType
(byte handshakeType) Returns whether or not a given handshake type is valid.boolean
verifyDigest
(int digestPos, byte[] handshakeMessage, byte[] key, int keyLen) Verifies the digest.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.red5.server.net.IHandshake
doHandshake
-
Field Details
-
log
protected org.slf4j.Logger log -
HANDSHAKE_TYPES
ConstantHANDSHAKE_TYPES
-
GENUINE_FMS_KEY
public static final byte[] GENUINE_FMS_KEYConstantGENUINE_FMS_KEY
-
GENUINE_FP_KEY
public static final byte[] GENUINE_FP_KEYConstantGENUINE_FP_KEY
-
DH_MODULUS_BYTES
protected static final byte[] DH_MODULUS_BYTES"Second Oakley Default Group" from RFC2409, section 6.2. -
XTEA_KEYS
protected static final byte[][] XTEA_KEYSXTEA keys for RTMPE (RTMP type 0x08) - 16 x 16 (little endian) -
BLOWFISH_KEYS
protected static final byte[][] BLOWFISH_KEYSBlowfish keys for RTMPE (RTMP type 0x09) - 16 x 24 -
DH_MODULUS
ConstantDH_MODULUS
-
DH_BASE
ConstantDH_BASE
-
DIGEST_LENGTH
protected static final int DIGEST_LENGTHConstantDIGEST_LENGTH=32
- See Also:
-
KEY_LENGTH
protected static final int KEY_LENGTHConstantKEY_LENGTH=128
- See Also:
-
random
Constantrandom
-
keyAgreement
-
cipherOut
-
cipherIn
-
xtea
protected org.bouncycastle.crypto.engines.XTEAEngine xtea -
blowfish
protected org.bouncycastle.crypto.engines.BlowfishEngine blowfish -
handshakeType
protected byte handshakeType -
handshakeBytes
protected byte[] handshakeBytes -
incomingPublicKey
protected byte[] incomingPublicKey -
outgoingPublicKey
protected byte[] outgoingPublicKey -
swfSize
protected int swfSize -
swfVerificationBytes
protected byte[] swfVerificationBytes -
algorithm
protected int algorithm -
fp9Handshake
protected boolean fp9Handshake
-
-
Constructor Details
-
RTMPHandshake
public RTMPHandshake()Constructor for RTMPHandshake.
-
RTMPHandshake
public RTMPHandshake(byte handshakeType) Constructor for RTMPHandshake.
- Parameters:
handshakeType
- a byte
-
-
Method Details
-
initRC4Encryption
protected void initRC4Encryption(byte[] sharedSecret) Prepare the ciphers.- Parameters:
sharedSecret
- shared secret byte sequence
-
initXteaEncryption
protected void initXteaEncryption(int keyId) initXteaEncryption.
- Parameters:
keyId
- a int
-
initBlowfishEncryption
protected void initBlowfishEncryption(int keyId) initBlowfishEncryption.
- Parameters:
keyId
- a int
-
generateKeyPair
Creates a Diffie-Hellman key pair.- Returns:
- dh keypair
-
getPublicKey
Returns the public key for a given key pair.- Parameters:
keyPair
- key pair- Returns:
- public key
-
createHandshakeBytes
protected abstract void createHandshakeBytes()Create the initial bytes for a request / response. -
validate
public abstract boolean validate(byte[] handshake) Determines the validation scheme for given input.- Specified by:
validate
in interfaceIHandshake
- Parameters:
handshake
- handshake byte sequence- Returns:
- true if its a supported validation scheme, false if unsupported
-
calculateDigest
public void calculateDigest(int digestPos, byte[] handshakeMessage, int handshakeOffset, byte[] key, int keyLen, byte[] digest, int digestOffset) Calculates the digest given the its offset in the handshake data.- Parameters:
digestPos
- digest positionhandshakeMessage
- handshake messagehandshakeOffset
- handshake message offsetkey
- contains the keykeyLen
- the length of the keydigest
- contains the calculated digestdigestOffset
- digest offset
-
verifyDigest
public boolean verifyDigest(int digestPos, byte[] handshakeMessage, byte[] key, int keyLen) Verifies the digest.- Parameters:
digestPos
- digest positionhandshakeMessage
- handshake messagekey
- contains the keykeyLen
- the length of the key- Returns:
- true if valid and false otherwise
-
calculateHMAC_SHA256
public void calculateHMAC_SHA256(byte[] message, int messageOffset, int messageLen, byte[] key, int keyLen, byte[] digest, int digestOffset) Calculates an HMAC SHA256 hash into the digest at the given offset.- Parameters:
message
- incoming bytesmessageOffset
- message offsetmessageLen
- message lengthkey
- incoming key byteskeyLen
- the length of the keydigest
- contains the calculated digestdigestOffset
- digest offset
-
calculateSwfVerification
public void calculateSwfVerification(byte[] handshakeMessage, byte[] swfHash, int swfSize) Calculates the swf verification token.- Parameters:
handshakeMessage
- servers handshake bytesswfHash
- hash of swfswfSize
- size of swf
-
getDHOffset
public int getDHOffset(int algorithm, byte[] handshake, int bufferOffset) Returns the DH offset from an array of bytes.- Parameters:
algorithm
- validation algorithmhandshake
- handshake sequencebufferOffset
- buffer offset- Returns:
- DH offset
-
getDHOffset1
protected int getDHOffset1(byte[] handshake, int bufferOffset) Returns the DH byte offset.- Parameters:
handshake
- handshake sequencebufferOffset
- buffer offset- Returns:
- dh offset
-
getDHOffset2
protected int getDHOffset2(byte[] handshake, int bufferOffset) Returns the DH byte offset.- Parameters:
handshake
- handshake sequencebufferOffset
- buffer offset- Returns:
- dh offset
-
getDigestOffset
public int getDigestOffset(int algorithm, byte[] handshake, int bufferOffset) Returns the digest offset using current validation scheme.- Parameters:
algorithm
- validation algorithmhandshake
- handshake sequencebufferOffset
- buffer offset- Returns:
- digest offset
-
getDigestOffset1
protected int getDigestOffset1(byte[] handshake, int bufferOffset) Returns a digest byte offset.- Parameters:
handshake
- handshake sequencebufferOffset
- buffer offset- Returns:
- digest offset
-
getDigestOffset2
protected int getDigestOffset2(byte[] handshake, int bufferOffset) Returns a digest byte offset.- Parameters:
handshake
- handshake sequencebufferOffset
- buffer offset- Returns:
- digest offset
-
getXteaSignature
public void getXteaSignature(byte[] array, int offset, int keyId) RTMPE type 8 uses XTEA on the regular signature http://en.wikipedia.org/wiki/XTEA- Parameters:
array
- array to get signatureoffset
- offset to start fromkeyId
- a int
-
getBlowfishSignature
public void getBlowfishSignature(byte[] array, int offset, int keyId) RTMPE type 9 uses Blowfish on the regular signature http://en.wikipedia.org/wiki/Blowfish_(cipher)- Parameters:
array
- array to get signatureoffset
- offset to start fromkeyId
- index of Blowfish key
-
validHandshakeType
public static final boolean validHandshakeType(byte handshakeType) Returns whether or not a given handshake type is valid.- Parameters:
handshakeType
- the type of handshake- Returns:
- true if valid and supported, false otherwise
-
useEncryption
public boolean useEncryption()Whether or not encryptions is in use.- Specified by:
useEncryption
in interfaceIHandshake
- Returns:
- true if handshake type is an encrypted type, false otherwise
-
setHandshakeType
public void setHandshakeType(byte handshakeType) Sets the handshake type. Currently only two types are supported, plain and encrypted.- Parameters:
handshakeType
- handshake type
-
getHandshakeType
public byte getHandshakeType()Returns the handshake type.- Returns:
- handshakeType
-
getCipherOut
Gets the DH offset in the handshake bytes array based on validation scheme Generates DH keypair Adds public key to handshake bytes- Returns:
- cipher
-
getCipherIn
Returns the contained handshake bytes. These are just random bytes if the player is using an non-versioned player.- Returns:
- cipher
-
getSwfVerificationBytes
public byte[] getSwfVerificationBytes()Returns the SWF verification bytes.- Returns:
- swf verification bytes
-