1

By reading the TLS 1.2 specs I have been able to manually decrypt/encrypt pretty much all the steps of a TLS connection (DHE_RSA_AES_CBC_128_SHA cipher suite).

One of things I am missing is how to verify the signature of the Diffie-Hellman Ephemeral parameters sent by the server. I was able to verify the Certificate chain, but not the initial DH parameters.

First of all, I compute signature^e mod n (e and n coming from the server Certificate). The result looks OK as its looks properly encoded (01FFFF...FF00...<hash>).

However, I am not sure what format to use to hash the DH parameters. I tried hashing the bytes as sent by the server and using DER encoding. I tried encoding just pubKey, and various mixes of p, g and pubKey. But I still cannot make the two hash numbers to match.

  • May I ask you how the 01FFFFF...FF00...<hash> is made. It seems that some padding is inserted in front of the hash. I want to know the rule. – Zeta Aug 22 '18 at 19:51
1

For TLS 1.2 the signature should be over the two hello nonces plus the ServerDHParams portion of the ServerKeyExchange message. See rfc5246 section 7.4.3 which shows the input to the digitally-signed construct from section 4.7.

But note earlier protocol versions are different for RSA signature. Two hashes, MD5 and SHA1, are concatenated, but are NOT encoded as an ASN.1 SEQUENCE with the AlgorithmIdentifier for the hash per section 10 of PKCS#1v1.5; instead they are directly formatted as block type 01 per section 8.1 (and modexp'ed in the usual way per 8.2-8.4). This is equivalent to omitting step 2 of EMSA-PKCS1-v1_5 in PKCS#1v2, although rfc4346 4.7 et pred are not totally clear about it.

ADDED in response to comment: what you described should indeed work; here is an example/demo using data from openssl s_client -msg:

>>> TLS 1.2 Handshake [length 00f2], ClientHello
    01 00 00 ee 03 03[57 20 2e 6d bc af 8f 40 58 f4
    0f bd 82 82 dc 78 00 36 2a 74 88 0b 14 71 ef f6
    65 45 db 1f 62 e8]00 00 84 c0 30 c0 2c c0 28 c0
    24 c0 14 c0 0a 00 a3 00 9f 00 6b 00 6a 00 39 00
    38 00 88 00 87 c0 32 c0 2e c0 2a c0 26 c0 0f c0
    05 00 9d 00 3d 00 35 00 84 c0 2f c0 2b c0 27 c0
    23 c0 13 c0 09 00 a2 00 9e 00 67 00 40 00 33 00
    32 c0 12 c0 08 00 9a 00 99 00 45 00 44 00 16 00
    13 c0 31 c0 2d c0 29 c0 25 c0 0e c0 04 c0 0d c0
    03 00 9c 00 3c 00 2f 00 96 00 41 00 0a 00 07 c0
    11 c0 07 c0 0c c0 02 00 05 00 04 00 ff 01 00 00
    41 00 0b 00 04 03 00 01 02 00 0a 00 08 00 06 00
    19 00 18 00 17 00 23 00 00 00 0d 00 20 00 1e 06
    01 06 02 06 03 05 01 05 02 05 03 04 01 04 02 04
    03 03 01 03 02 03 03 02 01 02 02 02 03 00 0f 00
    01 01
<<< TLS 1.2 Handshake [length 0051], ServerHello
    02 00 00 4d 03 03[57 20 2e 9a fe 0a b0 e9 cb 39
    75 23 03 05 52 4a 5a ab a1 2d 59 5e 57 86 ea 98
    98 52 2c e8 40 30]20 57 20 2e 9a 5e 4d 19 0d 1d
    17 5b 62 ef 83 7a 5c 83 a0 03 51 fa a7 2d 60 69
    bc 9c 2b b6 8c 37 c3 00 9f 00 00 05 ff 01 00 01
    00
<<< TLS 1.2 Handshake [length 01ab], Certificate
    0b 00 01 a7 00 01 a4 00 01 a1 30 82 01 9d 30 82
    01 06 a0 03 02 01 02 02 04 57 20 2e 12 30 0d 06
    09 2a 86 48 86 f7 0d 01 01 05 05 00 30 13 31 11
    30 0f 06 03 55 04 03 13 08 74 65 73 74 63 65 72
    74 30 1e 17 0d 31 36 30 34 32 37 30 33 31 32 31
    38 5a 17 0d 32 36 30 34 33 30 30 33 31 32 31 38
    5a 30 13 31 11 30 0f 06 03 55 04 03 13 08 74 65
    73 74 63 65 72 74[30 81 9f 30 0d 06 09 2a 86 48 \
    86 f7 0d 01 01 01 05 00 03 81 8d 00 30 81 89 02
    81 81 00 f1 f7 68 e8 f9 da 82 eb cd b6 bf 41 e8
    48 35 dc a4 18 3f e7 cf 4c 73 6f 9d c8 d0 1e 01
    15 4c 60 88 87 22 dd f2 a0 8c b0 27 55 e0 10 bc signing key
    e7 d6 37 c0 58 27 a9 7b 06 7e 0b 99 f8 ac c2 32 (in cert)
    8c d2 2a 46 74 a4 ef 27 1e f6 65 86 6e ca fe 34
    23 d5 21 fc d7 ab 05 2b 3e ed ba c8 44 24 36 0e
    8c f9 d6 d7 55 1c 56 09 ff 6f 0d 7e 08 4c 7a 59
    cf c6 f3 f9 47 6a 58 69 b3 08 7f 35 e6 6e f9 2d
    c4 02 0d 02 03 01 00 01]30 0d 06 09 2a 86 48 86 /
    f7 0d 01 01 05 05 00 03 81 81 00 be ba ea d9 11
    24 f9 b9 02 c8 35 e3 11 b6 00 0c 46 b1 ed 5a b5
    2b 0c b1 b5 ed 71 da 2c 25 e1 9c 7a 73 40 3d 91
    2b 45 9f 62 ef 43 15 fe 4f 46 a1 55 74 2f 4c 5d
    a5 69 0a 0e 39 e2 5e 03 49 b5 6e 34 21 cd 2f d1
    fa ec 6b 2f 10 32 25 6d e3 8b b4 d8 b7 86 25 59
    f7 86 be 1e dd cf cd 16 10 3f b7 78 4e 42 45 aa
    fa 58 0a c4 1d 52 fe a5 61 64 4b e5 55 8d de 36
    02 f1 d2 19 cd be ca 4a 93 2e 76
depth=0 CN = testcert
verify error:num=18:self signed certificate
verify return:1
<<< TLS 1.2 Handshake [length 020e], ServerKeyExchange
    0c 00 02 0a[00 80 fd 7f 53 81 1d 75 12 29 52 df \
    4a 9c 2e ec e4 e7 f6 11 b7 52 3c ef 44 00 c3 1e
    3f 80 b6 51 26 69 45 5d 40 22 51 fb 59 3d 8d 58
    fa bf c5 f5 ba 30 f6 cb 9b 55 6c d7 81 3b 80 1d 
    34 6f f2 66 60 b7 6b 99 50 a5 a4 9f 9f e8 04 7b P
    10 22 c2 4f bb a9 d7 fe b7 c6 1b f8 3b 57 e7 c6
    a8 a6 15 0f 04 fb 83 f6 d3 c5 1e c3 02 35 54 13
    5a 16 91 32 f6 75 f3 ae 2b 61 d7 2a ef f2 22 03
    19 9d d1 48 01 c7 00 80 f7 e1 a0 85 d6 9b 3d de -
    cb bc ab 5c 36 b8 57 b9 79 94 af bb fa 3a ea 82
    f9 57 4c 0b 3d 07 82 67 51 59 57 8e ba d4 59 4f
    e6 71 07 10 81 80 b4 49 16 71 23 e8 4c 28 16 13
    b7 cf 09 32 8c c8 a6 e1 3c 16 7a 8b 54 7c 8d 28 G
    e0 a3 ae 1e 2b b3 a6 75 91 6e a3 7f 0b fa 21 35
    62 f1 fb 62 7a 01 24 3b cc a4 f1 be a8 51 90 89
    a8 83 df e1 5a e5 9f 06 92 8b 66 5e 80 7b 55 25
    64 01 4c 3b fe cf 49 2a 00 80 8c 75 b3 17 44 2c -
    a0 0d 45 3d e9 d3 18 f1 d9 a2 88 85 66 01 4e 86
    e3 46 57 3d d7 64 3a cd c4 d3 14 d5 a3 27 1c 62
    7e c3 23 8a 16 b9 0c 1f 48 f6 c6 8b 2a 6d 36 35
    d7 d9 38 0a 89 cb 88 db 52 ce 3e 5d b4 b6 2a 33 Y
    aa b6 1f 0d 09 14 a5 4e 20 7e 4c 61 6d 22 71 7d
    da fc e7 3b 49 80 31 3d d6 e3 d1 75 a9 69 48 6b
    54 74 27 23 4f 95 63 a2 55 5f 8d da 66 9b 8f 49
    2a 21 0b 1d 31 21 b3 60 aa 0c]06 01 00 80[94 fa -
    c9 33 1e 8c 4c 9c 91 47 a4 52 2e 5a 91 a6 2b 99
    6b 4b 1d 33 1f 9a 9c 44 cc f4 47 9e 3a 7a 70 40
    ff b6 bd 23 0b 51 8b 5c 9a 9e 6d 69 a7 f6 a0 65 signature
    ca 4a 3b 49 46 34 bb 1a 7c 4e c3 5f e0 fc b3 d1
    1f 03 c0 f0 2e f4 87 23 75 3f d5 bc 56 82 4f 83
    f9 b2 da 41 b9 68 b9 33 af 0a ed 4d c2 d8 f0 4d
    2c 21 ad d3 5d 76 47 2f cc 8e 29 72 e5 d6 96 ec
    6d 96 2a c5 80 eb 7c cf 1b ea f6 66 fb fc]      /
<<< TLS 1.2 Handshake [length 0004], ServerHelloDone
    0e 00 00 00

Step 1: concatentate the marked pieces and hash

[dthomps@virt2 ~]$ cat temp1
                      57 20 2e 6d bc af 8f 40 58 f4
    0f bd 82 82 dc 78 00 36 2a 74 88 0b 14 71 ef f6
    65 45 db 1f 62 e8
                      57 20 2e 9a fe 0a b0 e9 cb 39
    75 23 03 05 52 4a 5a ab a1 2d 59 5e 57 86 ea 98
    98 52 2c e8 40 30
                00 80 fd 7f 53 81 1d 75 12 29 52 df
    4a 9c 2e ec e4 e7 f6 11 b7 52 3c ef 44 00 c3 1e
    3f 80 b6 51 26 69 45 5d 40 22 51 fb 59 3d 8d 58
    fa bf c5 f5 ba 30 f6 cb 9b 55 6c d7 81 3b 80 1d
    34 6f f2 66 60 b7 6b 99 50 a5 a4 9f 9f e8 04 7b
    10 22 c2 4f bb a9 d7 fe b7 c6 1b f8 3b 57 e7 c6
    a8 a6 15 0f 04 fb 83 f6 d3 c5 1e c3 02 35 54 13
    5a 16 91 32 f6 75 f3 ae 2b 61 d7 2a ef f2 22 03
    19 9d d1 48 01 c7 00 80 f7 e1 a0 85 d6 9b 3d de
    cb bc ab 5c 36 b8 57 b9 79 94 af bb fa 3a ea 82
    f9 57 4c 0b 3d 07 82 67 51 59 57 8e ba d4 59 4f
    e6 71 07 10 81 80 b4 49 16 71 23 e8 4c 28 16 13
    b7 cf 09 32 8c c8 a6 e1 3c 16 7a 8b 54 7c 8d 28
    e0 a3 ae 1e 2b b3 a6 75 91 6e a3 7f 0b fa 21 35
    62 f1 fb 62 7a 01 24 3b cc a4 f1 be a8 51 90 89
    a8 83 df e1 5a e5 9f 06 92 8b 66 5e 80 7b 55 25
    64 01 4c 3b fe cf 49 2a 00 80 8c 75 b3 17 44 2c
    a0 0d 45 3d e9 d3 18 f1 d9 a2 88 85 66 01 4e 86
    e3 46 57 3d d7 64 3a cd c4 d3 14 d5 a3 27 1c 62
    7e c3 23 8a 16 b9 0c 1f 48 f6 c6 8b 2a 6d 36 35
    d7 d9 38 0a 89 cb 88 db 52 ce 3e 5d b4 b6 2a 33
    aa b6 1f 0d 09 14 a5 4e 20 7e 4c 61 6d 22 71 7d
    da fc e7 3b 49 80 31 3d d6 e3 d1 75 a9 69 48 6b
    54 74 27 23 4f 95 63 a2 55 5f 8d da 66 9b 8f 49
    2a 21 0b 1d 31 21 b3 60 aa 0c
[dthomps@virt2 ~]$ xxd -r -p temp1 | openssl sha512
(stdin)= 3338170798d1c811b121fcdf6fa7f4292807f68cdb3e4b7817d91c0f5ac427e7b4b3871c99685e0f783362d0c9c564b5dcf9725b3949532b8870ca6fb93b3c8e

Step 2: get publickey into usable form

[dthomps@virt2 ~]$ cat tempk
                      30 81 9f 30 0d 06 09 2a 86 48
    86 f7 0d 01 01 01 05 00 03 81 8d 00 30 81 89 02
    81 81 00 f1 f7 68 e8 f9 da 82 eb cd b6 bf 41 e8
    48 35 dc a4 18 3f e7 cf 4c 73 6f 9d c8 d0 1e 01
    15 4c 60 88 87 22 dd f2 a0 8c b0 27 55 e0 10 bc
    e7 d6 37 c0 58 27 a9 7b 06 7e 0b 99 f8 ac c2 32
    8c d2 2a 46 74 a4 ef 27 1e f6 65 86 6e ca fe 34
    23 d5 21 fc d7 ab 05 2b 3e ed ba c8 44 24 36 0e
    8c f9 d6 d7 55 1c 56 09 ff 6f 0d 7e 08 4c 7a 59
    cf c6 f3 f9 47 6a 58 69 b3 08 7f 35 e6 6e f9 2d
    c4 02 0d 02 03 01 00 01
[dthomps@virt2 ~]$ xxd -r -p tempk |openssl rsa -inform d -pubin >tempk.pem
writing RSA key

Step 3: "verify-with-recovery" the signature value

[dthomps@virt2 ~]$ cat temp2
                                              94 fa
    c9 33 1e 8c 4c 9c 91 47 a4 52 2e 5a 91 a6 2b 99
    6b 4b 1d 33 1f 9a 9c 44 cc f4 47 9e 3a 7a 70 40
    ff b6 bd 23 0b 51 8b 5c 9a 9e 6d 69 a7 f6 a0 65
    ca 4a 3b 49 46 34 bb 1a 7c 4e c3 5f e0 fc b3 d1
    1f 03 c0 f0 2e f4 87 23 75 3f d5 bc 56 82 4f 83
    f9 b2 da 41 b9 68 b9 33 af 0a ed 4d c2 d8 f0 4d
    2c 21 ad d3 5d 76 47 2f cc 8e 29 72 e5 d6 96 ec
    6d 96 2a c5 80 eb 7c cf 1b ea f6 66 fb fc
[dthomps@virt2 ~]$ xxd -r -p temp2 | openssl rsautl -verify -inkey tempk.pem -pubin -pkcs |xxd -p
3051300d0609608648016503040203050004403338170798d1c811b121fc
df6fa7f4292807f68cdb3e4b7817d91c0f5ac427e7b4b3871c99685e0f78
3362d0c9c564b5dcf9725b3949532b8870ca6fb93b3c8e

which parses as

3051 = SEQUENCE OF
300d06096086480165030402030500 = AlgId for sha512
0440 = OCTET STRING containing 
3338170798d1c811b121fc
df6fa7f4292807f68cdb3e4b7817d91c0f5ac427e7b4b3871c99685e0f78
3362d0c9c564b5dcf9725b3949532b8870ca6fb93b3c8e
= the hash value above
  • Thank you for your answer. I'm afraid however that I still can't manage to come up with two matching hashes. When you say "the two hello nonces plus the ServerDHParams portion of the ServerKeyExchange", I tried taking a hash of ClientHello.random | ServerHello.random | p size | p | g size | g | pubKey size | pubKey. Or did I understand you wrong? I did read sections 7.4.3 and 4.7, but am still missing something. – user7094 Apr 26 '16 at 22:04
  • @user7094 yes that should work unless we're somehow understanding the notation differently; see example I added – dave_thompson_085 Apr 27 '16 at 4:07
  • Got it! After numerous tries I was able to find what I did wrong: I was using the wrong hash algorithm, using the one described in the certificate instead of the one in the DH parameters. But your openssl commands were very useful - I have been computing the numbers using Python to really understand how the algorithms work. – user7094 Apr 28 '16 at 1:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.