Start adding ACLs
This commit is contained in:
32 files changed
+4385
No files matched your search
@@ -0,0 +1,255 @@
|
||||
package threadmodels
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/sha512"
|
||||
"crypto/subtle"
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/lib/strkey"
|
||||
"github.com/libp2p/go-libp2p-core/crypto"
|
||||
crypto_pb "github.com/libp2p/go-libp2p-core/crypto/pb"
|
||||
"io"
|
||||
)
|
||||
|
||||
type SigningPubKey crypto.PubKey
|
||||
type SigningPrivKey crypto.PrivKey
|
||||
|
||||
var MinRsaKeyBits = 2048
|
||||
|
||||
var ErrKeyLengthTooSmall = errors.New("error key length too small")
|
||||
|
||||
type Key interface {
|
||||
Equals(Key) bool
|
||||
|
||||
Raw() ([]byte, error)
|
||||
}
|
||||
|
||||
type EncryptionPrivKey interface {
|
||||
Key
|
||||
|
||||
Decrypt([]byte) ([]byte, error)
|
||||
GetPublic() EncryptionPubKey
|
||||
}
|
||||
|
||||
type EncryptionPubKey interface {
|
||||
Key
|
||||
|
||||
Encrypt(data []byte) ([]byte, error)
|
||||
}
|
||||
|
||||
type EncryptionRsaPrivKey struct {
|
||||
privKey rsa.PrivateKey
|
||||
}
|
||||
|
||||
type EncryptionRsaPubKey struct {
|
||||
pubKey rsa.PublicKey
|
||||
}
|
||||
|
||||
func (e *EncryptionRsaPubKey) Equals(key Key) bool {
|
||||
other, ok := (key).(*EncryptionRsaPubKey)
|
||||
if !ok {
|
||||
return keyEquals(e, key)
|
||||
}
|
||||
|
||||
return e.pubKey.N.Cmp(other.pubKey.N) == 0 && e.pubKey.E == other.pubKey.E
|
||||
}
|
||||
|
||||
func (e *EncryptionRsaPubKey) Raw() ([]byte, error) {
|
||||
return x509.MarshalPKIXPublicKey(&e.pubKey)
|
||||
}
|
||||
|
||||
func (e *EncryptionRsaPubKey) Encrypt(data []byte) ([]byte, error) {
|
||||
hash := sha512.New()
|
||||
return rsa.EncryptOAEP(hash, rand.Reader, &e.pubKey, data, nil)
|
||||
}
|
||||
|
||||
func (e *EncryptionRsaPrivKey) Equals(key Key) bool {
|
||||
other, ok := (key).(*EncryptionRsaPrivKey)
|
||||
if !ok {
|
||||
return keyEquals(e, key)
|
||||
}
|
||||
|
||||
return e.privKey.N.Cmp(other.privKey.N) == 0 && e.privKey.E == other.privKey.E
|
||||
}
|
||||
|
||||
func (e *EncryptionRsaPrivKey) Raw() ([]byte, error) {
|
||||
b := x509.MarshalPKCS1PrivateKey(&e.privKey)
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (e *EncryptionRsaPrivKey) Decrypt(bytes []byte) ([]byte, error) {
|
||||
hash := sha512.New()
|
||||
return rsa.DecryptOAEP(hash, rand.Reader, &e.privKey, bytes, nil)
|
||||
}
|
||||
|
||||
func (e *EncryptionRsaPrivKey) GetPublic() EncryptionPubKey {
|
||||
return &EncryptionRsaPubKey{pubKey: e.privKey.PublicKey}
|
||||
}
|
||||
|
||||
func GenerateRandomRSAKeyPair(bits int) (EncryptionPrivKey, EncryptionPubKey, error) {
|
||||
return GenerateRSAKeyPair(bits, rand.Reader)
|
||||
}
|
||||
|
||||
func GenerateRSAKeyPair(bits int, src io.Reader) (EncryptionPrivKey, EncryptionPubKey, error) {
|
||||
if bits < MinRsaKeyBits {
|
||||
return nil, nil, ErrKeyLengthTooSmall
|
||||
}
|
||||
priv, err := rsa.GenerateKey(src, bits)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
pk := priv.PublicKey
|
||||
return &EncryptionRsaPrivKey{privKey: *priv}, &EncryptionRsaPubKey{pubKey: pk}, nil
|
||||
}
|
||||
|
||||
func NewEncryptionRsaPrivKeyFromBytes(bytes []byte) (EncryptionPrivKey, error) {
|
||||
sk, err := x509.ParsePKCS1PrivateKey(bytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if sk.N.BitLen() < MinRsaKeyBits {
|
||||
return nil, ErrKeyLengthTooSmall
|
||||
}
|
||||
return &EncryptionRsaPrivKey{privKey: *sk}, nil
|
||||
}
|
||||
|
||||
func NewEncryptionRsaPubKeyFromBytes(bytes []byte) (EncryptionPubKey, error) {
|
||||
pub, err := x509.ParsePKIXPublicKey(bytes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pk, ok := pub.(*rsa.PublicKey)
|
||||
if !ok {
|
||||
return nil, errors.New("not actually an rsa public key")
|
||||
}
|
||||
if pk.N.BitLen() < MinRsaKeyBits {
|
||||
return nil, ErrKeyLengthTooSmall
|
||||
}
|
||||
|
||||
return &EncryptionRsaPubKey{pubKey: *pk}, nil
|
||||
}
|
||||
|
||||
func NewSigningEd25519PubKeyFromBytes(bytes []byte) (SigningPubKey, error) {
|
||||
return crypto.UnmarshalEd25519PublicKey(bytes)
|
||||
}
|
||||
|
||||
func GenerateRandomEd25519KeyPair() (SigningPrivKey, SigningPubKey, error) {
|
||||
return crypto.GenerateEd25519Key(rand.Reader)
|
||||
}
|
||||
|
||||
func keyEquals(k1, k2 Key) bool {
|
||||
a, err := k1.Raw()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
b, err := k2.Raw()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return subtle.ConstantTimeCompare(a, b) == 1
|
||||
}
|
||||
|
||||
type Ed25519SigningPubKeyDecoder struct{}
|
||||
|
||||
func NewEd25519Decoder() *Ed25519SigningPubKeyDecoder {
|
||||
return &Ed25519SigningPubKeyDecoder{}
|
||||
}
|
||||
|
||||
func (e *Ed25519SigningPubKeyDecoder) DecodeFromBytes(bytes []byte) (SigningPubKey, error) {
|
||||
return NewSigningEd25519PubKeyFromBytes(bytes)
|
||||
}
|
||||
|
||||
func (e *Ed25519SigningPubKeyDecoder) DecodeFromString(identity string) (SigningPubKey, error) {
|
||||
pubKeyRaw, err := strkey.Decode(0x5b, identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return e.DecodeFromBytes(pubKeyRaw)
|
||||
}
|
||||
|
||||
func (e *Ed25519SigningPubKeyDecoder) DecodeFromStringIntoBytes(identity string) ([]byte, error) {
|
||||
return strkey.Decode(0x5b, identity)
|
||||
}
|
||||
|
||||
func (e *Ed25519SigningPubKeyDecoder) EncodeToString(pubkey crypto.PubKey) (string, error) {
|
||||
raw, err := pubkey.Raw()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strkey.Encode(0x5b, raw)
|
||||
}
|
||||
|
||||
type SigningPubKeyDecoder interface {
|
||||
DecodeFromBytes(bytes []byte) (SigningPubKey, error)
|
||||
DecodeFromString(identity string) (SigningPubKey, error)
|
||||
DecodeFromStringIntoBytes(identity string) ([]byte, error)
|
||||
}
|
||||
|
||||
// Below keys are required for testing and mocking purposes
|
||||
|
||||
type EmptyRecorderEncryptionKey struct {
|
||||
recordedEncrypted [][]byte
|
||||
recordedDecrypted [][]byte
|
||||
}
|
||||
|
||||
func (f *EmptyRecorderEncryptionKey) Equals(key Key) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (f *EmptyRecorderEncryptionKey) Raw() ([]byte, error) {
|
||||
panic("can't get bytes from this key")
|
||||
}
|
||||
|
||||
func (f *EmptyRecorderEncryptionKey) GetPublic() EncryptionPubKey {
|
||||
panic("this key doesn't have a public key")
|
||||
}
|
||||
|
||||
func (f *EmptyRecorderEncryptionKey) Encrypt(msg []byte) ([]byte, error) {
|
||||
f.recordedEncrypted = append(f.recordedEncrypted, msg)
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func (f *EmptyRecorderEncryptionKey) Decrypt(msg []byte) ([]byte, error) {
|
||||
f.recordedDecrypted = append(f.recordedDecrypted, msg)
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
type SignatureVerificationPayload struct {
|
||||
message []byte
|
||||
signature []byte
|
||||
}
|
||||
|
||||
type EmptyRecorderVerificationKey struct {
|
||||
verifications []SignatureVerificationPayload
|
||||
}
|
||||
|
||||
func (e *EmptyRecorderVerificationKey) Bytes() ([]byte, error) {
|
||||
panic("can't get bytes from this key")
|
||||
}
|
||||
|
||||
func (e *EmptyRecorderVerificationKey) Equals(key crypto.Key) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (e *EmptyRecorderVerificationKey) Raw() ([]byte, error) {
|
||||
panic("can't get bytes from this key")
|
||||
}
|
||||
|
||||
func (e *EmptyRecorderVerificationKey) Type() crypto_pb.KeyType {
|
||||
panic("can't get type from this key")
|
||||
}
|
||||
|
||||
func (e *EmptyRecorderVerificationKey) Verify(data []byte, sig []byte) (bool, error) {
|
||||
e.verifications = append(e.verifications, SignatureVerificationPayload{
|
||||
message: data,
|
||||
signature: sig,
|
||||
})
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func NewMockSigningPubKeyFromBytes(bytes []byte) (SigningPubKey, error) {
|
||||
return &EmptyRecorderVerificationKey{}, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package threadmodels
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogo/protobuf/proto"
|
||||
)
|
||||
|
||||
type Thread interface {
|
||||
ID() string
|
||||
GetLogs() ([]ThreadLog, error)
|
||||
GetRecord(ctx context.Context, recordID string) (*ThreadRecord, error)
|
||||
PushRecord(payload proto.Marshaler) (id string, err error)
|
||||
|
||||
// SubscribeForRecords()
|
||||
}
|
||||
|
||||
type SignedPayload struct {
|
||||
Payload []byte
|
||||
Signature []byte
|
||||
}
|
||||
|
||||
type ThreadRecord struct {
|
||||
PrevId string
|
||||
Id string
|
||||
LogId string
|
||||
Signed *SignedPayload
|
||||
}
|
||||
|
||||
type ThreadLog struct {
|
||||
ID string
|
||||
Head string
|
||||
Counter int64
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package threadmodels
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/lib/core/smartblock"
|
||||
"github.com/textileio/go-threads/core/thread"
|
||||
"hash/fnv"
|
||||
)
|
||||
|
||||
func CreateACLThreadID(k SigningPubKey, blockType smartblock.SmartBlockType) (thread.ID, error) {
|
||||
rndlen := 32
|
||||
buf := make([]byte, 8+rndlen)
|
||||
|
||||
// adding random bytes in the end
|
||||
_, err := rand.Read(buf[8 : 8+rndlen])
|
||||
if err != nil {
|
||||
panic("random read failed")
|
||||
}
|
||||
|
||||
keyBytes, err := k.Bytes()
|
||||
if err != nil {
|
||||
return thread.Undef, err
|
||||
}
|
||||
|
||||
hasher := fnv.New64()
|
||||
hasher.Write(keyBytes)
|
||||
res := hasher.Sum64()
|
||||
|
||||
// putting hash of the pubkey in the beginning
|
||||
binary.LittleEndian.PutUint64(buf[:8], res)
|
||||
|
||||
return threadIDFromBytes(blockType, buf)
|
||||
}
|
||||
|
||||
func VerifyACLThreadID(k SigningPubKey, threadId thread.ID) (bool, error) {
|
||||
bytes := threadId.Bytes()
|
||||
pubKeyBytes := threadId.Bytes()[len(bytes)-40 : len(bytes)-32]
|
||||
hash := binary.LittleEndian.Uint64(pubKeyBytes)
|
||||
|
||||
keyBytes, err := k.Bytes()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
hasher := fnv.New64()
|
||||
hasher.Write(keyBytes)
|
||||
realHash := hasher.Sum64()
|
||||
|
||||
return hash == realHash, nil
|
||||
}
|
||||
|
||||
func threadIDFromBytes(
|
||||
blockType smartblock.SmartBlockType,
|
||||
b []byte) (thread.ID, error) {
|
||||
blen := len(b)
|
||||
|
||||
// two 8 bytes (max) numbers plus num
|
||||
buf := make([]byte, 2*binary.MaxVarintLen64+blen)
|
||||
n := binary.PutUvarint(buf, thread.V1)
|
||||
n += binary.PutUvarint(buf[n:], uint64(thread.AccessControlled))
|
||||
n += binary.PutUvarint(buf[n:], uint64(blockType))
|
||||
|
||||
cn := copy(buf[n:], b)
|
||||
if cn != blen {
|
||||
return thread.Undef, fmt.Errorf("copy length is inconsistent")
|
||||
}
|
||||
|
||||
return thread.Cast(buf[:n+blen])
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package threadmodels
|
||||
|
||||
import (
|
||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/pkg/lib/core/smartblock"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateACLThreadIDVerify(t *testing.T) {
|
||||
_, pubKey, err := GenerateRandomEd25519KeyPair()
|
||||
if err != nil {
|
||||
t.Fatalf("should not return error after generating key pair: %v", err)
|
||||
}
|
||||
|
||||
thread, err := CreateACLThreadID(pubKey, smartblock.SmartBlockTypeWorkspace)
|
||||
if err != nil {
|
||||
t.Fatalf("should not return error after generating thread: %v", err)
|
||||
}
|
||||
|
||||
verified, err := VerifyACLThreadID(pubKey, thread)
|
||||
if err != nil {
|
||||
t.Fatalf("verification should not return error: %v", err)
|
||||
}
|
||||
|
||||
if !verified {
|
||||
t.Fatalf("the thread should be verified")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user