-
Notifications
You must be signed in to change notification settings - Fork 2
/
claims.go
152 lines (134 loc) · 4.53 KB
/
claims.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
package mstypes
import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"github.com/jcmturner/rpc/ndr"
"golang.org/x/net/http2/hpack"
)
// Compression format assigned numbers. https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-xca/a8b7cb0a-92a6-4187-a23b-5e14273b96f8
const (
CompressionFormatNone uint16 = 0
CompressionFormatLZNT1 uint16 = 2 // LZNT1 aka ntfs compression
CompressionFormatXPress uint16 = 3 // plain LZ77
CompressionFormatXPressHuff uint16 = 4 // LZ77+Huffman - The Huffman variant of the XPRESS compression format uses LZ77-style dictionary compression combined with Huffman coding.
)
// ClaimsSourceTypeAD https://msdn.microsoft.com/en-us/library/hh553809.aspx
const ClaimsSourceTypeAD uint16 = 1
// Claim Type assigned numbers
const (
ClaimTypeIDInt64 uint16 = 1
ClaimTypeIDUInt64 uint16 = 2
ClaimTypeIDString uint16 = 3
ClaimsTypeIDBoolean uint16 = 6
)
// ClaimsBlob implements https://msdn.microsoft.com/en-us/library/hh554119.aspx
type ClaimsBlob struct {
Size uint32
EncodedBlob EncodedBlob
}
// EncodedBlob are the bytes of the encoded Claims
type EncodedBlob []byte
// Size returns the size of the bytes of the encoded Claims
func (b EncodedBlob) Size(c interface{}) int {
cb := c.(ClaimsBlob)
return int(cb.Size)
}
// ClaimsSetMetadata implements https://msdn.microsoft.com/en-us/library/hh554073.aspx
type ClaimsSetMetadata struct {
ClaimsSetSize uint32
ClaimsSetBytes []byte `ndr:"pointer,conformant"`
CompressionFormat uint16 // Enum see constants for options
UncompressedClaimsSetSize uint32
ReservedType uint16
ReservedFieldSize uint32
ReservedField []byte `ndr:"pointer,conformant"`
}
// ClaimsSet reads the ClaimsSet type from the NDR encoded ClaimsSetBytes in the ClaimsSetMetadata
func (m *ClaimsSetMetadata) ClaimsSet() (c ClaimsSet, err error) {
if len(m.ClaimsSetBytes) < 1 {
err = errors.New("no bytes available for ClaimsSet")
return
}
// TODO switch statement to decompress ClaimsSetBytes
switch m.CompressionFormat {
case CompressionFormatLZNT1:
s := hex.EncodeToString(m.ClaimsSetBytes)
err = fmt.Errorf("ClaimsSet compressed, format LZNT1 not currently supported: %s", s)
return
case CompressionFormatXPress:
s := hex.EncodeToString(m.ClaimsSetBytes)
err = fmt.Errorf("ClaimsSet compressed, format XPress not currently supported: %s", s)
return
case CompressionFormatXPressHuff:
var b []byte
buff := bytes.NewBuffer(b)
_, e := hpack.HuffmanDecode(buff, m.ClaimsSetBytes)
if e != nil {
err = fmt.Errorf("error deflating: %v", e)
return
}
m.ClaimsSetBytes = buff.Bytes()
}
dec := ndr.NewDecoder(bytes.NewReader(m.ClaimsSetBytes))
err = dec.Decode(&c)
return
}
// ClaimsSet implements https://msdn.microsoft.com/en-us/library/hh554122.aspx
type ClaimsSet struct {
ClaimsArrayCount uint32
ClaimsArrays []ClaimsArray `ndr:"pointer,conformant"`
ReservedType uint16
ReservedFieldSize uint32
ReservedField []byte `ndr:"pointer,conformant"`
}
// ClaimsArray implements https://msdn.microsoft.com/en-us/library/hh536458.aspx
type ClaimsArray struct {
ClaimsSourceType uint16
ClaimsCount uint32
ClaimEntries []ClaimEntry `ndr:"pointer,conformant"`
}
// ClaimEntry is a NDR union that implements https://msdn.microsoft.com/en-us/library/hh536374.aspx
type ClaimEntry struct {
ID string `ndr:"pointer,conformant,varying"`
Type uint16 `ndr:"unionTag"`
TypeInt64 ClaimTypeInt64 `ndr:"unionField"`
TypeUInt64 ClaimTypeUInt64 `ndr:"unionField"`
TypeString ClaimTypeString `ndr:"unionField"`
TypeBool ClaimTypeBoolean `ndr:"unionField"`
}
// SwitchFunc is the ClaimEntry union field selection function
func (u ClaimEntry) SwitchFunc(_ interface{}) string {
switch u.Type {
case ClaimTypeIDInt64:
return "TypeInt64"
case ClaimTypeIDUInt64:
return "TypeUInt64"
case ClaimTypeIDString:
return "TypeString"
case ClaimsTypeIDBoolean:
return "TypeBool"
}
return ""
}
// ClaimTypeInt64 is a claim of type int64
type ClaimTypeInt64 struct {
ValueCount uint32
Value []int64 `ndr:"pointer,conformant"`
}
// ClaimTypeUInt64 is a claim of type uint64
type ClaimTypeUInt64 struct {
ValueCount uint32
Value []uint64 `ndr:"pointer,conformant"`
}
// ClaimTypeString is a claim of type string
type ClaimTypeString struct {
ValueCount uint32
Value []LPWSTR `ndr:"pointer,conformant"`
}
// ClaimTypeBoolean is a claim of type bool
type ClaimTypeBoolean struct {
ValueCount uint32
Value []bool `ndr:"pointer,conformant"`
}