0

I have this code (can be run in a playground):

import UIKit
import CryptoKit

let url: URL = URL(string: "https://apple.com")!

final class SSLExtractor: NSObject, URLSessionDelegate {
    private var session: URLSession!

    init(url: URL) {
        super.init()
        let session = URLSession.init(configuration: .default, delegate: self, delegateQueue: nil)
        session.dataTask(with: url).resume()
    }

    func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust,
              let serverTrust = challenge.protectionSpace.serverTrust,
              let publicKey = SecTrustCopyKey(serverTrust),
              let publicKeyData = SecKeyCopyExternalRepresentation(publicKey, nil)
        else { return }
        let publicKeyHash = SHA256.hash(data: publicKeyData as Data)
        print("publicKey: \(publicKey)")
        print("publicKey: \(publicKeyData)")
        print("publicKey: \((publicKeyData as Data).base64EncodedString() )")
        print(publicKeyHash)
    }
}

let extractor = SSLExtractor(url: url)

Supposedly the last print should give me the server's public key:

SHA256 digest: b0faa00170de7c1ac7994644efadb59f149656546394bd22c95527e78f1984b6

However, when I use OpenSSL:

$ openssl s_client -connect apple.com:443 | openssl x509 -pubkey -noout  | openssl rsa -pubin -outform der| openssl dgst -sha256

I get a different hash:

1786e93d8e16512ea34ea1475e39597e77d7e39239ba1a97dcd71f97e64d6619

How to get the correct hash ?

Edit: Also tried

              let certificate = SecTrustGetCertificateAtIndex(serverTrust, 0),
              let certifKey = SecCertificateCopyKey(certificate),
              let certifPubKey = SecKeyCopyPublicKey(certifKey),
              let certifPubKeyData = SecKeyCopyExternalRepresentation(certifPubKey, nil)

But I got the same result

Antzi
  • 12,831
  • 7
  • 48
  • 74
  • 1
    I don't get the same hash you do from OpenSSL; can you show the _cert_ you get with OpenSSL? (In PEM so no info is lost.) Yes, `openssl x509 -pubkey` outputs SPKI in PEM; to convert to binary which OpenSSL calls DER, pipe through `openssl rsa -pubin -outform der` (and then from that to the hash).Alternatively, discard the BEGIN and END lines and base64-decode the rest. – dave_thompson_085 Sep 29 '20 at 06:21
  • @dave_thompson_085 here is the pastbin of `openssl s_client -connect apple.com:443`: https://pastebin.com/ic3u7gPi – Antzi Sep 29 '20 at 06:47
  • @dave_thompson_085 I updated my answer to correctly hash the public key and not the pem. The hashes are still incorrect however. – Antzi Sep 29 '20 at 07:01
  • It is the same cert, but I realized I did my test on Windows: that cert's SPKI in PEM _with CRLF_ hashes to 93eacd62ace2ea5e7286b43ea12e3376583532f2794a77ec335ac3e0c46e3fc7 as I had, with LF to 58053494eb340ddc7d23608a5b4608d792f6777caa8782582ce246574980e230 as you had before, and DER 1786e93d8e16512ea34ea1475e39597e77d7e39239ba1a97dcd71f97e64d6619 as you have now. I don't know Swift and what it does (or should do) in the cases here, but I'd look at the 'external' publicKeyData or certifPubKeyData to see what's actually there. – dave_thompson_085 Sep 29 '20 at 08:19
  • @dave_thompson_085 Thanks a lot for your help. Turns out, both `der` and `pem` formats includes some headers. Swift returns the correct value, sans header. I'm not sure if there is an OpenSSL way to remove the `der` header, but if I did, I would get the same hash. – Antzi Sep 29 '20 at 08:40

1 Answers1

1

Only sort of an answer but too much for comments. You found that the 'external' publickey provided (and hashed) by Swift is a suffix of that used by OpenSSL.

OpenSSL uses, as I referenced, the 'SPKI' (SubjectPublicKeyInfo) structure defined by X.509/PKIX=RFC5280 section 4 which is a DER-encoded ASN.1 SEQUENCE containing an AlgorithmIdentifier and a BIT STRING cotaining embedded algorithm-specific data. RFC 7468 section 13 (which partly officializes OpenSSL, although it doesn't say so) confirms this is the content of a 'standard' public key representation, and RFC 3279 section 2.3.1 defines that for RSA (which the Apple cert's key is) the content of the BIT STRING part is the ASN.1 structure RSAPublicKey which actually was defined (though 3279 doesn't say so for the key, while it does for the signature in 2.2.1) in PKCS1 currently RFC8017 section A.1.1 (but unchanged from earlier versions).

Because of the way DER encoding works, the content of the BIT STRING -- in this case the ASN.1 structure RSAPublicKey -- is exactly a suffix of the encoding of the whole SPKI.

If we look at, and parse, the SPKI OpenSSL gets from the cert (with x509 -pubkey)

$ openssl rsa -pubin -in applespki -outform der |od -Ax -tx1
writing RSA key
000000 30 82 01 22 30 0d 06 09 2a 86 48 86 f7 0d 01 01
000010 01 05 00 03 82 01 0f 00 30 82 01 0a 02 82 01 01
000020 00 e0 e5 ca aa 34 ea 32 d1 f5 e3 56 e5 05 e8 07
000030 c3 d3 3b 86 93 60 94 37 20 9f 8d d0 17 8f ba da
000040 16 f1 b2 6a bc 41 7a 6f dc 22 87 4b 05 e8 57 48
000050 56 3c da a2 02 e5 57 73 94 95 18 dd 7c c9 b0 68
000060 fe 17 2c 8b 6f fa 42 a9 b1 a8 77 88 22 d4 41 08
000070 08 d9 80 59 92 bc e9 3a 0a 17 e0 2b 13 fe bf ec
000080 69 7d 61 15 14 21 71 73 a0 70 fd 6d a0 0f 46 13
000090 60 8d c1 bd 8c 66 60 04 05 e0 44 f0 a1 53 b7 00
0000a0 7f a3 f3 55 da d2 6c c6 dd 7f 83 79 1f 6e cb 1d
0000b0 78 3e d9 9f fa 58 34 38 41 c5 70 c1 c7 dd ea b0
0000c0 81 c0 d4 a3 18 a4 da 02 15 b8 cb 48 10 fa 42 86
0000d0 75 1c 55 51 6b 48 6e 37 43 98 09 af 4f 52 c0 c8
0000e0 75 40 a5 e7 65 ba 62 2a be 6c 2a 6d 72 5b 82 21
0000f0 d1 75 97 ea 7e 21 a1 04 f4 76 7c 85 db 50 c7 9f
000100 b5 d8 f7 80 15 ba a5 83 9e 2c da f0 73 c6 14 9a
000110 fd 35 07 41 4b 53 21 8d 0d 01 f1 05 b3 04 05 83
000120 cb 02 03 01 00 01
000126
$ openssl asn1parse -i -dump -in applespki1
    0:d=0  hl=4 l= 290 cons: SEQUENCE
    4:d=1  hl=2 l=  13 cons:  SEQUENCE
    6:d=2  hl=2 l=   9 prim:   OBJECT            :rsaEncryption
   17:d=2  hl=2 l=   0 prim:   NULL
   19:d=1  hl=4 l= 271 prim:  BIT STRING
      0000 - 00 30 82 01 0a 02 82 01-01 00 e0 e5 ca aa 34 ea   .0............4.
      0010 - 32 d1 f5 e3 56 e5 05 e8-07 c3 d3 3b 86 93 60 94   2...V......;..`.
      0020 - 37 20 9f 8d d0 17 8f ba-da 16 f1 b2 6a bc 41 7a   7 ..........j.Az
      0030 - 6f dc 22 87 4b 05 e8 57-48 56 3c da a2 02 e5 57   o.".K..WHV<....W
      0040 - 73 94 95 18 dd 7c c9 b0-68 fe 17 2c 8b 6f fa 42   s....|..h..,.o.B
      0050 - a9 b1 a8 77 88 22 d4 41-08 08 d9 80 59 92 bc e9   ...w.".A....Y...
      0060 - 3a 0a 17 e0 2b 13 fe bf-ec 69 7d 61 15 14 21 71   :...+....i}a..!q
      0070 - 73 a0 70 fd 6d a0 0f 46-13 60 8d c1 bd 8c 66 60   s.p.m..F.`....f`
      0080 - 04 05 e0 44 f0 a1 53 b7-00 7f a3 f3 55 da d2 6c   ...D..S.....U..l
      0090 - c6 dd 7f 83 79 1f 6e cb-1d 78 3e d9 9f fa 58 34   ....y.n..x>...X4
      00a0 - 38 41 c5 70 c1 c7 dd ea-b0 81 c0 d4 a3 18 a4 da   8A.p............
      00b0 - 02 15 b8 cb 48 10 fa 42-86 75 1c 55 51 6b 48 6e   ....H..B.u.UQkHn
      00c0 - 37 43 98 09 af 4f 52 c0-c8 75 40 a5 e7 65 ba 62   7C...OR..u@..e.b
      00d0 - 2a be 6c 2a 6d 72 5b 82-21 d1 75 97 ea 7e 21 a1   *.l*mr[.!.u..~!.
      00e0 - 04 f4 76 7c 85 db 50 c7-9f b5 d8 f7 80 15 ba a5   ..v|..P.........
      00f0 - 83 9e 2c da f0 73 c6 14-9a fd 35 07 41 4b 53 21   ..,..s....5.AKS!
      0100 - 8d 0d 01 f1 05 b3 04 05-83 cb 02 03 01 00 01      ...............

we can see the content of the BIT STRING, skipping the first byte 00 because of the way DER works for BIT STRING, and thus starting at offset 24=0x18 and continuing to the end, is itself a DER encoding of RSAPublicKey (30 82 01 0a is the SEQUENCE, 02 82 01 01 ... is the INTEGER (signed) modulus, 02 03 01 00 01 is the INTEGER (signed) publicExponent). OpenSSL 1.0.0 up (which is now ubiquitous, though I remember when it wasn't) can extract this, and get your expected hash:

$ openssl rsa -pubin -in applespki -RSAPublicKey_out -outform der |od -Ax -tx1
writing RSA key
000000 30 82 01 0a 02 82 01 01 00 e0 e5 ca aa 34 ea 32
000010 d1 f5 e3 56 e5 05 e8 07 c3 d3 3b 86 93 60 94 37
000020 20 9f 8d d0 17 8f ba da 16 f1 b2 6a bc 41 7a 6f
000030 dc 22 87 4b 05 e8 57 48 56 3c da a2 02 e5 57 73
000040 94 95 18 dd 7c c9 b0 68 fe 17 2c 8b 6f fa 42 a9
000050 b1 a8 77 88 22 d4 41 08 08 d9 80 59 92 bc e9 3a
000060 0a 17 e0 2b 13 fe bf ec 69 7d 61 15 14 21 71 73
000070 a0 70 fd 6d a0 0f 46 13 60 8d c1 bd 8c 66 60 04
000080 05 e0 44 f0 a1 53 b7 00 7f a3 f3 55 da d2 6c c6
000090 dd 7f 83 79 1f 6e cb 1d 78 3e d9 9f fa 58 34 38
0000a0 41 c5 70 c1 c7 dd ea b0 81 c0 d4 a3 18 a4 da 02
0000b0 15 b8 cb 48 10 fa 42 86 75 1c 55 51 6b 48 6e 37
0000c0 43 98 09 af 4f 52 c0 c8 75 40 a5 e7 65 ba 62 2a
0000d0 be 6c 2a 6d 72 5b 82 21 d1 75 97 ea 7e 21 a1 04
0000e0 f4 76 7c 85 db 50 c7 9f b5 d8 f7 80 15 ba a5 83
0000f0 9e 2c da f0 73 c6 14 9a fd 35 07 41 4b 53 21 8d
000100 0d 01 f1 05 b3 04 05 83 cb 02 03 01 00 01
00010e
$ openssl rsa -pubin -in applespki -RSAPublicKey_out -outform der |openssl sha256
writing RSA key
(stdin)= b0faa00170de7c1ac7994644efadb59f149656546394bd22c95527e78f1984b6

This only works for RSA. It might be interesting to see how your Swift code handles a certificated non-RSA key, although at present those are hard to find on the public web; the only stable ones I currently know of are at 'badssl' like https://ecc256.badssl.com .

Community
  • 1
  • 1
dave_thompson_085
  • 34,712
  • 6
  • 50
  • 70