2022-03-19 13:55:58 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2021-11-10 12:35:02 +00:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package activitypub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2022-06-14 04:10:36 +00:00
|
|
|
"strings"
|
2022-03-24 23:44:44 +00:00
|
|
|
"time"
|
2021-11-10 12:35:02 +00:00
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/activitypub"
|
|
|
|
gitea_context "code.gitea.io/gitea/modules/context"
|
2022-03-27 17:25:27 +00:00
|
|
|
"code.gitea.io/gitea/modules/httplib"
|
2021-11-10 12:35:02 +00:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-03-19 14:14:08 +00:00
|
|
|
|
2022-05-23 16:43:59 +00:00
|
|
|
ap "github.com/go-ap/activitypub"
|
2021-11-10 12:35:02 +00:00
|
|
|
"github.com/go-fed/httpsig"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getPublicKeyFromResponse(ctx context.Context, b []byte, keyID *url.URL) (p crypto.PublicKey, err error) {
|
2022-05-23 16:43:59 +00:00
|
|
|
person := ap.PersonNew(ap.IRI(keyID.String()))
|
|
|
|
err = person.UnmarshalJSON(b)
|
2021-11-10 12:35:02 +00:00
|
|
|
if err != nil {
|
2022-06-12 02:15:45 +00:00
|
|
|
err = fmt.Errorf("ActivityStreams type cannot be converted to one known to have publicKey property: %v", err)
|
2021-11-10 12:35:02 +00:00
|
|
|
return
|
|
|
|
}
|
2022-06-14 01:51:49 +00:00
|
|
|
pubKey := person.PublicKey
|
|
|
|
if pubKey.ID.String() != keyID.String() {
|
2021-11-10 12:35:02 +00:00
|
|
|
err = fmt.Errorf("cannot find publicKey with id: %s in %s", keyID, b)
|
|
|
|
return
|
|
|
|
}
|
2022-06-14 01:51:49 +00:00
|
|
|
pubKeyPem := pubKey.PublicKeyPem
|
2022-03-30 20:52:52 +00:00
|
|
|
block, _ := pem.Decode([]byte(pubKeyPem))
|
2021-11-10 12:35:02 +00:00
|
|
|
if block == nil || block.Type != "PUBLIC KEY" {
|
|
|
|
err = fmt.Errorf("could not decode publicKeyPem to PUBLIC KEY pem block type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p, err = x509.ParsePKIXPublicKey(block.Bytes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetch(iri *url.URL) (b []byte, err error) {
|
2022-03-30 20:52:52 +00:00
|
|
|
req := httplib.NewRequest(iri.String(), http.MethodGet)
|
2022-03-27 17:25:27 +00:00
|
|
|
req.Header("Accept", activitypub.ActivityStreamsContentType)
|
|
|
|
req.Header("Accept-Charset", "utf-8")
|
2022-06-14 04:10:36 +00:00
|
|
|
req.Header("Date", strings.ReplaceAll(time.Now().UTC().Format(time.RFC1123), "UTC", "GMT"))
|
2022-03-30 20:52:52 +00:00
|
|
|
resp, err := req.Response()
|
2021-11-10 12:35:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
err = fmt.Errorf("url IRI fetch [%s] failed with status (%d): %s", iri, resp.StatusCode, resp.Status)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b, err = io.ReadAll(resp.Body)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyHTTPSignatures(ctx *gitea_context.APIContext) (authenticated bool, err error) {
|
|
|
|
r := ctx.Req
|
|
|
|
|
|
|
|
// 1. Figure out what key we need to verify
|
2022-03-30 20:52:52 +00:00
|
|
|
v, err := httpsig.NewVerifier(r)
|
2021-11-10 12:35:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ID := v.KeyId()
|
2022-03-30 20:52:52 +00:00
|
|
|
idIRI, err := url.Parse(ID)
|
2021-11-10 12:35:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 2. Fetch the public key of the other actor
|
2022-03-30 20:52:52 +00:00
|
|
|
b, err := fetch(idIRI)
|
2021-11-10 12:35:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-06-14 01:51:49 +00:00
|
|
|
pubKey, err := getPublicKeyFromResponse(*ctx, b, idIRI)
|
2021-11-10 12:35:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// 3. Verify the other actor's key
|
|
|
|
algo := httpsig.Algorithm(setting.Federation.Algorithms[0])
|
2022-06-14 01:51:49 +00:00
|
|
|
authenticated = v.Verify(pubKey, algo) == nil
|
2021-11-10 12:35:02 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReqSignature function
|
|
|
|
func ReqSignature() func(ctx *gitea_context.APIContext) {
|
|
|
|
return func(ctx *gitea_context.APIContext) {
|
|
|
|
if authenticated, err := verifyHTTPSignatures(ctx); err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "verifyHttpSignatures", err)
|
|
|
|
} else if !authenticated {
|
|
|
|
ctx.Error(http.StatusForbidden, "reqSignature", "request signature verification failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|