簡體   English   中英

tls 1.2奇怪的密碼套件

[英]tls 1.2 strange cipher suites

我正在開發一個將實現TLS協議的Java應用程序。 由於我最近開始實施它。 在開發類型握手的記錄協議實現時,我從Google chrome和mozilla firefox獲得了奇怪的密碼套件。 文檔所述,握手協議中包含類型,握手長度,版本,隨機,會話ID長度,會話ID,密碼套件長度,密碼套件.......

我正在正確設置所有字段(在密碼套件之前),但是在密碼套件上,我從google chrome和firefox獲取了奇怪的值。

這是我的代碼,它接受端口4040上的連接。

   package server;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.security.KeyStore;
import java.security.SecureRandom;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

public class Server extends ServerSocket {
    public Server() throws IOException {
        super();
        // TODO Auto-generated constructor stub
    }
    public static void main(String args[]) 
    {
        System.setProperty("java.net.ssl.trustStore", "mam.store");
        System.setProperty("java.net.ssl.keyStorePassword", "mamoon");
        try {
            /*KeyStore ks  = KeyStore.getInstance("JKS");
            InputStream in = new FileInputStream("/Users/Ahmed/Desktop/mam.store");
            ks.load(in, "mamoon".toCharArray());
            in.close();
            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(ks,"mamoon".toCharArray());
            TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
            tmf.init(ks);
            SSLContext con = SSLContext.getInstance("TLSv1.2");
            con.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);

            SSLServerSocket socket = (SSLServerSocket)con.getServerSocketFactory().createServerSocket(4040);
            */
            ServerSocket socket = new ServerSocket(4040);
            System.out.println("Server is up.");

            Socket s = socket.accept();
            PrintWriter w = new PrintWriter(s.getOutputStream());
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream(),"ISO-8859-1"));
            String line;
            String packet = "";
            int l=0;
            while((line = br.readLine()) != null)
            {
                packet += line;
                System.out.println(line.length());
                if(packet.length() >= 100)
                {
                for(char c:packet.toCharArray())
                    System.out.print((int)c + "   ");
                System.out.println();
                new TLS(packet);
                break;
                }
            }
            System.out.println(l);
            //w.write("<html><body>hello world</body> </html>");
            //s.close();
            socket.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

這是解碼TLS數據包的類。

package server;

import java.math.BigInteger;
import java.nio.ByteBuffer;

public class TLS {
    String start = (char)22 + "" + (char)3;
    float version;
    int packetLength,handshakeLength,sessionIdLength;
    String random = "",sessionId = "";
    String message;
    String packetType,encryption;
    int cipherSuitsLength;
    int cipherSuitesId[];

    TLS(String packet) throws Exception
    {
        if(packet.startsWith(start))
        {
            version = Float.parseFloat((int)packet.charAt(1) + "."+(int)packet.charAt(2));
            packetLength = new BigInteger(new byte[]{(byte)packet.charAt(3),(byte)packet.charAt(4)}).intValue();
            System.out.println(packetLength);
                    //(byte)packet.charAt(3) << 8 | ((byte)packet.charAt(4) & 0xFF);
            if((int)packet.charAt(5) == 1)
                packetType = "Client Hello";
            else if((int)packet.charAt(5) == 2)
                packetType = "Server Hello";
            else if((int)packet.charAt(5) == 11)
                packetType = "Certificate";
            else if((int)packet.charAt(5) == 12)
                packetType = "Server Key Exchange";
            else if((int)packet.charAt(5) == 13)
                packetType = "Certificate Request";
            else if((int)packet.charAt(5) == 14)
                packetType = "Server Done";
            else if((int)packet.charAt(5) == 15)
                packetType = "Certificate Verify";
            else if((int)packet.charAt(5) == 16)
                packetType = "Client Key Exchange";
            else if((int)packet.charAt(5) == 20)
                packetType = "Finished";
            handshakeLength = new BigInteger(new byte[] {(byte)packet.charAt(6) , (byte)packet.charAt(7), (byte)packet.charAt(8)}).intValue(); 
                    //(byte)packet.charAt(7) << 16 | ((byte)packet.charAt(8) << 8 & 0xFF) | ((byte));
            System.out.println(handshakeLength);
            version = Float.parseFloat((int)packet.charAt(9) + "."+(int)packet.charAt(10));
            System.out.println(version);
            for(int a=11;a<11+32;a++)
            {   random += packet.charAt(a);
            System.out.println((int)packet.charAt(a));
            }
            System.out.println(random.length());
            sessionIdLength = (int)packet.charAt(43);
            System.out.println(sessionIdLength);
            int c = 44+sessionIdLength;
            for(int a=44;a<44+sessionIdLength;a++)
            {
                sessionId += packet.charAt(a);
            }
            System.out.println(sessionId);
            cipherSuitsLength = new BigInteger(new byte[] {(byte)packet.charAt(c),(byte) packet.charAt(c+1)}).intValue();
            System.out.println(cipherSuitsLength);
            cipherSuitesId = new int[cipherSuitsLength/2];
            c+=2;
            for(int a=0;a<cipherSuitesId.length;a++)
            {
                cipherSuitesId[a] = new BigInteger(new byte[]{(byte)packet.charAt(c),(byte)packet.charAt(c+1)}).intValue();
                c+=2;
                System.out.println(cipherSuitesId[a]);
            }
        }
        else
            throw new Exception("Not a TLS packet.");
    }
    public void processTLS(String packet)
    {

    }
}

我的代碼的輸出是

Server is up.
51
19
14
61
22   3   1   0   168   1   0   0   164   3   3   57   220   109   126   106   16   106   75   232   65   206   177   89   90   5   241   76   167   135   150   97   51   67   213   33   60   209   73   137   119   119   80   0   0   30   192   43   192   47   192   192   9   192   19   192   20   192   7   192   17   0   51   0   57   0   47   0   53   0   0   5   0   4   1   0   0   93   255   1   0   1   0   0   0   8   0   6   0   23   0   24   0   25   0   11   0   2   1   0   0   35   0   0   51   116   0   0   0   16   0   23   0   21   2   104   50   8   115   112   100   121   47   51   46   49   8   104   116   116   112   47   49   46   49   0   5   0   5   1   0   0   0   0   0   
168
164
3.3
57
220
109
126
106
16
106
75
232
65
206
177
89
90
5
241
76
167
135
150
97
51
67
213
33
60
209
73
137
119
119
80
32
0

30
-16341
-16337
-16192
2496
5056
5312
1984
4352
13056
14592
12032
13568
5
4
256
0

如您所見,密碼套件的長度為0 30,這意味着收到了15個密碼套件。 但是我確定沒有像192 43這樣的密碼套件ID。 請幫我解決一下這個。

您正在解析每個數據包,就好像它包含密碼套件。 他們沒有。

您還完全是從錯誤的腳開始的。 TLS記錄是二進制的,而String不是二進制數據的容器。

而且,使用這樣的結構的代碼,您將永遠無法到達任何地方。 在繼續前進時退出,或者看看實際的實現編碼方式。 沒什么好喜歡的。

192 43是TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256的ID。

Wireshark數據包捕獲。 TLS 1.2客戶端問候數據包。

是密碼套件ID的完整列表。

是的,還有另一個問題,第三密碼套件ID是192 192,但是不存在這樣的密碼套件。 那是因為br.readLine()語句。 它將跳過字符10,並將其視為換行符,但實際上是信息。 使用字節數組讀取數據是正確的方法。

DataInputStream s = new DataInputStream(socket.getInputStream());
byte data[] = new byte[s.available()];
s.readFully(data);

此實現將提供實際數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM