Commit 8fa2ee5b authored by Loïck Bonniot's avatar Loïck Bonniot

[p][c] Use bytes instead of strings for hashes

parent a163a1cb
Pipeline #319 failed with stage
......@@ -2,7 +2,6 @@ package sign
import (
"crypto/sha512"
"fmt"
"io/ioutil"
"path/filepath"
"time"
......@@ -26,7 +25,7 @@ type CreateManager struct {
filepath string
comment string
signers []string
hash string
hash []byte
filename string
}
......@@ -64,7 +63,7 @@ func (m *CreateManager) computeFile() error {
}
hash := sha512.Sum512(data)
m.hash = fmt.Sprintf("%x", hash)
m.hash = hash[:]
m.filename = filepath.Base(m.filepath)
return nil
......
package sign
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
......@@ -55,7 +56,7 @@ func TestComputeFile(t *testing.T) {
m := &CreateManager{filepath: fcontract}
err := m.computeFile()
assert.Equal(t, nil, err)
assert.Equal(t, "37fd29decfb2d689439478b1f64b60441534c1e373a7023676c94ac6772639edab46f80139d167a2741f159e62b3064eca58bb331d32cd10770f29064af2a9de", m.hash)
assert.Equal(t, "37fd29decfb2d689439478b1f64b60441534c1e373a7023676c94ac6772639edab46f80139d167a2741f159e62b3064eca58bb331d32cd10770f29064af2a9de", fmt.Sprintf("%x", m.hash))
assert.Equal(t, "contract.txt", m.filename)
}
......@@ -132,7 +132,7 @@ func (*Empty) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} }
// PostContractRequest message contains the contract as SHA-512 hash, its filename,
// the list of signers as an array of strings, and a comment
type PostContractRequest struct {
Hash string `protobuf:"bytes,1,opt,name=hash" json:"hash,omitempty"`
Hash []byte `protobuf:"bytes,1,opt,name=hash,proto3" json:"hash,omitempty"`
Filename string `protobuf:"bytes,2,opt,name=filename" json:"filename,omitempty"`
Signer []string `protobuf:"bytes,3,rep,name=signer" json:"signer,omitempty"`
Comment string `protobuf:"bytes,4,opt,name=comment" json:"comment,omitempty"`
......@@ -374,36 +374,36 @@ var _Platform_serviceDesc = grpc.ServiceDesc{
}
var fileDescriptor0 = []byte{
// 489 bytes of a gzipped FileDescriptorProto
// 490 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x94, 0x53, 0x5d, 0x8f, 0xd2, 0x40,
0x14, 0xdd, 0x42, 0x97, 0x8f, 0xbb, 0xbb, 0xd8, 0x0c, 0x68, 0x2a, 0x0f, 0x66, 0x33, 0x2f, 0x1a,
0x1f, 0xd0, 0xa0, 0x31, 0xd1, 0xf8, 0x52, 0x2b, 0x59, 0xd7, 0x07, 0x42, 0xa6, 0x5b, 0x7d, 0xae,
0xe5, 0x2e, 0x34, 0xd2, 0x0e, 0x4e, 0x07, 0x0d, 0x7f, 0xc6, 0x3f, 0xe4, 0x8f, 0xd2, 0xce, 0xb4,
0x43, 0xa8, 0x36, 0x26, 0xcb, 0x03, 0xcc, 0xb9, 0x1f, 0xe7, 0xde, 0x39, 0x67, 0x00, 0xb2, 0x62,
0x0b, 0x3f, 0x40, 0xf1, 0x3d, 0x89, 0x31, 0x9f, 0x6c, 0x05, 0x97, 0x9c, 0xb4, 0xa3, 0x6d, 0x42,
0x3d, 0xb8, 0xc7, 0x70, 0x95, 0xe4, 0x12, 0x05, 0xc3, 0x6f, 0x3b, 0xcc, 0x25, 0x19, 0xc1, 0x29,
0xa6, 0x51, 0xb2, 0x71, 0xad, 0x4b, 0xeb, 0x49, 0x9f, 0x95, 0x80, 0xb8, 0xd0, 0x15, 0x65, 0x81,
0xdb, 0xd2, 0x71, 0x03, 0xe9, 0x4f, 0x0b, 0xfa, 0x33, 0x21, 0xb8, 0xf0, 0xf9, 0x12, 0xc9, 0x63,
0xb0, 0xe3, 0xe2, 0x57, 0x37, 0x0f, 0xa6, 0xc3, 0x49, 0x31, 0x64, 0x72, 0xc8, 0x4e, 0xd4, 0x17,
0xd3, 0x05, 0x8a, 0x30, 0xc5, 0x3c, 0x8f, 0x56, 0x68, 0x08, 0x2b, 0x48, 0xe7, 0x60, 0x6b, 0xaa,
0x33, 0xe8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0xd0, 0xfb, 0x16, 0xce,
0x6b, 0x0a, 0x93, 0x87, 0xba, 0xa4, 0x49, 0xf5, 0x86, 0xee, 0x97, 0xd0, 0x3f, 0xe8, 0x49, 0xee,
0x57, 0x37, 0xa9, 0xeb, 0xfb, 0x6f, 0xd7, 0x97, 0x8e, 0xfe, 0xd3, 0xbe, 0xf8, 0x13, 0x00, 0x00,
0xff, 0xff, 0x35, 0x7d, 0xdf, 0xdc, 0xca, 0x03, 0x00, 0x00,
}
......@@ -57,7 +57,7 @@ message Empty {
// PostContractRequest message contains the contract as SHA-512 hash, its filename,
// the list of signers as an array of strings, and a comment
message PostContractRequest {
string hash = 1;
bytes hash = 1;
string filename = 2;
repeated string signer = 3;
string comment = 4;
......
......@@ -56,8 +56,8 @@ func TestAddSigner(t *testing.T) {
id := bson.NewObjectId()
c.AddSigner(nil, "mail1", "hash1")
c.AddSigner(&id, "mail2", "hash2")
c.AddSigner(nil, "mail1", []byte{0xaa})
c.AddSigner(&id, "mail2", []byte{})
signers := c.Signers
......@@ -66,10 +66,10 @@ func TestAddSigner(t *testing.T) {
}
assert.Equal(t, signers[0].Email, "mail1")
assert.Equal(t, signers[0].Hash, "hash1")
assert.Equal(t, signers[0].Hash, []byte{0xaa})
assert.Equal(t, signers[0].UserID.Hex(), "000000000000000000000000")
assert.Equal(t, signers[1].Email, "mail2")
assert.Equal(t, signers[1].Hash, "hash2")
assert.Equal(t, signers[1].Hash, []byte{})
assert.Equal(t, signers[1].UserID.Hex(), id.Hex())
}
......@@ -85,10 +85,10 @@ func assertContractEqual(t *testing.T, contract, fetched entities.Contract) {
func TestInsertContract(t *testing.T) {
dropDataset()
c := entities.NewContract()
c.AddSigner(nil, "mail1", "hash1")
c.AddSigner(nil, "mail1", "hash1")
c.AddSigner(nil, "mail1", []byte{0xaa})
c.AddSigner(nil, "mail1", []byte{0xaa})
c.File.Name = "file"
c.File.Hash = "hashFile"
c.File.Hash = []byte{0xff}
c.File.Hosted = false
c.Comment = "comment"
c.Ready = true
......@@ -118,17 +118,17 @@ func TestGetWaitingForUser(t *testing.T) {
dropDataset()
c1 := entities.NewContract()
c1.AddSigner(nil, "mail1", "")
c1.AddSigner(nil, "mail1", []byte{})
c1.Ready = false
c2 := entities.NewContract()
c2.AddSigner(nil, "mail1", "")
c2.AddSigner(&knownID, "mail2", "hash")
c2.AddSigner(nil, "mail1", []byte{})
c2.AddSigner(&knownID, "mail2", []byte{0x12})
c2.Ready = false
c3 := entities.NewContract()
c3.AddSigner(nil, "mail2", "")
c3.AddSigner(&knownID, "mail1", "hash")
c3.AddSigner(nil, "mail2", []byte{})
c3.AddSigner(&knownID, "mail1", []byte{0xaa})
c3.Ready = false
_, _ = repository.Collection.Insert(c1)
......
......@@ -70,7 +70,7 @@ func (c *Builder) checkInput() *api.ErrorCode {
return &api.ErrorCode{Code: api.ErrorCode_INVARG, Message: "Expecting a valid filename"}
}
if len(c.in.Hash) != sha512.Size*2 { // *2 because string format doubles the length
if len(c.in.Hash) != sha512.Size {
return &api.ErrorCode{Code: api.ErrorCode_INVARG, Message: "Expecting a valid sha512 hash"}
}
......@@ -117,7 +117,7 @@ func (c *Builder) addContract() error {
contract.AddSigner(&s.ID, s.Email, s.CertHash)
}
for _, s := range c.missingSigners {
contract.AddSigner(nil, s, "")
contract.AddSigner(nil, s, nil)
}
contract.Comment = c.in.Comment
......
......@@ -2,7 +2,6 @@ package contract_test
import (
"crypto/sha512"
"fmt"
"io/ioutil"
"path/filepath"
"testing"
......@@ -18,7 +17,6 @@ import (
var user1, user2, user3 *entities.User
var defaultHash = sha512.Sum512([]byte{0})
var defaultHashStr = fmt.Sprintf("%x", defaultHash)
func createDataset() {
......@@ -29,17 +27,17 @@ func createDataset() {
user1.Email = "user1@example.com"
user1.Expiration = time.Now().AddDate(1, 0, 0)
user1.Certificate = "Certificate1"
user1.CertHash = "Hash1"
user1.CertHash = []byte{0x01}
user2.Email = "user2@example.com"
user2.Expiration = time.Now().AddDate(1, 0, 0)
user2.Certificate = "Certificate2"
user2.CertHash = "Hash2"
user2.CertHash = []byte{0x02}
user3.Email = "user3@example.com"
user3.Expiration = time.Now().AddDate(0, 0, -1)
user3.Certificate = "Certificate3"
user3.CertHash = "Hash3"
user3.CertHash = []byte{0x03}
_, _ = manager.Get("users").Insert(user1)
_, _ = manager.Get("users").Insert(user2)
......@@ -91,7 +89,7 @@ func TestAddContract(t *testing.T) {
client := clientTest(t)
errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
Hash: defaultHashStr,
Hash: defaultHash[:],
Filename: "ContractFilename",
Signer: []string{user1.Email, user2.Email},
Comment: "ContractComment",
......@@ -107,7 +105,7 @@ func TestAddContract(t *testing.T) {
}
assert.Equal(t, 1, len(contracts))
assert.Equal(t, defaultHashStr, contracts[0].File.Hash)
assert.Equal(t, defaultHash[:], contracts[0].File.Hash)
assert.Equal(t, "ContractFilename", contracts[0].File.Name)
assert.Equal(t, "ContractComment", contracts[0].Comment)
assert.T(t, contracts[0].Ready)
......@@ -127,7 +125,7 @@ func TestAddContractMissingUser(t *testing.T) {
client := clientTest(t)
errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
Hash: defaultHashStr,
Hash: defaultHash[:],
Filename: "ContractFilename",
Signer: []string{user1.Email, user3.Email},
})
......@@ -142,7 +140,7 @@ func TestAddContractMissingUser(t *testing.T) {
}
assert.Equal(t, 1, len(contracts))
assert.Equal(t, defaultHashStr, contracts[0].File.Hash)
assert.Equal(t, defaultHash[:], contracts[0].File.Hash)
assert.Equal(t, "ContractFilename", contracts[0].File.Name)
assert.Equal(t, "", contracts[0].Comment)
assert.T(t, !contracts[0].Ready)
......@@ -152,7 +150,7 @@ func TestAddContractMissingUser(t *testing.T) {
assert.Equal(t, user1.CertHash, contracts[0].Signers[0].Hash)
assert.Equal(t, user1.Email, contracts[0].Signers[0].Email)
assert.Equal(t, "000000000000000000000000", contracts[0].Signers[1].UserID.Hex())
assert.Equal(t, "", contracts[0].Signers[1].Hash)
assert.Equal(t, []byte{}, contracts[0].Signers[1].Hash)
assert.Equal(t, user3.Email, contracts[0].Signers[1].Email)
}
......@@ -162,7 +160,7 @@ func TestAddContractNoUser(t *testing.T) {
client := clientTest(t)
errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
Hash: defaultHashStr,
Hash: defaultHash[:],
Filename: "ContractFilename",
Signer: []string{},
})
......@@ -185,7 +183,7 @@ func TestAddContractDuplicatedUser(t *testing.T) {
client := clientTest(t)
errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
Hash: defaultHashStr,
Hash: defaultHash[:],
Filename: "ContractFilename",
Signer: []string{user1.Email, user1.Email, user2.Email},
})
......@@ -209,7 +207,7 @@ func TestAddContractNoFilename(t *testing.T) {
client := clientTest(t)
errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
Hash: defaultHashStr,
Hash: defaultHash[:],
Signer: []string{user1.Email},
})
assert.Equal(t, nil, err)
......@@ -231,7 +229,7 @@ func TestAddContractBadHash(t *testing.T) {
client := clientTest(t)
errorCode, err := client.PostContract(context.Background(), &api.PostContractRequest{
Hash: "aVeryBadHash",
Hash: []byte{0xba, 0xd},
Filename: "ContractFilename",
Signer: []string{user1.Email},
})
......
......@@ -2,6 +2,7 @@ package contract
import (
"encoding/json"
"fmt"
"time"
"dfss/dfssp/entities"
......@@ -46,7 +47,7 @@ func GetJSON(c *entities.Contract, ttp *TTPJSON) ([]byte, error) {
Comment: c.Comment,
File: &FileJSON{
Name: c.File.Name,
Hash: c.File.Hash,
Hash: fmt.Sprintf("%x", c.File.Hash),
Hosted: c.File.Hosted,
},
Signers: make([]SignerJSON, len(c.Signers)),
......@@ -55,7 +56,7 @@ func GetJSON(c *entities.Contract, ttp *TTPJSON) ([]byte, error) {
for i, s := range c.Signers {
data.Signers[i].Email = s.Email
data.Signers[i].Hash = s.Hash
data.Signers[i].Hash = fmt.Sprintf("%x", s.Hash)
}
return json.MarshalIndent(data, "", " ")
......
......@@ -20,12 +20,12 @@ func TestGetJSON(t *testing.T) {
allow multiline and accents: éÉ`,
File: &entities.File{
Name: "filename.pdf",
Hash: "hash",
Hash: []byte{0x01, 0x02},
Hosted: false,
},
Signers: []entities.Signer{
entities.Signer{Email: "a", Hash: "ha"},
entities.Signer{Email: "b", Hash: "hb"},
entities.Signer{Email: "a", Hash: []byte{0xaa}},
entities.Signer{Email: "b", Hash: []byte{0xbb}},
},
}
......@@ -35,17 +35,17 @@ allow multiline and accents: éÉ`,
"Comment": "A test comment\nallow multiline and accents: éÉ",
"File": {
"Name": "filename.pdf",
"Hash": "hash",
"Hash": "0102",
"Hosted": false
},
"Signers": [
{
"Email": "a",
"Hash": "ha"
"Hash": "aa"
},
{
"Email": "b",
"Hash": "hb"
"Hash": "bb"
}
],
"Sequence": null,
......
......@@ -10,7 +10,7 @@ import (
// File : Represents a file structure
type File struct {
Name string `key:"name" bson:"name"` // Name of the File
Hash string `key:"hash" bson:"hash"` // Hash of the File
Hash []byte `key:"hash" bson:"hash"` // Hash of the File
Hosted bool `key:"hosted" bson:"hosted"` // True if hosted on the platform, else false
}
......@@ -18,7 +18,7 @@ type File struct {
type Signer struct {
UserID bson.ObjectId `key:"userId" bson:"userId"`
Email string `key:"email" bson:"email"`
Hash string `key:"hash" bson:"hash"`
Hash []byte `key:"hash" bson:"hash"`
}
// Contract : Informations about a contract to be signed
......@@ -44,7 +44,7 @@ func NewContract() *Contract {
}
// AddSigner : Add a signer to the contract
func (c *Contract) AddSigner(id *bson.ObjectId, email, hash string) {
func (c *Contract) AddSigner(id *bson.ObjectId, email string, hash []byte) {
signer := &Signer{}
signer.Email = email
......@@ -78,7 +78,7 @@ func (r *ContractRepository) GetWaitingForUser(email string) ([]Contract, error)
"signers": bson.M{
"$elemMatch": bson.M{
"email": email,
"hash": "",
"hash": []byte{},
}},
}, &res)
return res, err
......
......@@ -28,7 +28,7 @@ type User struct {
RegToken string `key:"regToken" bson:"regToken"` // Token used for registering a User
Csr string `key:"csr" bson:"csr"` // Certificate request at PEM format
Certificate string `key:"certificate" bson:"certificate"` // Certificate of the User
CertHash string `key:"certHash" bson:"certHash"` // Hash of the certificate
CertHash []byte `key:"certHash" bson:"certHash"` // Hash of the certificate
ConnInfo ConnectionInfo `key:"connInfo" bson:"connInfo"` // Information about the connection
}
......@@ -54,7 +54,7 @@ func NewUserRepository(collection *mgdb.MongoCollection) *UserRepository {
}
// FetchByMailAndHash : Fetches a User from its email and certificate hash
func (repository *UserRepository) FetchByMailAndHash(email, hash string) (*User, error) {
func (repository *UserRepository) FetchByMailAndHash(email string, hash []byte) (*User, error) {
var users []User
err := repository.Collection.FindAll(bson.M{"email": email, "certHash": hash}, &users)
if err != nil || len(users) == 0 {
......
......@@ -13,6 +13,6 @@ const contractDetails = `Signers :
{{range .Signers}} - {{.Email}}
{{end}}
Contract name : {{.File.Name}}
SHA-512 hash : {{.File.Hash}}
SHA-512 hash : {{printf "%x" .File.Hash}}
Comment : {{.Comment}}
`
......@@ -14,11 +14,11 @@ func TestInit(t *testing.T) {
func TestGet(t *testing.T) {
contract := entities.NewContract()
contract.File.Hash = "hash"
contract.File.Hash = []byte{0x01, 0x02, 0x11, 0xaa}
contract.File.Name = "name.pdf"
contract.Comment = "comment"
contract.AddSigner(nil, "mail@example.com", "")
contract.AddSigner(nil, "mail2@example.com", "")
contract.AddSigner(nil, "mail@example.com", nil)
contract.AddSigner(nil, "mail2@example.com", nil)
s, err := Get("contract", contract)
......@@ -32,7 +32,7 @@ Signers :
- mail2@example.com
Contract name : name.pdf
SHA-512 hash : hash
SHA-512 hash : 010211aa
Comment : comment
Yours faithfully,
......
......@@ -199,7 +199,7 @@ func Auth(pid *authority.PlatformID, manager *mgdb.MongoManager, certDuration in
}
// If the user already has a certificate and certificate hash in the database, does nothing
if user.Certificate != "" || user.CertHash != "" {
if user.Certificate != "" || len(user.CertHash) != 0 {
return nil, errors.New("User is already registered")
}
......@@ -221,7 +221,7 @@ func Auth(pid *authority.PlatformID, manager *mgdb.MongoManager, certDuration in
}
user.Certificate = string(cert)
user.CertHash = fmt.Sprintf("%x", certHash)
user.CertHash = certHash
user.Expiration = time.Now().AddDate(0, 0, certDuration)
// Updating the database
......
......@@ -75,11 +75,8 @@ func TestMain(m *testing.M) {
go func() { _ = net.Listen(InvalidServ, srv2) }()
// Run
err = collection.Drop()
code := m.Run()
// Teardown
// The collection is created automatically on
// first connection, that's why we do not recreate it manually
err = collection.Drop()
if err != nil {
......@@ -91,7 +88,7 @@ func TestMain(m *testing.M) {
}
func TestMongoFetchInexistantUser(t *testing.T) {
user, erro := repository.FetchByMailAndHash("dummyMail", "dummyHash")
user, erro := repository.FetchByMailAndHash("dummyMail", []byte{0x01})
if user != nil || erro != nil {
t.Fatal("User should not have been found and error should be nil")
}
......@@ -100,7 +97,7 @@ func TestMongoFetchInexistantUser(t *testing.T) {
func TestMongoInsertUser(t *testing.T) {
user := entities.NewUser()
user.Email = "dfss1@mpcs.tk"
user.CertHash = "dummy_hash"
user.CertHash = []byte{0x01, 0x02}
user.ConnInfo.IP = "127.0.0.1"
user.ConnInfo.Port = 1111
user.Csr = "csr1"
......@@ -117,7 +114,7 @@ func equalUsers(t *testing.T, user1, user2 *entities.User) {
t.Fatal("ID doesn't match : received ", user1.ID, " and ", user2.ID)
}
if user1.CertHash != user2.CertHash {
if string(user1.CertHash) != string(user2.CertHash) {
t.Fatal("CertHash doesn't match : received ", user1.CertHash, " and ", user2.CertHash)
}
......@@ -153,7 +150,7 @@ func equalUsers(t *testing.T, user1, user2 *entities.User) {
func TestMongoFetchUser(t *testing.T) {
user := entities.NewUser()
user.Email = "dfss2@mpcs.tk"
user.CertHash = "dummy_hash"
user.CertHash = nil
user.ConnInfo.IP = "127.0.0.2"
user.ConnInfo.Port = 2222
user.Csr = "csr2"
......@@ -240,7 +237,7 @@ func TestWrongAuthRequestContext(t *testing.T) {
}
assert.Equal(t, res.Certificate, "")
assert.Equal(t, res.CertHash, "")
assert.Equal(t, res.CertHash, []byte{})
// Invalid certificate request (none here)
request.Token = token
......@@ -255,7 +252,7 @@ func TestWrongAuthRequestContext(t *testing.T) {
t.Fatal(err)
}
assert.Equal(t, res.Certificate, "")
assert.Equal(t, res.CertHash, "")
assert.Equal(t, res.CertHash, []byte{})
}
func ExampleAuth() {
......@@ -305,7 +302,7 @@ func ExampleAuth() {
fmt.Println(err)
}
if res.Certificate == "" || res.CertHash == "" {
if res.Certificate == "" || res.CertHash == nil {
fmt.Println("The database should have been updated")
}
......@@ -347,7 +344,7 @@ func TestAuthTwice(t *testing.T) {
user.RegToken = token
user.Csr = string(csr)
user.Certificate = "foo"
user.CertHash = "foo"
user.CertHash = []byte{0xaa}
_, err = repository.Collection.Insert(*user)
if err != nil {
......
package tests
import (
"fmt"
"io/ioutil"
"os"