Debian Codegraph @

gssapi/NegotiationToken.go

raw symbols SCIP
1 package gssapi
2
3 import (
4 "errors"
5 "fmt"
6
7 "github.com/jcmturner/gofork/encoding/asn1"
8 "gopkg.in/jcmturner/gokrb5.v5/credentials"
9 "gopkg.in/jcmturner/gokrb5.v5/messages"
10 "gopkg.in/jcmturner/gokrb5.v5/types"
11 )
12
13 /*
14 https://msdn.microsoft.com/en-us/library/ms995330.aspx
15 */
16
17 // NegTokenInit implements Negotiation Token of type Init
18 type NegTokenInit struct {
19 MechTypes []asn1.ObjectIdentifier `asn1:"explicit,tag:0"`
20 ReqFlags ContextFlags `asn1:"explicit,optional,tag:1"`
21 MechToken []byte `asn1:"explicit,optional,tag:2"`
22 MechTokenMIC []byte `asn1:"explicit,optional,tag:3"`
23 }
24
25 // NegTokenResp implements Negotiation Token of type Resp/Targ
26 type NegTokenResp struct {
27 NegState asn1.Enumerated `asn1:"explicit,tag:0"`
28 SupportedMech asn1.ObjectIdentifier `asn1:"explicit,optional,tag:1"`
29 ResponseToken []byte `asn1:"explicit,optional,tag:2"`
30 MechListMIC []byte `asn1:"explicit,optional,tag:3"`
31 }
32
33 // NegTokenTarg implements Negotiation Token of type Resp/Targ
34 type NegTokenTarg NegTokenResp
35
36 // UnmarshalNegToken umarshals and returns either a NegTokenInit or a NegTokenResp.
37 //
38 // The boolean indicates if the response is a NegTokenInit.
39 // If error is nil and the boolean is false the response is a NegTokenResp.
40 func UnmarshalNegToken(b []byte) (bool, interface{}, error) {
41 var a asn1.RawValue
42 _, err := asn1.Unmarshal(b, &a)
43 if err != nil {
44 return false, nil, fmt.Errorf("error unmarshalling NegotiationToken: %v", err)
45 }
46 switch a.Tag {
47 case 0:
48 var negToken NegTokenInit
49 _, err = asn1.Unmarshal(a.Bytes, &negToken)
50 if err != nil {
51 return false, nil, fmt.Errorf("error unmarshalling NegotiationToken type %d (Init): %v", a.Tag, err)
52 }
53 return true, negToken, nil
54 case 1:
55 var negToken NegTokenResp
56 _, err = asn1.Unmarshal(a.Bytes, &negToken)
57 if err != nil {
58 return false, nil, fmt.Errorf("error unmarshalling NegotiationToken type %d (Resp/Targ): %v", a.Tag, err)
59 }
60 return false, negToken, nil
61 default:
62 return false, nil, errors.New("unknown choice type for NegotiationToken")
63 }
64
65 }
66
67 // Marshal an Init negotiation token
68 func (n *NegTokenInit) Marshal() ([]byte, error) {
69 b, err := asn1.Marshal(*n)
70 if err != nil {
71 return nil, err
72 }
73 nt := asn1.RawValue{
74 Tag: 0,
75 Class: 2,
76 IsCompound: true,
77 Bytes: b,
78 }
79 nb, err := asn1.Marshal(nt)
80 if err != nil {
81 return nil, err
82 }
83 return nb, nil
84 }
85
86 // Marshal a Resp/Targ negotiation token
87 func (n *NegTokenResp) Marshal() ([]byte, error) {
88 b, err := asn1.Marshal(*n)
89 if err != nil {
90 return nil, err
91 }
92 nt := asn1.RawValue{
93 Tag: 1,
94 Class: 2,
95 IsCompound: true,
96 Bytes: b,
97 }
98 nb, err := asn1.Marshal(nt)
99 if err != nil {
100 return nil, err
101 }
102 return nb, nil
103 }
104
105 // NewNegTokenInitKrb5 creates new Init negotiation token for Kerberos 5
106 func NewNegTokenInitKrb5(creds credentials.Credentials, tkt messages.Ticket, sessionKey types.EncryptionKey) (NegTokenInit, error) {
107 mt, err := NewAPREQMechToken(creds, tkt, sessionKey, []int{GSS_C_INTEG_FLAG, GSS_C_CONF_FLAG}, []int{})
108 if err != nil {
109 return NegTokenInit{}, fmt.Errorf("error getting MechToken; %v", err)
110 }
111 mtb, err := mt.Marshal()
112 if err != nil {
113 return NegTokenInit{}, fmt.Errorf("error marshalling MechToken; %v", err)
114 }
115 return NegTokenInit{
116 MechTypes: []asn1.ObjectIdentifier{MechTypeOIDKRB5},
117 MechToken: mtb,
118 }, nil
119 }