Debian Codegraph @

gssapi/gssapi.go

raw symbols SCIP
1 // Package gssapi implements Generic Security Services Application Program Interface required for SPNEGO kerberos authentication.
2 package gssapi
3
4 import (
5 "errors"
6 "fmt"
7
8 "github.com/jcmturner/gofork/encoding/asn1"
9 "gopkg.in/jcmturner/gokrb5.v5/asn1tools"
10 "gopkg.in/jcmturner/gokrb5.v5/credentials"
11 "gopkg.in/jcmturner/gokrb5.v5/messages"
12 "gopkg.in/jcmturner/gokrb5.v5/types"
13 )
14
15 // SPNEGO_OID is the OID for SPNEGO header type.
16 var SPNEGO_OID = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 2}
17
18 // SPNEGO header struct
19 type SPNEGO struct {
20 Init bool
21 Resp bool
22 NegTokenInit NegTokenInit
23 NegTokenResp NegTokenResp
24 }
25
26 // Unmarshal SPNEGO negotiation token
27 func (s *SPNEGO) Unmarshal(b []byte) error {
28 var r []byte
29 var err error
30 if b[0] != byte(161) {
31 // Not a NegTokenResp/Targ could be a NegTokenInit
32 var oid asn1.ObjectIdentifier
33 r, err = asn1.UnmarshalWithParams(b, &oid, fmt.Sprintf("application,explicit,tag:%v", 0))
34 if err != nil {
35 return fmt.Errorf("not a valid SPNEGO token: %v", err)
36 }
37 // Check the OID is the SPNEGO OID value
38 if !oid.Equal(SPNEGO_OID) {
39 return fmt.Errorf("OID %s does not match SPNEGO OID %s", oid.String(), SPNEGO_OID.String())
40 }
41 } else {
42 // Could be a NegTokenResp/Targ
43 r = b
44 }
45
46 var a asn1.RawValue
47 _, err = asn1.Unmarshal(r, &a)
48 if err != nil {
49 return fmt.Errorf("error unmarshalling SPNEGO: %v", err)
50 }
51 switch a.Tag {
52 case 0:
53 _, err = asn1.Unmarshal(a.Bytes, &s.NegTokenInit)
54 if err != nil {
55 return fmt.Errorf("error unmarshalling NegotiationToken type %d (Init): %v", a.Tag, err)
56 }
57 s.Init = true
58 case 1:
59 _, err = asn1.Unmarshal(a.Bytes, &s.NegTokenResp)
60 if err != nil {
61 return fmt.Errorf("error unmarshalling NegotiationToken type %d (Resp/Targ): %v", a.Tag, err)
62 }
63 s.Resp = true
64 default:
65 return errors.New("unknown choice type for NegotiationToken")
66 }
67 return nil
68 }
69
70 // Marshal SPNEGO negotiation token
71 func (s *SPNEGO) Marshal() ([]byte, error) {
72 var b []byte
73 if s.Init {
74 hb, _ := asn1.Marshal(SPNEGO_OID)
75 tb, err := s.NegTokenInit.Marshal()
76 if err != nil {
77 return b, fmt.Errorf("could not marshal NegTokenInit: %v", err)
78 }
79 b = append(hb, tb...)
80 return asn1tools.AddASNAppTag(b, 0), nil
81 }
82 if s.Resp {
83 b, err := s.NegTokenResp.Marshal()
84 if err != nil {
85 return b, fmt.Errorf("could not marshal NegTokenResp: %v", err)
86 }
87 return b, nil
88 }
89 return b, errors.New("SPNEGO cannot be marshalled. It contains neither a NegTokenInit or NegTokenResp")
90 }
91
92 // GetSPNEGOKrbNegTokenInit returns an SPNEGO struct containing a NegTokenInit.
93 func GetSPNEGOKrbNegTokenInit(creds credentials.Credentials, tkt messages.Ticket, sessionKey types.EncryptionKey) (SPNEGO, error) {
94 negTokenInit, err := NewNegTokenInitKrb5(creds, tkt, sessionKey)
95 if err != nil {
96 return SPNEGO{}, fmt.Errorf("could not create NegTokenInit: %v", err)
97 }
98 return SPNEGO{
99 Init: true,
100 NegTokenInit: negTokenInit,
101 }, nil
102 }