diff --git a/bindings/contact.go b/bindings/contact.go index 8e24e486141fa9da7f06f61e6ddbb54b353c7924..251c810f40c6e31a6212b8e2eaee770c86724d00 100644 --- a/bindings/contact.go +++ b/bindings/contact.go @@ -6,7 +6,6 @@ import ( "gitlab.com/elixxir/crypto/contact" "gitlab.com/elixxir/primitives/fact" "gitlab.com/xx_network/crypto/signature/rsa" - "gitlab.com/xx_network/primitives/id" ) // ReceptionIdentity struct @@ -47,57 +46,6 @@ func (c *Cmix) MakeIdentity() ([]byte, error) { return json.Marshal(&I) } -// GetContactFromIdentity accepts a marshalled ReceptionIdentity object and returns a marshalled contact.Contact object -func (c *Cmix) GetContactFromIdentity(identity []byte) ([]byte, error) { - unmarshalledIdentity, err := c.unmarshalIdentity(identity) - if err != nil { - return nil, err - } - - grp := c.api.GetStorage().GetE2EGroup() - - dhPub := grp.ExpG(unmarshalledIdentity.DHKeyPrivate, grp.NewInt(1)) - - ct := contact.Contact{ - ID: unmarshalledIdentity.ID, - DhPubKey: dhPub, - OwnershipProof: nil, - Facts: nil, - } - - return ct.Marshal(), nil -} - -func (c *Cmix) unmarshalIdentity(marshaled []byte) (xxdk.ReceptionIdentity, error) { - newIdentity := xxdk.ReceptionIdentity{} - - // Unmarshal given identity into ReceptionIdentity object - givenIdentity := ReceptionIdentity{} - err := json.Unmarshal(marshaled, &givenIdentity) - if err != nil { - return xxdk.ReceptionIdentity{}, err - } - - newIdentity.ID, err = id.Unmarshal(givenIdentity.ID) - if err != nil { - return xxdk.ReceptionIdentity{}, err - } - - newIdentity.DHKeyPrivate = c.api.GetStorage().GetE2EGroup().NewInt(1) - err = newIdentity.DHKeyPrivate.UnmarshalJSON(givenIdentity.DHKeyPrivate) - if err != nil { - return xxdk.ReceptionIdentity{}, err - } - - newIdentity.RSAPrivatePem, err = rsa.LoadPrivateKeyFromPem(givenIdentity.RSAPrivatePem) - if err != nil { - return xxdk.ReceptionIdentity{}, err - } - - newIdentity.Salt = givenIdentity.Salt - return newIdentity, nil -} - // GetIDFromContact accepts a marshalled contact.Contact object & returns a marshalled id.ID object func GetIDFromContact(marshaled []byte) ([]byte, error) { cnt, err := contact.Unmarshal(marshaled) diff --git a/bindings/e2e.go b/bindings/e2e.go index 6134db25db442727b6d32873f1f11d795f8d8f2c..fe7a44061acb1921c28a0fb09dc61c19a22e660d 100644 --- a/bindings/e2e.go +++ b/bindings/e2e.go @@ -7,11 +7,14 @@ package bindings import ( + "encoding/json" "gitlab.com/elixxir/client/auth" "gitlab.com/elixxir/client/cmix/identity/receptionID" "gitlab.com/elixxir/client/cmix/rounds" "gitlab.com/elixxir/client/xxdk" "gitlab.com/elixxir/crypto/contact" + "gitlab.com/xx_network/crypto/signature/rsa" + "gitlab.com/xx_network/primitives/id" ) // e2eTrackerSingleton is used to track E2e objects so that @@ -37,7 +40,7 @@ func (e *E2e) Login(cmixId int, callbacks AuthCallbacks, identity []byte) (*E2e, return nil, err } - newIdentity, err := cmix.unmarshalIdentity(identity) + newIdentity, err := e.unmarshalIdentity(identity) if err != nil { return nil, err } @@ -56,6 +59,46 @@ func (e *E2e) Login(cmixId int, callbacks AuthCallbacks, identity []byte) (*E2e, return e2eTrackerSingleton.make(newE2e), nil } +// GetContactFromIdentity accepts a marshalled ReceptionIdentity object and returns a marshalled contact.Contact object +func (e *E2e) GetContactFromIdentity(identity []byte) ([]byte, error) { + unmarshalledIdentity, err := e.unmarshalIdentity(identity) + if err != nil { + return nil, err + } + return unmarshalledIdentity.GetContact(e.api.GetStorage().GetE2EGroup()).Marshal(), nil +} + +// unmarshalIdentity is a helper function for taking in a marshalled xxdk.ReceptionIdentity and making it an object +func (e *E2e) unmarshalIdentity(marshaled []byte) (xxdk.ReceptionIdentity, error) { + newIdentity := xxdk.ReceptionIdentity{} + + // Unmarshal given identity into ReceptionIdentity object + givenIdentity := ReceptionIdentity{} + err := json.Unmarshal(marshaled, &givenIdentity) + if err != nil { + return xxdk.ReceptionIdentity{}, err + } + + newIdentity.ID, err = id.Unmarshal(givenIdentity.ID) + if err != nil { + return xxdk.ReceptionIdentity{}, err + } + + newIdentity.DHKeyPrivate = e.api.GetStorage().GetE2EGroup().NewInt(1) + err = newIdentity.DHKeyPrivate.UnmarshalJSON(givenIdentity.DHKeyPrivate) + if err != nil { + return xxdk.ReceptionIdentity{}, err + } + + newIdentity.RSAPrivatePem, err = rsa.LoadPrivateKeyFromPem(givenIdentity.RSAPrivatePem) + if err != nil { + return xxdk.ReceptionIdentity{}, err + } + + newIdentity.Salt = givenIdentity.Salt + return newIdentity, nil +} + // AuthCallbacks is the bindings-specific interface for auth.Callbacks methods. type AuthCallbacks interface { Request(contact, receptionId []byte, ephemeralId, roundId int64) diff --git a/connect/authenticated.go b/connect/authenticated.go index 0638c292c02f7f1fff154013a4bebba6f3e339bb..f5a54833a21aeba421b78cd9b27e5f5df5085cb2 100644 --- a/connect/authenticated.go +++ b/connect/authenticated.go @@ -66,7 +66,7 @@ func ConnectWithAuthentication(recipient contact.Contact, e2eClient *xxdk.E2e, } // Build the authenticated connection and return - identity := e2eClient.GetTransmissionIdentity() + identity := e2eClient.GetReceptionIdentity() return connectWithAuthentication(conn, timeStart, recipient, identity.Salt, identity.RSAPrivatePem, e2eClient.GetRng(), e2eClient.GetCmix(), p) } diff --git a/xxdk/e2e.go b/xxdk/e2e.go index 1dabcf44eb15cd2826c759ca45a7a9dc7ea145d6..f281f778cc71f7776493f197849e2952f3deb96e 100644 --- a/xxdk/e2e.go +++ b/xxdk/e2e.go @@ -209,8 +209,8 @@ func (m *E2e) GetUser() user.Info { return u } -// GetTransmissionIdentity returns a safe copy of the E2e ReceptionIdentity -func (m *E2e) GetTransmissionIdentity() ReceptionIdentity { +// GetReceptionIdentity returns a safe copy of the E2e ReceptionIdentity +func (m *E2e) GetReceptionIdentity() ReceptionIdentity { return m.e2eIdentity.DeepCopy() } diff --git a/xxdk/identity.go b/xxdk/identity.go index 52959db88ced70e0722f18d430397f90e728db6b..da708d6c2a33d28b3df580642cc79aa16a5c2bee 100644 --- a/xxdk/identity.go +++ b/xxdk/identity.go @@ -7,6 +7,7 @@ package xxdk import ( + "gitlab.com/elixxir/crypto/contact" "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/diffieHellman" "gitlab.com/xx_network/crypto/csprng" @@ -59,13 +60,26 @@ func MakeReceptionIdentity(rng csprng.Source, grp *cyclic.Group) (ReceptionIdent } // DeepCopy produces a safe copy of a ReceptionIdentity -func (t ReceptionIdentity) DeepCopy() ReceptionIdentity { - saltCopy := make([]byte, len(t.Salt)) - copy(saltCopy, t.Salt) +func (r ReceptionIdentity) DeepCopy() ReceptionIdentity { + saltCopy := make([]byte, len(r.Salt)) + copy(saltCopy, r.Salt) return ReceptionIdentity{ - ID: t.ID.DeepCopy(), - RSAPrivatePem: t.RSAPrivatePem, + ID: r.ID.DeepCopy(), + RSAPrivatePem: r.RSAPrivatePem, Salt: saltCopy, - DHKeyPrivate: t.DHKeyPrivate.DeepCopy(), + DHKeyPrivate: r.DHKeyPrivate.DeepCopy(), } } + +// GetContact accepts a xxdk.ReceptionIdentity object and returns a contact.Contact object +func (r ReceptionIdentity) GetContact(grp *cyclic.Group) contact.Contact { + dhPub := grp.ExpG(r.DHKeyPrivate, grp.NewInt(1)) + + ct := contact.Contact{ + ID: r.ID, + DhPubKey: dhPub, + OwnershipProof: nil, + Facts: nil, + } + return ct +}