Debian Codegraph @

types/Authenticator.go

raw symbols SCIP
1 // Package types provides Kerberos 5 data types.
2 package types
3
4 import (
5 "crypto/rand"
6 "fmt"
7 "math"
8 "math/big"
9 "time"
10
11 "github.com/jcmturner/gofork/encoding/asn1"
12 "gopkg.in/jcmturner/gokrb5.v5/asn1tools"
13 "gopkg.in/jcmturner/gokrb5.v5/iana"
14 "gopkg.in/jcmturner/gokrb5.v5/iana/asnAppTag"
15 )
16
17 // Authenticator - A record containing information that can be shown to have been recently generated using the session key known only by the client and server.
18 // https://tools.ietf.org/html/rfc4120#section-5.5.1
19 type Authenticator struct {
20 AVNO int `asn1:"explicit,tag:0"`
21 CRealm string `asn1:"generalstring,explicit,tag:1"`
22 CName PrincipalName `asn1:"explicit,tag:2"`
23 Cksum Checksum `asn1:"explicit,optional,tag:3"`
24 Cusec int `asn1:"explicit,tag:4"`
25 CTime time.Time `asn1:"generalized,explicit,tag:5"`
26 SubKey EncryptionKey `asn1:"explicit,optional,tag:6"`
27 SeqNumber int64 `asn1:"explicit,optional,tag:7"`
28 AuthorizationData AuthorizationData `asn1:"explicit,optional,tag:8"`
29 }
30
31 // NewAuthenticator creates a new Authenticator.
32 func NewAuthenticator(realm string, cname PrincipalName) (Authenticator, error) {
33 seq, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32))
34 if err != nil {
35 return Authenticator{}, err
36 }
37 t := time.Now().UTC()
38 return Authenticator{
39 AVNO: iana.PVNO,
40 CRealm: realm,
41 CName: cname,
42 Cksum: Checksum{},
43 Cusec: int((t.UnixNano() / int64(time.Microsecond)) - (t.Unix() * 1e6)),
44 CTime: t,
45 SeqNumber: seq.Int64(),
46 }, nil
47 }
48
49 // GenerateSeqNumberAndSubKey sets the Authenticator's sequence number and subkey.
50 func (a *Authenticator) GenerateSeqNumberAndSubKey(keyType int32, keySize int) error {
51 seq, err := rand.Int(rand.Reader, big.NewInt(math.MaxUint32))
52 if err != nil {
53 return err
54 }
55 a.SeqNumber = seq.Int64()
56 //Generate subkey value
57 sk := make([]byte, keySize, keySize)
58 rand.Read(sk)
59 a.SubKey = EncryptionKey{
60 KeyType: keyType,
61 KeyValue: sk,
62 }
63 return nil
64 }
65
66 // Unmarshal bytes into the Authenticator.
67 func (a *Authenticator) Unmarshal(b []byte) error {
68 _, err := asn1.UnmarshalWithParams(b, a, fmt.Sprintf("application,explicit,tag:%v", asnAppTag.Authenticator))
69 return err
70 }
71
72 // Marshal the Authenticator.
73 func (a *Authenticator) Marshal() ([]byte, error) {
74 b, err := asn1.Marshal(*a)
75 if err != nil {
76 return nil, err
77 }
78 b = asn1tools.AddASNAppTag(b, asnAppTag.Authenticator)
79 return b, nil
80 }