forked from LeakIX/ntlmssp
/
crypto.go
67 lines (57 loc) · 1.49 KB
/
crypto.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package http
import (
"crypto"
"crypto/x509"
"hash"
"github.com/PIngBZ/ntlmssp"
)
func signatureAlgorithmHash(algo x509.SignatureAlgorithm) crypto.Hash {
for _, details := range []struct {
algo x509.SignatureAlgorithm
hash crypto.Hash
}{
{x509.MD2WithRSA, crypto.Hash(0)},
{x509.MD5WithRSA, crypto.MD5},
{x509.SHA1WithRSA, crypto.SHA1},
{x509.SHA256WithRSA, crypto.SHA256},
{x509.SHA384WithRSA, crypto.SHA384},
{x509.SHA512WithRSA, crypto.SHA512},
{x509.DSAWithSHA1, crypto.SHA1},
{x509.DSAWithSHA256, crypto.SHA256},
{x509.ECDSAWithSHA1, crypto.SHA1},
{x509.ECDSAWithSHA256, crypto.SHA256},
{x509.ECDSAWithSHA384, crypto.SHA384},
{x509.ECDSAWithSHA512, crypto.SHA512},
{x509.SHA256WithRSAPSS, crypto.SHA256},
{x509.SHA384WithRSAPSS, crypto.SHA384},
{x509.SHA512WithRSAPSS, crypto.SHA512},
} {
if details.algo == algo {
return details.hash
}
}
return crypto.Hash(0)
}
func generateCertificateHash(cert *x509.Certificate) []byte {
algorithm := signatureAlgorithmHash(cert.SignatureAlgorithm)
var hash hash.Hash
switch algorithm {
case crypto.Hash(0):
return nil
case crypto.MD5, crypto.SHA1:
hash = crypto.SHA256.New()
default:
hash = algorithm.New()
}
hash.Write(cert.Raw)
return hash.Sum(nil)
}
func generateChannelBindings(cert *x509.Certificate) *ntlmssp.ChannelBindings {
b := generateCertificateHash(cert)
if b == nil {
return nil
}
return &ntlmssp.ChannelBindings{
ApplicationData: concat([]byte(ntlmssp.TLSServerEndPoint+":"), b),
}
}