Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Basic Crayfish registration support
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Sep 14, 2021
1 parent ec7fe01 commit 92fd984
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 57 deletions.
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ type Config struct {
AccountCapabilities AccountCapabilities `yaml:"accountCapabilities"` // Account Attrributes are used in order to track the support of different function for signal
DiscoverableByPhoneNumber bool `yaml:"discoverableByPhoneNumber"` // If the user should be found by his phone number
ProfileKey []byte `yaml:"profileKey"` // The profile key is used in many places to encrypt the avatar, name etc and also in groupsv2 context
Name string `yaml:"name"`
UnidentifiedAccessKey []byte `yaml:"unidentifiedAccessKey"` // The access key for unidentified users
Certificate []byte `yaml:"certificate"` // The access key for unidentified users
Name string `yaml:"name"` // The username
UnidentifiedAccessKey []byte `yaml:"unidentifiedAccessKey"` // The access key for unidentified users
Certificate []byte `yaml:"certificate"` // The access key for unidentified users
CrayfishSupport bool `yaml:"cryfishSupport"` // weather the client uses cryfish or not
}

// AccountCapabilities describes what functions axolotl supports
Expand Down
40 changes: 20 additions & 20 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ type RegistrationLockFailure struct {
}

// verifyCode verificates the account with signal server
func verifyCode(code string, pin *string, credentials *transport.AuthCredentials) (error, *transport.AuthCredentials) {
func verifyCode(code string, pin *string, credentials *transport.AuthCredentials) (*transport.AuthCredentials, error) {
code = strings.Replace(code, "-", "", -1)
key := identityKey.PrivateKey.Key()[:]
unidentifiedAccessKey, err := unidentifiedAccess.DeriveAccessKeyFrom(key)
Expand Down Expand Up @@ -261,12 +261,12 @@ func verifyCode(code string, pin *string, credentials *transport.AuthCredentials
log.Debugln("[textsecure] verifyCode", vd)
body, err := json.Marshal(vd)
if err != nil {
return err, nil
return nil, err
}
resp, err := transport.Transport.PutJSON(fmt.Sprintf(VERIFY_ACCOUNT_CODE_PATH, code), body)
if err != nil {
log.Errorln("[textsecure] verifyCode", err)
return err, nil
return nil, err
}
if resp.IsError() {

Expand All @@ -278,13 +278,20 @@ func verifyCode(code string, pin *string, credentials *transport.AuthCredentials
v := RegistrationLockFailure{}
err := json.Unmarshal([]byte(newStr), &v)
if err != nil {
return err, nil
return nil, err
}
return fmt.Errorf(fmt.Sprintf("RegistrationLockFailure \n Time to wait \n %s", newStr)), &v.Credentials
return &v.Credentials, fmt.Errorf(fmt.Sprintf("RegistrationLockFailure \n Time to wait \n %s", newStr))
} else {
return resp, nil
return nil, resp
}
}
// extract uuid
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Body)
newStr := buf.String()
log.Errorln("[textsecure] successful registration", newStr)
defer resp.Body.Close()

config.ConfigFile.AccountCapabilities = vd.Capabilities
return nil, nil
}
Expand Down Expand Up @@ -543,16 +550,6 @@ func getPreKeys(UUID string, deviceID string) (*preKeyResponse, error) {
return k, nil
}

// jsonContact is the data returned by the server for each registered contact
type jsonContact struct {
Token string `json:"token"`
Voice string `json:"voice"`
Video string `json:"video"`
}
type jsonContacts struct {
Contacts []jsonContact `json:"contacts"`
}

func getContactDiscoveryRegisteredUsers(authorization string, request *contactDiscoveryCrypto.DiscoveryRequest, cookies string, mrenclave string) (*contactDiscoveryCrypto.DiscoveryResponse, error) {
log.Debugln("[textsecure] getContactDiscoveryRegisteredUser")
body, err := json.Marshal(*request)
Expand Down Expand Up @@ -612,27 +609,30 @@ func GetRegisteredContacts() ([]contacts.Contact, error) {

authCredentials, err := transport.GetCredendtails(DIRECTORY_AUTH_PATH)
if err != nil {
return nil, fmt.Errorf("Could not get auth credentials %v", err)
return nil, fmt.Errorf("could not get auth credentials %v", err)
}
remoteAttestation := contactsDiscovery.RemoteAttestation{}
attestations, err := remoteAttestation.GetAndVerifyMultiRemoteAttestation(CDS_MRENCLAVE,
authCredentials.AsBasic(),
)
if err != nil {
return nil, fmt.Errorf("could not get remote attestation %v", err)
}
log.Debugln("[textsecure] GetRegisteredContacts assestations")
request, err := contactDiscoveryCrypto.CreateDiscoveryRequest(tokens, attestations)
if err != nil {
return nil, fmt.Errorf("Could not get create createDiscoveryRequest %v", err)
return nil, fmt.Errorf("could not get create createDiscoveryRequest %v", err)
}
log.Debugln("[textsecure] GetRegisteredContacts contactDiscoveryRequest")

response, err := getContactDiscoveryRegisteredUsers(authCredentials.AsBasic(), request, remoteAttestation.Cookies, CDS_MRENCLAVE)
if err != nil {
return nil, fmt.Errorf("Could not get get ContactDiscovery %v", err)
return nil, fmt.Errorf("could not get get ContactDiscovery %v", err)
}

responseData, err := contactDiscoveryCrypto.GetDiscoveryResponseData(*response, attestations)
if err != nil {
return nil, fmt.Errorf("Could not get get ContactDiscovery data %v", err)
return nil, fmt.Errorf("could not get get ContactDiscovery data %v", err)
}
uuidlength := 16
ind := 0
Expand Down
83 changes: 53 additions & 30 deletions textsecure.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ type Client struct {
SyncSentHandler func(*Message, uint64)
RegistrationDone func()
GetUsername func() string
RegisterWithCrayfish func(*RegistrationInfo) (*CrayfishRegistration, error)
}

var (
Expand Down Expand Up @@ -354,53 +355,75 @@ func renewSenderCertificate() error {

}

type CrayfishRegistration struct {
UUID string `json:"uuid"`
}

func registerDevice() error {
var err error
if config.ConfigFile.Tel == "" {
config.ConfigFile.Tel = client.GetPhoneNumber()
if config.ConfigFile.Tel == "" {
return errors.New("empty phone number")
}
}
rootCa.SetupCA(config.ConfigFile.RootCA)
transport.SetupTransporter(config.ConfigFile.Server, config.ConfigFile.Tel, registrationInfo.password, config.ConfigFile.UserAgent, config.ConfigFile.ProxyServer)
// try to register without token
code, responseCode, err := requestCode(config.ConfigFile.Tel, config.ConfigFile.VerificationType, "")
if responseCode != nil {
if *responseCode == responseNeedCaptcha {
// Need to generate a token on https://signalcaptchas.org/registration/generate.html
log.Infoln("[textsecure] registration needs captcha")

captcha := client.GetCaptchaToken()
code, _, err = requestCode(config.ConfigFile.Tel, config.ConfigFile.VerificationType, captcha)
if err != nil {
if config.ConfigFile.CrayfishSupport {
crayfishRegistration, err := client.RegisterWithCrayfish(&registrationInfo)

if err != nil {
return err
}
if err != nil {
log.Debugln("[textsecure] verifyCode", err)
}
config.ConfigFile.UUID = crayfishRegistration.UUID
config.ConfigFile.AccountCapabilities = config.AccountCapabilities{
UUID: false,
Gv2: true,
Storage: false,
}
} else {
rootCa.SetupCA(config.ConfigFile.RootCA)
transport.SetupTransporter(config.ConfigFile.Server, config.ConfigFile.Tel, registrationInfo.password, config.ConfigFile.UserAgent, config.ConfigFile.ProxyServer)
// try to register without token
code, responseCode, err := requestCode(config.ConfigFile.Tel, config.ConfigFile.VerificationType, "")
if responseCode != nil {
if *responseCode == responseNeedCaptcha {
// Need to generate a token on https://signalcaptchas.org/registration/generate.html
log.Infoln("[textsecure] registration needs captcha")

captcha := client.GetCaptchaToken()
code, _, err = requestCode(config.ConfigFile.Tel, config.ConfigFile.VerificationType, captcha)
if err != nil {
return err
}
} else {
return err
}
} else {
} else if err != nil {
return err
}
} else if err != nil {
return err
}
if config.ConfigFile.VerificationType != "dev" {
code = client.GetVerificationCode()
}
err, credentials := verifyCode(code, nil, nil)
if err != nil {
if credentials != nil {
log.Warnln("[textsecure] verfication failed, try again with pin", err.Error())
pin := client.GetPin()
err, credentials = verifyCode(code, &pin, credentials)
if err != nil {
if config.ConfigFile.VerificationType != "dev" {
code = client.GetVerificationCode()
}
credentials, err := verifyCode(code, nil, nil)
if err != nil {
if credentials != nil {
log.Warnln("[textsecure] verfication failed, try again with pin", err.Error())
pin := client.GetPin()
credentials, err = verifyCode(code, &pin, credentials)
if err != nil {
log.Warnln("[textsecure] verfication failed", err.Error())
return err
}
} else {
log.Warnln("[textsecure] verfication failed", err.Error())
return err
}
} else {
log.Warnln("[textsecure] verfication failed", err.Error())
return err

}

}

err = generatePreKeys()
if err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ func SetupCDNTransporter(cdnUrl string, tel string, password string, userAgent s

var DirectoryTransport *httpTransporter

func SetupDirectoryTransporter(Url string, tel string, password string, userAgent string, proxyServer string) {
func SetupDirectoryTransporter(Url string, uuid string, password string, userAgent string, proxyServer string) {
// setupCA()
DirectoryTransport = newHTTPTransporter(Url, tel, password, userAgent, proxyServer, rootCa.DirectoryCA)
DirectoryTransport = newHTTPTransporter(Url, uuid, password, userAgent, proxyServer, rootCa.DirectoryCA)
}

var StorageTransport *httpTransporter

func SetupStorageTransporter(Url string, tel string, password string, userAgent string, proxyServer string) {
func SetupStorageTransporter(Url string, uuid string, password string, userAgent string, proxyServer string) {
// setupCA()
StorageTransport = newHTTPTransporter(Url, tel, password, userAgent, proxyServer, rootCa.DirectoryCA)
StorageTransport = newHTTPTransporter(Url, uuid, password, userAgent, proxyServer, rootCa.DirectoryCA)
}

type response struct {
Expand Down

0 comments on commit 92fd984

Please sign in to comment.