| 1 |
package messages |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
|
| 6 |
"github.com/jcmturner/gofork/encoding/asn1" |
| 7 |
"gopkg.in/jcmturner/gokrb5.v5/asn1tools" |
| 8 |
"gopkg.in/jcmturner/gokrb5.v5/crypto" |
| 9 |
"gopkg.in/jcmturner/gokrb5.v5/iana" |
| 10 |
"gopkg.in/jcmturner/gokrb5.v5/iana/asnAppTag" |
| 11 |
"gopkg.in/jcmturner/gokrb5.v5/iana/keyusage" |
| 12 |
"gopkg.in/jcmturner/gokrb5.v5/iana/msgtype" |
| 13 |
"gopkg.in/jcmturner/gokrb5.v5/krberror" |
| 14 |
"gopkg.in/jcmturner/gokrb5.v5/types" |
| 15 |
) |
| 16 |
|
| 17 |
type marshalAPReq struct { |
| 18 |
PVNO int `asn1:"explicit,tag:0"` |
| 19 |
MsgType int `asn1:"explicit,tag:1"` |
| 20 |
APOptions asn1.BitString `asn1:"explicit,tag:2"` |
| 21 |
|
| 22 |
Ticket asn1.RawValue `asn1:"explicit,tag:3"` |
| 23 |
Authenticator types.EncryptedData `asn1:"explicit,tag:4"` |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
type APReq struct { |
| 28 |
PVNO int `asn1:"explicit,tag:0"` |
| 29 |
MsgType int `asn1:"explicit,tag:1"` |
| 30 |
APOptions asn1.BitString `asn1:"explicit,tag:2"` |
| 31 |
Ticket Ticket `asn1:"explicit,tag:3"` |
| 32 |
Authenticator types.EncryptedData `asn1:"explicit,tag:4"` |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
func NewAPReq(tkt Ticket, sessionKey types.EncryptionKey, auth types.Authenticator) (APReq, error) { |
| 37 |
var a APReq |
| 38 |
ed, err := encryptAuthenticator(auth, sessionKey, tkt) |
| 39 |
if err != nil { |
| 40 |
return a, krberror.Errorf(err, krberror.KRBMsgError, "error creating Authenticator for AP_REQ") |
| 41 |
} |
| 42 |
a = APReq{ |
| 43 |
PVNO: iana.PVNO, |
| 44 |
MsgType: msgtype.KRB_AP_REQ, |
| 45 |
APOptions: types.NewKrbFlags(), |
| 46 |
Ticket: tkt, |
| 47 |
Authenticator: ed, |
| 48 |
} |
| 49 |
return a, nil |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
func encryptAuthenticator(a types.Authenticator, sessionKey types.EncryptionKey, tkt Ticket) (types.EncryptedData, error) { |
| 54 |
var ed types.EncryptedData |
| 55 |
m, err := a.Marshal() |
| 56 |
if err != nil { |
| 57 |
return ed, krberror.Errorf(err, krberror.EncodingError, "marshaling error of EncryptedData form of Authenticator") |
| 58 |
} |
| 59 |
usage := authenticatorKeyUsage(tkt.SName) |
| 60 |
ed, err = crypto.GetEncryptedData(m, sessionKey, uint32(usage), tkt.EncPart.KVNO) |
| 61 |
if err != nil { |
| 62 |
return ed, krberror.Errorf(err, krberror.EncryptingError, "error encrypting Authenticator") |
| 63 |
} |
| 64 |
return ed, nil |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
func (a *APReq) DecryptAuthenticator(sessionKey types.EncryptionKey) (auth types.Authenticator, err error) { |
| 70 |
usage := authenticatorKeyUsage(a.Ticket.SName) |
| 71 |
ab, e := crypto.DecryptEncPart(a.Authenticator, sessionKey, uint32(usage)) |
| 72 |
if e != nil { |
| 73 |
err = fmt.Errorf("error decrypting authenticator: %v", e) |
| 74 |
return |
| 75 |
} |
| 76 |
e = auth.Unmarshal(ab) |
| 77 |
if e != nil { |
| 78 |
err = fmt.Errorf("error unmarshaling authenticator") |
| 79 |
return |
| 80 |
} |
| 81 |
return |
| 82 |
} |
| 83 |
|
| 84 |
func authenticatorKeyUsage(pn types.PrincipalName) int { |
| 85 |
if pn.NameString[0] == "krbtgt" { |
| 86 |
return keyusage.TGS_REQ_PA_TGS_REQ_AP_REQ_AUTHENTICATOR |
| 87 |
} |
| 88 |
return keyusage.AP_REQ_AUTHENTICATOR |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
func (a *APReq) Unmarshal(b []byte) error { |
| 93 |
var m marshalAPReq |
| 94 |
_, err := asn1.UnmarshalWithParams(b, &m, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.APREQ)) |
| 95 |
if err != nil { |
| 96 |
return krberror.Errorf(err, krberror.EncodingError, "unmarshal error of AP_REQ") |
| 97 |
} |
| 98 |
if m.MsgType != msgtype.KRB_AP_REQ { |
| 99 |
return krberror.NewErrorf(krberror.KRBMsgError, "message ID does not indicate an AP_REQ. Expected: %v; Actual: %v", msgtype.KRB_AP_REQ, m.MsgType) |
| 100 |
} |
| 101 |
a.PVNO = m.PVNO |
| 102 |
a.MsgType = m.MsgType |
| 103 |
a.APOptions = m.APOptions |
| 104 |
a.Authenticator = m.Authenticator |
| 105 |
a.Ticket, err = UnmarshalTicket(m.Ticket.Bytes) |
| 106 |
if err != nil { |
| 107 |
return krberror.Errorf(err, krberror.EncodingError, "unmarshaling error of Ticket within AP_REQ") |
| 108 |
} |
| 109 |
return nil |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
func (a *APReq) Marshal() ([]byte, error) { |
| 114 |
m := marshalAPReq{ |
| 115 |
PVNO: a.PVNO, |
| 116 |
MsgType: a.MsgType, |
| 117 |
APOptions: a.APOptions, |
| 118 |
Authenticator: a.Authenticator, |
| 119 |
} |
| 120 |
var b []byte |
| 121 |
b, err := a.Ticket.Marshal() |
| 122 |
if err != nil { |
| 123 |
return b, err |
| 124 |
} |
| 125 |
m.Ticket = asn1.RawValue{ |
| 126 |
Class: asn1.ClassContextSpecific, |
| 127 |
IsCompound: true, |
| 128 |
Tag: 3, |
| 129 |
Bytes: b, |
| 130 |
} |
| 131 |
mk, err := asn1.Marshal(m) |
| 132 |
if err != nil { |
| 133 |
return mk, krberror.Errorf(err, krberror.EncodingError, "marshaling error of AP_REQ") |
| 134 |
} |
| 135 |
mk = asn1tools.AddASNAppTag(mk, asnAppTag.APREQ) |
| 136 |
return mk, nil |
| 137 |
} |