diff --git a/server/Makefile b/server/Makefile index e4c2e7e6..bdebf450 100644 --- a/server/Makefile +++ b/server/Makefile @@ -1,4 +1,4 @@ -.PHONY: all tidy generate lint vet test coverage pushdoc +.PHONY: all tidy build install lint vet test coverage # Default "make" target to check locally that everything is ok, BEFORE pushing remotely all: lint vet test build @@ -26,6 +26,13 @@ build: tidy # Building executable @go build ./smc/smccli @go build ./blockchain/chaincli + @go build ./registration + +install: tidy + # Building executable + @go install ./smc/smccli + @go install ./blockchain/chaincli + @go install ./registration coverage: tidy # Test and generate a coverage output usable by sonarcloud diff --git a/server/test/passport.jpg b/server/passport.jpg similarity index 100% rename from server/test/passport.jpg rename to server/passport.jpg diff --git a/server/test/admin/blockchain.go b/server/test/admin/blockchain.go index 2fd98d63..350c00f6 100644 --- a/server/test/admin/blockchain.go +++ b/server/test/admin/blockchain.go @@ -62,7 +62,7 @@ func BlockchainGetDocIDs(adminPubkey kyber.Point) []registry.RegistrationID { } // BlockchainGetDocument polls the blockchain to get the encrypted document -func BlockchainGetSecret(id registry.RegistrationID, pk kyber.Point) smc.Secret { +func BlockchainGetSecret(id registry.RegistrationID, pk kyber.Point) (smc.Secret, []byte) { encodedPk, err := pk.MarshalBinary() if err != nil { log.Fatal().Msgf("error: %v", err) @@ -82,5 +82,5 @@ func BlockchainGetSecret(id registry.RegistrationID, pk kyber.Point) smc.Secret log.Error().Msgf("error decoding response: %v", err) } - return secret + return secret, nil } diff --git a/server/test/admin/database.go b/server/test/admin/database.go index 861f3399..cb4de9a0 100644 --- a/server/test/admin/database.go +++ b/server/test/admin/database.go @@ -13,7 +13,7 @@ import ( const registrationServer = "localhost:3001" func RegistrationAdminGetDocument(docid registry.RegistrationID) registry.RegistrationData { - resp, err := http.Get("localhost:3000/admin/document?id=" + string(docid.ID)) + resp, err := http.Get(registrationServer + "/admin/document?id=" + string(docid.ID)) if err != nil { log.Fatal().Msgf("error: %v", err) } @@ -31,7 +31,7 @@ func RegistrationAdminGetDocument(docid registry.RegistrationID) registry.Regist } func RegistrationAdminUpdateDocument(docid registry.RegistrationID) error { - resp, err := http.Get("localhost:3000/admin/document?id=" + string(docid.ID)) + resp, err := http.Get(registrationServer + "/admin/document?id=" + string(docid.ID)) if err != nil { log.Fatal().Msgf("error: %v", err) } diff --git a/server/test/admin/smc.go b/server/test/admin/smc.go index 411490a3..af7ec085 100644 --- a/server/test/admin/smc.go +++ b/server/test/admin/smc.go @@ -18,7 +18,8 @@ const smcServer = "localhost:3002" // SmcReencryptSecret re-encrypts the secret with the new public key // and returns a xhatenc value that can be used to reveal the secret -func SmcReencryptSecret(pk kyber.Point, secret string) (kyber.Point, error) { +// first argument is supposed to be the proof +func SmcReencryptSecret(_ []byte, pk kyber.Point, secret string) (kyber.Point, error) { resp, err := http.Post(smcServer+"/reencrypt", "application/json", bytes.NewBuffer([]byte(`{"pubk": "`+encodePublickey(pk)+`", "encrypted": "`+secret+`"}`))) if err != nil { diff --git a/server/test/test.go b/server/test/test.go index 14a393d4..45ff36af 100644 --- a/server/test/test.go +++ b/server/test/test.go @@ -20,18 +20,25 @@ func main() { // --------------------------------------------------------- // create a document and save it encrypted into the database - doc := createDocument("John Doe", "12AB456789", 0, "passport.jpg") + log.Info().Msg("CREATE document for test purpose") + doc := createDocument("John Doe", "12AB456789", 0, "./passport.jpg") log.Info().Msg("SUCCESS! created new document") // add the document to the registry - docid := user.RegistrationAdd(doc, symKey) + log.Info().Msg("ADD document to the registry") + docid, err := user.RegistrationAdd(doc, symKey) + if err != nil { + log.Fatal().Msgf("error: %v", err) + } log.Info().Msgf("SUCCESS! added document ID=%v", docid) // get the SMC pub key + log.Info().Msg("FETCH SMC key") smcKey := user.SmcGetKey() log.Info().Msgf("SUCCESS! got SMC key: %v", smcKey) // add secret = symKey to the blockchain + log.Info().Msg("ADD secret to the blockchain") secret := user.BlockchainEncryptAndAddSecret(smcKey, symKey, docid) log.Info().Msgf("SUCCESS! added secret=%v with ID=%v to blockchain", secret, docid) @@ -45,10 +52,10 @@ func main() { docIDs := admin.BlockchainGetDocIDs(pk) for _, id := range docIDs { - secret := admin.BlockchainGetSecret(id, pk) + secret, proof := admin.BlockchainGetSecret(id, pk) log.Info().Msgf("secret: %v", secret) - xhatenc, err := admin.SmcReencryptSecret(pk, secret.Data) + xhatenc, err := admin.SmcReencryptSecret(proof, pk, secret.Data) if err != nil { log.Fatal().Msgf("error: %v", err) } @@ -65,6 +72,9 @@ func main() { log.Fatal().Msg("symmetric key mismatch") } + // encrypt binary array with symmetric key + // and save it to the registry + // TODO: get the encrypted document from the registry // TODO: decrypt the document - optional // TODO: update the document status to registered diff --git a/server/test/user/database.go b/server/test/user/database.go index c3e43e81..bbed2c74 100644 --- a/server/test/user/database.go +++ b/server/test/user/database.go @@ -5,35 +5,86 @@ import ( "context" "encoding/binary" "encoding/json" + "io" + "mime/multipart" "net/http" - "net/url" + "strconv" "github.com/rs/zerolog/log" "go.dedis.ch/hbt/server/registration/registry" "go.dedis.ch/hbt/server/test/key" ) -const registrationServer = "localhost:3000" +const registrationServer = "http://localhost:3000" // RegistrationAdd adds a new registration to the registry -func RegistrationAdd(data registry.RegistrationData, symKey []byte) registry.RegistrationID { - // Encrypt the data - encrypted, err := encryptRegistrationData(data, symKey) +func RegistrationAdd(data registry.RegistrationData, symKey []byte) ( + registry.RegistrationID, + error, +) { + var b bytes.Buffer + w := multipart.NewWriter(&b) + + fw, err := w.CreateFormField("pubkey") if err != nil { - log.Fatal().Msgf("error: %v", err) + return registry.RegistrationID{}, err + } + if _, err = io.Copy(fw, bytes.NewReader(symKey)); err != nil { + return registry.RegistrationID{}, err } - // Add the encrypted document to the registry - resp, err := http.PostForm(registrationServer+"/document", - url.Values{ - "name": {string(encrypted.Name)}, - "passport": {string(encrypted.Passport)}, - "role": {string(encrypted.Role)}, - "picture": {string(encrypted.Picture)}, - "registered": {string(encrypted.Registered)}, - }) + fw, err = w.CreateFormField("name") if err != nil { - log.Fatal().Msgf("error: %v", err) + return registry.RegistrationID{}, err + } + if _, err = io.Copy(fw, bytes.NewReader([]byte(data.Name))); err != nil { + return registry.RegistrationID{}, err + } + + fw, err = w.CreateFormField("passport") + if err != nil { + return registry.RegistrationID{}, err + } + if _, err = io.Copy(fw, bytes.NewReader([]byte(data.Passport))); err != nil { + return registry.RegistrationID{}, err + } + + fw, err = w.CreateFormField("role") + if err != nil { + return registry.RegistrationID{}, err + } + if _, err = io.Copy(fw, + bytes.NewReader([]byte(strconv.FormatUint(data.Role, 10)))); err != nil { + return registry.RegistrationID{}, err + } + + fw, err = w.CreateFormField("registered") + if err != nil { + return registry.RegistrationID{}, err + } + if _, err = io.Copy(fw, + bytes.NewReader([]byte(strconv.FormatBool(data.Registered)))); err != nil { + return registry.RegistrationID{}, err + } + + fw, err = w.CreateFormField("picture") + if err != nil { + return registry.RegistrationID{}, err + } + if _, err = io.Copy(fw, bytes.NewReader(data.Picture)); err != nil { + return registry.RegistrationID{}, err + } + + defer w.Close() + + req, err := http.NewRequest(http.MethodPost, registrationServer+"/document", &b) + if err != nil { + return registry.RegistrationID{}, err + } + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return registry.RegistrationID{}, err } defer resp.Body.Close() @@ -42,10 +93,10 @@ func RegistrationAdd(data registry.RegistrationData, symKey []byte) registry.Reg var docid registry.RegistrationID err = json.NewDecoder(resp.Body).Decode(&docid) if err != nil { - log.Error().Msgf("error decoding response: %v", err) + return registry.RegistrationID{}, err } - return docid + return docid, err } // RegistrationGet polls the data to see if registered