-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathike_auth.go
210 lines (202 loc) · 6.45 KB
/
ike_auth.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package ike
import (
"fmt"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/msgboxio/ike/protocol"
"github.com/pkg/errors"
)
// authFromSession creates IKE_AUTH messages
func authFromSession(sess *Session) (*Message, error) {
// proposal
var prop protocol.Proposals
// part of signed octet
var initB []byte
var idPayloadType protocol.PayloadType
if sess.isInitiator {
prop = protocol.ProposalFromTransform(protocol.ESP, sess.cfg.ProposalEsp, sess.EspSpiI)
// initiators's signed octet
// initI | Nr | prf(sk_pi | IDi )
initB = sess.initIb
idPayloadType = protocol.PayloadTypeIDi
} else {
prop = protocol.ProposalFromTransform(protocol.ESP, sess.cfg.ProposalEsp, sess.EspSpiR)
// responder's signed octet
// initR | Ni | prf(sk_pr | IDr )
initB = sess.initRb
idPayloadType = protocol.PayloadTypeIDr
}
authMsg := makeAuth(
&authParams{
isInitiator: sess.isInitiator,
isTransportMode: sess.cfg.IsTransportMode,
spiI: sess.IkeSpiI,
spiR: sess.IkeSpiR,
proposals: prop,
tsI: sess.cfg.TsI,
tsR: sess.cfg.TsR,
lifetime: sess.cfg.Lifetime,
})
id := sess.authLocal.Identity()
// add CERT
switch id.AuthMethod() {
case protocol.AUTH_RSA_DIGITAL_SIGNATURE, protocol.AUTH_DIGITAL_SIGNATURE:
certID, ok := id.(*CertIdentity)
if !ok {
// should never happen
return nil, errors.New("missing Certificate Identity")
}
if certID.Certificate == nil {
return nil, errors.New("missing Certificate")
}
authMsg.Payloads.Add(&protocol.CertPayload{
PayloadHeader: &protocol.PayloadHeader{},
CertEncodingType: protocol.X_509_CERTIFICATE_SIGNATURE,
Data: certID.Certificate.Raw,
})
}
// add ID
iDp := &protocol.IdPayload{
PayloadHeader: &protocol.PayloadHeader{},
IdPayloadType: idPayloadType,
IdType: id.IdType(),
Data: id.Id(),
}
authMsg.Payloads.Add(iDp)
// signature
signature, err := sess.authLocal.Sign(initB, iDp, sess.Logger)
if err != nil {
return nil, err
}
authMsg.Payloads.Add(&protocol.AuthPayload{
PayloadHeader: &protocol.PayloadHeader{},
AuthMethod: id.AuthMethod(),
Data: signature,
})
return authMsg, nil
}
// auth respones can be a valid auth message, auth resp with AUTHENTICATION_FAILED
// or INFORMATIONAL with AUTHENTICATION_FAILED
func checkAuthResponseForSession(sess *Session, msg *Message) (err error) {
// check if reply
if err = msg.CheckFlags(); err != nil {
return
}
// other flag combos have been checked
if !msg.IkeHeader.Flags.IsResponse() {
return errors.Wrap(protocol.ERR_INVALID_SYNTAX, "IKE_AUTH: unexpected request")
}
// is an INFORMATIONAL ERROR
if msg.IkeHeader.ExchangeType == protocol.INFORMATIONAL {
// expect it to be this
return errors.Wrap(errPeerRemovedIkeSa, "IKE_AUTH: INFORMATIONAL AUTHENTICATION_FAILED")
}
// must be an AUTH message
if msg.IkeHeader.ExchangeType != protocol.IKE_AUTH {
return errors.Wrap(protocol.ERR_INVALID_SYNTAX, "IKE_AUTH: incorrect type")
}
// ensure other payloads are present
if err = msg.EnsurePayloads(authRPayloads); err != nil {
// not a proper AUTH response
// check for NOTIFICATION : AUTHENTICATION_FAILED
for _, n := range msg.Payloads.GetNotifications() {
if n.NotificationType == protocol.AUTHENTICATION_FAILED {
err = errors.Wrap(errPeerRemovedIkeSa, "IKE_AUTH: response AUTHENTICATION_FAILED")
return
}
}
}
return
}
func checkAuthRequestForSession(sess *Session, msg *Message) (err error) {
// must be a request
if err = msg.CheckFlags(); err != nil {
return err
}
if msg.IkeHeader.Flags.IsResponse() {
return errors.Wrap(protocol.ERR_INVALID_SYNTAX, "IKE_AUTH: responder received response")
}
// must be an AUTH message
if msg.IkeHeader.ExchangeType != protocol.IKE_AUTH {
return errors.Wrap(protocol.ERR_INVALID_SYNTAX, "IKE_AUTH: incorrect type")
}
// ensure other payloads are present
if err := msg.EnsurePayloads(authIPayloads); err != nil {
return err
}
return nil
}
func handleAuthForSession(sess *Session, msg *Message) (spi protocol.Spi, lt time.Duration, err error) {
// can we authenticate ?
if err = authenticateSession(sess, msg); err != nil {
err = errors.Wrap(protocol.ERR_AUTHENTICATION_FAILED, err.Error())
return
}
for _, n := range msg.Payloads.GetNotifications() {
if nErr, ok := protocol.GetIkeErrorCode(n.NotificationType); ok {
// for example, due to FAILED_CP_REQUIRED, NO_PROPOSAL_CHOSEN, TS_UNACCEPTABLE etc
// TODO - for now, we should simply end the IKE_SA
err = errors.Wrap(nErr, "peer notified")
return
}
}
// are SA parameters ok?
params, err := parseSaAndSelectors(msg)
if err != nil {
return
}
spi, lt, err = checkSelectorsForSession(sess, params)
return
}
func authenticateSession(sess *Session, msg *Message) (err error) {
// authenticate peer
var idP *protocol.IdPayload
var initB []byte
if sess.isInitiator {
initB = sess.initRb
idP = msg.Payloads.Get(protocol.PayloadTypeIDr).(*protocol.IdPayload)
} else {
initB = sess.initIb
idP = msg.Payloads.Get(protocol.PayloadTypeIDi).(*protocol.IdPayload)
}
authP := msg.Payloads.Get(protocol.PayloadTypeAUTH).(*protocol.AuthPayload)
chain, err := msg.Payloads.GetCertchain()
if err != nil {
return err
}
return sess.authPeer.Verify(initB, idP, authP.AuthMethod, authP.Data, chain, sess.Logger)
}
// checkSelectorsForSession returns Peer Spi
func checkSelectorsForSession(sess *Session, params *authParams) (spi protocol.Spi, lt time.Duration, err error) {
if err = sess.cfg.CheckProposals(protocol.ESP, params.proposals); err != nil {
sess.Logger.Log("BAD_PROPOSAL", err,
"PEER", spew.Sprintf("%#v", params.proposals),
"OUR", spew.Sprintf("%#v", sess.cfg.ProposalEsp))
return
}
// selectors
if err = sess.cfg.CheckSelectors(params.tsI, params.tsR, params.isTransportMode); err != nil {
sess.Logger.Log("BAD_SELECTORS", err,
"PEER", fmt.Sprintf("[INI]%s<=>%s[RES]", params.tsI, params.tsR),
"OUR_SELECTORS", fmt.Sprintf("[INI]%s<=>%s[RES]", sess.cfg.TsI, sess.cfg.TsR))
return
}
// message looks OK
if sess.isInitiator {
spi = append([]byte{}, params.spiR...)
} else {
spi = append([]byte{}, params.spiI...)
}
lt = params.lifetime
log := []interface{}{"SELECTORS", fmt.Sprintf("[INI]%s<=>%s[RES]", sess.cfg.TsI, sess.cfg.TsR)}
if params.lifetime != 0 {
log = append(log, "LIFETIME", params.lifetime)
}
if params.isTransportMode {
log = append(log, "MODE", "TRANSPORT")
} else {
log = append(log, "MODE", "TUNNEL")
}
sess.Logger.Log(log...)
return
}