forked from go-macaroon/macaroon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacaroon.go
More file actions
216 lines (193 loc) · 6.15 KB
/
Copy pathmacaroon.go
File metadata and controls
216 lines (193 loc) · 6.15 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Package macaroon implements macaroons as described in the paper:
// "Macaroons: Cookies with Contextual Caveats for Decentralized Authorization
// in the Cloud".
package macaroon
import (
"bytes"
"crypto/hmac"
"fmt"
"google.golang.org/protobuf/proto"
)
// Condition represents a macaroon caveat condition.
type Condition []byte
// CheckFunc is called for every first-party caveat ID during verification.
type CheckFunc func(condition Condition) error
// NewRoot creates a new root macaroon with no caveats.
func NewRoot(keySeed, id []byte, loc string) *Macaroon {
m := &Macaroon{Id: bytes.Clone(id)}
if loc != "" {
m.Location = proto.String(loc)
}
derivedKey := hashKey(keySeed)
m.Signature = hmacDigest(derivedKey, m.Id)
return m
}
// New creates a new root macaroon with the given caveats.
// Returns an error if no conditions are provided.
func New(keySeed, id []byte, loc string, conditions []Condition) (*Macaroon, error) {
if len(conditions) == 0 {
return nil, fmt.Errorf("insufficient macaroon conditions")
}
m := NewRoot(keySeed, id, loc)
for _, condition := range conditions {
m.addCaveat(&Caveat{Id: condition})
}
return m, nil
}
// VerifySignature verifies the signature of the given macaroon.
func (m *Macaroon) VerifySignature(keySeed []byte, discharges []*Macaroon) error {
vctx := newVerificationContext(m, discharges)
return vctx.verifySignatures(m, keySeed)
}
// IsThirdParty checks if the caveats verification id is not empty.
func (c *Caveat) IsThirdParty() bool {
return len(c.VerificationId) > 0
}
// BindForDischarge prepares the macaroon for being used to discharge the
// macaroon with the given signature.
//
// Macaroon must be bound for discharge before using it in the discharges
// argument to Verify.
func BindForDischarge(m *Macaroon, sig []byte) *Macaroon {
bound := proto.Clone(m).(*Macaroon)
bound.Signature = bindForRequest(sig, m.Signature)
return bound
}
// bindForRequest binds the given discharge signature to the given signature of
// its parent macaroon.
func bindForRequest(rootSig []byte, dischargeSig []byte) []byte {
if bytes.Equal(rootSig, dischargeSig) {
return dischargeSig
}
return hmacConcat(nil, rootSig, dischargeSig)
}
// WithCondition attenuates a macaroon with a new first-party caveat.
func WithCondition(prev *Macaroon, condition Condition) *Macaroon {
m := proto.Clone(prev).(*Macaroon)
m.addCaveat(&Caveat{Id: condition})
return m
}
// WithConditions attenuates a macaroon with new first-party caveats.
func WithConditions(prev *Macaroon, conditions []Condition) *Macaroon {
m := proto.Clone(prev).(*Macaroon)
for _, condition := range conditions {
m.addCaveat(&Caveat{Id: condition})
}
return m
}
// addCaveat adds a caveat to a macaroon, modifying it's signature.
func (m *Macaroon) addCaveat(caveat *Caveat) {
m.Caveats = append(m.Caveats, caveat)
if len(caveat.VerificationId) == 0 {
m.Signature = hmacDigest(m.Signature, caveat.Id)
} else {
m.Signature = hmacConcat(m.Signature, caveat.VerificationId, caveat.Id)
}
}
// WithThirdPartyCaveat attenuates a macaroon with a new third-party caveat.
func WithThirdPartyCaveat(prev *Macaroon, rootKey, caveatID []byte, loc string) (*Macaroon, error) {
derivedKey := hashKey(rootKey)
verificationID, err := encryptKey(prev.Signature, derivedKey)
if err != nil {
return nil, err
}
m := proto.Clone(prev).(*Macaroon)
m.addCaveat(&Caveat{
Id: caveatID,
VerificationId: verificationID,
Location: loc,
})
return m, nil
}
// CheckConditions invokes the given check for all first-party caveats.
func CheckConditions(m *Macaroon, discharges []*Macaroon, check CheckFunc) error {
for _, cav := range m.Caveats {
if cav.IsThirdParty() {
discharge, _, err := findDischarge(discharges, cav.Id)
if err != nil {
return err
}
if err := CheckConditions(discharge, discharges, check); err != nil {
return err
}
} else if err := check(cav.Id); err != nil {
return err
}
}
return nil
}
type verificationContext struct {
used []bool
discharges []*Macaroon
rootSig []byte
}
func newVerificationContext(root *Macaroon, discharges []*Macaroon) *verificationContext {
return &verificationContext{
used: make([]bool, len(discharges)),
discharges: discharges,
rootSig: root.Signature,
}
}
func (vctx *verificationContext) verifySignatures(root *Macaroon, keySeed []byte) error {
if len(root.Signature) != hashLen {
return fmt.Errorf("invalid signature length: %d", len(root.Signature))
}
derivedKey := hashKey(keySeed)
if err := vctx.verifySignaturesNested(root, derivedKey, false); err != nil {
return err
}
for i, wasUsed := range vctx.used {
if !wasUsed {
return fmt.Errorf("discharge macaroon %q was not used", vctx.discharges[i].Id)
}
}
return nil
}
func (vctx *verificationContext) verifySignaturesNested(m *Macaroon, key []byte, thirdParty bool) error {
digest := hmacDigest(key, m.Id)
for i, cav := range m.Caveats {
if cav.IsThirdParty() {
cavKey, err := decryptKey(digest, cav.VerificationId)
if err != nil {
return fmt.Errorf("failed to decrypt caveat key %d: %v", i, err)
}
dm, didx, err := findDischarge(vctx.discharges, cav.Id)
if err != nil {
return err
}
if err := vctx.markUsed(didx); err != nil {
return err
}
if err := vctx.verifySignaturesNested(dm, cavKey, true); err != nil {
return err
}
digest = hmacConcat(digest, cav.VerificationId, cav.Id)
} else {
digest = hmacDigest(digest, cav.Id)
}
}
if thirdParty {
digest = bindForRequest(vctx.rootSig, digest)
}
if !hmac.Equal(digest, m.Signature) {
return fmt.Errorf("signature mismatch after caveat verification")
}
return nil
}
// markUsed marks a discharge as used or returns an error if it was already used.
func (vctx *verificationContext) markUsed(idx int) error {
if vctx.used[idx] {
m := vctx.discharges[idx]
return fmt.Errorf("discharge macaroon %q was used more than once", m.Id)
}
vctx.used[idx] = true
return nil
}
func findDischarge(discharges []*Macaroon, id []byte) (*Macaroon, int, error) {
for idx, m := range discharges {
if bytes.Equal(m.Id, id) {
return m, idx, nil
}
}
return nil, 0, fmt.Errorf("cannot find discharge macaroon for caveat %x", id)
}