signal.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright (c) 2022 Tulir Asokan
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package store
  7. import (
  8. "context"
  9. "fmt"
  10. "go.mau.fi/libsignal/ecc"
  11. groupRecord "go.mau.fi/libsignal/groups/state/record"
  12. "go.mau.fi/libsignal/keys/identity"
  13. "go.mau.fi/libsignal/protocol"
  14. "go.mau.fi/libsignal/serialize"
  15. "go.mau.fi/libsignal/state/record"
  16. "go.mau.fi/libsignal/state/store"
  17. )
  18. var SignalProtobufSerializer = serialize.NewProtoBufSerializer()
  19. var _ store.SignalProtocol = (*Device)(nil)
  20. func (device *Device) GetIdentityKeyPair() *identity.KeyPair {
  21. return identity.NewKeyPair(
  22. identity.NewKey(ecc.NewDjbECPublicKey(*device.IdentityKey.Pub)),
  23. ecc.NewDjbECPrivateKey(*device.IdentityKey.Priv),
  24. )
  25. }
  26. func (device *Device) GetLocalRegistrationID() uint32 {
  27. return device.RegistrationID
  28. }
  29. func (device *Device) SaveIdentity(ctx context.Context, address *protocol.SignalAddress, identityKey *identity.Key) error {
  30. addrString := address.String()
  31. err := device.Identities.PutIdentity(ctx, addrString, identityKey.PublicKey().PublicKey())
  32. if err != nil {
  33. return fmt.Errorf("failed to save identity of %s: %w", addrString, err)
  34. }
  35. return nil
  36. }
  37. func (device *Device) IsTrustedIdentity(ctx context.Context, address *protocol.SignalAddress, identityKey *identity.Key) (bool, error) {
  38. addrString := address.String()
  39. isTrusted, err := device.Identities.IsTrustedIdentity(ctx, addrString, identityKey.PublicKey().PublicKey())
  40. if err != nil {
  41. return false, fmt.Errorf("failed to check if %s's identity is trusted: %w", addrString, err)
  42. }
  43. return isTrusted, nil
  44. }
  45. func (device *Device) LoadPreKey(ctx context.Context, id uint32) (*record.PreKey, error) {
  46. preKey, err := device.PreKeys.GetPreKey(ctx, id)
  47. if err != nil {
  48. return nil, fmt.Errorf("failed to load prekey %d: %w", id, err)
  49. }
  50. if preKey == nil {
  51. return nil, nil
  52. }
  53. return record.NewPreKey(preKey.KeyID, ecc.NewECKeyPair(
  54. ecc.NewDjbECPublicKey(*preKey.Pub),
  55. ecc.NewDjbECPrivateKey(*preKey.Priv),
  56. ), nil), nil
  57. }
  58. func (device *Device) RemovePreKey(ctx context.Context, id uint32) error {
  59. err := device.PreKeys.RemovePreKey(ctx, id)
  60. if err != nil {
  61. return fmt.Errorf("failed to remove prekey %d: %w", id, err)
  62. }
  63. return nil
  64. }
  65. func (device *Device) StorePreKey(ctx context.Context, preKeyID uint32, preKeyRecord *record.PreKey) error {
  66. panic("not implemented")
  67. }
  68. func (device *Device) ContainsPreKey(ctx context.Context, preKeyID uint32) (bool, error) {
  69. panic("not implemented")
  70. }
  71. func (device *Device) LoadSession(ctx context.Context, address *protocol.SignalAddress) (*record.Session, error) {
  72. addrString := address.String()
  73. if sess := getCachedSession(ctx, addrString); sess != nil {
  74. return sess, nil
  75. }
  76. rawSess, err := device.Sessions.GetSession(ctx, addrString)
  77. if err != nil {
  78. return nil, fmt.Errorf("failed to load session with %s: %w", addrString, err)
  79. }
  80. if rawSess == nil {
  81. return record.NewSession(SignalProtobufSerializer.Session, SignalProtobufSerializer.State), nil
  82. }
  83. sess, err := record.NewSessionFromBytes(rawSess, SignalProtobufSerializer.Session, SignalProtobufSerializer.State)
  84. if err != nil {
  85. return nil, fmt.Errorf("failed to deserialize session with %s: %w", addrString, err)
  86. }
  87. return sess, nil
  88. }
  89. func (device *Device) GetSubDeviceSessions(ctx context.Context, name string) ([]uint32, error) {
  90. panic("not implemented")
  91. }
  92. func (device *Device) StoreSession(ctx context.Context, address *protocol.SignalAddress, record *record.Session) error {
  93. addrString := address.String()
  94. if putCachedSession(ctx, addrString, record) {
  95. return nil
  96. }
  97. err := device.Sessions.PutSession(ctx, addrString, record.Serialize())
  98. if err != nil {
  99. return fmt.Errorf("failed to store session with %s: %w", addrString, err)
  100. }
  101. return nil
  102. }
  103. func (device *Device) ContainsSession(ctx context.Context, remoteAddress *protocol.SignalAddress) (bool, error) {
  104. addrString := remoteAddress.String()
  105. hasSession, err := device.Sessions.HasSession(ctx, addrString)
  106. if err != nil {
  107. return false, fmt.Errorf("failed to check if store has session for %s: %w", addrString, err)
  108. }
  109. return hasSession, nil
  110. }
  111. func (device *Device) DeleteSession(ctx context.Context, remoteAddress *protocol.SignalAddress) error {
  112. panic("not implemented")
  113. }
  114. func (device *Device) DeleteAllSessions(ctx context.Context) error {
  115. panic("not implemented")
  116. }
  117. func (device *Device) LoadSignedPreKey(ctx context.Context, signedPreKeyID uint32) (*record.SignedPreKey, error) {
  118. if signedPreKeyID == device.SignedPreKey.KeyID {
  119. return record.NewSignedPreKey(signedPreKeyID, 0, ecc.NewECKeyPair(
  120. ecc.NewDjbECPublicKey(*device.SignedPreKey.Pub),
  121. ecc.NewDjbECPrivateKey(*device.SignedPreKey.Priv),
  122. ), *device.SignedPreKey.Signature, nil), nil
  123. }
  124. return nil, nil
  125. }
  126. func (device *Device) LoadSignedPreKeys(ctx context.Context) ([]*record.SignedPreKey, error) {
  127. panic("not implemented")
  128. }
  129. func (device *Device) StoreSignedPreKey(ctx context.Context, signedPreKeyID uint32, record *record.SignedPreKey) error {
  130. panic("not implemented")
  131. }
  132. func (device *Device) ContainsSignedPreKey(ctx context.Context, signedPreKeyID uint32) (bool, error) {
  133. panic("not implemented")
  134. }
  135. func (device *Device) RemoveSignedPreKey(ctx context.Context, signedPreKeyID uint32) error {
  136. panic("not implemented")
  137. }
  138. func (device *Device) StoreSenderKey(ctx context.Context, senderKeyName *protocol.SenderKeyName, keyRecord *groupRecord.SenderKey) error {
  139. groupID := senderKeyName.GroupID()
  140. senderString := senderKeyName.Sender().String()
  141. err := device.SenderKeys.PutSenderKey(ctx, groupID, senderString, keyRecord.Serialize())
  142. if err != nil {
  143. return fmt.Errorf("failed to store sender key from %s for %s: %w", senderString, groupID, err)
  144. }
  145. return nil
  146. }
  147. func (device *Device) LoadSenderKey(ctx context.Context, senderKeyName *protocol.SenderKeyName) (*groupRecord.SenderKey, error) {
  148. groupID := senderKeyName.GroupID()
  149. senderString := senderKeyName.Sender().String()
  150. rawKey, err := device.SenderKeys.GetSenderKey(ctx, groupID, senderString)
  151. if err != nil {
  152. return nil, fmt.Errorf("failed to load sender key from %s for %s: %w", senderString, groupID, err)
  153. }
  154. if rawKey == nil {
  155. return groupRecord.NewSenderKey(SignalProtobufSerializer.SenderKeyRecord, SignalProtobufSerializer.SenderKeyState), nil
  156. }
  157. key, err := groupRecord.NewSenderKeyFromBytes(rawKey, SignalProtobufSerializer.SenderKeyRecord, SignalProtobufSerializer.SenderKeyState)
  158. if err != nil {
  159. return nil, fmt.Errorf("failed to deserialize sender key from %s for %s: %w", senderString, groupID, err)
  160. }
  161. return key, nil
  162. }