BouncyCastle tsutils 检查版本

BouncyCastle tlsutils checkVersion

BouncyCastle org.bouncycastle.crypto.tls.TlsUtils 有以下方法

protected static void checkVersion(InputStream inputstream, TlsProtocolHandler tlsprotocolhandler)
    throws IOException
{
    int i = inputstream.read();
    int j = inputstream.read();
    if (i != 3 || j != 1)
    {
        tlsprotocolhandler.failWithError((short)2, (short)70);
    }
}

protected static void checkVersion(byte abyte0[], TlsProtocolHandler tlsprotocolhandler)
    throws IOException
{
    if (abyte0[0] != 3 || abyte0[1] != 1)
    {
        tlsprotocolhandler.failWithError((short)2, (short)70);
    }
}

这里检查的 3 和 1 是什么?

这是一个叫做 "magic numbers" 的坏事的一个很好的例子 :-) 摘自 InputStream.read() 的 javadoc:

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.

这意味着ij是从流中读取的版本号。而且它们必须是版本 3 和版本 1failWithError 方法也获得传递的幻数。 TlsProtocolHandler 有常量,我不知道为什么作者不使用它们

2: AL_fatal
70: AP_protocol_version

source

查看代码 checkVersion 是在握手阶段 (ServerHello) 调用的。这里检查协议版本。请参阅此 wikipedia article 的版本章节以查找版本号。主要版本 3,次要版本 1 是 TLS 1.0.