/
tls_ka.go
171 lines (154 loc) · 4.78 KB
/
tls_ka.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tls
import (
"encoding/json"
"math/big"
"regexp"
"strconv"
"github.com/zmap/zgrab/ztools/keys"
)
// SignatureAndHash is a signatureAndHash that implements json.Marshaler and
// json.Unmarshaler
type SignatureAndHash SignatureScheme
type auxSignatureAndHash struct {
SignatureAlgorithm string `json:"signature_algorithm"`
HashAlgorithm string `json:"hash_algorithm"`
}
// MarshalJSON implements the json.Marshaler interface
func (sh *SignatureAndHash) MarshalJSON() ([]byte, error) {
aux := auxSignatureAndHash{
SignatureAlgorithm: nameForSignature(byte(*sh)),
HashAlgorithm: nameForHash(byte(*sh >> 8)),
}
return json.Marshal(&aux)
}
var unknownAlgorithmRegex = regexp.MustCompile(`unknown\.(\d+)`)
// UnmarshalJSON implements the json.Unmarshaler interface
func (sh *SignatureAndHash) UnmarshalJSON(b []byte) error {
aux := new(auxSignatureAndHash)
if err := json.Unmarshal(b, aux); err != nil {
return err
}
// TODO implement
panic("unimplemented")
}
func (ka *rsaKeyAgreement) RSAParams() *keys.RSAPublicKey {
out := new(keys.RSAPublicKey)
out.PublicKey = ka.publicKey
return out
}
func (ka *ecdheKeyAgreement) ECDHParams() *keys.ECDHParams {
out := new(keys.ECDHParams)
out.TLSCurveID = keys.TLSCurveID(ka.curveID)
out.ServerPublic = &keys.ECPoint{}
if ka.x != nil {
out.ServerPublic.X = new(big.Int)
out.ServerPublic.X.Set(ka.x)
}
if ka.y != nil {
out.ServerPublic.Y = new(big.Int)
out.ServerPublic.Y.Set(ka.y)
}
if len(ka.serverPrivKey) > 0 {
out.ServerPrivate = new(keys.ECDHPrivateParams)
out.ServerPrivate.Length = len(ka.serverPrivKey)
out.ServerPrivate.Value = make([]byte, len(ka.serverPrivKey))
copy(out.ServerPrivate.Value, ka.serverPrivKey)
}
return out
}
func (ka *ecdheKeyAgreement) ClientECDHParams() *keys.ECDHParams {
out := new(keys.ECDHParams)
out.TLSCurveID = keys.TLSCurveID(ka.curveID)
out.ClientPublic = &keys.ECPoint{}
if ka.clientX != nil {
out.ClientPublic.X = new(big.Int)
out.ClientPublic.X.Set(ka.clientX)
}
if ka.clientY != nil {
out.ClientPublic.Y = new(big.Int)
out.ClientPublic.Y.Set(ka.clientY)
}
if len(ka.clientPrivKey) > 0 {
out.ClientPrivate = new(keys.ECDHPrivateParams)
out.ClientPrivate.Length = len(ka.clientPrivKey)
out.ClientPrivate.Value = make([]byte, len(ka.clientPrivKey))
copy(out.ClientPrivate.Value, ka.clientPrivKey)
}
return out
}
func (ka *dheKeyAgreement) DHParams() *keys.DHParams {
out := new(keys.DHParams)
if ka.p != nil {
out.Prime = new(big.Int).Set(ka.p)
}
if ka.g != nil {
out.Generator = new(big.Int).Set(ka.g)
}
if ka.yServer != nil {
out.ServerPublic = new(big.Int).Set(ka.yServer)
if ka.yOurs != nil && ka.xOurs != nil && ka.yServer.Cmp(ka.yOurs) == 0 {
out.ServerPrivate = new(big.Int).Set(ka.xOurs)
}
}
return out
}
func (ka *dheKeyAgreement) ClientDHParams() *keys.DHParams {
out := new(keys.DHParams)
if ka.p != nil {
out.Prime = new(big.Int).Set(ka.p)
}
if ka.g != nil {
out.Generator = new(big.Int).Set(ka.g)
}
if ka.yClient != nil {
out.ClientPublic = new(big.Int).Set(ka.yClient)
if ka.yOurs != nil && ka.xOurs != nil && ka.yClient.Cmp(ka.yOurs) == 0 {
out.ClientPrivate = new(big.Int).Set(ka.xOurs)
}
}
return out
}
// DigitalSignature represents a signature for a digitally-signed-struct in the
// TLS record protocol. It is dependent on the version of TLS in use. In TLS
// 1.2, the first two bytes of the signature specify the signature and hash
// algorithms. These are contained the TLSSignature.Raw field, but also parsed
// out into TLSSignature.SigHashExtension. In older versions of TLS, the
// signature and hash extension is not used, and so
// TLSSignature.SigHashExtension will be empty. The version string is stored in
// TLSSignature.TLSVersion.
type DigitalSignature struct {
Raw []byte `json:"raw"`
Type string `json:"type,omitempty"`
Valid bool `json:"valid"`
SigHashExtension *SignatureAndHash `json:"signature_and_hash_type,omitempty"`
Version TLSVersion `json:"tls_version"`
}
func signatureTypeToName(sigType uint8) string {
switch sigType {
case signature12_RSA:
return "rsa"
case signature12_DSA:
return "dsa"
case signature12_ECDSA:
return "ecdsa"
default:
break
}
return "unknown." + strconv.Itoa(int(sigType))
}
func (ka *signedKeyAgreement) Signature() *DigitalSignature {
out := DigitalSignature{
Raw: ka.raw,
Type: signatureTypeToName(ka.sigType),
Valid: ka.valid,
Version: TLSVersion(ka.version),
}
if ka.version >= VersionTLS12 {
out.SigHashExtension = new(SignatureAndHash)
*out.SigHashExtension = SignatureAndHash(ka.sh)
}
return &out
}