Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
Sarah Jamie Lewis | 8369ae605b | |
Sarah Jamie Lewis | 1066862f58 | |
Sarah Jamie Lewis | a2cfcaf8a4 |
76
.drone.yml
76
.drone.yml
|
@ -1,45 +1,34 @@
|
||||||
---
|
workspace:
|
||||||
kind: pipeline
|
base: /go
|
||||||
type: docker
|
path: src/cwtch.im/tapir
|
||||||
name: linux-test
|
|
||||||
|
|
||||||
steps:
|
pipeline:
|
||||||
- name: fetch
|
fetch:
|
||||||
image: golang:1.17.5
|
image: golang
|
||||||
volumes:
|
|
||||||
- name: deps
|
|
||||||
path: /go
|
|
||||||
commands:
|
commands:
|
||||||
- wget https://git.openprivacy.ca/openprivacy/buildfiles/raw/master/tor/tor
|
- wget https://git.openprivacy.ca/openprivacy/buildfiles/raw/master/tor/tor
|
||||||
- wget https://git.openprivacy.ca/openprivacy/buildfiles/raw/master/tor/torrc
|
- wget https://git.openprivacy.ca/openprivacy/buildfiles/raw/master/tor/torrc
|
||||||
- chmod a+x tor
|
- chmod a+x tor
|
||||||
- export GO111MODULE=on
|
- export GO111MODULE=on
|
||||||
- go mod download
|
- go mod vendor
|
||||||
- go install honnef.co/go/tools/cmd/staticcheck@latest
|
- go get -u golang.org/x/lint/golint
|
||||||
- name: quality
|
quality:
|
||||||
image: golang:1.17.5
|
image: golang
|
||||||
volumes:
|
|
||||||
- name: deps
|
|
||||||
path: /go
|
|
||||||
commands:
|
commands:
|
||||||
- staticcheck ./...
|
- go list ./... | xargs go vet
|
||||||
- name: units-tests
|
- go list ./... | xargs golint -set_exit_status
|
||||||
image: golang:1.17.5
|
units-tests:
|
||||||
volumes:
|
image: golang
|
||||||
- name: deps
|
|
||||||
path: /go
|
|
||||||
commands:
|
commands:
|
||||||
- export PATH=`pwd`:$PATH
|
- export PATH=$PATH:/go/src/cwtch.im/tapir
|
||||||
- sh testing/tests.sh
|
- sh testing/tests.sh
|
||||||
- name: integ-test
|
integ-test:
|
||||||
image: golang:1.17.5
|
image: golang
|
||||||
volumes:
|
|
||||||
- name: deps
|
|
||||||
path: /go
|
|
||||||
commands:
|
commands:
|
||||||
- export PATH=`pwd`:$PATH
|
- ./tor -f ./torrc
|
||||||
- go test -race -v git.openprivacy.ca/cwtch.im/tapir/testing
|
- sleep 15
|
||||||
- name: notify-email
|
- go test -v cwtch.im/tapir/testing
|
||||||
|
notify-email:
|
||||||
image: drillster/drone-email
|
image: drillster/drone-email
|
||||||
host: build.openprivacy.ca
|
host: build.openprivacy.ca
|
||||||
port: 25
|
port: 25
|
||||||
|
@ -47,27 +36,10 @@ steps:
|
||||||
from: drone@openprivacy.ca
|
from: drone@openprivacy.ca
|
||||||
when:
|
when:
|
||||||
status: [ failure ]
|
status: [ failure ]
|
||||||
- name: notify-gogs
|
notify-gogs:
|
||||||
image: openpriv/drone-gogs
|
image: openpriv/drone-gogs
|
||||||
pull: if-not-exists
|
|
||||||
when:
|
when:
|
||||||
event: pull_request
|
event: pull_request
|
||||||
status: [ success, changed, failure ]
|
status: [ success, changed, failure ]
|
||||||
environment:
|
secrets: [gogs_account_token]
|
||||||
GOGS_ACCOUNT_TOKEN:
|
gogs_url: https://git.openprivacy.ca
|
||||||
from_secret: gogs_account_token
|
|
||||||
settings:
|
|
||||||
gogs_url: https://git.openprivacy.ca
|
|
||||||
|
|
||||||
volumes:
|
|
||||||
# gopath where bin and pkg lives to persist across steps
|
|
||||||
- name: deps
|
|
||||||
temp: {}
|
|
||||||
|
|
||||||
trigger:
|
|
||||||
repo: cwtch.im/tapir
|
|
||||||
branch: master
|
|
||||||
event:
|
|
||||||
- push
|
|
||||||
- pull_request
|
|
||||||
- tag
|
|
||||||
|
|
|
@ -5,6 +5,3 @@ coverage.out
|
||||||
/testing/tor/
|
/testing/tor/
|
||||||
/applications/tor/
|
/applications/tor/
|
||||||
*.db
|
*.db
|
||||||
/applications/tokenboard/tor/
|
|
||||||
fuzzing/
|
|
||||||
*.cover.out
|
|
||||||
|
|
8
LICENSE
8
LICENSE
|
@ -1,8 +0,0 @@
|
||||||
MIT License
|
|
||||||
Copyright (c) 2019 Open Privacy Research Society
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
@ -1,7 +1,7 @@
|
||||||
package tapir
|
package tapir
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/core"
|
"cwtch.im/tapir/primitives/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Capability defines a status granted to a connection, from an application. That allows the connection to access
|
// Capability defines a status granted to a connection, from an application. That allows the connection to access
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package applications
|
package applications
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.openprivacy.ca/cwtch.im/tapir"
|
"cwtch.im/tapir"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ApplicationChain is a meta-app that can be used to build complex applications from other applications
|
// ApplicationChain is a meta-app that can be used to build complex applications from other applications
|
||||||
|
@ -40,17 +40,13 @@ func (appchain *ApplicationChain) NewInstance() tapir.Application {
|
||||||
func (appchain *ApplicationChain) Init(connection tapir.Connection) {
|
func (appchain *ApplicationChain) Init(connection tapir.Connection) {
|
||||||
appchain.TranscriptApp.Init(connection)
|
appchain.TranscriptApp.Init(connection)
|
||||||
for i, app := range appchain.apps {
|
for i, app := range appchain.apps {
|
||||||
// propagate the transcript to the app
|
|
||||||
app.PropagateTranscript(appchain.transcript)
|
app.PropagateTranscript(appchain.transcript)
|
||||||
// apply the app to the connection
|
|
||||||
connection.SetApp(app)
|
|
||||||
// initialize the application given the connection
|
|
||||||
app.Init(connection)
|
app.Init(connection)
|
||||||
// if we hit our guard then carry on, otherwise close...
|
if connection.HasCapability(appchain.capabilities[i]) == false {
|
||||||
if !connection.HasCapability(appchain.capabilities[i]) {
|
|
||||||
connection.Close()
|
connection.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
connection.SetApp(app)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,11 @@ package applications
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/subtle"
|
"crypto/subtle"
|
||||||
|
"cwtch.im/tapir"
|
||||||
|
"cwtch.im/tapir/primitives"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives"
|
"git.openprivacy.ca/openprivacy/libricochet-go/utils"
|
||||||
torProvider "git.openprivacy.ca/openprivacy/connectivity/tor"
|
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
|
||||||
"golang.org/x/crypto/ed25519"
|
"golang.org/x/crypto/ed25519"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -49,26 +49,21 @@ func (ea *AuthApp) Init(connection tapir.Connection) {
|
||||||
|
|
||||||
// If we are an outbound connection we can perform an additional check to ensure that the server sent us back the correct long term
|
// If we are an outbound connection we can perform an additional check to ensure that the server sent us back the correct long term
|
||||||
// public key
|
// public key
|
||||||
if connection.IsOutbound() && torProvider.GetTorV3Hostname(remoteAuthMessage.LongTermPublicKey) != connection.Hostname() {
|
if connection.IsOutbound() && utils.GetTorV3Hostname(remoteAuthMessage.LongTermPublicKey) != connection.Hostname() {
|
||||||
log.Errorf("The remote server (%v) has attempted to authenticate with a different public key %v", connection.Hostname(), torProvider.GetTorV3Hostname(remoteAuthMessage.LongTermPublicKey))
|
log.Errorf("The remote server (%v) has attempted to authenticate with a different public key %v", connection.Hostname(), utils.GetTorV3Hostname(remoteAuthMessage.LongTermPublicKey))
|
||||||
connection.Close()
|
connection.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Perform the triple-diffie-hellman exchange.
|
// Perform the triple-diffie-hellman exchange.
|
||||||
key, err := primitives.Perform3DH(connection.ID(), &ephemeralIdentity, remoteAuthMessage.LongTermPublicKey, remoteAuthMessage.EphemeralPublicKey, connection.IsOutbound())
|
key := primitives.Perform3DH(connection.ID(), &ephemeralIdentity, remoteAuthMessage.LongTermPublicKey, remoteAuthMessage.EphemeralPublicKey, connection.IsOutbound())
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Failed Auth Challenge %v", err)
|
|
||||||
connection.Close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
connection.SetEncryptionKey(key)
|
connection.SetEncryptionKey(key)
|
||||||
|
|
||||||
// We just successfully unmarshaled both of these, so we can safely ignore the err return from these functions.
|
// We just successfully unmarshaled both of these, so we can safely ignore the err return from these functions.
|
||||||
challengeRemote, _ := json.Marshal(remoteAuthMessage)
|
challengeRemote, _ := json.Marshal(remoteAuthMessage)
|
||||||
challengeLocal, _ := json.Marshal(authMessage)
|
challengeLocal, _ := json.Marshal(authMessage)
|
||||||
|
|
||||||
// Define canonical labels so both sides of the connection can generate the same key
|
// Define canonical labels so both sides of the
|
||||||
var outboundAuthMessage []byte
|
var outboundAuthMessage []byte
|
||||||
var outboundHostname string
|
var outboundHostname string
|
||||||
var inboundAuthMessage []byte
|
var inboundAuthMessage []byte
|
||||||
|
@ -76,11 +71,11 @@ func (ea *AuthApp) Init(connection tapir.Connection) {
|
||||||
|
|
||||||
if connection.IsOutbound() {
|
if connection.IsOutbound() {
|
||||||
outboundHostname = connection.ID().Hostname()
|
outboundHostname = connection.ID().Hostname()
|
||||||
inboundHostname = torProvider.GetTorV3Hostname(remoteAuthMessage.LongTermPublicKey)
|
inboundHostname = utils.GetTorV3Hostname(remoteAuthMessage.LongTermPublicKey)
|
||||||
outboundAuthMessage = challengeLocal
|
outboundAuthMessage = challengeLocal
|
||||||
inboundAuthMessage = challengeRemote
|
inboundAuthMessage = challengeRemote
|
||||||
} else {
|
} else {
|
||||||
outboundHostname = torProvider.GetTorV3Hostname(remoteAuthMessage.LongTermPublicKey)
|
outboundHostname = utils.GetTorV3Hostname(remoteAuthMessage.LongTermPublicKey)
|
||||||
inboundHostname = connection.ID().Hostname()
|
inboundHostname = connection.ID().Hostname()
|
||||||
outboundAuthMessage = challengeRemote
|
outboundAuthMessage = challengeRemote
|
||||||
inboundAuthMessage = challengeLocal
|
inboundAuthMessage = challengeLocal
|
||||||
|
@ -101,19 +96,17 @@ func (ea *AuthApp) Init(connection tapir.Connection) {
|
||||||
|
|
||||||
// Since we have set the encryption key on the connection the connection will encrypt any messages we send with that key
|
// Since we have set the encryption key on the connection the connection will encrypt any messages we send with that key
|
||||||
// To test that the remote peer has done the same we calculate a challenge hash based on the transcript so far and send it to them
|
// To test that the remote peer has done the same we calculate a challenge hash based on the transcript so far and send it to them
|
||||||
// along with our hostname
|
|
||||||
// We expect the remote to do the same, and compare the two.
|
// We expect the remote to do the same, and compare the two.
|
||||||
// If successful we extend our auth capability to the connection and reassert the hostname.
|
// If successful we extend our auth capability to the connection and reassert the hostname.
|
||||||
// We note that the only successful scenario here requires that the remote peer have successfully derived the same
|
// We note that the only successful scenario here requires that the remote peer have successfully derived the same
|
||||||
// encryption key and the same transcript challenge.
|
// encryption key and the same transcript challenge.
|
||||||
connection.Send(append(challengeBytes, []byte(connection.ID().Hostname())...))
|
connection.Send(challengeBytes)
|
||||||
remoteChallenge := connection.Expect()
|
remoteChallenge := connection.Expect()
|
||||||
assertedHostname := torProvider.GetTorV3Hostname(remoteAuthMessage.LongTermPublicKey)
|
if subtle.ConstantTimeCompare(challengeBytes, remoteChallenge) == 1 {
|
||||||
if subtle.ConstantTimeCompare(append(challengeBytes, []byte(assertedHostname)...), remoteChallenge) == 1 {
|
connection.SetHostname(utils.GetTorV3Hostname(remoteAuthMessage.LongTermPublicKey))
|
||||||
connection.SetHostname(assertedHostname)
|
|
||||||
connection.SetCapability(AuthCapability)
|
connection.SetCapability(AuthCapability)
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("Failed Decrypt Challenge: [%x] [%x]\n", remoteChallenge, challengeBytes)
|
log.Errorf("Failed Decrypt Challenge: [%x] [%x]\n", remoteChallenge, challengeBytes)
|
||||||
connection.Close()
|
connection.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,9 +2,9 @@ package applications
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"cwtch.im/tapir"
|
||||||
|
"cwtch.im/tapir/primitives"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir"
|
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives"
|
|
||||||
"golang.org/x/crypto/ed25519"
|
"golang.org/x/crypto/ed25519"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -17,6 +17,7 @@ type MockConnection struct {
|
||||||
func (mc *MockConnection) Init(outbound bool) {
|
func (mc *MockConnection) Init(outbound bool) {
|
||||||
mc.id, _ = primitives.InitializeEphemeralIdentity()
|
mc.id, _ = primitives.InitializeEphemeralIdentity()
|
||||||
mc.outbound = outbound
|
mc.outbound = outbound
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mc MockConnection) Hostname() string {
|
func (mc MockConnection) Hostname() string {
|
||||||
|
@ -58,9 +59,8 @@ func (MockConnection) SetEncryptionKey(key [32]byte) {
|
||||||
// no op
|
// no op
|
||||||
}
|
}
|
||||||
|
|
||||||
func (MockConnection) Send(message []byte) error {
|
func (MockConnection) Send(message []byte) {
|
||||||
// no op
|
// no op
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (MockConnection) Close() {
|
func (MockConnection) Close() {
|
||||||
|
@ -80,10 +80,6 @@ func (MockConnection) IsClosed() bool {
|
||||||
panic("implement me")
|
panic("implement me")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (MockConnection) Broadcast(message []byte, capability tapir.Capability) error {
|
|
||||||
panic("implement me")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAuthApp_Failed(t *testing.T) {
|
func TestAuthApp_Failed(t *testing.T) {
|
||||||
var authApp AuthApp
|
var authApp AuthApp
|
||||||
ai := authApp.NewInstance()
|
ai := authApp.NewInstance()
|
||||||
|
|
|
@ -2,10 +2,9 @@ package applications
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir"
|
"cwtch.im/tapir"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/core"
|
"cwtch.im/tapir/primitives/core"
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
ristretto "github.com/gtank/ristretto255"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ProofOfWorkApplication forces the incoming connection to do proof of work before granting a capability
|
// ProofOfWorkApplication forces the incoming connection to do proof of work before granting a capability
|
||||||
|
@ -67,20 +66,9 @@ func (powapp *ProofOfWorkApplication) solveChallenge(challenge []byte, prng core
|
||||||
var sum [32]byte
|
var sum [32]byte
|
||||||
solution := []byte{}
|
solution := []byte{}
|
||||||
solve := make([]byte, len(challenge)+32)
|
solve := make([]byte, len(challenge)+32)
|
||||||
// reuse our allocation
|
|
||||||
buf := make([]byte, 64)
|
|
||||||
next := new(ristretto.Scalar)
|
|
||||||
encodedSolution := make([]byte, 0, 32)
|
|
||||||
for !solved {
|
for !solved {
|
||||||
err := prng.Next(buf, next)
|
|
||||||
if err != nil {
|
|
||||||
// this will cause the challenge to fail...
|
|
||||||
log.Errorf("error completing challenge: %v", err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//lint:ignore SA1019 API this is "deprecated", but without it it will cause an allocation on every single check
|
solution = prng.Next().Bytes()
|
||||||
solution = next.Encode(encodedSolution)
|
|
||||||
|
|
||||||
copy(solve[0:], solution[:])
|
copy(solve[0:], solution[:])
|
||||||
copy(solve[len(solution):], challenge[:])
|
copy(solve[len(solution):], challenge[:])
|
||||||
|
@ -92,8 +80,6 @@ func (powapp *ProofOfWorkApplication) solveChallenge(challenge []byte, prng core
|
||||||
solved = false
|
solved = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// reuse this allocated memory next time...
|
|
||||||
encodedSolution = encodedSolution[:0]
|
|
||||||
}
|
}
|
||||||
log.Debugf("Validated Challenge %v: %v %v\n", challenge, solution, sum)
|
log.Debugf("Validated Challenge %v: %v %v\n", challenge, solution, sum)
|
||||||
return solution[:]
|
return solution[:]
|
||||||
|
@ -101,9 +87,6 @@ func (powapp *ProofOfWorkApplication) solveChallenge(challenge []byte, prng core
|
||||||
|
|
||||||
// ValidateChallenge returns true if the message and spamguard pass the challenge
|
// ValidateChallenge returns true if the message and spamguard pass the challenge
|
||||||
func (powapp *ProofOfWorkApplication) validateChallenge(challenge []byte, solution []byte) bool {
|
func (powapp *ProofOfWorkApplication) validateChallenge(challenge []byte, solution []byte) bool {
|
||||||
if len(solution) != 32 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
solve := make([]byte, len(challenge)+32)
|
solve := make([]byte, len(challenge)+32)
|
||||||
copy(solve[0:], solution[0:32])
|
copy(solve[0:], solution[0:32])
|
||||||
copy(solve[32:], challenge[:])
|
copy(solve[32:], challenge[:])
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
package applications
|
package applications
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cwtch.im/tapir"
|
||||||
|
"cwtch.im/tapir/primitives/privacypass"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/privacypass"
|
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TokenApplication provides Tokens for PoW
|
// TokenApplication provides Tokens for PoW
|
||||||
|
@ -16,52 +16,41 @@ type TokenApplication struct {
|
||||||
|
|
||||||
// HasTokensCapability is granted once the client has obtained signed tokens
|
// HasTokensCapability is granted once the client has obtained signed tokens
|
||||||
const HasTokensCapability = tapir.Capability("HasTokensCapability")
|
const HasTokensCapability = tapir.Capability("HasTokensCapability")
|
||||||
const numTokens = 10
|
|
||||||
|
|
||||||
// NewInstance should always return a new instantiation of the application.
|
// NewInstance should always return a new instantiation of the application.
|
||||||
func (tokenapp *TokenApplication) NewInstance() tapir.Application {
|
func (powapp *TokenApplication) NewInstance() tapir.Application {
|
||||||
app := new(TokenApplication)
|
app := new(TokenApplication)
|
||||||
app.TokenService = tokenapp.TokenService
|
app.TokenService = powapp.TokenService
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init is run when the connection is first started.
|
// Init is run when the connection is first started.
|
||||||
func (tokenapp *TokenApplication) Init(connection tapir.Connection) {
|
func (powapp *TokenApplication) Init(connection tapir.Connection) {
|
||||||
tokenapp.Transcript().NewProtocol("token-app")
|
powapp.Transcript().NewProtocol("token-app")
|
||||||
log.Debugf(tokenapp.Transcript().OutputTranscriptToAudit())
|
|
||||||
if connection.IsOutbound() {
|
if connection.IsOutbound() {
|
||||||
tokens, blinded := privacypass.GenerateBlindedTokenBatch(numTokens)
|
tokens, blinded := privacypass.GenerateBlindedTokenBatch(10)
|
||||||
data, _ := json.Marshal(blinded)
|
data, _ := json.Marshal(blinded)
|
||||||
connection.Send(data)
|
connection.Send(data)
|
||||||
var signedBatch privacypass.SignedBatchWithProof
|
var signedBatch privacypass.SignedBatchWithProof
|
||||||
err := json.Unmarshal(connection.Expect(), &signedBatch)
|
err := json.Unmarshal(connection.Expect(), &signedBatch)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
verified := privacypass.UnblindSignedTokenBatch(tokens, blinded, signedBatch.SignedTokens, tokenapp.TokenService.Y, signedBatch.Proof, tokenapp.Transcript())
|
verified := privacypass.UnblindSignedTokenBatch(tokens, blinded, signedBatch.SignedTokens, powapp.TokenService.Y, signedBatch.Proof, powapp.Transcript())
|
||||||
if verified {
|
if verified {
|
||||||
log.Debugf("Successfully obtained signed tokens")
|
log.Debugf("Successfully obtained signed tokens")
|
||||||
tokenapp.Tokens = tokens
|
powapp.Tokens = tokens
|
||||||
connection.SetCapability(HasTokensCapability)
|
connection.SetCapability(HasTokensCapability)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// This will close the connection by default and no tokens will be available.
|
|
||||||
// This usecase can be checked by the existing WaitForCapabilityOrClose() function using the HasTokensCapability
|
|
||||||
// If the connection closes without the HasTokensCapability then the error can be handled by whatever client needs it
|
|
||||||
log.Debugf("Failed to verify signed token batch")
|
log.Debugf("Failed to verify signed token batch")
|
||||||
}
|
}
|
||||||
return
|
} else {
|
||||||
}
|
var blinded []privacypass.BlindedToken
|
||||||
|
err := json.Unmarshal(connection.Expect(), &blinded)
|
||||||
// We are the server
|
if err == nil {
|
||||||
var blinded []privacypass.BlindedToken
|
batchProof := powapp.TokenService.SignBlindedTokenBatch(blinded, powapp.Transcript())
|
||||||
err := json.Unmarshal(connection.Expect(), &blinded)
|
data, _ := json.Marshal(batchProof)
|
||||||
if err == nil {
|
connection.Send(data)
|
||||||
batchProof, err := tokenapp.TokenService.SignBlindedTokenBatch(blinded, tokenapp.Transcript())
|
|
||||||
if err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Debugf(tokenapp.Transcript().OutputTranscriptToAudit())
|
|
||||||
data, _ := json.Marshal(batchProof)
|
|
||||||
connection.Send(data)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
package tokenboard
|
package tokenboard
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cwtch.im/tapir"
|
||||||
|
"cwtch.im/tapir/applications"
|
||||||
|
"cwtch.im/tapir/primitives/auditable"
|
||||||
|
"cwtch.im/tapir/primitives/privacypass"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/applications"
|
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/auditable"
|
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/privacypass"
|
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewTokenBoardClient generates a new Client for Token Board
|
// NewTokenBoardClient generates a new Client for Token Board
|
||||||
|
@ -60,6 +60,7 @@ func (ta *Client) Listen() {
|
||||||
|
|
||||||
var message Message
|
var message Message
|
||||||
json.Unmarshal(data, &message)
|
json.Unmarshal(data, &message)
|
||||||
|
log.Debugf("Received a Message: %v", message)
|
||||||
switch message.MessageType {
|
switch message.MessageType {
|
||||||
case postResultMessage:
|
case postResultMessage:
|
||||||
log.Debugf("Post result: %x", message.PostResult.Proof)
|
log.Debugf("Post result: %x", message.PostResult.Proof)
|
||||||
|
@ -100,7 +101,7 @@ func (ta *Client) PurchaseTokens() {
|
||||||
|
|
||||||
// Post sends a Post Request to the server
|
// Post sends a Post Request to the server
|
||||||
func (ta *Client) Post(message auditable.Message) bool {
|
func (ta *Client) Post(message auditable.Message) bool {
|
||||||
token, err := ta.paymentHandler.NextToken(message, ta.connection.Hostname())
|
token, err := ta.paymentHandler.NextToken(message)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
data, _ := json.Marshal(Message{MessageType: postRequestMessage, PostRequest: postRequest{Token: token, Message: message}})
|
data, _ := json.Marshal(Message{MessageType: postRequestMessage, PostRequest: postRequest{Token: token, Message: message}})
|
||||||
ta.connection.Send(data)
|
ta.connection.Send(data)
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
package tokenboard
|
package tokenboard
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/auditable"
|
"cwtch.im/tapir/primitives/auditable"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/privacypass"
|
"cwtch.im/tapir/primitives/privacypass"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AppHandler allows clients to react to specific events.
|
// AppHandler allows clients to react to specific events.
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
package tokenboard
|
package tokenboard
|
||||||
|
|
||||||
// NOTE: This is a sketch implementation, Not suitable for production use. The real auditable store is still being designed.
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cwtch.im/tapir"
|
||||||
|
"cwtch.im/tapir/applications"
|
||||||
|
"cwtch.im/tapir/primitives/auditable"
|
||||||
|
"cwtch.im/tapir/primitives/privacypass"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/applications"
|
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/auditable"
|
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/privacypass"
|
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewTokenBoardServer generates new Server for Token Board
|
// NewTokenBoardServer generates new Server for Token Board
|
||||||
|
@ -57,7 +55,7 @@ func (ta *Server) Listen() {
|
||||||
|
|
||||||
var message Message
|
var message Message
|
||||||
json.Unmarshal(data, &message)
|
json.Unmarshal(data, &message)
|
||||||
|
log.Debugf("Received a Message: %v", message)
|
||||||
switch message.MessageType {
|
switch message.MessageType {
|
||||||
case postRequestMessage:
|
case postRequestMessage:
|
||||||
postrequest := message.PostRequest
|
postrequest := message.PostRequest
|
||||||
|
@ -79,7 +77,7 @@ func (ta *Server) Listen() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ta *Server) postMessageRequest(token privacypass.SpentToken, message auditable.Message) {
|
func (ta *Server) postMessageRequest(token privacypass.SpentToken, message auditable.Message) {
|
||||||
if err := ta.TokenService.SpendToken(token, append(message, ta.connection.ID().Hostname()...)); err == nil {
|
if err := ta.TokenService.SpendToken(token, message); err == nil {
|
||||||
log.Debugf("Token is valid")
|
log.Debugf("Token is valid")
|
||||||
signedproof := ta.AuditableStore.Add(message)
|
signedproof := ta.AuditableStore.Add(message)
|
||||||
data, _ := json.Marshal(Message{MessageType: postResultMessage, PostResult: postResult{true, signedproof}})
|
data, _ := json.Marshal(Message{MessageType: postResultMessage, PostResult: postResult{true, signedproof}})
|
||||||
|
|
|
@ -1,16 +1,15 @@
|
||||||
package tokenboard
|
package tokenboard
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cwtch.im/tapir"
|
||||||
|
"cwtch.im/tapir/applications"
|
||||||
|
"cwtch.im/tapir/networks/tor"
|
||||||
|
"cwtch.im/tapir/primitives"
|
||||||
|
"cwtch.im/tapir/primitives/auditable"
|
||||||
|
"cwtch.im/tapir/primitives/privacypass"
|
||||||
"errors"
|
"errors"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/applications"
|
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/networks/tor"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives"
|
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/auditable"
|
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/privacypass"
|
|
||||||
"git.openprivacy.ca/openprivacy/connectivity"
|
|
||||||
torProvider "git.openprivacy.ca/openprivacy/connectivity/tor"
|
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
|
||||||
"os"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -38,7 +37,8 @@ type FreePaymentHandler struct {
|
||||||
|
|
||||||
func (fph *FreePaymentHandler) MakePayment() {
|
func (fph *FreePaymentHandler) MakePayment() {
|
||||||
id, sk := primitives.InitializeEphemeralIdentity()
|
id, sk := primitives.InitializeEphemeralIdentity()
|
||||||
client := new(tor.BaseOnionService)
|
var client tapir.Service
|
||||||
|
client = new(tor.BaseOnionService)
|
||||||
client.Init(fph.ACN, sk, &id)
|
client.Init(fph.ACN, sk, &id)
|
||||||
|
|
||||||
tokenApplication := new(applications.TokenApplication)
|
tokenApplication := new(applications.TokenApplication)
|
||||||
|
@ -47,46 +47,30 @@ func (fph *FreePaymentHandler) MakePayment() {
|
||||||
ChainApplication(new(applications.ProofOfWorkApplication), applications.SuccessfulProofOfWorkCapability).
|
ChainApplication(new(applications.ProofOfWorkApplication), applications.SuccessfulProofOfWorkCapability).
|
||||||
ChainApplication(tokenApplication, applications.HasTokensCapability)
|
ChainApplication(tokenApplication, applications.HasTokensCapability)
|
||||||
client.Connect(fph.ServerHostname, powTokenApp)
|
client.Connect(fph.ServerHostname, powTokenApp)
|
||||||
conn, err := client.WaitForCapabilityOrClose(fph.ServerHostname, applications.HasTokensCapability)
|
client.WaitForCapabilityOrClose(fph.ServerHostname, applications.HasTokensCapability)
|
||||||
if err == nil {
|
conn, _ := client.GetConnection(fph.ServerHostname)
|
||||||
powtapp, _ := conn.App().(*applications.TokenApplication)
|
powtapp, _ := conn.App().(*applications.TokenApplication)
|
||||||
fph.tokens = append(fph.tokens, powtapp.Tokens...)
|
fph.tokens = append(fph.tokens, powtapp.Tokens...)
|
||||||
log.Debugf("Transcript: %v", powtapp.Transcript().OutputTranscriptToAudit())
|
log.Debugf("Transcript: %v", powtapp.Transcript().OutputTranscriptToAudit())
|
||||||
conn.Close()
|
conn.Close()
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Debugf("Error making payment: %v", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fph *FreePaymentHandler) NextToken(data []byte, hostname string) (privacypass.SpentToken, error) {
|
func (fph *FreePaymentHandler) NextToken(data []byte) (privacypass.SpentToken, error) {
|
||||||
if len(fph.tokens) == 0 {
|
if len(fph.tokens) == 0 {
|
||||||
return privacypass.SpentToken{}, errors.New("No more tokens")
|
return privacypass.SpentToken{}, errors.New("No more tokens")
|
||||||
}
|
}
|
||||||
token := fph.tokens[0]
|
token := fph.tokens[0]
|
||||||
fph.tokens = fph.tokens[1:]
|
fph.tokens = fph.tokens[1:]
|
||||||
return token.SpendToken(append(data, hostname...)), nil
|
return token.SpendToken(data), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTokenBoardApp(t *testing.T) {
|
func TestTokenBoardApp(t *testing.T) {
|
||||||
// numRoutinesStart := runtime.NumGoroutine()
|
// numRoutinesStart := runtime.NumGoroutine()
|
||||||
log.SetLevel(log.LevelDebug)
|
log.SetLevel(log.LevelDebug)
|
||||||
log.Infof("Number of goroutines open at start: %d", runtime.NumGoroutine())
|
log.Infof("Number of goroutines open at start: %d", runtime.NumGoroutine())
|
||||||
|
|
||||||
os.MkdirAll("./tor/", 0700)
|
|
||||||
builder := new(torProvider.TorrcBuilder)
|
|
||||||
builder.WithSocksPort(9059).WithControlPort(9060).WithHashedPassword("tapir-integration-test").Build("./tor/torrc")
|
|
||||||
torDataDir := ""
|
|
||||||
var err error
|
|
||||||
if torDataDir, err = os.MkdirTemp("./tor/", "data-dir-"); err != nil {
|
|
||||||
t.Fatalf("could not create data dir")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Connect to Tor
|
// Connect to Tor
|
||||||
acn, err := torProvider.NewTorACNWithAuth("./", "", torDataDir, 9060, torProvider.HashedPasswordAuthenticator{Password: "tapir-integration-test"})
|
var acn connectivity.ACN
|
||||||
|
acn, _ = connectivity.StartTor("./", "")
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("could not launch ACN %v", err)
|
|
||||||
}
|
|
||||||
acn.WaitTillBootstrapped()
|
acn.WaitTillBootstrapped()
|
||||||
|
|
||||||
// Generate Server Key
|
// Generate Server Key
|
||||||
|
@ -102,25 +86,27 @@ func TestTokenBoardApp(t *testing.T) {
|
||||||
clientAuditableStore.Init(publicsid)
|
clientAuditableStore.Init(publicsid)
|
||||||
|
|
||||||
// Init the Server running the Simple App.
|
// Init the Server running the Simple App.
|
||||||
service := new(tor.BaseOnionService)
|
var service tapir.Service
|
||||||
|
service = new(tor.BaseOnionService)
|
||||||
service.Init(acn, sk, &sid)
|
service.Init(acn, sk, &sid)
|
||||||
|
|
||||||
// Goroutine Management
|
// Goroutine Management
|
||||||
sg := new(sync.WaitGroup)
|
sg := new(sync.WaitGroup)
|
||||||
sg.Add(1)
|
sg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
service.Listen(NewTokenBoardServer(tokenService, serverAuditableStore))
|
service.Listen(NewTokenBoardServer(&tokenService, serverAuditableStore))
|
||||||
sg.Done()
|
sg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Init the Server running the PoW Token App.
|
// Init the Server running the PoW Token App.
|
||||||
powTokenService := new(tor.BaseOnionService)
|
var powTokenService tapir.Service
|
||||||
|
powTokenService = new(tor.BaseOnionService)
|
||||||
spowid, spowk := primitives.InitializeEphemeralIdentity()
|
spowid, spowk := primitives.InitializeEphemeralIdentity()
|
||||||
powTokenService.Init(acn, spowk, &spowid)
|
powTokenService.Init(acn, spowk, &spowid)
|
||||||
sg.Add(1)
|
sg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
tokenApplication := new(applications.TokenApplication)
|
tokenApplication := new(applications.TokenApplication)
|
||||||
tokenApplication.TokenService = tokenService
|
tokenApplication.TokenService = &tokenService
|
||||||
powTokenApp := new(applications.ApplicationChain).
|
powTokenApp := new(applications.ApplicationChain).
|
||||||
ChainApplication(new(applications.ProofOfWorkApplication), applications.SuccessfulProofOfWorkCapability).
|
ChainApplication(new(applications.ProofOfWorkApplication), applications.SuccessfulProofOfWorkCapability).
|
||||||
ChainApplication(tokenApplication, applications.HasTokensCapability)
|
ChainApplication(tokenApplication, applications.HasTokensCapability)
|
||||||
|
@ -128,11 +114,12 @@ func TestTokenBoardApp(t *testing.T) {
|
||||||
sg.Done()
|
sg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
time.Sleep(time.Second * 60) // wait for server to initialize
|
time.Sleep(time.Second * 30) // wait for server to initialize
|
||||||
id, sk := primitives.InitializeEphemeralIdentity()
|
id, sk := primitives.InitializeEphemeralIdentity()
|
||||||
client := new(tor.BaseOnionService)
|
var client tapir.Service
|
||||||
|
client = new(tor.BaseOnionService)
|
||||||
client.Init(acn, sk, &id)
|
client.Init(acn, sk, &id)
|
||||||
client.Connect(sid.Hostname(), NewTokenBoardClient(clientAuditableStore, Handler{Store: clientAuditableStore}, &FreePaymentHandler{ACN: acn, TokenService: tokenService, ServerHostname: spowid.Hostname()}))
|
client.Connect(sid.Hostname(), NewTokenBoardClient(clientAuditableStore, Handler{Store: clientAuditableStore}, &FreePaymentHandler{ACN: acn, TokenService: &tokenService, ServerHostname: spowid.Hostname()}))
|
||||||
client.WaitForCapabilityOrClose(sid.Hostname(), applications.AuthCapability)
|
client.WaitForCapabilityOrClose(sid.Hostname(), applications.AuthCapability)
|
||||||
conn, _ := client.GetConnection(sid.Hostname())
|
conn, _ := client.GetConnection(sid.Hostname())
|
||||||
tba, _ := conn.App().(*Client)
|
tba, _ := conn.App().(*Client)
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
package applications
|
package applications
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.openprivacy.ca/cwtch.im/tapir"
|
"cwtch.im/tapir"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/core"
|
"cwtch.im/tapir/primitives/core"
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TranscriptApp defines a Tapir Meta-App which provides a global cryptographic transcript
|
// TranscriptApp defines a Tapir Meta-App which provides a global cryptographic transcript
|
||||||
|
@ -19,9 +18,6 @@ func (TranscriptApp) NewInstance() tapir.Application {
|
||||||
|
|
||||||
// Init initializes the cryptographic transcript
|
// Init initializes the cryptographic transcript
|
||||||
func (ta *TranscriptApp) Init(connection tapir.Connection) {
|
func (ta *TranscriptApp) Init(connection tapir.Connection) {
|
||||||
if ta.transcript != nil {
|
|
||||||
ta.panic()
|
|
||||||
}
|
|
||||||
ta.transcript = core.NewTranscript("tapir-transcript")
|
ta.transcript = core.NewTranscript("tapir-transcript")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,19 +28,5 @@ func (ta *TranscriptApp) Transcript() *core.Transcript {
|
||||||
|
|
||||||
// PropagateTranscript overrides the default transcript and propagates a transcript from a previous session
|
// PropagateTranscript overrides the default transcript and propagates a transcript from a previous session
|
||||||
func (ta *TranscriptApp) PropagateTranscript(transcript *core.Transcript) {
|
func (ta *TranscriptApp) PropagateTranscript(transcript *core.Transcript) {
|
||||||
if ta.transcript != nil {
|
|
||||||
ta.panic()
|
|
||||||
}
|
|
||||||
ta.transcript = transcript
|
ta.transcript = transcript
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ta *TranscriptApp) panic() {
|
|
||||||
// Note: if this is ever happens it is a critical application bug
|
|
||||||
// This will prevent a misuse of application chains that cause an earlier
|
|
||||||
// transcript to be overwritten. Since we expect the security of many higher level applications
|
|
||||||
// to be reliant on the randomness provided by the transcript we want to be actively hostile to any potential
|
|
||||||
// misuse.
|
|
||||||
log.Errorf("apps should not attempt to intitalize or overwrite a transcript once one has been initialized - this is a CRITICAL bug and so we have safely crashed")
|
|
||||||
// We could silently fail to do anything here, but that is likely more dangerous in the long run...
|
|
||||||
panic("apps should not attempt to intitalize or overwrite a transcript a transcript once one has been initialized - this is a CRITICAL bug and so we have safely crashed")
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,22 +0,0 @@
|
||||||
package applications
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestTranscriptApp(t *testing.T) {
|
|
||||||
ta := new(TranscriptApp)
|
|
||||||
ta.Init(MockConnection{})
|
|
||||||
ta.Transcript().NewProtocol("test")
|
|
||||||
ta.transcript.CommitToTranscript("test-commit")
|
|
||||||
t.Logf(ta.Transcript().OutputTranscriptToAudit())
|
|
||||||
|
|
||||||
// Now we test panic'ing....
|
|
||||||
defer func() {
|
|
||||||
if r := recover(); r == nil {
|
|
||||||
t.Errorf("The code did not panic - it definitely should have")
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// Attempt to reinitialized the transcript, apps should *never* do this and we want to be hostile to that
|
|
||||||
// behaviour
|
|
||||||
ta.Init(MockConnection{})
|
|
||||||
}
|
|
21
go.mod
21
go.mod
|
@ -1,21 +1,6 @@
|
||||||
module git.openprivacy.ca/cwtch.im/tapir
|
module cwtch.im/tapir
|
||||||
|
|
||||||
go 1.17
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
filippo.io/edwards25519 v1.0.0
|
git.openprivacy.ca/openprivacy/libricochet-go v1.0.4
|
||||||
git.openprivacy.ca/openprivacy/connectivity v1.8.6
|
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f
|
||||||
git.openprivacy.ca/openprivacy/log v1.0.3
|
|
||||||
github.com/gtank/merlin v0.1.1
|
|
||||||
github.com/gtank/ristretto255 v0.1.3-0.20210930101514-6bb39798585c
|
|
||||||
go.etcd.io/bbolt v1.3.6
|
|
||||||
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
|
||||||
git.openprivacy.ca/openprivacy/bine v0.0.4 // indirect
|
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
|
||||||
github.com/mimoo/StrobeGo v0.0.0-20220103164710-9a04d6ca976b // indirect
|
|
||||||
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b // indirect
|
|
||||||
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect
|
|
||||||
)
|
)
|
||||||
|
|
66
go.sum
66
go.sum
|
@ -1,56 +1,28 @@
|
||||||
filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
|
git.openprivacy.ca/openprivacy/libricochet-go v1.0.4 h1:GWLMJ5jBSIC/gFXzdbbeVz7fIAn2FTgW8+wBci6/3Ek=
|
||||||
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
|
git.openprivacy.ca/openprivacy/libricochet-go v1.0.4/go.mod h1:yMSG1gBaP4f1U+RMZXN85d29D39OK5s8aTpyVRoH5FY=
|
||||||
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
|
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 h1:w1UutsfOrms1J05zt7ISrnJIXKzwaspym5BTKGx93EI=
|
||||||
git.openprivacy.ca/openprivacy/bine v0.0.4 h1:CO7EkGyz+jegZ4ap8g5NWRuDHA/56KKvGySR6OBPW+c=
|
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412/go.mod h1:WPjqKcmVOxf0XSf3YxCJs6N6AOSrOx3obionmG7T0y0=
|
||||||
git.openprivacy.ca/openprivacy/bine v0.0.4/go.mod h1:13ZqhKyqakDsN/ZkQkIGNULsmLyqtXc46XBcnuXm/mU=
|
github.com/cretz/bine v0.1.0 h1:1/fvhLE+fk0bPzjdO5Ci+0ComYxEMuB1JhM4X5skT3g=
|
||||||
git.openprivacy.ca/openprivacy/connectivity v1.8.6 h1:g74PyDGvpMZ3+K0dXy3mlTJh+e0rcwNk0XF8owzkmOA=
|
github.com/cretz/bine v0.1.0/go.mod h1:6PF6fWAvYtwjRGkAuDEJeWNOv3a2hUouSP/yRYXmvHw=
|
||||||
git.openprivacy.ca/openprivacy/connectivity v1.8.6/go.mod h1:Hn1gpOx/bRZp5wvCtPQVJPXrfeUH0EGiG/Aoa0vjGLg=
|
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||||
git.openprivacy.ca/openprivacy/log v1.0.3 h1:E/PMm4LY+Q9s3aDpfySfEDq/vYQontlvNj/scrPaga0=
|
|
||||||
git.openprivacy.ca/openprivacy/log v1.0.3/go.mod h1:gGYK8xHtndRLDymFtmjkG26GaMQNgyhioNS82m812Iw=
|
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||||
github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is=
|
|
||||||
github.com/gtank/merlin v0.1.1/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s=
|
|
||||||
github.com/gtank/ristretto255 v0.1.3-0.20210930101514-6bb39798585c h1:gkfmnY4Rlt3VINCo4uKdpvngiibQyoENVj5Q88sxXhE=
|
|
||||||
github.com/gtank/ristretto255 v0.1.3-0.20210930101514-6bb39798585c/go.mod h1:tDPFhGdt3hJWqtKwx57i9baiB1Cj0yAg22VOPUqm5vY=
|
|
||||||
github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM=
|
|
||||||
github.com/mimoo/StrobeGo v0.0.0-20220103164710-9a04d6ca976b h1:QrHweqAtyJ9EwCaGHBu1fghwxIPiopAHV06JlXrMHjk=
|
|
||||||
github.com/mimoo/StrobeGo v0.0.0-20220103164710-9a04d6ca976b/go.mod h1:xxLb2ip6sSUts3g1irPVHyk/DGslwQsNOo9I7smJfNU=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||||
go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU=
|
golang.org/x/crypto v0.0.0-20190128193316-c7b33c32a30b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||||
go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4=
|
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f h1:R423Cnkcp5JABoeemiGEPlt9tHXFfw5kvc0yqlxRPWo=
|
||||||
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d h1:3qF+Z8Hkrw9sOhrFHti9TlB1Hkac1x+DNRkv0XQiFjo=
|
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||||
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
|
||||||
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b h1:ZmngSVLe/wycRns9MKikG9OWIEjGcGAkacif7oYQaUY=
|
|
||||||
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 h1:UiNENfZ8gDvpiWw7IpOMQ27spWmThO1RwwdQVbJahJM=
|
|
||||||
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
package tor
|
package tor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"cwtch.im/tapir"
|
||||||
|
"cwtch.im/tapir/primitives"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir"
|
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
"git.openprivacy.ca/openprivacy/connectivity"
|
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
|
||||||
"golang.org/x/crypto/ed25519"
|
"golang.org/x/crypto/ed25519"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -16,34 +15,11 @@ import (
|
||||||
|
|
||||||
// BaseOnionService is a concrete implementation of the service interface over Tor onion services.
|
// BaseOnionService is a concrete implementation of the service interface over Tor onion services.
|
||||||
type BaseOnionService struct {
|
type BaseOnionService struct {
|
||||||
connections sync.Map
|
connections sync.Map
|
||||||
acn connectivity.ACN
|
acn connectivity.ACN
|
||||||
id *primitives.Identity
|
id *primitives.Identity
|
||||||
privateKey ed25519.PrivateKey
|
privateKey ed25519.PrivateKey
|
||||||
ls connectivity.ListenService
|
ls connectivity.ListenService
|
||||||
lock sync.Mutex
|
|
||||||
port int
|
|
||||||
shutdownChannel chan bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// Metrics provides a report of useful information about the status of the service e.g. the number of active
|
|
||||||
// connections
|
|
||||||
func (s *BaseOnionService) Metrics() tapir.ServiceMetrics {
|
|
||||||
s.lock.Lock()
|
|
||||||
defer s.lock.Unlock()
|
|
||||||
|
|
||||||
count := 0
|
|
||||||
s.connections.Range(func(key, value interface{}) bool {
|
|
||||||
connection := value.(tapir.Connection)
|
|
||||||
if !connection.IsClosed() {
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
return tapir.ServiceMetrics{
|
|
||||||
ConnectionCount: count,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init initializes a BaseOnionService with a given private key and identity
|
// Init initializes a BaseOnionService with a given private key and identity
|
||||||
|
@ -54,176 +30,50 @@ func (s *BaseOnionService) Init(acn connectivity.ACN, sk ed25519.PrivateKey, id
|
||||||
s.acn = acn
|
s.acn = acn
|
||||||
s.id = id
|
s.id = id
|
||||||
s.privateKey = sk
|
s.privateKey = sk
|
||||||
s.port = 9878
|
|
||||||
// blocking so we can wait on shutdown for closing of this goroutine
|
|
||||||
s.shutdownChannel = make(chan bool)
|
|
||||||
go func() {
|
|
||||||
for s.waitOrTimeout() {
|
|
||||||
s.GarbageCollect()
|
|
||||||
}
|
|
||||||
log.Debugf("closing down garbage collection goroutine")
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *BaseOnionService) waitOrTimeout() bool {
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
||||||
defer cancel()
|
|
||||||
select {
|
|
||||||
case <-s.shutdownChannel:
|
|
||||||
return false
|
|
||||||
case <-ctx.Done():
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetPort configures the port that the service uses.
|
|
||||||
func (s *BaseOnionService) SetPort(port int) {
|
|
||||||
s.port = port
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitForCapabilityOrClose blocks until the connection has the given capability or the underlying connection is closed
|
// WaitForCapabilityOrClose blocks until the connection has the given capability or the underlying connection is closed
|
||||||
// (through error or user action)
|
// (through error or user action)
|
||||||
func (s *BaseOnionService) WaitForCapabilityOrClose(cid string, name tapir.Capability) (tapir.Connection, error) {
|
func (s *BaseOnionService) WaitForCapabilityOrClose(cid string, name tapir.Capability) (tapir.Connection, error) {
|
||||||
attempts := 0
|
conn, err := s.GetConnection(cid)
|
||||||
for {
|
if err == nil {
|
||||||
if attempts > 4 {
|
for {
|
||||||
s.connections.Range(func(key, value interface{}) bool {
|
|
||||||
connection := value.(tapir.Connection)
|
|
||||||
if connection.Hostname() == cid {
|
|
||||||
if !connection.IsClosed() {
|
|
||||||
connection.Close()
|
|
||||||
s.connections.Delete(key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
log.Debugf("WaitForCapabilityOrClose attempts exceeded for %v, all connections closed", cid)
|
|
||||||
return nil, errors.New("failed to acquire capability after multiple attempts, forcibly closing all connections with the peer")
|
|
||||||
}
|
|
||||||
|
|
||||||
if attempts > 0 {
|
|
||||||
// Allow connections to be torn down / closed before checking again
|
|
||||||
// There is no point in busy looping...
|
|
||||||
time.Sleep(time.Second * time.Duration(attempts))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Increment Attempts
|
|
||||||
attempts++
|
|
||||||
|
|
||||||
log.Debugf("Lookup up a connection %v...", cid)
|
|
||||||
// Lookup the connection...
|
|
||||||
conn, err := s.GetConnection(cid)
|
|
||||||
|
|
||||||
// If there are no active connections then return an error...
|
|
||||||
if conn == nil {
|
|
||||||
log.Debugf("no active connection found for %v", cid)
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
// if we have only one connection and it has the desired capability then return the connection with
|
|
||||||
// no error...
|
|
||||||
if conn.HasCapability(name) {
|
if conn.HasCapability(name) {
|
||||||
return conn, nil
|
return conn, nil
|
||||||
}
|
}
|
||||||
|
if conn.IsClosed() {
|
||||||
log.Debugf("Found 1 connections for %v, but it lacks the desired capability %v", cid, name)
|
return nil, errors.New("connection is closed")
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// If We have 2 connections for the same hostname...
|
|
||||||
if err != nil {
|
|
||||||
log.Debugf("found duplicate connections for %v <-> %v %v", s.id.Hostname(), cid, err)
|
|
||||||
|
|
||||||
inboundCount := 0
|
|
||||||
|
|
||||||
// By convention the lowest lexicographical hostname purges all their outbounds to the higher
|
|
||||||
// hostname
|
|
||||||
// Which should only leave a single connection remaining (as we dedupe on connect too)
|
|
||||||
// This does allow people to attempt to guarantee being the outbound connection but due to the bidirectional
|
|
||||||
// authentication this should never result in an advantage in the protocol.
|
|
||||||
// Close all outbound connections to connection
|
|
||||||
s.connections.Range(func(key, value interface{}) bool {
|
|
||||||
connection := value.(tapir.Connection)
|
|
||||||
if connection.Hostname() == cid {
|
|
||||||
if !connection.IsClosed() && connection.IsOutbound() && s.id.Hostname() < cid {
|
|
||||||
connection.Close()
|
|
||||||
s.connections.Delete(key)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !connection.IsClosed() && !connection.IsOutbound() {
|
|
||||||
inboundCount++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
// If we have more than 1 inbound count then forcibly close all connections...
|
|
||||||
// This shouldn't happen honestly, but if it does then it can cause an infinite check here
|
|
||||||
if inboundCount > 1 {
|
|
||||||
s.connections.Range(func(key, value interface{}) bool {
|
|
||||||
connection := value.(tapir.Connection)
|
|
||||||
if connection.Hostname() == cid {
|
|
||||||
if !connection.IsClosed() {
|
|
||||||
connection.Close()
|
|
||||||
s.connections.Delete(key)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
return nil, errors.New("multiple inbound connections found and closed; the only resolution to this is to close them all and try connecting again")
|
|
||||||
}
|
}
|
||||||
|
time.Sleep(time.Millisecond * 200)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetConnection returns a connection for a given hostname.
|
// GetConnection returns a connection for a given hostname.
|
||||||
func (s *BaseOnionService) GetConnection(hostname string) (tapir.Connection, error) {
|
func (s *BaseOnionService) GetConnection(hostname string) (tapir.Connection, error) {
|
||||||
conn := make([]tapir.Connection, 0)
|
var conn tapir.Connection
|
||||||
s.connections.Range(func(key, value interface{}) bool {
|
s.connections.Range(func(key, value interface{}) bool {
|
||||||
|
log.Debugf("Checking %v", key)
|
||||||
connection := value.(tapir.Connection)
|
connection := value.(tapir.Connection)
|
||||||
if connection.Hostname() == hostname {
|
if connection.Hostname() == hostname {
|
||||||
if !connection.IsClosed() {
|
if !connection.IsClosed() {
|
||||||
conn = append(conn, connection)
|
conn = connection
|
||||||
} else {
|
return false
|
||||||
// Delete this Closed Connection
|
|
||||||
s.connections.Delete(key)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
if conn == nil {
|
||||||
if len(conn) == 0 {
|
|
||||||
return nil, errors.New("no connection found")
|
return nil, errors.New("no connection found")
|
||||||
}
|
}
|
||||||
if len(conn) > 1 {
|
return conn, nil
|
||||||
// If there are multiple connections we return the first one but also notify the user (this should be a
|
|
||||||
// temporary edgecase (see Connect))
|
|
||||||
return conn[0], errors.New("multiple connections found")
|
|
||||||
}
|
|
||||||
return conn[0], nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GarbageCollect iterates through the connection pool and cleans up any connections that are closed
|
|
||||||
// that haven't been removed from the map.
|
|
||||||
func (s *BaseOnionService) GarbageCollect() {
|
|
||||||
log.Debugf("running garbage collection...")
|
|
||||||
s.connections.Range(func(key, value interface{}) bool {
|
|
||||||
connection := value.(tapir.Connection)
|
|
||||||
if connection.IsClosed() {
|
|
||||||
// Delete this Closed Connection
|
|
||||||
s.connections.Delete(key)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect initializes a new outbound connection to the given peer, using the defined Application
|
// Connect initializes a new outbound connection to the given peer, using the defined Application
|
||||||
func (s *BaseOnionService) Connect(hostname string, app tapir.Application) (bool, error) {
|
func (s *BaseOnionService) Connect(hostname string, app tapir.Application) (bool, error) {
|
||||||
currconn, _ := s.GetConnection(hostname)
|
_, err := s.GetConnection(hostname)
|
||||||
|
if err == nil {
|
||||||
// We already have a connection
|
|
||||||
if currconn != nil {
|
|
||||||
// Note: This check is not 100% reliable. And we may end up with two connections between peers
|
// Note: This check is not 100% reliable. And we may end up with two connections between peers
|
||||||
// This can happen when a client connects to a server as the server is connecting to the client
|
// This can happen when a client connects to a server as the server is connecting to the client
|
||||||
// Because at the start of the connection the server cannot derive the true hostname of the client until it
|
// Because at the start of the connection the server cannot derive the true hostname of the client until it
|
||||||
|
@ -231,7 +81,6 @@ func (s *BaseOnionService) Connect(hostname string, app tapir.Application) (bool
|
||||||
// We mitigate this by performing multiple checks when Connect'ing
|
// We mitigate this by performing multiple checks when Connect'ing
|
||||||
return true, errors.New("already connected to " + hostname)
|
return true, errors.New("already connected to " + hostname)
|
||||||
}
|
}
|
||||||
|
|
||||||
// connects to a remote server
|
// connects to a remote server
|
||||||
// spins off to a connection struct
|
// spins off to a connection struct
|
||||||
log.Debugf("Connecting to %v", hostname)
|
log.Debugf("Connecting to %v", hostname)
|
||||||
|
@ -242,14 +91,14 @@ func (s *BaseOnionService) Connect(hostname string, app tapir.Application) (bool
|
||||||
// Second check. If we didn't catch a double connection attempt before the Open we *should* catch it now because
|
// Second check. If we didn't catch a double connection attempt before the Open we *should* catch it now because
|
||||||
// the auth protocol is quick and Open over onion connections can take some time.
|
// the auth protocol is quick and Open over onion connections can take some time.
|
||||||
// Again this isn't 100% reliable.
|
// Again this isn't 100% reliable.
|
||||||
tconn, _ := s.GetConnection(hostname)
|
_, err := s.GetConnection(hostname)
|
||||||
if tconn != nil {
|
if err == nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
return true, errors.New("already connected to " + hostname)
|
return true, errors.New("already connected to " + hostname)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("Connected to %v [%v]", hostname, connectionID)
|
log.Debugf("Connected to %v [%v]", hostname, connectionID)
|
||||||
s.connections.Store(connectionID, tapir.NewConnection(s, s.id, hostname, true, conn, app.NewInstance()))
|
s.connections.Store(connectionID, tapir.NewConnection(s.id, hostname, true, conn, app.NewInstance()))
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
log.Debugf("Error connecting to %v %v", hostname, err)
|
log.Debugf("Error connecting to %v %v", hostname, err)
|
||||||
|
@ -268,19 +117,16 @@ func (s *BaseOnionService) getNewConnectionID() string {
|
||||||
func (s *BaseOnionService) Listen(app tapir.Application) error {
|
func (s *BaseOnionService) Listen(app tapir.Application) error {
|
||||||
// accepts a new connection
|
// accepts a new connection
|
||||||
// spins off to a connection struct
|
// spins off to a connection struct
|
||||||
s.lock.Lock()
|
ls, err := s.acn.Listen(s.privateKey, 9878)
|
||||||
ls, err := s.acn.Listen(s.privateKey, s.port)
|
|
||||||
s.ls = ls
|
s.ls = ls
|
||||||
s.lock.Unlock()
|
log.Debugf("Starting a service on %v ", ls.AddressFull())
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Debugf("Starting a service on %v ", s.ls.AddressFull())
|
|
||||||
for {
|
for {
|
||||||
conn, err := s.ls.Accept()
|
conn, err := s.ls.Accept()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
tempHostname := s.getNewConnectionID()
|
tempHostname := s.getNewConnectionID()
|
||||||
log.Debugf("Accepted connection from %v", tempHostname)
|
log.Debugf("Accepted connection from %v", tempHostname)
|
||||||
s.connections.Store(tempHostname, tapir.NewConnection(s, s.id, tempHostname, false, conn, app.NewInstance()))
|
s.connections.Store(tempHostname, tapir.NewConnection(s.id, tempHostname, false, conn, app.NewInstance()))
|
||||||
} else {
|
} else {
|
||||||
log.Debugf("Error accepting connection %v", err)
|
log.Debugf("Error accepting connection %v", err)
|
||||||
return err
|
return err
|
||||||
|
@ -293,33 +139,10 @@ func (s *BaseOnionService) Listen(app tapir.Application) error {
|
||||||
|
|
||||||
// Shutdown closes the service and ensures that any connections are closed.
|
// Shutdown closes the service and ensures that any connections are closed.
|
||||||
func (s *BaseOnionService) Shutdown() {
|
func (s *BaseOnionService) Shutdown() {
|
||||||
s.lock.Lock()
|
s.ls.Close()
|
||||||
defer s.lock.Unlock()
|
|
||||||
if s.ls != nil {
|
|
||||||
s.ls.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// close all existing connections manually
|
|
||||||
s.connections.Range(func(key, value interface{}) bool {
|
s.connections.Range(func(key, value interface{}) bool {
|
||||||
connection := value.(tapir.Connection)
|
connection := value.(tapir.Connection)
|
||||||
connection.Close()
|
connection.Close()
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
// wait for the return of our garbage collection goroutine
|
|
||||||
s.shutdownChannel <- true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Broadcast sends a message to all connections who possess the given capability
|
|
||||||
func (s *BaseOnionService) Broadcast(message []byte, capability tapir.Capability) error {
|
|
||||||
s.lock.Lock()
|
|
||||||
defer s.lock.Unlock()
|
|
||||||
s.connections.Range(func(key, value interface{}) bool {
|
|
||||||
connection := value.(tapir.Connection)
|
|
||||||
if connection.HasCapability(capability) {
|
|
||||||
connection.Send(message)
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package persistence
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
bolt "go.etcd.io/bbolt"
|
bolt "go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -53,7 +53,6 @@ func (bp *BoltPersistence) Check(bucket string, name string) (bool, error) {
|
||||||
val = b.Get([]byte(name))
|
val = b.Get([]byte(name))
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
} else if val != nil {
|
} else if val != nil {
|
||||||
|
|
|
@ -1,18 +1,14 @@
|
||||||
package persistence
|
package persistence
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBoltPersistence_Open(t *testing.T) {
|
func TestBoltPersistence_Open(t *testing.T) {
|
||||||
os.Remove("test.dbgi")
|
var db Service
|
||||||
db := new(BoltPersistence)
|
db = new(BoltPersistence)
|
||||||
db.Open("test.dbgi")
|
db.Open("test.dbgi")
|
||||||
db.Setup([]string{"tokens"})
|
db.Setup([]string{"tokens"})
|
||||||
// 2020.02: Fails in WSL1 because of a mmap issue.
|
|
||||||
// https://github.com/microsoft/WSL/issues/4873
|
|
||||||
// Scheduled to be fixed in the 20h1 Win10 release
|
|
||||||
db.Persist("tokens", "random_value", true)
|
db.Persist("tokens", "random_value", true)
|
||||||
|
|
||||||
var exists bool
|
var exists bool
|
||||||
|
|
Binary file not shown.
|
@ -14,15 +14,11 @@ import (
|
||||||
// Alice Ephemeral <-> Bob Ephemeral
|
// Alice Ephemeral <-> Bob Ephemeral
|
||||||
//
|
//
|
||||||
// Through this, a unique session key is derived. The exchange is offline-deniable (in the context of Tapir and Onion Service)
|
// Through this, a unique session key is derived. The exchange is offline-deniable (in the context of Tapir and Onion Service)
|
||||||
func Perform3DH(longtermIdentity *Identity, ephemeralIdentity *Identity, remoteLongTermPublicKey ed25519.PublicKey, remoteEphemeralPublicKey ed25519.PublicKey, outbound bool) ([32]byte, error) {
|
func Perform3DH(longtermIdentity *Identity, ephemeralIdentity *Identity, remoteLongTermPublicKey ed25519.PublicKey, remoteEphemeralPublicKey ed25519.PublicKey, outbound bool) [32]byte {
|
||||||
// 3DH Handshake
|
// 3DH Handshake
|
||||||
l2e, err1 := longtermIdentity.EDH(remoteEphemeralPublicKey)
|
l2e := longtermIdentity.EDH(remoteEphemeralPublicKey)
|
||||||
e2l, err2 := ephemeralIdentity.EDH(remoteLongTermPublicKey)
|
e2l := ephemeralIdentity.EDH(remoteLongTermPublicKey)
|
||||||
e2e, err3 := ephemeralIdentity.EDH(remoteEphemeralPublicKey)
|
e2e := ephemeralIdentity.EDH(remoteEphemeralPublicKey)
|
||||||
|
|
||||||
if err1 != nil || err2 != nil || err3 != nil {
|
|
||||||
return [32]byte{}, err1
|
|
||||||
}
|
|
||||||
|
|
||||||
// We need to define an order for the result concatenation so that both sides derive the same key.
|
// We need to define an order for the result concatenation so that both sides derive the same key.
|
||||||
var result [96]byte
|
var result [96]byte
|
||||||
|
@ -35,5 +31,5 @@ func Perform3DH(longtermIdentity *Identity, ephemeralIdentity *Identity, remoteL
|
||||||
copy(result[32:64], l2e)
|
copy(result[32:64], l2e)
|
||||||
copy(result[64:96], e2e)
|
copy(result[64:96], e2e)
|
||||||
}
|
}
|
||||||
return sha3.Sum256(result[:]), nil
|
return sha3.Sum256(result[:])
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
package auditable
|
package auditable
|
||||||
|
|
||||||
// WARNING NOTE: This is a sketch implementation, Not suitable for production use. The real auditable store is still being designed.
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cwtch.im/tapir/persistence"
|
||||||
|
"cwtch.im/tapir/primitives"
|
||||||
|
"cwtch.im/tapir/primitives/core"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/persistence"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives"
|
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/core"
|
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
|
||||||
"golang.org/x/crypto/ed25519"
|
"golang.org/x/crypto/ed25519"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
@ -25,6 +23,7 @@ type State struct {
|
||||||
Messages []Message
|
Messages []Message
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
const (
|
const (
|
||||||
auditableDataStoreProtocol = "auditable-data-store"
|
auditableDataStoreProtocol = "auditable-data-store"
|
||||||
newMessage = "new-message"
|
newMessage = "new-message"
|
||||||
|
@ -136,7 +135,7 @@ func (as *Store) AppendState(state State) error {
|
||||||
// verify that our state matches the servers signed state
|
// verify that our state matches the servers signed state
|
||||||
// this is *not* a security check, as a rogue server can simply sign any state
|
// this is *not* a security check, as a rogue server can simply sign any state
|
||||||
// however committing to a state allows us to build fraud proofs for malicious servers later on.
|
// however committing to a state allows us to build fraud proofs for malicious servers later on.
|
||||||
if !ed25519.Verify(as.identity.PublicKey(), as.LatestCommit, state.SignedProof) {
|
if ed25519.Verify(as.identity.PublicKey(), as.LatestCommit, state.SignedProof) == false {
|
||||||
return errors.New("state is not consistent, the server is malicious")
|
return errors.New("state is not consistent, the server is malicious")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -159,7 +158,7 @@ func (as *Store) MergeState(state State) error {
|
||||||
// and not the cause (which could be reordered messages, dropped messages, additional messages or any combination)
|
// and not the cause (which could be reordered messages, dropped messages, additional messages or any combination)
|
||||||
func (as *Store) VerifyFraudProof(fraudCommit []byte, signedFraudProof SignedProof, key ed25519.PublicKey) (bool, error) {
|
func (as *Store) VerifyFraudProof(fraudCommit []byte, signedFraudProof SignedProof, key ed25519.PublicKey) (bool, error) {
|
||||||
|
|
||||||
if !ed25519.Verify(key, fraudCommit, signedFraudProof) {
|
if ed25519.Verify(key, fraudCommit, signedFraudProof) == false {
|
||||||
// This could happen due to misuse of this function (trying to verify a proof with the wrong public key)
|
// This could happen due to misuse of this function (trying to verify a proof with the wrong public key)
|
||||||
// This could happen if the server lies to us and submits a fake state proof, however we cannot use this to
|
// This could happen if the server lies to us and submits a fake state proof, however we cannot use this to
|
||||||
// prove that the server is acting maliciously
|
// prove that the server is acting maliciously
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
package auditable
|
package auditable
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"cwtch.im/tapir/persistence"
|
||||||
|
"cwtch.im/tapir/primitives"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/persistence"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives"
|
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
package primitives
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"math"
|
||||||
|
"math/big"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BloomFilter implements a bloom filter
|
||||||
|
type BloomFilter struct {
|
||||||
|
B []bool
|
||||||
|
lock sync.Mutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// Init constructs a bloom filter of size m
|
||||||
|
func (bf *BloomFilter) Init(m int64) {
|
||||||
|
bf.B = make([]bool, m)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hash transforms a message to a set of bit flips
|
||||||
|
func (bf *BloomFilter) Hash(msg []byte) []int {
|
||||||
|
|
||||||
|
// Not the fastest hash function ever, but cryptographic security is more important than speed.
|
||||||
|
hash1 := sha256.Sum256(append([]byte("h1"), msg...))
|
||||||
|
hash2 := sha256.Sum256(append([]byte("h2"), msg...))
|
||||||
|
hash3 := sha256.Sum256(append([]byte("h3"), msg...))
|
||||||
|
hash4 := sha256.Sum256(append([]byte("h4"), msg...))
|
||||||
|
|
||||||
|
m := int64(len(bf.B))
|
||||||
|
// Number of bytes needed to pick a position from [0,m)
|
||||||
|
B := int(math.Ceil(math.Log2(float64(m)) / 8.0))
|
||||||
|
|
||||||
|
p1 := big.NewInt(0).SetBytes(hash1[:B]).Int64()
|
||||||
|
p2 := big.NewInt(0).SetBytes(hash2[:B]).Int64()
|
||||||
|
p3 := big.NewInt(0).SetBytes(hash3[:B]).Int64()
|
||||||
|
p4 := big.NewInt(0).SetBytes(hash4[:B]).Int64()
|
||||||
|
|
||||||
|
return []int{int(p1), int(p2), int(p3), int(p4)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert updates the BloomFilter (suitable for concurrent use)
|
||||||
|
func (bf *BloomFilter) Insert(msg []byte) {
|
||||||
|
pos := bf.Hash(msg)
|
||||||
|
bf.lock.Lock()
|
||||||
|
defer bf.lock.Unlock()
|
||||||
|
bf.B[pos[0]] = true
|
||||||
|
bf.B[pos[1]] = true
|
||||||
|
bf.B[pos[2]] = true
|
||||||
|
bf.B[pos[3]] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check returns true if the messages might be in the BloomFilter
|
||||||
|
// (No false positives, possible false negatives due to the probabilistic nature of the filter)
|
||||||
|
func (bf *BloomFilter) Check(msg []byte) bool {
|
||||||
|
pos := bf.Hash(msg)
|
||||||
|
if bf.B[pos[0]] && bf.B[pos[1]] && bf.B[pos[2]] && bf.B[pos[3]] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package primitives
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBloomFilter_Insert(t *testing.T) {
|
||||||
|
bf := new(BloomFilter)
|
||||||
|
bf.Init(256)
|
||||||
|
|
||||||
|
fp := 0
|
||||||
|
for i := 0; i < 256; i++ {
|
||||||
|
input := []byte("test" + strconv.Itoa(256+i))
|
||||||
|
if bf.Check(input) {
|
||||||
|
t.Log("False Positive!")
|
||||||
|
fp++
|
||||||
|
}
|
||||||
|
bf.Insert(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Num false positives %v %v%%", fp, (float64(fp)/256.0)*100)
|
||||||
|
|
||||||
|
}
|
|
@ -2,29 +2,32 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
"github.com/bwesterb/go-ristretto"
|
||||||
"github.com/gtank/merlin"
|
|
||||||
ristretto "github.com/gtank/ristretto255"
|
|
||||||
"golang.org/x/crypto/sha3"
|
"golang.org/x/crypto/sha3"
|
||||||
|
"hash"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Transcript provides a consistent transcript primitive for our protocols
|
// Transcript implements a transcript of a public coin argument.
|
||||||
//
|
//
|
||||||
// We have the following goals:
|
// We have the following goals:
|
||||||
// - Allow sequential proofs over a common transcript (ensuring a single proof cannot be extracted standalone)
|
// - Provide a consisted transcript API for our zero knowledge protocols
|
||||||
// - be able to produce a human-readable transcript for auditing.
|
// - Allow sequential proofs over a common transcript (ensuring a single proof cannot be extracted standalone)
|
||||||
|
// - produce an auditable human-readable transcript.
|
||||||
//
|
//
|
||||||
// The design of this API was inspired by Merlin: https://docs.rs/crate/merlin/
|
// The design of this API was inspired by Merlin: https://docs.rs/crate/merlin/
|
||||||
|
//
|
||||||
|
// At some point we might want to extend this to be compatible with Merlin transcripts, built on STROBE
|
||||||
type Transcript struct {
|
type Transcript struct {
|
||||||
merlinTranscript *merlin.Transcript
|
hash hash.Hash
|
||||||
transcript string
|
transcript string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTranscript creates a new Transcript with the given Label, the label should be unique to the application
|
// NewTranscript creates a new Transcript with the given Label, the label should be unique to the application
|
||||||
func NewTranscript(label string) *Transcript {
|
func NewTranscript(label string) *Transcript {
|
||||||
transcript := new(Transcript)
|
transcript := new(Transcript)
|
||||||
transcript.merlinTranscript = merlin.NewTranscript(label)
|
transcript.hash = sha3.New256()
|
||||||
|
transcript.AddToTranscript("protocol", []byte(label))
|
||||||
return transcript
|
return transcript
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,13 +36,7 @@ func NewTranscript(label string) *Transcript {
|
||||||
func (t *Transcript) AddToTranscript(label string, b []byte) {
|
func (t *Transcript) AddToTranscript(label string, b []byte) {
|
||||||
op := fmt.Sprintf("%s (%d) %x;", label, len(b), b)
|
op := fmt.Sprintf("%s (%d) %x;", label, len(b), b)
|
||||||
t.transcript = fmt.Sprintf("%v\n%v", t.transcript, op)
|
t.transcript = fmt.Sprintf("%v\n%v", t.transcript, op)
|
||||||
t.merlinTranscript.AppendMessage([]byte(label), b)
|
t.hash.Write([]byte(op))
|
||||||
}
|
|
||||||
|
|
||||||
// AddElementToTranscript appends a value to the transcript with the given label
|
|
||||||
// This binds the given data to the label.
|
|
||||||
func (t *Transcript) AddElementToTranscript(label string, element *ristretto.Element) {
|
|
||||||
t.AddToTranscript(label, element.Bytes())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// OutputTranscriptToAudit outputs a human-readable copy of the transcript so far.
|
// OutputTranscriptToAudit outputs a human-readable copy of the transcript so far.
|
||||||
|
@ -52,13 +49,14 @@ func (t Transcript) OutputTranscriptToAudit() string {
|
||||||
func (t *Transcript) NewProtocol(label string) {
|
func (t *Transcript) NewProtocol(label string) {
|
||||||
op := fmt.Sprintf("---- new-protcol: %s ----", label)
|
op := fmt.Sprintf("---- new-protcol: %s ----", label)
|
||||||
t.transcript = fmt.Sprintf("%v\n%v", t.transcript, op)
|
t.transcript = fmt.Sprintf("%v\n%v", t.transcript, op)
|
||||||
t.merlinTranscript.AppendMessage([]byte("protocol"), []byte(label))
|
t.hash.Write([]byte(op))
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommitToTranscript generates a challenge based on the current transcript, it also commits the challenge to the transcript.
|
// CommitToTranscript generates a challenge based on the current transcript, it also commits the challenge to the transcript.
|
||||||
func (t *Transcript) CommitToTranscript(label string) []byte {
|
func (t *Transcript) CommitToTranscript(label string) []byte {
|
||||||
b := t.merlinTranscript.ExtractBytes([]byte(label), 64)
|
t.AddToTranscript("commit", []byte(label))
|
||||||
t.transcript = fmt.Sprintf("%v\nextract %v: %v", t.transcript, label, b)
|
b := t.hash.Sum([]byte{})
|
||||||
|
t.AddToTranscript(label, b)
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,43 +66,26 @@ type PRNG struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next returns the next "random" scalar from the PRNG
|
// Next returns the next "random" scalar from the PRNG
|
||||||
func (prng *PRNG) Next(buf []byte, next *ristretto.Scalar) error {
|
func (prng *PRNG) Next() *ristretto.Scalar {
|
||||||
n, err := io.ReadFull(prng.prng, buf)
|
buf := [32]byte{}
|
||||||
if n != 64 || err != nil {
|
io.ReadFull(prng.prng, buf[:])
|
||||||
log.Errorf("could not read prng: %v %v", n, err)
|
return new(ristretto.Scalar).SetBytes(&buf)
|
||||||
return fmt.Errorf("error fetching complete output from prng: %v", err)
|
|
||||||
}
|
|
||||||
next.SetUniformBytes(buf)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommitToPRNG commits the label to the transcript and derives a PRNG from the transcript.
|
// CommitToPRNG commits the label to the transcript and derives a PRNG from the transcript.
|
||||||
func (t *Transcript) CommitToPRNG(label string) PRNG {
|
func (t *Transcript) CommitToPRNG(label string) PRNG {
|
||||||
b := t.merlinTranscript.ExtractBytes([]byte(label), 64)
|
t.AddToTranscript("commit-prng", []byte(label))
|
||||||
|
b := t.hash.Sum([]byte{})
|
||||||
|
t.AddToTranscript(label, b)
|
||||||
prng := sha3.NewShake256()
|
prng := sha3.NewShake256()
|
||||||
prng.Write(b)
|
prng.Write(b)
|
||||||
return PRNG{prng: prng}
|
return PRNG{prng: prng}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommitToGenerator derives a verifiably random generator from the transcript
|
|
||||||
func (t *Transcript) CommitToGenerator(label string) *ristretto.Element {
|
|
||||||
c := t.CommitToTranscript(label)
|
|
||||||
result, _ := new(ristretto.Element).SetUniformBytes(c)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
// CommitToGenerators derives a set of verifiably random generators from the transcript
|
|
||||||
func (t *Transcript) CommitToGenerators(label string, n int) (generators []*ristretto.Element) {
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
generators = append(generators, t.CommitToGenerator(fmt.Sprintf("%v-%d", label, i)))
|
|
||||||
}
|
|
||||||
return generators
|
|
||||||
}
|
|
||||||
|
|
||||||
// CommitToTranscriptScalar is a convenience method for CommitToTranscript which returns a ristretto Scalar
|
// CommitToTranscriptScalar is a convenience method for CommitToTranscript which returns a ristretto Scalar
|
||||||
func (t *Transcript) CommitToTranscriptScalar(label string) *ristretto.Scalar {
|
func (t *Transcript) CommitToTranscriptScalar(label string) *ristretto.Scalar {
|
||||||
c := t.CommitToTranscript(label)
|
c := t.CommitToTranscript(label)
|
||||||
s := new(ristretto.Scalar)
|
cs := [32]byte{}
|
||||||
s.SetUniformBytes(c[:])
|
copy(cs[:], c[:])
|
||||||
return s
|
return new(ristretto.Scalar).SetBytes(&cs)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,7 @@ func TestNewTranscript(t *testing.T) {
|
||||||
|
|
||||||
transcript.AddToTranscript("action", []byte("test data"))
|
transcript.AddToTranscript("action", []byte("test data"))
|
||||||
|
|
||||||
firstAudit := transcript.OutputTranscriptToAudit()
|
if transcript.OutputTranscriptToAudit() != transcript.OutputTranscriptToAudit() {
|
||||||
secondAudit := transcript.OutputTranscriptToAudit()
|
|
||||||
if firstAudit != secondAudit {
|
|
||||||
t.Fatalf("Multiple Audit Calls should not impact underlying Transcript")
|
t.Fatalf("Multiple Audit Calls should not impact underlying Transcript")
|
||||||
}
|
}
|
||||||
t.Logf("%v", transcript.OutputTranscriptToAudit())
|
t.Logf("%v", transcript.OutputTranscriptToAudit())
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
package primitives
|
||||||
|
|
||||||
|
import (
|
||||||
|
"cwtch.im/tapir/primitives/core"
|
||||||
|
"github.com/bwesterb/go-ristretto"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DLProof Encapsulates a Discrete Log / Schnorr Proof
|
||||||
|
// Note that these parameters are read-only.
|
||||||
|
type DLProof struct {
|
||||||
|
V, A ristretto.Point
|
||||||
|
R ristretto.Scalar
|
||||||
|
}
|
||||||
|
|
||||||
|
// DiscreteLogProof - Proof of Knowledge of Exponent
|
||||||
|
// Given V = xG
|
||||||
|
// Peggy: z := choose randomly from Zq
|
||||||
|
// A := zG
|
||||||
|
// c := H(transcript(G,V,A)) mod q
|
||||||
|
// r := (z + cx) mod q
|
||||||
|
//
|
||||||
|
// Sends A,r,V to Vicky
|
||||||
|
func DiscreteLogProof(x ristretto.Scalar, v ristretto.Point, transcript *core.Transcript) (proof DLProof) {
|
||||||
|
|
||||||
|
transcript.AddToTranscript("G", new(ristretto.Point).SetBase().Bytes())
|
||||||
|
|
||||||
|
// We bind the proof to our public V
|
||||||
|
proof.V = v
|
||||||
|
transcript.AddToTranscript("V", proof.V.Bytes())
|
||||||
|
|
||||||
|
// Generate a random z
|
||||||
|
// A := zG
|
||||||
|
z := new(ristretto.Scalar).Rand()
|
||||||
|
proof.A = *new(ristretto.Point).ScalarMultBase(z)
|
||||||
|
transcript.AddToTranscript("A", proof.A.Bytes())
|
||||||
|
|
||||||
|
// Derive Challenge
|
||||||
|
c := transcript.CommitToTranscriptScalar("c")
|
||||||
|
|
||||||
|
// r := (z + cx) mod p
|
||||||
|
cx := new(ristretto.Scalar).Mul(c, &x)
|
||||||
|
proof.R = *new(ristretto.Scalar).Add(z, cx)
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// VerifyDiscreteLogProof validates a given Schnorr Proof
|
||||||
|
// Vicky gets A,r,V from Peggy
|
||||||
|
// Vicky computes c := H(transcript(G,V,A)) mod q
|
||||||
|
// Vicky checks rG := A + cV
|
||||||
|
// rG ?= zG + cV
|
||||||
|
// (z+cx)G ?= zG + cV
|
||||||
|
// ?= zG + cxG
|
||||||
|
// Thus demonstrating that Peggy knows the discrete log to V
|
||||||
|
func VerifyDiscreteLogProof(proof DLProof, transcript *core.Transcript) bool {
|
||||||
|
|
||||||
|
transcript.AddToTranscript("G", new(ristretto.Point).SetBase().Bytes())
|
||||||
|
transcript.AddToTranscript("V", proof.V.Bytes())
|
||||||
|
transcript.AddToTranscript("A", proof.A.Bytes())
|
||||||
|
c := transcript.CommitToTranscriptScalar("c")
|
||||||
|
|
||||||
|
// Compute left hand side
|
||||||
|
lhs := new(ristretto.Point).ScalarMultBase(&proof.R)
|
||||||
|
|
||||||
|
// Compute right hand side
|
||||||
|
cV := new(ristretto.Point).ScalarMult(&proof.V, c)
|
||||||
|
rhs := new(ristretto.Point).Add(&proof.A, cV)
|
||||||
|
|
||||||
|
// Result of verification: lhs ?= rhs
|
||||||
|
return lhs.Equals(rhs)
|
||||||
|
}
|
|
@ -2,8 +2,7 @@ package primitives
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/utils"
|
"git.openprivacy.ca/openprivacy/libricochet-go/utils"
|
||||||
torProvider "git.openprivacy.ca/openprivacy/connectivity/tor"
|
|
||||||
"golang.org/x/crypto/ed25519"
|
"golang.org/x/crypto/ed25519"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -43,14 +42,14 @@ func (i *Identity) PublicKey() ed25519.PublicKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
// EDH performs a diffie-hellman operation on this identities private key with the given public key.
|
// EDH performs a diffie-hellman operation on this identities private key with the given public key.
|
||||||
func (i *Identity) EDH(key ed25519.PublicKey) ([]byte, error) {
|
func (i *Identity) EDH(key ed25519.PublicKey) []byte {
|
||||||
secret, err := utils.EDH(*i.edpk, key)
|
secret := utils.EDH(*i.edpk, key)
|
||||||
return secret[:], err
|
return secret[:]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hostname provides the onion address associated with this Identity.
|
// Hostname provides the onion address associated with this Identity.
|
||||||
func (i *Identity) Hostname() string {
|
func (i *Identity) Hostname() string {
|
||||||
return torProvider.GetTorV3Hostname(*i.edpubk)
|
return utils.GetTorV3Hostname(*i.edpubk)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sign produces a signature for a given message attributable to the given identity
|
// Sign produces a signature for a given message attributable to the given identity
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package primitives
|
package primitives
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/subtle"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -10,28 +9,9 @@ func TestIdentity_EDH(t *testing.T) {
|
||||||
id1, _ := InitializeEphemeralIdentity()
|
id1, _ := InitializeEphemeralIdentity()
|
||||||
id2, _ := InitializeEphemeralIdentity()
|
id2, _ := InitializeEphemeralIdentity()
|
||||||
|
|
||||||
k1, err1 := id1.EDH(id2.PublicKey())
|
k1 := id1.EDH(id2.PublicKey())
|
||||||
k2, err2 := id2.EDH(id1.PublicKey())
|
k2 := id2.EDH(id1.PublicKey())
|
||||||
|
|
||||||
if err1 == nil && err2 == nil && subtle.ConstantTimeCompare(k1, k2) == 1 {
|
t.Logf("k1: %x\nk2: %x\n", k1, k2)
|
||||||
t.Logf("k1: %x\nk2: %x\n", k1, k2)
|
|
||||||
} else {
|
|
||||||
t.Fatalf("The derived keys should be identical")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkEDH(b *testing.B) {
|
|
||||||
id1, _ := InitializeEphemeralIdentity()
|
|
||||||
id2, _ := InitializeEphemeralIdentity()
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
k1, err1 := id1.EDH(id2.PublicKey())
|
|
||||||
k2, err2 := id2.EDH(id1.PublicKey())
|
|
||||||
if err1 == nil && err2 == nil && subtle.ConstantTimeCompare(k1, k2) == 1 {
|
|
||||||
//b.Logf("k1: %x\nk2: %x\n", k1, k2)
|
|
||||||
} else {
|
|
||||||
b.Fatalf("The derived keys should be identical")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,37 +1,30 @@
|
||||||
package privacypass
|
package privacypass
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"cwtch.im/tapir/primitives/core"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/core"
|
"github.com/bwesterb/go-ristretto"
|
||||||
ristretto "github.com/gtank/ristretto255"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// DLEQProof encapsulates a Chaum-Pedersen DLEQ Proof
|
// DLEQProof encapsulates a Chaum-Pedersen DLEQ Proof
|
||||||
// gut In Ernest F. Brickell, editor,CRYPTO’92,volume 740 ofLNCS, pages 89–105. Springer, Heidelberg,August 1993
|
// David Chaum and Torben P. Pedersen. Wallet databaseswith observers. In Ernest F. Brickell, editor,CRYPTO’92,volume 740 ofLNCS, pages 89–105. Springer, Heidelberg,August 1993
|
||||||
type DLEQProof struct {
|
type DLEQProof struct {
|
||||||
C *ristretto.Scalar
|
C *ristretto.Scalar
|
||||||
S *ristretto.Scalar
|
S *ristretto.Scalar
|
||||||
}
|
}
|
||||||
|
|
||||||
// DiscreteLogEquivalenceProof constructs a valid DLEQProof for the given parameters and transcript
|
// DiscreteLogEquivalenceProof constructs a valid DLEQProof for the given parameters and transcript
|
||||||
// Given Y = kX & Q = kP
|
// Given P = kX, Q = kP, Y=kX
|
||||||
// Peggy: t := choose randomly from Zq
|
// Peggy: t := choose randomly from Zq
|
||||||
//
|
// A := tX
|
||||||
// A := tX
|
// B := tP
|
||||||
// B := tP
|
// c := H(transcript(X,Y,P,Q,A,B))
|
||||||
// c := H(transcript(X,Y,P,Q,A,B))
|
// s := (t + ck) mod q
|
||||||
// s := (t + ck) mod q
|
|
||||||
//
|
//
|
||||||
// Sends c,s to Vicky
|
// Sends c,s to Vicky
|
||||||
func DiscreteLogEquivalenceProof(k *ristretto.Scalar, X *ristretto.Element, Y *ristretto.Element, P *ristretto.Element, Q *ristretto.Element, transcript *core.Transcript) DLEQProof {
|
func DiscreteLogEquivalenceProof(k *ristretto.Scalar, X *ristretto.Point, Y *ristretto.Point, P *ristretto.Point, Q *ristretto.Point, transcript *core.Transcript) DLEQProof {
|
||||||
private := make([]byte, 64)
|
t := new(ristretto.Scalar).Rand()
|
||||||
rand.Read(private)
|
A := new(ristretto.Point).ScalarMult(X, t)
|
||||||
t, err := new(ristretto.Scalar).SetUniformBytes(private)
|
B := new(ristretto.Point).ScalarMult(P, t)
|
||||||
if err != nil {
|
|
||||||
return DLEQProof{ristretto.NewScalar(), ristretto.NewScalar()}
|
|
||||||
}
|
|
||||||
A := new(ristretto.Element).ScalarMult(t, X)
|
|
||||||
B := new(ristretto.Element).ScalarMult(t, P)
|
|
||||||
|
|
||||||
transcript.AddToTranscript(DLEQX, X.Bytes())
|
transcript.AddToTranscript(DLEQX, X.Bytes())
|
||||||
transcript.AddToTranscript(DLEQY, Y.Bytes())
|
transcript.AddToTranscript(DLEQY, Y.Bytes())
|
||||||
|
@ -41,31 +34,29 @@ func DiscreteLogEquivalenceProof(k *ristretto.Scalar, X *ristretto.Element, Y *r
|
||||||
transcript.AddToTranscript(DLEQB, B.Bytes())
|
transcript.AddToTranscript(DLEQB, B.Bytes())
|
||||||
|
|
||||||
c := transcript.CommitToTranscriptScalar("c")
|
c := transcript.CommitToTranscriptScalar("c")
|
||||||
s := new(ristretto.Scalar).Subtract(t, new(ristretto.Scalar).Multiply(c, k))
|
s := new(ristretto.Scalar).Sub(t, new(ristretto.Scalar).Mul(c, k))
|
||||||
return DLEQProof{c, s}
|
return DLEQProof{c, s}
|
||||||
}
|
}
|
||||||
|
|
||||||
// VerifyDiscreteLogEquivalenceProof verifies the DLEQ for the given parameters and transcript
|
// VerifyDiscreteLogEquivalenceProof verifies the DLEQ for the given parameters and transcript
|
||||||
// Given Y = kX & Q = kP and Proof = (c,s)
|
// Given P = kX, Q = kP, Y=kX, and Proof = (c,s)
|
||||||
// Vicky: X' := sX
|
// Vicky: X' := sX
|
||||||
//
|
// Y' := cY
|
||||||
// Y' := cY
|
// P' := sP
|
||||||
// P' := sP
|
// Q' := cQ
|
||||||
// Q' := cQ
|
// A' = X'+Y' == sX + cY ?= sX + ckX == (s+ck)X == tX == A
|
||||||
// A' = X'+Y' == sX + cY ?= sG + ckG == (s+ck)X == tX == A
|
// B' = P'+Q' == sP + cQ ?= sP + ckP == (s+ck)P == tP == B
|
||||||
// B' = P'+Q' == sP + cQ ?= sP + ckP == (s+ck)P == tP == B
|
// c' := H(transcript(X,Y,P,Q,A',B'))
|
||||||
// c' := H(transcript(X,Y,P,Q,A',B'))
|
|
||||||
//
|
|
||||||
// Tests c ?= c
|
// Tests c ?= c
|
||||||
func VerifyDiscreteLogEquivalenceProof(dleq DLEQProof, X *ristretto.Element, Y *ristretto.Element, P *ristretto.Element, Q *ristretto.Element, transcript *core.Transcript) bool {
|
func VerifyDiscreteLogEquivalenceProof(dleq DLEQProof, X *ristretto.Point, Y *ristretto.Point, P *ristretto.Point, Q *ristretto.Point, transcript *core.Transcript) bool {
|
||||||
|
|
||||||
Xs := new(ristretto.Element).ScalarMult(dleq.S, X)
|
Xs := new(ristretto.Point).ScalarMult(X, dleq.S)
|
||||||
Yc := new(ristretto.Element).ScalarMult(dleq.C, Y)
|
Yc := new(ristretto.Point).ScalarMult(Y, dleq.C)
|
||||||
Ps := new(ristretto.Element).ScalarMult(dleq.S, P)
|
Ps := new(ristretto.Point).ScalarMult(P, dleq.S)
|
||||||
Qc := new(ristretto.Element).ScalarMult(dleq.C, Q)
|
Qc := new(ristretto.Point).ScalarMult(Q, dleq.C)
|
||||||
|
|
||||||
A := new(ristretto.Element).Add(Xs, Yc)
|
A := new(ristretto.Point).Add(Xs, Yc)
|
||||||
B := new(ristretto.Element).Add(Ps, Qc)
|
B := new(ristretto.Point).Add(Ps, Qc)
|
||||||
|
|
||||||
transcript.AddToTranscript(DLEQX, X.Bytes())
|
transcript.AddToTranscript(DLEQX, X.Bytes())
|
||||||
transcript.AddToTranscript(DLEQY, Y.Bytes())
|
transcript.AddToTranscript(DLEQY, Y.Bytes())
|
||||||
|
@ -74,5 +65,5 @@ func VerifyDiscreteLogEquivalenceProof(dleq DLEQProof, X *ristretto.Element, Y *
|
||||||
transcript.AddToTranscript(DLEQA, A.Bytes())
|
transcript.AddToTranscript(DLEQA, A.Bytes())
|
||||||
transcript.AddToTranscript(DLEQB, B.Bytes())
|
transcript.AddToTranscript(DLEQB, B.Bytes())
|
||||||
|
|
||||||
return transcript.CommitToTranscriptScalar("c").Equal(dleq.C) == 1
|
return transcript.CommitToTranscriptScalar("c").Equals(dleq.C)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,12 +3,10 @@ package privacypass
|
||||||
import (
|
import (
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/json"
|
"cwtch.im/tapir/primitives/core"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/core"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
"github.com/bwesterb/go-ristretto"
|
||||||
ristretto "github.com/gtank/ristretto255"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/sha3"
|
"golang.org/x/crypto/sha3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,22 +15,17 @@ import (
|
||||||
type Token struct {
|
type Token struct {
|
||||||
t []byte
|
t []byte
|
||||||
r *ristretto.Scalar
|
r *ristretto.Scalar
|
||||||
W *ristretto.Element
|
W *ristretto.Point
|
||||||
}
|
|
||||||
|
|
||||||
// GetT returns the underlying bytes for token for use in constraint proofs.
|
|
||||||
func (t Token) GetT() []byte {
|
|
||||||
return t.t
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// BlindedToken encapsulates a Blinded Token
|
// BlindedToken encapsulates a Blinded Token
|
||||||
type BlindedToken struct {
|
type BlindedToken struct {
|
||||||
P *ristretto.Element
|
P *ristretto.Point
|
||||||
}
|
}
|
||||||
|
|
||||||
// SignedToken encapsulates a Signed (Blinded) Token
|
// SignedToken encapsulates a Signed (Blinded) Token
|
||||||
type SignedToken struct {
|
type SignedToken struct {
|
||||||
Q *ristretto.Element
|
Q *ristretto.Point
|
||||||
}
|
}
|
||||||
|
|
||||||
// SpentToken encapsulates the parameters needed to spend a Token
|
// SpentToken encapsulates the parameters needed to spend a Token
|
||||||
|
@ -44,8 +37,7 @@ type SpentToken struct {
|
||||||
// TokenPaymentHandler defines an interface with external payment processors
|
// TokenPaymentHandler defines an interface with external payment processors
|
||||||
type TokenPaymentHandler interface {
|
type TokenPaymentHandler interface {
|
||||||
MakePayment()
|
MakePayment()
|
||||||
// Next Token
|
NextToken(data []byte) (SpentToken, error)
|
||||||
NextToken(data []byte, hostname string) (SpentToken, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenBlindedToken initializes the Token
|
// GenBlindedToken initializes the Token
|
||||||
|
@ -53,28 +45,25 @@ type TokenPaymentHandler interface {
|
||||||
func (t *Token) GenBlindedToken() BlindedToken {
|
func (t *Token) GenBlindedToken() BlindedToken {
|
||||||
t.t = make([]byte, 32)
|
t.t = make([]byte, 32)
|
||||||
rand.Read(t.t)
|
rand.Read(t.t)
|
||||||
t.r = new(ristretto.Scalar)
|
t.r = new(ristretto.Scalar).Rand()
|
||||||
b := make([]byte, 64)
|
|
||||||
rand.Read(b)
|
|
||||||
t.r.SetUniformBytes(b)
|
|
||||||
|
|
||||||
Ht := sha3.Sum512(t.t)
|
Ht := sha3.Sum256(t.t)
|
||||||
T, _ := new(ristretto.Element).SetUniformBytes(Ht[:])
|
log.Debugf("token: %x", Ht)
|
||||||
P := new(ristretto.Element).ScalarMult(t.r, T)
|
T := new(ristretto.Point).SetElligator(&Ht)
|
||||||
|
P := new(ristretto.Point).ScalarMult(T, t.r)
|
||||||
return BlindedToken{P}
|
return BlindedToken{P}
|
||||||
}
|
}
|
||||||
|
|
||||||
// unblindSignedToken unblinds a token that has been signed by a server
|
// unblindSignedToken unblinds a token that has been signed by a server
|
||||||
func (t *Token) unblindSignedToken(token SignedToken) {
|
func (t *Token) unblindSignedToken(token SignedToken) {
|
||||||
t.W = new(ristretto.Element).ScalarMult(new(ristretto.Scalar).Invert(t.r), token.Q)
|
t.W = new(ristretto.Point).ScalarMult(token.Q, new(ristretto.Scalar).Inverse(t.r))
|
||||||
}
|
}
|
||||||
|
|
||||||
// SpendToken binds the token with data and then redeems the token
|
// SpendToken binds the token with data and then redeems the token
|
||||||
func (t *Token) SpendToken(data []byte) SpentToken {
|
func (t *Token) SpendToken(data []byte) SpentToken {
|
||||||
key := sha3.Sum256(append(t.t, t.W.Bytes()...))
|
key := sha3.Sum256(append(t.t, t.W.Bytes()...))
|
||||||
mac := hmac.New(sha3.New512, key[:])
|
mac := hmac.New(sha3.New512, key[:])
|
||||||
mac.Write(data)
|
return SpentToken{t.t, mac.Sum(data)}
|
||||||
return SpentToken{t.t, mac.Sum(nil)}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenerateBlindedTokenBatch generates a batch of blinded tokens (and their unblinded equivalents)
|
// GenerateBlindedTokenBatch generates a batch of blinded tokens (and their unblinded equivalents)
|
||||||
|
@ -87,35 +76,28 @@ func GenerateBlindedTokenBatch(num int) (tokens []*Token, blindedTokens []Blinde
|
||||||
}
|
}
|
||||||
|
|
||||||
// verifyBatchProof verifies a given batch proof (see also UnblindSignedTokenBatch)
|
// verifyBatchProof verifies a given batch proof (see also UnblindSignedTokenBatch)
|
||||||
func verifyBatchProof(dleq DLEQProof, Y *ristretto.Element, blindedTokens []BlindedToken, signedTokens []SignedToken, transcript *core.Transcript) bool {
|
func verifyBatchProof(dleq DLEQProof, Y *ristretto.Point, blindedTokens []BlindedToken, signedTokens []SignedToken, transcript *core.Transcript) bool {
|
||||||
transcript.NewProtocol(BatchProofProtocol)
|
transcript.NewProtocol(BatchProofProtocol)
|
||||||
transcript.AddToTranscript(BatchProofX, ristretto.NewGeneratorElement().Bytes())
|
transcript.AddToTranscript(BatchProofX, new(ristretto.Point).SetBase().Bytes())
|
||||||
transcript.AddToTranscript(BatchProofY, Y.Bytes())
|
transcript.AddToTranscript(BatchProofY, Y.Bytes())
|
||||||
transcript.AddToTranscript(BatchProofPVector, []byte(fmt.Sprintf("%v", blindedTokens)))
|
transcript.AddToTranscript(BatchProofPVector, []byte(fmt.Sprintf("%v", blindedTokens)))
|
||||||
transcript.AddToTranscript(BatchProofQVector, []byte(fmt.Sprintf("%v", signedTokens)))
|
transcript.AddToTranscript(BatchProofQVector, []byte(fmt.Sprintf("%v", signedTokens)))
|
||||||
prng := transcript.CommitToPRNG("w")
|
prng := transcript.CommitToPRNG("w")
|
||||||
M := ristretto.NewIdentityElement()
|
M := new(ristretto.Point).SetZero()
|
||||||
Z := ristretto.NewIdentityElement()
|
Z := new(ristretto.Point).SetZero()
|
||||||
buf := make([]byte, 64)
|
|
||||||
c := new(ristretto.Scalar)
|
|
||||||
for i := range blindedTokens {
|
for i := range blindedTokens {
|
||||||
err := prng.Next(buf, c)
|
c := prng.Next()
|
||||||
if err != nil {
|
M = new(ristretto.Point).Add(new(ristretto.Point).ScalarMult(blindedTokens[i].P, c), M)
|
||||||
log.Errorf("error verifying batch proof: %v", err)
|
Z = new(ristretto.Point).Add(new(ristretto.Point).ScalarMult(signedTokens[i].Q, c), Z)
|
||||||
return false
|
|
||||||
}
|
|
||||||
M = new(ristretto.Element).Add(new(ristretto.Element).ScalarMult(c, blindedTokens[i].P), M)
|
|
||||||
Z = new(ristretto.Element).Add(new(ristretto.Element).ScalarMult(c, signedTokens[i].Q), Z)
|
|
||||||
}
|
}
|
||||||
return VerifyDiscreteLogEquivalenceProof(dleq, ristretto.NewGeneratorElement(), Y, M, Z, transcript)
|
return VerifyDiscreteLogEquivalenceProof(dleq, new(ristretto.Point).SetBase(), Y, M, Z, transcript)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnblindSignedTokenBatch taking in a set of tokens, their blinded & signed counterparts, a server public key (Y), a DLEQ proof and a transcript
|
// UnblindSignedTokenBatch taking in a set of tokens, their blinded & signed counterparts, a server public key (Y), a DLEQ proof and a transcript
|
||||||
// verifies that the signing procedure has taken place correctly and unblinds the tokens.
|
// verifies that the signing procedure has taken place correctly and unblinds the tokens.
|
||||||
func UnblindSignedTokenBatch(tokens []*Token, blindedTokens []BlindedToken, signedTokens []SignedToken, Y *ristretto.Element, proof DLEQProof, transcript *core.Transcript) bool {
|
func UnblindSignedTokenBatch(tokens []*Token, blindedTokens []BlindedToken, signedTokens []SignedToken, Y *ristretto.Point, proof DLEQProof, transcript *core.Transcript) bool {
|
||||||
verified := verifyBatchProof(proof, Y, blindedTokens, signedTokens, transcript)
|
verified := verifyBatchProof(proof, Y, blindedTokens, signedTokens, transcript)
|
||||||
if !verified {
|
if !verified {
|
||||||
log.Debugf("Failed to unblind tokens: %v", transcript.OutputTranscriptToAudit())
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for i, t := range tokens {
|
for i, t := range tokens {
|
||||||
|
@ -123,16 +105,3 @@ func UnblindSignedTokenBatch(tokens []*Token, blindedTokens []BlindedToken, sign
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON - in order to store tokens in a serialized form we need to expose the private, unexported value
|
|
||||||
// `t`. Note that `r` is not needed to spend the token, and as such we effectively destroy it when we serialize.
|
|
||||||
// Ideally, go would let us do this with an annotation, alas.
|
|
||||||
func (t Token) MarshalJSON() ([]byte, error) {
|
|
||||||
return json.Marshal(struct {
|
|
||||||
T []byte `json:"t"`
|
|
||||||
W *ristretto.Element
|
|
||||||
}{
|
|
||||||
T: t.t,
|
|
||||||
W: t.W,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
package privacypass
|
package privacypass
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/sha512"
|
"cwtch.im/tapir/persistence"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/persistence"
|
"cwtch.im/tapir/primitives/core"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/core"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
|
||||||
"github.com/gtank/ristretto255"
|
|
||||||
"golang.org/x/crypto/sha3"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -34,59 +31,17 @@ func TestToken_SpendToken(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestToken_ConstrainToToken(t *testing.T) {
|
|
||||||
server := NewTokenServer()
|
|
||||||
|
|
||||||
token := new(Token)
|
|
||||||
blindedToken := token.GenBlindedToken()
|
|
||||||
|
|
||||||
signedToken := server.SignBlindedToken(blindedToken)
|
|
||||||
token.unblindSignedToken(signedToken)
|
|
||||||
|
|
||||||
spentToken := token.SpendToken([]byte("Hello"))
|
|
||||||
|
|
||||||
if server.SpendToken(spentToken, []byte("Hello World")) == nil {
|
|
||||||
t.Errorf("Token Should be InValid")
|
|
||||||
}
|
|
||||||
|
|
||||||
token2 := new(Token)
|
|
||||||
blindedToken2 := token2.GenBlindedToken()
|
|
||||||
Ht := sha3.Sum512(token.t)
|
|
||||||
T, _ := new(ristretto255.Element).SetUniformBytes(Ht[:])
|
|
||||||
// Constraint forces T = kW to be part of the batch proof
|
|
||||||
// And because the batch proof must prove that *all* inputs share the same key and also checks the servers public key
|
|
||||||
// We get a consistency check for almost free.
|
|
||||||
signedTokens, err := server.SignBlindedTokenBatchWithConstraint([]BlindedToken{blindedToken2}, token.t, core.NewTranscript(""))
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("error signing tokens with constraints")
|
|
||||||
}
|
|
||||||
|
|
||||||
transcript := core.NewTranscript("")
|
|
||||||
|
|
||||||
// NOTE: For this to work token.t and token.W need to be obtain by the client from known source e.g. a public message board.
|
|
||||||
t.Logf("Result of constaint proof %v", UnblindSignedTokenBatch([]*Token{token2}, []BlindedToken{blindedToken2, {P: T}}, append(signedTokens.SignedTokens, SignedToken{token.W}), server.Y, signedTokens.Proof, transcript))
|
|
||||||
t.Log(transcript.OutputTranscriptToAudit())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestGenerateBlindedTokenBatch(t *testing.T) {
|
func TestGenerateBlindedTokenBatch(t *testing.T) {
|
||||||
log.SetLevel(log.LevelDebug)
|
log.SetLevel(log.LevelDebug)
|
||||||
db := new(persistence.BoltPersistence)
|
db := new(persistence.BoltPersistence)
|
||||||
db.Open("tokens.db")
|
db.Open("tokens.db")
|
||||||
|
server := NewTokenServerFromStore(db)
|
||||||
fakeRand := sha512.Sum512([]byte{})
|
|
||||||
k, _ := ristretto255.NewScalar().SetUniformBytes(fakeRand[:])
|
|
||||||
server := NewTokenServerFromStore(k, db)
|
|
||||||
defer server.Close()
|
|
||||||
|
|
||||||
clientTranscript := core.NewTranscript("privacyPass")
|
clientTranscript := core.NewTranscript("privacyPass")
|
||||||
serverTranscript := core.NewTranscript("privacyPass")
|
serverTranscript := core.NewTranscript("privacyPass")
|
||||||
|
|
||||||
tokens, blindedTokens := GenerateBlindedTokenBatch(10)
|
tokens, blindedTokens := GenerateBlindedTokenBatch(10)
|
||||||
batchProof, err := server.SignBlindedTokenBatch(blindedTokens, serverTranscript)
|
batchProof := server.SignBlindedTokenBatch(blindedTokens, serverTranscript)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("error constructing signed/blinded token batch: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
verified := UnblindSignedTokenBatch(tokens, blindedTokens, batchProof.SignedTokens, server.Y, batchProof.Proof, clientTranscript)
|
verified := UnblindSignedTokenBatch(tokens, blindedTokens, batchProof.SignedTokens, server.Y, batchProof.Proof, clientTranscript)
|
||||||
|
|
||||||
|
@ -110,5 +65,5 @@ func TestGenerateBlindedTokenBatch(t *testing.T) {
|
||||||
if verified {
|
if verified {
|
||||||
t.Errorf("Something went wrong, the proof passed with wrong transcript: %s", wrongTranscript.OutputTranscriptToAudit())
|
t.Errorf("Something went wrong, the proof passed with wrong transcript: %s", wrongTranscript.OutputTranscriptToAudit())
|
||||||
}
|
}
|
||||||
|
db.Close()
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,11 @@ package privacypass
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/rand"
|
"cwtch.im/tapir/persistence"
|
||||||
|
"cwtch.im/tapir/primitives/core"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/persistence"
|
"github.com/bwesterb/go-ristretto"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives/core"
|
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
|
||||||
ristretto "github.com/gtank/ristretto255"
|
|
||||||
"golang.org/x/crypto/sha3"
|
"golang.org/x/crypto/sha3"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
@ -16,7 +14,7 @@ import (
|
||||||
// TokenServer implements a token server.
|
// TokenServer implements a token server.
|
||||||
type TokenServer struct {
|
type TokenServer struct {
|
||||||
k *ristretto.Scalar
|
k *ristretto.Scalar
|
||||||
Y *ristretto.Element
|
Y *ristretto.Point
|
||||||
seen map[string]bool
|
seen map[string]bool
|
||||||
persistanceService persistence.Service
|
persistanceService persistence.Service
|
||||||
mutex sync.Mutex
|
mutex sync.Mutex
|
||||||
|
@ -24,114 +22,58 @@ type TokenServer struct {
|
||||||
|
|
||||||
// SignedBatchWithProof encapsulates a signed batch of blinded tokens with a batch proof for verification
|
// SignedBatchWithProof encapsulates a signed batch of blinded tokens with a batch proof for verification
|
||||||
type SignedBatchWithProof struct {
|
type SignedBatchWithProof struct {
|
||||||
SignedTokens []SignedToken `json:"st"`
|
SignedTokens []SignedToken
|
||||||
Proof DLEQProof `json:"dp"`
|
Proof DLEQProof
|
||||||
}
|
}
|
||||||
|
|
||||||
const tokenBucket = "tokens"
|
const tokenBucket = "tokens"
|
||||||
|
|
||||||
// NewTokenServer generates a new TokenServer (used mostly for testing with ephemeral instances)
|
// NewTokenServer generates a new TokenServer (used mostly for testing with ephemeral instances)
|
||||||
func NewTokenServer() *TokenServer {
|
func NewTokenServer() TokenServer {
|
||||||
k := new(ristretto.Scalar)
|
k := new(ristretto.Scalar).Rand()
|
||||||
b := make([]byte, 64)
|
return TokenServer{k, new(ristretto.Point).ScalarMultBase(k), make(map[string]bool), nil, sync.Mutex{}}
|
||||||
_, err := rand.Read(b)
|
|
||||||
if err != nil {
|
|
||||||
// unable to generate secure random numbers
|
|
||||||
panic("unable to generate secure random numbers")
|
|
||||||
}
|
|
||||||
k.SetUniformBytes(b)
|
|
||||||
return &TokenServer{k, new(ristretto.Element).ScalarBaseMult(k), make(map[string]bool), nil, sync.Mutex{}}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTokenServerFromStore generates a new TokenServer backed by a persistence service.
|
// NewTokenServerFromStore generates a new TokenServer backed by a persistence service.
|
||||||
func NewTokenServerFromStore(k *ristretto.Scalar, persistenceService persistence.Service) *TokenServer {
|
func NewTokenServerFromStore(persistenceService persistence.Service) TokenServer {
|
||||||
tokenServer := NewTokenServer()
|
k := new(ristretto.Scalar).Rand()
|
||||||
persistenceService.Setup([]string{tokenBucket})
|
persistenceService.Setup([]string{tokenBucket})
|
||||||
|
return TokenServer{k, new(ristretto.Point).ScalarMultBase(k), make(map[string]bool), persistenceService, sync.Mutex{}}
|
||||||
// recalculate public key from k
|
|
||||||
tokenServer.k = k
|
|
||||||
tokenServer.Y = new(ristretto.Element).ScalarBaseMult(tokenServer.k)
|
|
||||||
|
|
||||||
tokenServer.persistanceService = persistenceService
|
|
||||||
return tokenServer
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close ensures that the database is properly closed...
|
|
||||||
func (ts *TokenServer) Close() {
|
|
||||||
ts.mutex.Lock()
|
|
||||||
defer ts.mutex.Unlock()
|
|
||||||
ts.persistanceService.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SignBlindedToken calculates kP for the given BlindedToken P
|
// SignBlindedToken calculates kP for the given BlindedToken P
|
||||||
func (ts *TokenServer) SignBlindedToken(bt BlindedToken) SignedToken {
|
func (ts *TokenServer) SignBlindedToken(bt BlindedToken) SignedToken {
|
||||||
Q := new(ristretto.Element).ScalarMult(ts.k, bt.P)
|
Q := new(ristretto.Point).ScalarMult(bt.P, ts.k)
|
||||||
return SignedToken{Q}
|
return SignedToken{Q}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SignBlindedTokenBatch signs a batch of blinded tokens under a given transcript
|
// SignBlindedTokenBatch signs a batch of blinded tokens under a given transcript
|
||||||
func (ts *TokenServer) SignBlindedTokenBatch(blindedTokens []BlindedToken, transcript *core.Transcript) (*SignedBatchWithProof, error) {
|
func (ts *TokenServer) SignBlindedTokenBatch(blindedTokens []BlindedToken, transcript *core.Transcript) SignedBatchWithProof {
|
||||||
var signedTokens []SignedToken
|
var signedTokens []SignedToken
|
||||||
for _, bt := range blindedTokens {
|
for _, bt := range blindedTokens {
|
||||||
signedTokens = append(signedTokens, ts.SignBlindedToken(bt))
|
signedTokens = append(signedTokens, ts.SignBlindedToken(bt))
|
||||||
}
|
}
|
||||||
|
return SignedBatchWithProof{signedTokens, ts.constructBatchProof(blindedTokens, signedTokens, transcript)}
|
||||||
proof, err := ts.constructBatchProof(blindedTokens, signedTokens, transcript)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
signedProof := SignedBatchWithProof{signedTokens, *proof}
|
|
||||||
return &signedProof, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SignBlindedTokenBatchWithConstraint signs a batch of blinded tokens under a given transcript given a constraint that the tokens must be signed
|
|
||||||
// by the same public key as an existing token
|
|
||||||
func (ts *TokenServer) SignBlindedTokenBatchWithConstraint(blindedTokens []BlindedToken, constraintToken []byte, transcript *core.Transcript) (*SignedBatchWithProof, error) {
|
|
||||||
var signedTokens []SignedToken
|
|
||||||
for _, bt := range blindedTokens {
|
|
||||||
signedTokens = append(signedTokens, ts.SignBlindedToken(bt))
|
|
||||||
}
|
|
||||||
Ht := sha3.Sum512(constraintToken)
|
|
||||||
T, err := new(ristretto.Element).SetUniformBytes(Ht[:])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// W == kT
|
|
||||||
W := new(ristretto.Element).ScalarMult(ts.k, T)
|
|
||||||
blindedTokens = append(blindedTokens, BlindedToken{P: T})
|
|
||||||
proof, err := ts.constructBatchProof(blindedTokens, append(signedTokens, SignedToken{Q: W}), transcript)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
signedProof := SignedBatchWithProof{signedTokens, *proof}
|
|
||||||
return &signedProof, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// constructBatchProof construct a batch proof that all the signed tokens have been signed correctly
|
// constructBatchProof construct a batch proof that all the signed tokens have been signed correctly
|
||||||
func (ts *TokenServer) constructBatchProof(blindedTokens []BlindedToken, signedTokens []SignedToken, transcript *core.Transcript) (*DLEQProof, error) {
|
func (ts *TokenServer) constructBatchProof(blindedTokens []BlindedToken, signedTokens []SignedToken, transcript *core.Transcript) DLEQProof {
|
||||||
transcript.NewProtocol(BatchProofProtocol)
|
transcript.NewProtocol(BatchProofProtocol)
|
||||||
transcript.AddToTranscript(BatchProofX, ristretto.NewGeneratorElement().Bytes())
|
transcript.AddToTranscript(BatchProofX, new(ristretto.Point).SetBase().Bytes())
|
||||||
transcript.AddToTranscript(BatchProofY, ts.Y.Bytes())
|
transcript.AddToTranscript(BatchProofY, ts.Y.Bytes())
|
||||||
transcript.AddToTranscript(BatchProofPVector, []byte(fmt.Sprintf("%v", blindedTokens)))
|
transcript.AddToTranscript(BatchProofPVector, []byte(fmt.Sprintf("%v", blindedTokens)))
|
||||||
transcript.AddToTranscript(BatchProofQVector, []byte(fmt.Sprintf("%v", signedTokens)))
|
transcript.AddToTranscript(BatchProofQVector, []byte(fmt.Sprintf("%v", signedTokens)))
|
||||||
prng := transcript.CommitToPRNG("w")
|
prng := transcript.CommitToPRNG("w")
|
||||||
|
|
||||||
M := ristretto.NewIdentityElement()
|
M := new(ristretto.Point).SetZero()
|
||||||
Z := ristretto.NewIdentityElement()
|
Z := new(ristretto.Point).SetZero()
|
||||||
|
|
||||||
buf := make([]byte, 64)
|
|
||||||
c := new(ristretto.Scalar)
|
|
||||||
for i := range blindedTokens {
|
for i := range blindedTokens {
|
||||||
err := prng.Next(buf, c)
|
c := prng.Next()
|
||||||
if err != nil {
|
M = new(ristretto.Point).Add(new(ristretto.Point).ScalarMult(blindedTokens[i].P, c), M)
|
||||||
log.Errorf("error constructing batch proof: %v", err)
|
Z = new(ristretto.Point).Add(new(ristretto.Point).ScalarMult(signedTokens[i].Q, c), Z)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
M = new(ristretto.Element).Add(new(ristretto.Element).ScalarMult(c, blindedTokens[i].P), M)
|
|
||||||
Z = new(ristretto.Element).Add(new(ristretto.Element).ScalarMult(c, signedTokens[i].Q), Z)
|
|
||||||
}
|
}
|
||||||
proof := DiscreteLogEquivalenceProof(ts.k, ristretto.NewGeneratorElement(), ts.Y, M, Z, transcript)
|
return DiscreteLogEquivalenceProof(ts.k, new(ristretto.Point).SetBase(), ts.Y, M, Z, transcript)
|
||||||
return &proof, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SpendToken returns true a SpentToken is valid and has never been spent before, false otherwise.
|
// SpendToken returns true a SpentToken is valid and has never been spent before, false otherwise.
|
||||||
|
@ -144,22 +86,18 @@ func (ts *TokenServer) SpendToken(token SpentToken, data []byte) error {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
spent, err := ts.persistanceService.Check(tokenBucket, hex.EncodeToString(token.T))
|
spent, err := ts.persistanceService.Check(tokenBucket, hex.EncodeToString(token.T))
|
||||||
if err != nil || spent {
|
if err != nil || spent == true {
|
||||||
return fmt.Errorf("token: %v has already been spent", token)
|
return fmt.Errorf("token: %v has already been spent", token)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ht := sha3.Sum512(token.T)
|
Ht := sha3.Sum256(token.T)
|
||||||
T, err := new(ristretto.Element).SetUniformBytes(Ht[:])
|
T := new(ristretto.Point).SetElligator(&Ht)
|
||||||
if err != nil {
|
W := new(ristretto.Point).ScalarMult(T, ts.k)
|
||||||
return err
|
|
||||||
}
|
|
||||||
W := new(ristretto.Element).ScalarMult(ts.k, T)
|
|
||||||
key := sha3.Sum256(append(token.T, W.Bytes()...))
|
key := sha3.Sum256(append(token.T, W.Bytes()...))
|
||||||
mac := hmac.New(sha3.New512, key[:])
|
mac := hmac.New(sha3.New512, key[:])
|
||||||
mac.Write(data)
|
K := mac.Sum(data)
|
||||||
computedMAC := mac.Sum(nil)
|
result := hmac.Equal(token.MAC, K)
|
||||||
result := hmac.Equal(token.MAC, computedMAC)
|
if result == true {
|
||||||
if result {
|
|
||||||
if ts.persistanceService == nil {
|
if ts.persistanceService == nil {
|
||||||
ts.seen[hex.EncodeToString(token.T)] = true
|
ts.seen[hex.EncodeToString(token.T)] = true
|
||||||
} else {
|
} else {
|
||||||
|
|
99
service.go
99
service.go
|
@ -2,30 +2,23 @@ package tapir
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"cwtch.im/tapir/primitives"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
"git.openprivacy.ca/openprivacy/connectivity"
|
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
|
||||||
"golang.org/x/crypto/ed25519"
|
"golang.org/x/crypto/ed25519"
|
||||||
"golang.org/x/crypto/nacl/secretbox"
|
"golang.org/x/crypto/nacl/secretbox"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ServiceMetrics outlines higher level information about the service e.g. counts of connections
|
|
||||||
type ServiceMetrics struct {
|
|
||||||
ConnectionCount int
|
|
||||||
}
|
|
||||||
|
|
||||||
// Service defines the interface for a Tapir Service
|
// Service defines the interface for a Tapir Service
|
||||||
type Service interface {
|
type Service interface {
|
||||||
Init(acn connectivity.ACN, privateKey ed25519.PrivateKey, identity *primitives.Identity)
|
Init(acn connectivity.ACN, privateKey ed25519.PrivateKey, identity *primitives.Identity)
|
||||||
Connect(hostname string, application Application) (bool, error)
|
Connect(hostname string, application Application) (bool, error)
|
||||||
Listen(application Application) error
|
Listen(application Application) error
|
||||||
GetConnection(connectionID string) (Connection, error)
|
GetConnection(connectionID string) (Connection, error)
|
||||||
Metrics() ServiceMetrics
|
|
||||||
Broadcast(message []byte, capability Capability) error
|
|
||||||
WaitForCapabilityOrClose(connectionID string, capability Capability) (Connection, error)
|
WaitForCapabilityOrClose(connectionID string, capability Capability) (Connection, error)
|
||||||
Shutdown()
|
Shutdown()
|
||||||
}
|
}
|
||||||
|
@ -40,18 +33,17 @@ type Connection interface {
|
||||||
HasCapability(name Capability) bool
|
HasCapability(name Capability) bool
|
||||||
SetCapability(name Capability)
|
SetCapability(name Capability)
|
||||||
SetEncryptionKey(key [32]byte)
|
SetEncryptionKey(key [32]byte)
|
||||||
Send(message []byte) error
|
Send(message []byte)
|
||||||
Close()
|
Close()
|
||||||
App() Application
|
App() Application
|
||||||
SetApp(application Application)
|
SetApp(application Application)
|
||||||
IsClosed() bool
|
IsClosed() bool
|
||||||
Broadcast(message []byte, capability Capability) error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connection defines a Tapir Connection
|
// Connection defines a Tapir Connection
|
||||||
type connection struct {
|
type connection struct {
|
||||||
hostname string
|
hostname string
|
||||||
conn io.ReadWriteCloser
|
conn net.Conn
|
||||||
capabilities sync.Map
|
capabilities sync.Map
|
||||||
encrypted bool
|
encrypted bool
|
||||||
key [32]byte
|
key [32]byte
|
||||||
|
@ -60,22 +52,17 @@ type connection struct {
|
||||||
outbound bool
|
outbound bool
|
||||||
closed bool
|
closed bool
|
||||||
MaxLength int
|
MaxLength int
|
||||||
lock sync.Mutex
|
|
||||||
service Service
|
|
||||||
expectBuffer []byte
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConnection creates a new Connection
|
// NewConnection creates a new Connection
|
||||||
func NewConnection(service Service, id *primitives.Identity, hostname string, outbound bool, conn io.ReadWriteCloser, app Application) Connection {
|
func NewConnection(id *primitives.Identity, hostname string, outbound bool, conn net.Conn, app Application) Connection {
|
||||||
connection := new(connection)
|
connection := new(connection)
|
||||||
connection.hostname = hostname
|
connection.hostname = hostname
|
||||||
connection.conn = conn
|
connection.conn = conn
|
||||||
connection.app = app
|
connection.app = app
|
||||||
connection.identity = id
|
connection.identity = id
|
||||||
connection.outbound = outbound
|
connection.outbound = outbound
|
||||||
connection.MaxLength = 8192
|
connection.MaxLength = 1024
|
||||||
connection.service = service
|
|
||||||
connection.expectBuffer = make([]byte, 8192)
|
|
||||||
go connection.app.Init(connection)
|
go connection.app.Init(connection)
|
||||||
return connection
|
return connection
|
||||||
}
|
}
|
||||||
|
@ -87,23 +74,17 @@ func (c *connection) ID() *primitives.Identity {
|
||||||
|
|
||||||
// App returns the overarching application using this Connection.
|
// App returns the overarching application using this Connection.
|
||||||
func (c *connection) App() Application {
|
func (c *connection) App() Application {
|
||||||
c.lock.Lock()
|
|
||||||
defer c.lock.Unlock()
|
|
||||||
return c.app
|
return c.app
|
||||||
}
|
}
|
||||||
|
|
||||||
// App returns the overarching application using this Connection.
|
// App returns the overarching application using this Connection.
|
||||||
func (c *connection) SetApp(application Application) {
|
func (c *connection) SetApp(application Application) {
|
||||||
c.lock.Lock()
|
|
||||||
defer c.lock.Unlock()
|
|
||||||
c.app = application
|
c.app = application
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hostname returns the hostname of the connection (if the connection has not been authorized it will return the
|
// Hostname returns the hostname of the connection (if the connection has not been authorized it will return the
|
||||||
// temporary hostname identifier)
|
// temporary hostname identifier)
|
||||||
func (c *connection) Hostname() string {
|
func (c *connection) Hostname() string {
|
||||||
c.lock.Lock()
|
|
||||||
defer c.lock.Unlock()
|
|
||||||
return c.hostname
|
return c.hostname
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,15 +96,11 @@ func (c *connection) IsOutbound() bool {
|
||||||
|
|
||||||
// IsClosed returns true if the connection is closed (connections cannot be reopened)
|
// IsClosed returns true if the connection is closed (connections cannot be reopened)
|
||||||
func (c *connection) IsClosed() bool {
|
func (c *connection) IsClosed() bool {
|
||||||
c.lock.Lock()
|
|
||||||
defer c.lock.Unlock()
|
|
||||||
return c.closed
|
return c.closed
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetHostname sets the hostname on the connection
|
// SetHostname sets the hostname on the connection
|
||||||
func (c *connection) SetHostname(hostname string) {
|
func (c *connection) SetHostname(hostname string) {
|
||||||
c.lock.Lock()
|
|
||||||
defer c.lock.Unlock()
|
|
||||||
log.Debugf("[%v -- %v] Asserting Remote Hostname: %v", c.identity.Hostname(), c.hostname, hostname)
|
log.Debugf("[%v -- %v] Asserting Remote Hostname: %v", c.identity.Hostname(), c.hostname, hostname)
|
||||||
c.hostname = hostname
|
c.hostname = hostname
|
||||||
}
|
}
|
||||||
|
@ -142,78 +119,59 @@ func (c *connection) HasCapability(name Capability) bool {
|
||||||
|
|
||||||
// Close forcibly closes the connection
|
// Close forcibly closes the connection
|
||||||
func (c *connection) Close() {
|
func (c *connection) Close() {
|
||||||
c.lock.Lock()
|
|
||||||
defer c.lock.Unlock()
|
|
||||||
c.closeInner()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *connection) closeInner() {
|
|
||||||
c.closed = true
|
c.closed = true
|
||||||
c.conn.Close()
|
c.conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expect blocks and reads a single Tapir packet , from the connection.
|
// Expect blocks and reads a single Tapir packet , from the connection.
|
||||||
func (c *connection) Expect() []byte {
|
func (c *connection) Expect() []byte {
|
||||||
// Multiple goroutines may invoke methods on a Conn simultaneously.
|
buffer := make([]byte, c.MaxLength)
|
||||||
// As such we don't need to mutex around closed.
|
n, err := io.ReadFull(c.conn, buffer)
|
||||||
n, err := io.ReadFull(c.conn, c.expectBuffer)
|
|
||||||
|
|
||||||
if n != c.MaxLength || err != nil {
|
if n != c.MaxLength || err != nil {
|
||||||
log.Debugf("[%v -> %v] Wire Error Reading, Read %d bytes, Error: %v", c.hostname, c.identity.Hostname(), n, err)
|
log.Errorf("[%v -> %v] Wire Error Reading, Read %d bytes, Error: %v", c.hostname, c.identity.Hostname(), n, err)
|
||||||
c.Close() // use the full close function which acquires a lock for the connection state...
|
c.conn.Close()
|
||||||
|
c.closed = true
|
||||||
return []byte{}
|
return []byte{}
|
||||||
}
|
}
|
||||||
c.lock.Lock()
|
|
||||||
defer c.lock.Unlock()
|
|
||||||
if c.encrypted {
|
if c.encrypted {
|
||||||
var decryptNonce [24]byte
|
var decryptNonce [24]byte
|
||||||
copy(decryptNonce[:], c.expectBuffer[:24])
|
copy(decryptNonce[:], buffer[:24])
|
||||||
decrypted, ok := secretbox.Open(nil, c.expectBuffer[24:], &decryptNonce, &c.key)
|
decrypted, ok := secretbox.Open(nil, buffer[24:], &decryptNonce, &c.key)
|
||||||
if ok {
|
if ok {
|
||||||
copy(c.expectBuffer, decrypted)
|
copy(buffer, decrypted)
|
||||||
} else {
|
} else {
|
||||||
log.Errorf("[%v -> %v] Error Decrypting Message On Wire", c.hostname, c.identity.Hostname())
|
log.Errorf("[%v -> %v] Error Decrypting Message On Wire", c.hostname, c.identity.Hostname())
|
||||||
c.closeInner()
|
c.conn.Close()
|
||||||
|
c.closed = true
|
||||||
return []byte{}
|
return []byte{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
length, _ := binary.Uvarint(c.expectBuffer[0:2])
|
len, _ := binary.Uvarint(buffer[0:2])
|
||||||
if length+2 >= uint64(c.MaxLength) {
|
|
||||||
return []byte{}
|
|
||||||
}
|
|
||||||
//cplog.Debugf("[%v -> %v] Wire Receive: (%d) %x", c.hostname, c.ID.Hostname(), len, buffer)
|
//cplog.Debugf("[%v -> %v] Wire Receive: (%d) %x", c.hostname, c.ID.Hostname(), len, buffer)
|
||||||
return c.expectBuffer[2 : length+2]
|
return buffer[2 : len+2]
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetEncryptionKey turns on application-level encryption on the connection using the given key.
|
// SetEncryptionKey turns on application-level encryption on the connection using the given key.
|
||||||
func (c *connection) SetEncryptionKey(key [32]byte) {
|
func (c *connection) SetEncryptionKey(key [32]byte) {
|
||||||
c.lock.Lock()
|
|
||||||
defer c.lock.Unlock()
|
|
||||||
c.key = key
|
c.key = key
|
||||||
c.encrypted = true
|
c.encrypted = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send writes a given message to a Tapir packet (of 1024 bytes in length).
|
// Send writes a given message to a Tapir packet (of 1024 bytes in length).
|
||||||
func (c *connection) Send(message []byte) error {
|
func (c *connection) Send(message []byte) {
|
||||||
|
|
||||||
// We can only encode messages up to maxLength
|
|
||||||
if len(message) >= c.MaxLength {
|
|
||||||
log.Errorf("attempting to send a message that is too big")
|
|
||||||
return errors.New("message too long")
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer := make([]byte, c.MaxLength)
|
buffer := make([]byte, c.MaxLength)
|
||||||
binary.PutUvarint(buffer[0:2], uint64(len(message)))
|
binary.PutUvarint(buffer[0:2], uint64(len(message)))
|
||||||
copy(buffer[2:], message)
|
copy(buffer[2:], message)
|
||||||
|
|
||||||
c.lock.Lock()
|
|
||||||
defer c.lock.Unlock()
|
|
||||||
if c.encrypted {
|
if c.encrypted {
|
||||||
var nonce [24]byte
|
var nonce [24]byte
|
||||||
if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil {
|
if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil {
|
||||||
log.Errorf("Could not read sufficient randomness %v. Closing connection", err)
|
log.Errorf("Could not read sufficient randomness %v. Closing connection", err)
|
||||||
c.closeInner()
|
c.conn.Close()
|
||||||
return errors.New("could not read random")
|
c.closed = true
|
||||||
}
|
}
|
||||||
// MaxLength - 40 = MaxLength - 24 nonce bytes and 16 auth tag.
|
// MaxLength - 40 = MaxLength - 24 nonce bytes and 16 auth tag.
|
||||||
encrypted := secretbox.Seal(nonce[:], buffer[0:c.MaxLength-40], &nonce, &c.key)
|
encrypted := secretbox.Seal(nonce[:], buffer[0:c.MaxLength-40], &nonce, &c.key)
|
||||||
|
@ -222,12 +180,7 @@ func (c *connection) Send(message []byte) error {
|
||||||
log.Debugf("[%v -> %v] Wire Send %x", c.identity.Hostname(), c.hostname, buffer)
|
log.Debugf("[%v -> %v] Wire Send %x", c.identity.Hostname(), c.hostname, buffer)
|
||||||
_, err := c.conn.Write(buffer)
|
_, err := c.conn.Write(buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.closeInner()
|
c.conn.Close()
|
||||||
|
c.closed = true
|
||||||
}
|
}
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Broadcast sends a message to all active service connections with a given capability
|
|
||||||
func (c *connection) Broadcast(message []byte, capability Capability) error {
|
|
||||||
return c.service.Broadcast(message, capability)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ go list ./... | xargs go vet
|
||||||
echo ""
|
echo ""
|
||||||
echo "Linting:"
|
echo "Linting:"
|
||||||
|
|
||||||
staticcheck ./...
|
go list ./... | xargs golint
|
||||||
|
|
||||||
|
|
||||||
echo "Time to format"
|
echo "Time to format"
|
||||||
|
@ -21,4 +21,4 @@ ineffassign .
|
||||||
|
|
||||||
# misspell (https://github.com/client9/misspell/cmd/misspell)
|
# misspell (https://github.com/client9/misspell/cmd/misspell)
|
||||||
echo "Checking for misspelled words..."
|
echo "Checking for misspelled words..."
|
||||||
misspell . | grep -v "testing/" | grep -v "vendor/" | grep -v "go.sum" | grep -v ".idea"
|
misspell . | grep -v "vendor/" | grep -v "go.sum" | grep -v ".idea"
|
||||||
|
|
|
@ -1,17 +1,15 @@
|
||||||
package testing
|
package testing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.openprivacy.ca/cwtch.im/tapir"
|
"cwtch.im/tapir"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/applications"
|
"cwtch.im/tapir/applications"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/networks/tor"
|
"cwtch.im/tapir/networks/tor"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives"
|
"cwtch.im/tapir/primitives"
|
||||||
"git.openprivacy.ca/openprivacy/connectivity"
|
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
|
||||||
torProvider "git.openprivacy.ca/openprivacy/connectivity/tor"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
"git.openprivacy.ca/openprivacy/libricochet-go/utils"
|
||||||
"golang.org/x/crypto/ed25519"
|
"golang.org/x/crypto/ed25519"
|
||||||
"os"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/pprof"
|
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -62,28 +60,16 @@ func TestTapir(t *testing.T) {
|
||||||
log.SetLevel(log.LevelDebug)
|
log.SetLevel(log.LevelDebug)
|
||||||
log.Infof("Number of goroutines open at start: %d", runtime.NumGoroutine())
|
log.Infof("Number of goroutines open at start: %d", runtime.NumGoroutine())
|
||||||
// Connect to Tor
|
// Connect to Tor
|
||||||
os.MkdirAll("./tor/", 0700)
|
var acn connectivity.ACN
|
||||||
builder := new(torProvider.TorrcBuilder)
|
acn, _ = connectivity.StartTor("./", "")
|
||||||
builder.WithSocksPort(9059).WithControlPort(9060).WithHashedPassword("tapir-integration-test").Build("./tor/torrc")
|
|
||||||
|
|
||||||
torDataDir := ""
|
|
||||||
var err error
|
|
||||||
if torDataDir, err = os.MkdirTemp("./tor/", "data-dir-"); err != nil {
|
|
||||||
t.Fatalf("could not create data dir")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Connect to Tor
|
|
||||||
acn, err := torProvider.NewTorACNWithAuth("./", "", torDataDir, 9060, torProvider.HashedPasswordAuthenticator{Password: "tapir-integration-test"})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("could not launch ACN %v", err)
|
|
||||||
}
|
|
||||||
acn.WaitTillBootstrapped()
|
acn.WaitTillBootstrapped()
|
||||||
|
|
||||||
// Generate Server Keys
|
// Generate Server Keys
|
||||||
id, sk := primitives.InitializeEphemeralIdentity()
|
id, sk := primitives.InitializeEphemeralIdentity()
|
||||||
|
|
||||||
// Init the Server running the Simple App.
|
// Init the Server running the Simple App.
|
||||||
service := new(tor.BaseOnionService)
|
var service tapir.Service
|
||||||
|
service = new(tor.BaseOnionService)
|
||||||
service.Init(acn, sk, &id)
|
service.Init(acn, sk, &id)
|
||||||
|
|
||||||
// Goroutine Management
|
// Goroutine Management
|
||||||
|
@ -100,59 +86,42 @@ func TestTapir(t *testing.T) {
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
// Init a Client to Connect to the Server
|
// Init a Client to Connect to the Server
|
||||||
client, clienthostname := genclient(acn)
|
client, clienthostname := genclient(acn)
|
||||||
go connectclient(t, client, id.PublicKey(), wg)
|
go connectclient(client, id.PublicKey(), wg)
|
||||||
CheckConnection(service, clienthostname, wg)
|
CheckConnection(service, clienthostname, wg)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
// Wait for Garbage Collection...
|
// Wait for Server to Sync
|
||||||
time.Sleep(time.Second * 60)
|
time.Sleep(time.Second * 2)
|
||||||
log.Infof("Closing ACN...")
|
log.Infof("Closing ACN...")
|
||||||
client.Shutdown()
|
|
||||||
service.Shutdown()
|
|
||||||
acn.Close()
|
acn.Close()
|
||||||
sg.Wait()
|
sg.Wait()
|
||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 2)
|
||||||
log.Infof("Number of goroutines open at close: %d", runtime.NumGoroutine())
|
log.Infof("Number of goroutines open at close: %d", runtime.NumGoroutine())
|
||||||
if numRoutinesStart != runtime.NumGoroutine() {
|
if numRoutinesStart != runtime.NumGoroutine() {
|
||||||
|
|
||||||
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
|
|
||||||
|
|
||||||
t.Errorf("Potential goroutine leak: Num Start:%v NumEnd: %v", numRoutinesStart, runtime.NumGoroutine())
|
t.Errorf("Potential goroutine leak: Num Start:%v NumEnd: %v", numRoutinesStart, runtime.NumGoroutine())
|
||||||
}
|
}
|
||||||
if !AuthSuccess {
|
if !AuthSuccess {
|
||||||
t.Fatalf("Integration Test FAILED, client did not auth with server")
|
t.Fatalf("Integration Test FAILED, client did not auth with server")
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func genclient(acn connectivity.ACN) (tapir.Service, string) {
|
func genclient(acn connectivity.ACN) (tapir.Service, string) {
|
||||||
id, sk := primitives.InitializeEphemeralIdentity()
|
id, sk := primitives.InitializeEphemeralIdentity()
|
||||||
client := new(tor.BaseOnionService)
|
var client tapir.Service
|
||||||
|
client = new(tor.BaseOnionService)
|
||||||
client.Init(acn, sk, &id)
|
client.Init(acn, sk, &id)
|
||||||
return client, id.Hostname()
|
return client, id.Hostname()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client will Connect and launch it's own Echo App goroutine.
|
// Client will Connect and launch it's own Echo App goroutine.
|
||||||
func connectclient(t *testing.T, client tapir.Service, key ed25519.PublicKey, group *sync.WaitGroup) {
|
func connectclient(client tapir.Service, key ed25519.PublicKey, group *sync.WaitGroup) {
|
||||||
client.Connect(torProvider.GetTorV3Hostname(key), new(SimpleApp))
|
client.Connect(utils.GetTorV3Hostname(key), new(SimpleApp))
|
||||||
|
|
||||||
// Once connected, it shouldn't take long to authenticate and run the application. So for the purposes of this demo
|
// Once connected, it shouldn't take long to authenticate and run the application. So for the purposes of this demo
|
||||||
// we will wait a little while then exit.
|
// we will wait a little while then exit.
|
||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 5)
|
||||||
|
|
||||||
conn, _ := client.GetConnection(torProvider.GetTorV3Hostname(key))
|
conn, _ := client.GetConnection(utils.GetTorV3Hostname(key))
|
||||||
log.Debugf("Client has Auth: %v", conn.HasCapability(applications.AuthCapability))
|
log.Debugf("Client has Auth: %v", conn.HasCapability(applications.AuthCapability))
|
||||||
|
|
||||||
if conn.HasCapability(applications.AuthCapability) == false {
|
|
||||||
t.Errorf("tapir auth failed")
|
|
||||||
}
|
|
||||||
|
|
||||||
// attempt to send a message that is too long
|
|
||||||
var long [8195]byte
|
|
||||||
err := conn.Send(long[:])
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf("should have errored on message being too long...")
|
|
||||||
}
|
|
||||||
|
|
||||||
AuthSuccess = true
|
AuthSuccess = true
|
||||||
group.Done()
|
group.Done()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
package testing
|
package testing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.openprivacy.ca/cwtch.im/tapir"
|
"cwtch.im/tapir"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/applications"
|
"cwtch.im/tapir/applications"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/networks/tor"
|
"cwtch.im/tapir/networks/tor"
|
||||||
"git.openprivacy.ca/cwtch.im/tapir/primitives"
|
"cwtch.im/tapir/primitives"
|
||||||
torProvider "git.openprivacy.ca/openprivacy/connectivity/tor"
|
"git.openprivacy.ca/openprivacy/libricochet-go/connectivity"
|
||||||
"git.openprivacy.ca/openprivacy/log"
|
"git.openprivacy.ca/openprivacy/libricochet-go/log"
|
||||||
|
"git.openprivacy.ca/openprivacy/libricochet-go/utils"
|
||||||
"golang.org/x/crypto/ed25519"
|
"golang.org/x/crypto/ed25519"
|
||||||
"os"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -21,23 +21,8 @@ func TestTapirMaliciousRemote(t *testing.T) {
|
||||||
log.SetLevel(log.LevelDebug)
|
log.SetLevel(log.LevelDebug)
|
||||||
log.Infof("Number of goroutines open at start: %d", runtime.NumGoroutine())
|
log.Infof("Number of goroutines open at start: %d", runtime.NumGoroutine())
|
||||||
// Connect to Tor
|
// Connect to Tor
|
||||||
os.MkdirAll("./tor/", 0700)
|
var acn connectivity.ACN
|
||||||
builder := new(torProvider.TorrcBuilder)
|
acn, _ = connectivity.StartTor("./", "")
|
||||||
builder.WithHashedPassword("tapir-integration-test").Build("./tor/torrc")
|
|
||||||
|
|
||||||
// Connect to Tor
|
|
||||||
|
|
||||||
torDataDir := ""
|
|
||||||
var err error
|
|
||||||
if torDataDir, err = os.MkdirTemp("./tor/", "data-dir-"); err != nil {
|
|
||||||
t.Fatalf("could not create data dir")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Connect to Tor
|
|
||||||
acn, err := torProvider.NewTorACNWithAuth("./", "", torDataDir, 9051, torProvider.HashedPasswordAuthenticator{Password: "tapir-integration-test"})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("could not launch ACN %v", err)
|
|
||||||
}
|
|
||||||
acn.WaitTillBootstrapped()
|
acn.WaitTillBootstrapped()
|
||||||
|
|
||||||
// Generate Server Keys, not we generate two sets
|
// Generate Server Keys, not we generate two sets
|
||||||
|
@ -45,7 +30,8 @@ func TestTapirMaliciousRemote(t *testing.T) {
|
||||||
id2, sk2 := primitives.InitializeEphemeralIdentity()
|
id2, sk2 := primitives.InitializeEphemeralIdentity()
|
||||||
|
|
||||||
// Init the Server running the Simple App.
|
// Init the Server running the Simple App.
|
||||||
service := new(tor.BaseOnionService)
|
var service tapir.Service
|
||||||
|
service = new(tor.BaseOnionService)
|
||||||
// Initialize an onion service with one identity, but the auth app with another, this should
|
// Initialize an onion service with one identity, but the auth app with another, this should
|
||||||
// trigger a failure in authentication protocol
|
// trigger a failure in authentication protocol
|
||||||
service.Init(acn, sk2, &id)
|
service.Init(acn, sk2, &id)
|
||||||
|
@ -70,11 +56,9 @@ func TestTapirMaliciousRemote(t *testing.T) {
|
||||||
// Wait for Server to Sync
|
// Wait for Server to Sync
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 2)
|
||||||
log.Infof("closing ACN...")
|
log.Infof("closing ACN...")
|
||||||
client.Shutdown()
|
|
||||||
service.Shutdown()
|
|
||||||
acn.Close()
|
acn.Close()
|
||||||
sg.Wait()
|
sg.Wait()
|
||||||
time.Sleep(time.Second * 5) // wait for goroutines to finish...
|
time.Sleep(time.Second * 2)
|
||||||
log.Infof("Number of goroutines open at close: %d", runtime.NumGoroutine())
|
log.Infof("Number of goroutines open at close: %d", runtime.NumGoroutine())
|
||||||
if numRoutinesStart != runtime.NumGoroutine() {
|
if numRoutinesStart != runtime.NumGoroutine() {
|
||||||
t.Errorf("Potential goroutine leak: Num Start:%v NumEnd: %v", numRoutinesStart, runtime.NumGoroutine())
|
t.Errorf("Potential goroutine leak: Num Start:%v NumEnd: %v", numRoutinesStart, runtime.NumGoroutine())
|
||||||
|
@ -83,17 +67,17 @@ func TestTapirMaliciousRemote(t *testing.T) {
|
||||||
|
|
||||||
// Client will Connect and launch it's own Echo App goroutine.
|
// Client will Connect and launch it's own Echo App goroutine.
|
||||||
func connectclientandfail(client tapir.Service, key ed25519.PublicKey, group *sync.WaitGroup, t *testing.T) {
|
func connectclientandfail(client tapir.Service, key ed25519.PublicKey, group *sync.WaitGroup, t *testing.T) {
|
||||||
client.Connect(torProvider.GetTorV3Hostname(key), new(applications.AuthApp))
|
client.Connect(utils.GetTorV3Hostname(key), new(applications.AuthApp))
|
||||||
|
|
||||||
// Once connected, it shouldn't take long to authenticate and run the application. So for the purposes of this demo
|
// Once connected, it shouldn't take long to authenticate and run the application. So for the purposes of this demo
|
||||||
// we will wait a little while then exit.
|
// we will wait a little while then exit.
|
||||||
time.Sleep(time.Second * 5)
|
time.Sleep(time.Second * 5)
|
||||||
|
|
||||||
log.Infof("Checking connection status...")
|
log.Infof("Checking connection status...")
|
||||||
conn, err := client.GetConnection(torProvider.GetTorV3Hostname(key))
|
conn, err := client.GetConnection(utils.GetTorV3Hostname(key))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
group.Done()
|
group.Done()
|
||||||
t.Errorf("Connection should have failed! %v %v", conn, err)
|
t.Fatalf("Connection should have failed! %v %v", conn, err)
|
||||||
}
|
}
|
||||||
log.Infof("Successfully failed to authenticate...")
|
log.Infof("Successfully failed to authenticate...")
|
||||||
group.Done()
|
group.Done()
|
||||||
|
|
|
@ -2,20 +2,13 @@
|
||||||
|
|
||||||
set -e
|
set -e
|
||||||
pwd
|
pwd
|
||||||
go test -race ${1} -coverprofile=applications.cover.out -v ./applications
|
go test ${1} -coverprofile=applications.cover.out -v ./applications
|
||||||
go test -race ${1} -coverprofile=applications.tokenboard.cover.out -v ./applications/tokenboard
|
go test ${1} -coverprofile=applications.tokenboard.cover.out -v ./applications/tokenboard
|
||||||
# persistence is broken in WSL
|
go test ${1} -coverprofile=primitives.cover.out -v ./primitives
|
||||||
if grep -q -v Microsoft /proc/version; then
|
go test ${1} -coverprofile=primitives.auditable.cover.out -v ./primitives/auditable
|
||||||
go test -race ${1} -coverprofile=persistence.cover.out -v ./persistence
|
go test ${1} -coverprofile=primitives.core.cover.out -v ./primitives/core
|
||||||
fi
|
go test ${1} -coverprofile=primitives.privacypass.cover.out -v ./primitives/privacypass
|
||||||
go test -race ${1} -coverprofile=primitives.cover.out -v ./primitives
|
go test -bench "BenchmarkAuditableStore" -benchtime 1000x primitives/auditable/*.go
|
||||||
go test -race ${1} -coverprofile=primitives.auditable.cover.out -v ./primitives/auditable
|
|
||||||
go test -race ${1} -coverprofile=primitives.core.cover.out -v ./primitives/core
|
|
||||||
# persistence is broken in WSL
|
|
||||||
if grep -q -v Microsoft /proc/version; then
|
|
||||||
go test -race ${1} -coverprofile=primitives.privacypass.cover.out -v ./primitives/privacypass
|
|
||||||
go test -bench "BenchmarkAuditableStore" -benchtime 1000x primitives/auditable/*.go
|
|
||||||
fi
|
|
||||||
echo "mode: set" > coverage.out && cat *.cover.out | grep -v mode: | sort -r | \
|
echo "mode: set" > coverage.out && cat *.cover.out | grep -v mode: | sort -r | \
|
||||||
awk '{if($1 != last) {print $0;last=$1}}' >> coverage.out
|
awk '{if($1 != last) {print $0;last=$1}}' >> coverage.out
|
||||||
rm -rf *.cover.out
|
rm -rf *.cover.out
|
||||||
|
|
|
@ -1,50 +0,0 @@
|
||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"crypto/sha512"
|
|
||||||
"filippo.io/edwards25519"
|
|
||||||
"golang.org/x/crypto/curve25519"
|
|
||||||
"golang.org/x/crypto/ed25519"
|
|
||||||
)
|
|
||||||
|
|
||||||
// EDH implements diffie hellman using curve25519 keys derived from ed25519 keys
|
|
||||||
func EDH(privateKey ed25519.PrivateKey, remotePublicKey ed25519.PublicKey) ([]byte, error) {
|
|
||||||
var privKeyBytes [64]byte
|
|
||||||
var remotePubKeyBytes [32]byte
|
|
||||||
copy(privKeyBytes[:], privateKey[:])
|
|
||||||
copy(remotePubKeyBytes[:], remotePublicKey[:])
|
|
||||||
var curve25519priv [32]byte
|
|
||||||
|
|
||||||
PrivateKeyToCurve25519(&curve25519priv, &privKeyBytes)
|
|
||||||
remoteCurve25519pub, err := ed25519PublicKeyToCurve25519New(remotePublicKey)
|
|
||||||
if err != nil {
|
|
||||||
return []byte{}, err
|
|
||||||
}
|
|
||||||
secret, err := curve25519.X25519(curve25519priv[:], remoteCurve25519pub[:])
|
|
||||||
return secret, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// reproduced from https://github.com/FiloSottile/age/blob/main/agessh/agessh.go#L190
|
|
||||||
func ed25519PublicKeyToCurve25519New(pk ed25519.PublicKey) ([]byte, error) {
|
|
||||||
// See https://blog.filippo.io/using-ed25519-keys-for-encryption and
|
|
||||||
// https://pkg.go.dev/filippo.io/edwards25519#Point.BytesMontgomery.
|
|
||||||
p, err := new(edwards25519.Point).SetBytes(pk)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return p.BytesMontgomery(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// PrivateKeyToCurve25519 converts an ed25519 private key into a corresponding
|
|
||||||
// curve25519 private key
|
|
||||||
func PrivateKeyToCurve25519(curve25519Private *[32]byte, privateKey *[64]byte) {
|
|
||||||
h := sha512.New()
|
|
||||||
h.Write(privateKey[:32])
|
|
||||||
digest := h.Sum(nil)
|
|
||||||
|
|
||||||
digest[0] &= 248
|
|
||||||
digest[31] &= 127
|
|
||||||
digest[31] |= 64
|
|
||||||
|
|
||||||
copy(curve25519Private[:], digest)
|
|
||||||
}
|
|
Loading…
Reference in New Issue