-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkey-loader.go
More file actions
217 lines (189 loc) · 5.81 KB
/
key-loader.go
File metadata and controls
217 lines (189 loc) · 5.81 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
217
package auth
import (
"context"
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"io"
"net/http"
"github.com/dadrus/httpsig"
vocab "github.com/go-ap/activitypub"
"github.com/go-ap/client"
"github.com/go-ap/errors"
"github.com/go-ap/jsonld"
)
// LoadRemoteKey fetches a remote Public Key and returns it's owner.
func LoadRemoteKey(_ context.Context, c ActivityPubClient, iri vocab.IRI) (vocab.Actor, *vocab.PublicKey, error) {
return (&localRemoteLoader{c: c}).loadRemoteKey(iri)
}
type keyLoader interface {
loadKey(string) (vocab.Actor, *vocab.PublicKey, error)
}
type localRemoteLoader struct {
actor vocab.Actor
c ActivityPubClient
st readStore
}
var errEmptyIRI = errors.Newf("empty IRI")
func (k localRemoteLoader) loadRemoteKey(iri vocab.IRI) (vocab.Actor, *vocab.PublicKey, error) {
if k.c == nil {
return AnonymousActor, nil, errInvalidClient
}
if iri == "" {
return AnonymousActor, nil, errEmptyIRI
}
req, err := client.FetchRequest(context.Background(), string(iri), http.MethodGet)
if err != nil {
return AnonymousActor, nil, errors.Annotatef(err, "unable to create request: %s", iri)
}
resp, err := k.c.Do(req)
if err != nil {
return AnonymousActor, nil, errors.Annotatef(err, "unable to fetch key: %s", iri)
}
defer func() {
_ = resp.Body.Close()
}()
var body []byte
if body, err = io.ReadAll(resp.Body); err != nil {
return AnonymousActor, nil, err
}
switch resp.StatusCode {
case http.StatusGone:
return AnonymousActor, nil, errors.Gonef("key does not exist: %s", iri)
case http.StatusOK, http.StatusNotModified:
// OK
default:
if errb, _ := errors.UnmarshalJSON(body); len(errb) > 0 {
err = errors.AnnotateFromStatus(errors.Join(errb...), resp.StatusCode, "unable to fetch key: %s", iri)
} else {
if len(body) > 0 {
err = errors.AnnotateFromStatus(errors.Newf("%s", body[:min(512, len(body))]), resp.StatusCode, "unable to fetch key: %s", iri)
} else {
err = errors.NewFromStatus(resp.StatusCode, "unable to fetch key: %s", iri)
}
}
return AnonymousActor, nil, err
}
key := new(vocab.PublicKey)
act := vocab.Actor{}
// NOTE(marius): try to decode the response body as a PublicKey
if err = jsonld.Unmarshal(body, key); err != nil || key.Owner == vocab.EmptyIRI {
// NOTE(marius): then we try to decode the body as an Actor
err = jsonld.Unmarshal(body, &act)
}
// NOTE(marius): if we were unable to decode a PublicKey, nor an Actor that matches the IRI, we have failed.
if !(key.ID.Equal(iri) || act.ID.Equal(iri)) {
if err != nil {
err = errors.Annotatef(err, "unable to decode key or actor: %s", iri)
} else {
err = errors.Newf("unable to decode key or actor: %s", iri)
}
return AnonymousActor, nil, err
}
if act.ID.Equal(iri) {
key = &act.PublicKey
}
// NOTE(marius): we successfully loaded a PublicKey, we try to load the Actor from its Owner property
if key.ID.Equal(iri) {
// NOTE(marius): the SWICG document linked at the LoadActorFromIRIKey method mentions
// that we can use both key.Owner or key.Controller, however we don't have Controller
// in the PublicKey struct. We should probably change that.
it, err := k.c.LoadIRI(key.Owner)
if err != nil {
return AnonymousActor, key, errors.NewFromStatus(resp.StatusCode, "unable to fetch actor: %s", iri)
}
_ = vocab.OnActor(it, func(actor *vocab.Actor) error {
act = *actor
return nil
})
}
return act, key, nil
}
func (k localRemoteLoader) loadLocalKey(iri vocab.IRI) (vocab.Actor, *vocab.PublicKey, error) {
if k.st == nil {
return AnonymousActor, nil, errInvalidStorage
}
act := AnonymousActor
u, err := iri.URL()
if err != nil {
return act, nil, errors.Annotatef(err, "invalid URL to load: %s", iri)
}
if u.Fragment != "" {
u.Fragment = ""
iri = vocab.IRI(u.String())
}
var key *vocab.PublicKey
// NOTE(marius): in the case of the locally saved actors, we don't have *YET* public keys stored
// as independent objects.
// Therefore, there's no need to check if the IRI belongs to a Key object, and if that's the case
// then dereference the owner, as we do in the remote case.
it, err := k.st.Load(iri)
if err != nil {
return act, nil, err
}
err = vocab.OnActor(it, func(a *vocab.Actor) error {
act = *a
key = &a.PublicKey
return nil
})
return act, key, nil
}
func (k localRemoteLoader) loadKey(keyID string) (vocab.Actor, *vocab.PublicKey, error) {
// NOTE(marius): we first try to verify with the copy of the key stored locally if it exists.
actor, key, _ := k.loadLocalKey(vocab.IRI(keyID))
if key != nil {
return actor, key, nil
}
// NOTE(marius): if local verification fails, we try to fetch a fresh copy of the key and try again.
return k.loadRemoteKey(vocab.IRI(keyID))
}
func (k *localRemoteLoader) ResolveKey(_ context.Context, id string) (httpsig.Key, error) {
key := httpsig.Key{KeyID: id}
act, pub, err := k.loadKey(id)
if err != nil {
return key, err
}
k.actor = act
key.Algorithm, key.Key, err = rfcAlgorithmFromPublicKey(pub)
return key, err
}
func rfcAlgorithmFromPublicKey(pub *vocab.PublicKey) (httpsig.SignatureAlgorithm, crypto.PublicKey, error) {
pkey, err := toCryptoPublicKey(*pub)
if err != nil {
return "", nil, err
}
var key crypto.PublicKey
var alg httpsig.SignatureAlgorithm
switch pk := pkey.(type) {
case *rsa.PublicKey:
switch pk.Size() {
case 128, 256:
alg = httpsig.RsaPkcs1v15Sha256
case 384:
alg = httpsig.RsaPkcs1v15Sha384
case 512:
alg = httpsig.RsaPkcs1v15Sha512
}
key = pk
case *ecdsa.PublicKey:
if p := pk.Params(); p != nil {
switch p.BitSize {
case 128, 256:
alg = httpsig.EcdsaP256Sha256
case 384:
alg = httpsig.EcdsaP384Sha384
case 512:
alg = httpsig.EcdsaP521Sha512
}
}
key = pk
case ed25519.PublicKey:
alg = httpsig.Ed25519
key = pk
}
return alg, key, nil
}
func (k *localRemoteLoader) Actor() vocab.Actor {
return k.actor
}