@open-e2ee/signal-protocol-sdk 0.4.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (255) hide show
  1. package/CHANGELOG.md +126 -5
  2. package/COMMERCIAL.md +1 -1
  3. package/README.md +10 -7
  4. package/SECURITY.md +2 -2
  5. package/THIRD_PARTY_NOTICES.md +1 -1
  6. package/device/README.md +7 -5
  7. package/dist/client/client.d.ts +3 -2
  8. package/dist/client/client.d.ts.map +1 -1
  9. package/dist/client/client.js +58 -25
  10. package/dist/client/client.js.map +1 -1
  11. package/dist/client/config.d.ts +46 -7
  12. package/dist/client/config.d.ts.map +1 -1
  13. package/dist/client/config.js +2 -2
  14. package/dist/client/config.js.map +1 -1
  15. package/dist/client/constants.d.ts +4 -2
  16. package/dist/client/constants.d.ts.map +1 -1
  17. package/dist/client/constants.js +4 -2
  18. package/dist/client/constants.js.map +1 -1
  19. package/dist/client/group-outbox.d.ts +38 -0
  20. package/dist/client/group-outbox.d.ts.map +1 -0
  21. package/dist/client/group-outbox.js +224 -0
  22. package/dist/client/group-outbox.js.map +1 -0
  23. package/dist/client/group-sealed-sender.d.ts +28 -0
  24. package/dist/client/group-sealed-sender.d.ts.map +1 -0
  25. package/dist/client/group-sealed-sender.js +74 -0
  26. package/dist/client/group-sealed-sender.js.map +1 -0
  27. package/dist/client/hosted-connection.d.ts +16 -0
  28. package/dist/client/hosted-connection.d.ts.map +1 -0
  29. package/dist/client/hosted-connection.js +255 -0
  30. package/dist/client/hosted-connection.js.map +1 -0
  31. package/dist/client/hosted-push.d.ts +57 -0
  32. package/dist/client/hosted-push.d.ts.map +1 -0
  33. package/dist/client/hosted-push.js +115 -0
  34. package/dist/client/hosted-push.js.map +1 -0
  35. package/dist/client/hosted-transport.d.ts +158 -0
  36. package/dist/client/hosted-transport.d.ts.map +1 -0
  37. package/dist/client/hosted-transport.js +926 -0
  38. package/dist/client/hosted-transport.js.map +1 -0
  39. package/dist/client/hosted.d.ts +223 -0
  40. package/dist/client/hosted.d.ts.map +1 -0
  41. package/dist/client/hosted.js +541 -0
  42. package/dist/client/hosted.js.map +1 -0
  43. package/dist/client/index.d.ts +5 -3
  44. package/dist/client/index.d.ts.map +1 -1
  45. package/dist/client/index.js +2 -0
  46. package/dist/client/index.js.map +1 -1
  47. package/dist/client/messages.d.ts.map +1 -1
  48. package/dist/client/messages.js +4 -5
  49. package/dist/client/messages.js.map +1 -1
  50. package/dist/client/relay-subscription.d.ts.map +1 -1
  51. package/dist/client/relay-subscription.js +37 -7
  52. package/dist/client/relay-subscription.js.map +1 -1
  53. package/dist/client/{sealed-sender-ops.d.ts → sealed-sender.d.ts} +39 -22
  54. package/dist/client/sealed-sender.d.ts.map +1 -0
  55. package/dist/client/sealed-sender.js +296 -0
  56. package/dist/client/sealed-sender.js.map +1 -0
  57. package/dist/client/signal-service-cipher.d.ts +23 -26
  58. package/dist/client/signal-service-cipher.d.ts.map +1 -1
  59. package/dist/client/signal-service-cipher.js +468 -706
  60. package/dist/client/signal-service-cipher.js.map +1 -1
  61. package/dist/client/types.d.ts +18 -3
  62. package/dist/client/types.d.ts.map +1 -1
  63. package/dist/client/types.js +6 -0
  64. package/dist/client/types.js.map +1 -1
  65. package/dist/device/index.d.ts +2 -2
  66. package/dist/device/index.d.ts.map +1 -1
  67. package/dist/device/index.js +1 -1
  68. package/dist/device/index.js.map +1 -1
  69. package/dist/device/transfer.d.ts +7 -43
  70. package/dist/device/transfer.d.ts.map +1 -1
  71. package/dist/device/transfer.js +31 -150
  72. package/dist/device/transfer.js.map +1 -1
  73. package/dist/device/types.d.ts +6 -47
  74. package/dist/device/types.d.ts.map +1 -1
  75. package/dist/device/types.js +1 -1
  76. package/dist/device/types.js.map +1 -1
  77. package/dist/index.d.ts +6 -5
  78. package/dist/index.d.ts.map +1 -1
  79. package/dist/index.js +2 -1
  80. package/dist/index.js.map +1 -1
  81. package/dist/internal/encoding/proto/pq-ratchet-serialize.js +1 -1
  82. package/dist/internal/encoding/proto/pq-ratchet-serialize.js.map +1 -1
  83. package/dist/internal/encoding/proto/primitives.js +1 -1
  84. package/dist/internal/encoding/proto/primitives.js.map +1 -1
  85. package/dist/internal/groups/manager.d.ts +3 -3
  86. package/dist/internal/groups/manager.d.ts.map +1 -1
  87. package/dist/internal/groups/manager.js +5 -4
  88. package/dist/internal/groups/manager.js.map +1 -1
  89. package/dist/internal/protocol/sealed-sender/certificate.d.ts +10 -4
  90. package/dist/internal/protocol/sealed-sender/certificate.d.ts.map +1 -1
  91. package/dist/internal/protocol/sealed-sender/certificate.js +64 -7
  92. package/dist/internal/protocol/sealed-sender/certificate.js.map +1 -1
  93. package/dist/internal/protocol/sealed-sender/decryption.d.ts +34 -47
  94. package/dist/internal/protocol/sealed-sender/decryption.d.ts.map +1 -1
  95. package/dist/internal/protocol/sealed-sender/decryption.js +95 -182
  96. package/dist/internal/protocol/sealed-sender/decryption.js.map +1 -1
  97. package/dist/internal/protocol/sealed-sender/encryption.d.ts +49 -64
  98. package/dist/internal/protocol/sealed-sender/encryption.d.ts.map +1 -1
  99. package/dist/internal/protocol/sealed-sender/encryption.js +120 -123
  100. package/dist/internal/protocol/sealed-sender/encryption.js.map +1 -1
  101. package/dist/internal/protocol/sealed-sender/index.d.ts +5 -7
  102. package/dist/internal/protocol/sealed-sender/index.d.ts.map +1 -1
  103. package/dist/internal/protocol/sealed-sender/index.js +4 -12
  104. package/dist/internal/protocol/sealed-sender/index.js.map +1 -1
  105. package/dist/internal/protocol/sealed-sender/{v2-helpers.d.ts → multi-recipient-crypto.d.ts} +1 -1
  106. package/dist/internal/protocol/sealed-sender/multi-recipient-crypto.d.ts.map +1 -0
  107. package/dist/internal/protocol/sealed-sender/{v2-helpers.js → multi-recipient-crypto.js} +1 -1
  108. package/dist/internal/protocol/sealed-sender/multi-recipient-crypto.js.map +1 -0
  109. package/dist/internal/protocol/sealed-sender/{v2-binary.d.ts → multi-recipient-message.d.ts} +3 -2
  110. package/dist/internal/protocol/sealed-sender/multi-recipient-message.d.ts.map +1 -0
  111. package/dist/internal/protocol/sealed-sender/{v2-binary.js → multi-recipient-message.js} +16 -8
  112. package/dist/internal/protocol/sealed-sender/multi-recipient-message.js.map +1 -0
  113. package/dist/internal/protocol/sealed-sender/proto/index.d.ts +3 -0
  114. package/dist/internal/protocol/sealed-sender/proto/index.d.ts.map +1 -1
  115. package/dist/internal/protocol/sealed-sender/proto/index.js +25 -2
  116. package/dist/internal/protocol/sealed-sender/proto/index.js.map +1 -1
  117. package/dist/internal/protocol/sealed-sender/trust-root.d.ts +11 -1
  118. package/dist/internal/protocol/sealed-sender/trust-root.d.ts.map +1 -1
  119. package/dist/internal/protocol/sealed-sender/trust-root.js +13 -1
  120. package/dist/internal/protocol/sealed-sender/trust-root.js.map +1 -1
  121. package/dist/internal/protocol/sealed-sender/types.d.ts +20 -135
  122. package/dist/internal/protocol/sealed-sender/types.d.ts.map +1 -1
  123. package/dist/internal/protocol/sealed-sender/types.js +15 -6
  124. package/dist/internal/protocol/sealed-sender/types.js.map +1 -1
  125. package/dist/internal/protocol/sender-keys/manager.d.ts +22 -1
  126. package/dist/internal/protocol/sender-keys/manager.d.ts.map +1 -1
  127. package/dist/internal/protocol/sender-keys/manager.js +39 -4
  128. package/dist/internal/protocol/sender-keys/manager.js.map +1 -1
  129. package/dist/internal/protocol/spqr/braid-serialize.js +1 -1
  130. package/dist/internal/protocol/spqr/braid-serialize.js.map +1 -1
  131. package/dist/internal/protocol/spqr/ml-kem-braid/state-machine.js +4 -4
  132. package/dist/internal/protocol/spqr/ml-kem-braid/state-machine.js.map +1 -1
  133. package/dist/internal/protocol/zk/groups/index.d.ts +1 -1
  134. package/dist/internal/protocol/zk/groups/index.d.ts.map +1 -1
  135. package/dist/internal/protocol/zk/groups/index.js +1 -1
  136. package/dist/internal/protocol/zk/groups/index.js.map +1 -1
  137. package/dist/internal/protocol/zk/groups/profile-key-credential.d.ts +37 -12
  138. package/dist/internal/protocol/zk/groups/profile-key-credential.d.ts.map +1 -1
  139. package/dist/internal/protocol/zk/groups/profile-key-credential.js +104 -31
  140. package/dist/internal/protocol/zk/groups/profile-key-credential.js.map +1 -1
  141. package/dist/local/store/expo/adapter.d.ts +1 -0
  142. package/dist/local/store/expo/adapter.d.ts.map +1 -1
  143. package/dist/local/store/expo/adapter.js +3 -0
  144. package/dist/local/store/expo/adapter.js.map +1 -1
  145. package/dist/local/store/expo/key-storage.d.ts +1 -0
  146. package/dist/local/store/expo/key-storage.d.ts.map +1 -1
  147. package/dist/local/store/expo/key-storage.js +4 -0
  148. package/dist/local/store/expo/key-storage.js.map +1 -1
  149. package/dist/local/store/memory/adapter.d.ts +1 -0
  150. package/dist/local/store/memory/adapter.d.ts.map +1 -1
  151. package/dist/local/store/memory/adapter.js +4 -0
  152. package/dist/local/store/memory/adapter.js.map +1 -1
  153. package/dist/local/store/node/adapter.d.ts +1 -0
  154. package/dist/local/store/node/adapter.d.ts.map +1 -1
  155. package/dist/local/store/node/adapter.js +3 -0
  156. package/dist/local/store/node/adapter.js.map +1 -1
  157. package/dist/local/store/node/database.d.ts +1 -0
  158. package/dist/local/store/node/database.d.ts.map +1 -1
  159. package/dist/local/store/node/database.js +5 -0
  160. package/dist/local/store/node/database.js.map +1 -1
  161. package/dist/local/store/react-native/adapter.d.ts +1 -0
  162. package/dist/local/store/react-native/adapter.d.ts.map +1 -1
  163. package/dist/local/store/react-native/adapter.js +4 -0
  164. package/dist/local/store/react-native/adapter.js.map +1 -1
  165. package/dist/local/store/react-native/reference-backend.js +1 -1
  166. package/dist/local/store/react-native/reference-backend.js.map +1 -1
  167. package/dist/local/store/reliability.d.ts +63 -0
  168. package/dist/local/store/reliability.d.ts.map +1 -0
  169. package/dist/local/store/reliability.js +219 -0
  170. package/dist/local/store/reliability.js.map +1 -0
  171. package/dist/local/store/web/adapter.d.ts +1 -0
  172. package/dist/local/store/web/adapter.d.ts.map +1 -1
  173. package/dist/local/store/web/adapter.js +5 -0
  174. package/dist/local/store/web/adapter.js.map +1 -1
  175. package/dist/media/index.d.ts.map +1 -1
  176. package/dist/media/index.js +7 -4
  177. package/dist/media/index.js.map +1 -1
  178. package/dist/remote/object-store/convex-r2/server.d.ts +4 -2
  179. package/dist/remote/object-store/convex-r2/server.d.ts.map +1 -1
  180. package/dist/remote/object-store/convex-r2/server.js +8 -0
  181. package/dist/remote/object-store/convex-r2/server.js.map +1 -1
  182. package/dist/remote/object-store/convex-r2/storage.d.ts +5 -1
  183. package/dist/remote/object-store/convex-r2/storage.d.ts.map +1 -1
  184. package/dist/remote/object-store/convex-r2/storage.js +1 -0
  185. package/dist/remote/object-store/convex-r2/storage.js.map +1 -1
  186. package/dist/remote/object-store/types.d.ts +2 -0
  187. package/dist/remote/object-store/types.d.ts.map +1 -1
  188. package/dist/remote/object-store/validation.d.ts.map +1 -1
  189. package/dist/remote/object-store/validation.js +3 -0
  190. package/dist/remote/object-store/validation.js.map +1 -1
  191. package/dist/remote/relay/convex/backend.d.ts +7 -18
  192. package/dist/remote/relay/convex/backend.d.ts.map +1 -1
  193. package/dist/remote/relay/convex/backend.js +15 -24
  194. package/dist/remote/relay/convex/backend.js.map +1 -1
  195. package/dist/remote/relay/convex/component/_generated/component.d.ts +10 -18
  196. package/dist/remote/relay/convex/component/_generated/component.d.ts.map +1 -1
  197. package/dist/remote/relay/convex/component/certificates.d.ts.map +1 -1
  198. package/dist/remote/relay/convex/component/certificates.js +14 -5
  199. package/dist/remote/relay/convex/component/certificates.js.map +1 -1
  200. package/dist/remote/relay/convex/component/messages.d.ts +3 -17
  201. package/dist/remote/relay/convex/component/messages.d.ts.map +1 -1
  202. package/dist/remote/relay/convex/component/messages.js +28 -56
  203. package/dist/remote/relay/convex/component/messages.js.map +1 -1
  204. package/dist/remote/relay/convex/component/schema.d.ts +6 -6
  205. package/dist/remote/relay/convex/component/schema.js +2 -2
  206. package/dist/remote/relay/convex/component/schema.js.map +1 -1
  207. package/dist/remote/relay/convex/component/zkAuth.d.ts +7 -1
  208. package/dist/remote/relay/convex/component/zkAuth.d.ts.map +1 -1
  209. package/dist/remote/relay/convex/component/zkAuth.js +29 -14
  210. package/dist/remote/relay/convex/component/zkAuth.js.map +1 -1
  211. package/dist/remote/relay/convex/relay.d.ts +3 -6
  212. package/dist/remote/relay/convex/relay.d.ts.map +1 -1
  213. package/dist/remote/relay/convex/relay.js +23 -50
  214. package/dist/remote/relay/convex/relay.js.map +1 -1
  215. package/dist/remote/relay/index.d.ts +1 -1
  216. package/dist/remote/relay/index.d.ts.map +1 -1
  217. package/dist/remote/relay/index.js.map +1 -1
  218. package/dist/remote/relay/memory/adapter.d.ts +5 -11
  219. package/dist/remote/relay/memory/adapter.d.ts.map +1 -1
  220. package/dist/remote/relay/memory/adapter.js +41 -34
  221. package/dist/remote/relay/memory/adapter.js.map +1 -1
  222. package/dist/remote/relay/types.d.ts +19 -29
  223. package/dist/remote/relay/types.d.ts.map +1 -1
  224. package/dist/types/api.d.ts +2 -0
  225. package/dist/types/api.d.ts.map +1 -1
  226. package/dist/types/errors.d.ts +8 -1
  227. package/dist/types/errors.d.ts.map +1 -1
  228. package/dist/types/errors.js +9 -2
  229. package/dist/types/errors.js.map +1 -1
  230. package/dist/types/protocol-config.d.ts +1 -1
  231. package/dist/username/link.d.ts.map +1 -1
  232. package/dist/username/link.js +1 -1
  233. package/dist/username/link.js.map +1 -1
  234. package/docs/ASSURANCE.md +5 -5
  235. package/docs/DEVIATIONS.md +32 -61
  236. package/docs/GETTING_STARTED.md +7 -0
  237. package/docs/SECURITY.md +6 -0
  238. package/package.json +2 -2
  239. package/remote/relay/convex/component/README.md +28 -24
  240. package/scripts/oe-groups.mjs +5 -1
  241. package/dist/client/sealed-sender-ops.d.ts.map +0 -1
  242. package/dist/client/sealed-sender-ops.js +0 -203
  243. package/dist/client/sealed-sender-ops.js.map +0 -1
  244. package/dist/internal/protocol/sealed-sender/decryption-v2.d.ts +0 -46
  245. package/dist/internal/protocol/sealed-sender/decryption-v2.d.ts.map +0 -1
  246. package/dist/internal/protocol/sealed-sender/decryption-v2.js +0 -198
  247. package/dist/internal/protocol/sealed-sender/decryption-v2.js.map +0 -1
  248. package/dist/internal/protocol/sealed-sender/encryption-v2.d.ts +0 -56
  249. package/dist/internal/protocol/sealed-sender/encryption-v2.d.ts.map +0 -1
  250. package/dist/internal/protocol/sealed-sender/encryption-v2.js +0 -163
  251. package/dist/internal/protocol/sealed-sender/encryption-v2.js.map +0 -1
  252. package/dist/internal/protocol/sealed-sender/v2-binary.d.ts.map +0 -1
  253. package/dist/internal/protocol/sealed-sender/v2-binary.js.map +0 -1
  254. package/dist/internal/protocol/sealed-sender/v2-helpers.d.ts.map +0 -1
  255. package/dist/internal/protocol/sealed-sender/v2-helpers.js.map +0 -1
@@ -12,7 +12,7 @@
12
12
  *
13
13
  */
14
14
  import { REVOKED_CERTIFICATE_IDS } from './types.js';
15
- import { verify, sign, bytesToBase64, base64ToBytes } from '../../crypto/index.js';
15
+ import { verify, sign, bytesToBase64, base64ToBytes, constantTimeEqual } from '../../crypto/index.js';
16
16
  import { encodeServerCertificateData, decodeServerCertificateData, encodeServerCertificate, decodeServerCertificate, encodeSenderCertificateData, decodeSenderCertificateData, encodeSenderCertificate, decodeSenderCertificate, } from './proto/index.js';
17
17
  const GENERIC_ERROR = 'Sealed sender verification failed';
18
18
  // ============================================================================
@@ -26,17 +26,27 @@ const GENERIC_ERROR = 'Sealed sender verification failed';
26
26
  * 3. Return full ServerCertificate
27
27
  *
28
28
  */
29
- export async function createServerCertificate(id, publicKey, trustRootPrivateKey) {
29
+ export async function createServerCertificate(id, publicKey, trustRootPrivateKey, validity) {
30
+ if (!Number.isSafeInteger(validity.notBefore) ||
31
+ !Number.isSafeInteger(validity.notAfter) ||
32
+ validity.notBefore <= 0 ||
33
+ validity.notAfter < validity.notBefore) {
34
+ throw new Error(GENERIC_ERROR);
35
+ }
30
36
  // Encode inner Certificate protobuf
31
37
  const certificateBytesRaw = encodeServerCertificateData({
32
38
  id,
33
39
  key: base64ToBytes(publicKey),
40
+ notBefore: validity.notBefore,
41
+ notAfter: validity.notAfter,
34
42
  });
35
43
  // Sign with trust root
36
44
  const signatureBase64 = await sign(trustRootPrivateKey, certificateBytesRaw);
37
45
  return {
38
46
  id,
39
47
  publicKey,
48
+ notBefore: validity.notBefore,
49
+ notAfter: validity.notAfter,
40
50
  certificateBytes: bytesToBase64(certificateBytesRaw),
41
51
  signature: signatureBase64,
42
52
  };
@@ -51,6 +61,12 @@ export async function createServerCertificate(id, publicKey, trustRootPrivateKey
51
61
  *
52
62
  */
53
63
  export async function createSenderCertificate(fields, signer, signerPrivateKey) {
64
+ if (base64ToBytes(fields.relayScopeId).length !== 16 ||
65
+ !Number.isSafeInteger(fields.expires) ||
66
+ fields.expires <= 0 ||
67
+ fields.expires > signer.notAfter) {
68
+ throw new Error(GENERIC_ERROR);
69
+ }
54
70
  // Serialize the signer as outer ServerCertificate protobuf
55
71
  const signerBytes = encodeServerCertificate({
56
72
  certificate: base64ToBytes(signer.certificateBytes),
@@ -64,6 +80,7 @@ export async function createSenderCertificate(fields, signer, signerPrivateKey)
64
80
  expires: fields.expires,
65
81
  identityKey: base64ToBytes(fields.senderIdentityKey),
66
82
  signerCertificate: signerBytes,
83
+ relayScopeId: base64ToBytes(fields.relayScopeId),
67
84
  });
68
85
  // Sign with signer's private key
69
86
  const signatureBase64 = await sign(signerPrivateKey, certificateBytesRaw);
@@ -73,6 +90,7 @@ export async function createSenderCertificate(fields, signer, signerPrivateKey)
73
90
  senderIdentityKey: fields.senderIdentityKey,
74
91
  expires: fields.expires,
75
92
  senderE164: fields.senderE164,
93
+ relayScopeId: fields.relayScopeId,
76
94
  signer,
77
95
  certificateBytes: bytesToBase64(certificateBytesRaw),
78
96
  signature: signatureBase64,
@@ -120,7 +138,10 @@ export function deserializeSenderCertificate(bytes) {
120
138
  // Step 2: Decode inner Certificate protobuf → parsed fields + signer bytes
121
139
  const inner = decodeSenderCertificateData(outer.certificate);
122
140
  // Validate inner Certificate has required fields
123
- if (!inner.senderUuid || !inner.identityKey.length || !inner.signerCertificate.length) {
141
+ if (!inner.senderUuid ||
142
+ !inner.identityKey.length ||
143
+ !inner.signerCertificate.length ||
144
+ inner.relayScopeId.length !== 16) {
124
145
  throw new Error('missing required fields');
125
146
  }
126
147
  // Step 3: Decode signer bytes as outer ServerCertificate protobuf
@@ -132,13 +153,19 @@ export function deserializeSenderCertificate(bytes) {
132
153
  // Step 4: Decode server cert inner protobuf → {id, key}
133
154
  const signerInner = decodeServerCertificateData(signerOuter.certificate);
134
155
  // Validate server cert inner data
135
- if (!signerInner.key.length) {
156
+ if (!signerInner.key.length ||
157
+ !Number.isSafeInteger(signerInner.notBefore) ||
158
+ !Number.isSafeInteger(signerInner.notAfter) ||
159
+ signerInner.notBefore <= 0 ||
160
+ signerInner.notAfter < signerInner.notBefore) {
136
161
  throw new Error('missing required fields');
137
162
  }
138
163
  // Build the full ServerCertificate
139
164
  const signer = {
140
165
  id: signerInner.id,
141
166
  publicKey: bytesToBase64(signerInner.key),
167
+ notBefore: signerInner.notBefore,
168
+ notAfter: signerInner.notAfter,
142
169
  certificateBytes: bytesToBase64(signerOuter.certificate),
143
170
  signature: bytesToBase64(signerOuter.signature),
144
171
  };
@@ -148,6 +175,7 @@ export function deserializeSenderCertificate(bytes) {
148
175
  senderIdentityKey: bytesToBase64(inner.identityKey),
149
176
  expires: inner.expires,
150
177
  senderE164: inner.senderE164,
178
+ relayScopeId: bytesToBase64(inner.relayScopeId),
151
179
  signer,
152
180
  certificateBytes: bytesToBase64(outer.certificate),
153
181
  signature: bytesToBase64(outer.signature),
@@ -172,16 +200,36 @@ export function deserializeSenderCertificate(bytes) {
172
200
  * @param cert Certificate to validate
173
201
  * @param trustRoots Ed25519 trust root public keys
174
202
  * @param currentTime Optional timestamp for expiration check (defaults to Date.now())
203
+ * @param policy Required deployment scope and issuer revocation policy
175
204
  * @throws Error (generic) if validation fails
176
205
  */
177
- export async function validateSenderCertificate(cert, trustRoots, currentTime) {
206
+ export async function validateSenderCertificate(cert, trustRoots, currentTime, policy) {
178
207
  const now = currentTime ?? Date.now();
179
208
  // Must have at least one trust root
180
209
  if (trustRoots.length === 0) {
181
210
  throw new Error(GENERIC_ERROR);
182
211
  }
212
+ if (base64ToBytes(policy.expectedRelayScopeId).length !== 16) {
213
+ throw new Error(GENERIC_ERROR);
214
+ }
183
215
  // Step 1: Check revocation by server certificate ID (cheapest check)
184
- if (REVOKED_CERTIFICATE_IDS.includes(cert.signer.id)) {
216
+ if (REVOKED_CERTIFICATE_IDS.includes(cert.signer.id) ||
217
+ policy.revokedIssuerKeyIds?.includes(cert.signer.id)) {
218
+ throw new Error(GENERIC_ERROR);
219
+ }
220
+ if (base64ToBytes(cert.relayScopeId).length !== 16 ||
221
+ !constantTimeEqual(base64ToBytes(cert.relayScopeId), base64ToBytes(policy.expectedRelayScopeId))) {
222
+ throw new Error(GENERIC_ERROR);
223
+ }
224
+ if (!Number.isSafeInteger(cert.expires) ||
225
+ cert.expires <= 0 ||
226
+ cert.signer.notBefore > cert.signer.notAfter ||
227
+ !Number.isSafeInteger(cert.signer.notBefore) ||
228
+ !Number.isSafeInteger(cert.signer.notAfter) ||
229
+ cert.signer.notBefore <= 0 ||
230
+ now < cert.signer.notBefore ||
231
+ now > cert.signer.notAfter ||
232
+ cert.expires > cert.signer.notAfter) {
185
233
  throw new Error(GENERIC_ERROR);
186
234
  }
187
235
  // Check every trust root so the control flow does not reveal which root
@@ -224,13 +272,22 @@ export async function validateSenderCertificate(cert, trustRoots, currentTime) {
224
272
  *
225
273
  * @param cert Server certificate to validate
226
274
  * @param trustRoots Ed25519 trust root public keys
275
+ * @param currentTime Optional timestamp for issuer validity (defaults to Date.now())
227
276
  * @throws Error (generic) if validation fails
228
277
  */
229
- export async function validateServerCertificate(cert, trustRoots) {
278
+ export async function validateServerCertificate(cert, trustRoots, currentTime = Date.now()) {
230
279
  // Check revocation
231
280
  if (REVOKED_CERTIFICATE_IDS.includes(cert.id)) {
232
281
  throw new Error(GENERIC_ERROR);
233
282
  }
283
+ if (!Number.isSafeInteger(cert.notBefore) ||
284
+ !Number.isSafeInteger(cert.notAfter) ||
285
+ cert.notBefore <= 0 ||
286
+ cert.notAfter < cert.notBefore ||
287
+ currentTime < cert.notBefore ||
288
+ currentTime > cert.notAfter) {
289
+ throw new Error(GENERIC_ERROR);
290
+ }
234
291
  // Validate public key length
235
292
  const publicKeyBytes = base64ToBytes(cert.publicKey);
236
293
  if (publicKeyBytes.length !== 32) {
@@ -1 +1 @@
1
- {"version":3,"file":"certificate.js","sourceRoot":"","sources":["../../../../internal/protocol/sealed-sender/certificate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAGH,OAAO,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAC;AAGlD,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC1E,OAAO,EACL,2BAA2B,EAC3B,2BAA2B,EAC3B,uBAAuB,EACvB,uBAAuB,EACvB,2BAA2B,EAC3B,2BAA2B,EAC3B,uBAAuB,EACvB,uBAAuB,GACxB,MAAM,SAAS,CAAC;AAIjB,MAAM,aAAa,GAAG,mCAAmC,CAAC;AAE1D,+EAA+E;AAC/E,uBAAuB;AACvB,+EAA+E;AAE/E;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,EAAU,EACV,SAAiB,EACjB,mBAA+B;IAE/B,oCAAoC;IACpC,MAAM,mBAAmB,GAAG,2BAA2B,CAAC;QACtD,EAAE;QACF,GAAG,EAAE,aAAa,CAAC,SAAS,CAAC;KAC9B,CAAC,CAAC;IAEH,uBAAuB;IACvB,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,mBAAmB,EAAE,mBAAmB,CAAC,CAAC;IAE7E,OAAO;QACL,EAAE;QACF,SAAS;QACT,gBAAgB,EAAE,aAAa,CAAC,mBAAmB,CAAC;QACpD,SAAS,EAAE,e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a,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACrD,IAAI,cAAc,CAAC,MAAM,KAAK,EAAE,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC;IACjC,CAAC;IAED,sCAAsC;IACtC,IAAI,QAAQ,GAAG,KAAK,CAAC;IACrB,KAAK,MAAM,IAAI,IAAI,UAAU,EAAE,CAAC;QAC9B,IAAI,CAAC;YACH,MAAM,EAAE,GAAG,MAAM,MAAM,CACrB,IAAiB,EACjB,aAAa,CAAC,IAAI,CAAC,gBAAgB,CAAC,EACpC,IAAI,CAAC,SAAsB,CAC5B,CAAC;YACF,QAAQ,GAAG,QAAQ,IAAI,EAAE,CAAC;QAC5B,CAAC;QAAC,MAAM,CAAC;YACP,gCAAgC;QAClC,CAAC;IACH,CAAC;IACD,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,IAAI,KAAK,CAAC,aAAa,CAAC,CAAC;IACjC,CAAC;AACH,CAAC"}
1
+ {"version":3,"file":"certificate.js","sourceRoot":"","sources":["../../../../internal/protocol/sealed-sender/certificate.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAOH,OAAO,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAC;AAGlD,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,aAAa,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAC7F,OAAO,EACL,2BAA2B,EAC3B,2BAA2B,EAC3B,uBAAuB,EACvB,uBAAuB,EACvB,2BAA2B,EAC3B,2BAA2B,EAC3B,uBAAuB,EACvB,uBAAuB,GACxB,MAAM,SAAS,CAAC;AAIjB,MAAM,aAAa,GAAG,mCAAmC,CAAC;AAE1D,+EAA+E;AAC/E,uBAAuB;AACvB,+EAA+E;AAE/E;;;;;;;GAOG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,EAAU,EACV,SAAiB,EACjB,mBAA+B,EAC/B,QAAi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ption (Unseal) for Sealed Sender
2
+ * Sealed Sender V2 Multi-Recipient Decryption
3
+ *
4
+ * Implements V2 multi-recipient sealed-sender decryption.
5
+ *
6
+ * Algorithm:
7
+ * 1. Parse version, find my recipient entry by serviceId
8
+ * 2. M = apply_agreement_xor(recipient_identity, ephemeral_pub, encrypted_key)
9
+ * 3. Verify E_pub matches X25519(derive_private_from_M(M)) - authenticates XOR decryption
10
+ * 4. K = HKDF(M, LABEL_K)
11
+ * 5. usmc = AES-GCM-SIV.Decrypt(K, nonce=zeros, ciphertext) - authenticated decryption
12
+ * 6. Parse certificate from usmc to get sender identity
13
+ * 7. Verify auth_tag using identity-to-identity ECDH (binds sender to message)
14
+ * 8. Validate sender certificate, return content
15
+ *
16
+ * IMPORTANT: Auth tag is verified AFTER decryption because it requires sender identity
17
+ * from the certificate. This is safe because:
18
+ * - AES-GCM-SIV provides authenticated encryption
19
+ * - Ephemeral key consistency check authenticates the XOR decryption
20
+ * - Auth tag binds the sender's identity to the message
3
21
  *
4
- * Implements two decryption stages:
5
- * - Stage 1 (Ephemeral): Decrypts sender's identity public key
6
- * - Stage 2 (Sender): Decrypts certificate + message
7
- *
8
- * Security:
9
- * - MAC is verified BEFORE decryption at each stage (critical for security)
10
- * - All errors use generic messages to prevent fingerprinting
11
- * - Constant-time MAC comparison prevents timing attacks
12
- * - Sender identity is verified against certificate
13
- *
14
- * @see https://signal.org/blog/sealed-sender/
15
22
  */
16
- import type { UnsealOptions, UnidentifiedSenderMessageContent } from './types.js';
23
+ import type { UnsealOptions, SealedSenderMessageContent } from './types.js';
17
24
  /** Generic error message for all unseal failures */
18
25
  export {};
19
26
  /**
20
- * Unseal a Sealed Sender message using two-stage decryption.
21
- *
22
- * Reveals the sender identity only after authenticating the encrypted
23
- * certificate.
24
- *
25
- * Two-Stage Algorithm:
26
- *
27
- * Stage 1 - Ephemeral Layer (verify and decrypt sender identity):
28
- * 1. Extract ephemeralPublic, encryptedStatic, encryptedMessage
29
- * 2. Parse encryptedStatic: e_ciphertext (32 bytes) || e_mac (10 bytes)
30
- * 3. Compute shared secret: e_ss = X25519(recipientIdentityPrivate, ephemeralPublic)
31
- * 4. Build salt: e_salt = "UnidentifiedDelivery" || recipientIdentityPublic || ephemeralPublic
32
- * 5. Derive keys: e_material = HKDF(salt=e_salt, ikm=e_ss, length=96)
33
- * 6. **VERIFY MAC FIRST**: Compare e_mac with HMAC(e_macKey, e_ciphertext)
34
- * 7. Decrypt: senderIdentityPublic = AES-CTR(e_cipherKey, iv=0, e_ciphertext)
35
- *
36
- * Stage 2 - Sender Layer (verify and decrypt message):
37
- * 1. Parse encryptedMessage: s_ciphertext || s_mac (10 bytes)
38
- * 2. Compute shared secret: s_ss = X25519(recipientIdentityPrivate, senderIdentityPublic)
39
- * 3. Build salt: s_salt = e_chain || e_ciphertext || e_mac
40
- * 4. Derive keys: s_material = HKDF(salt=s_salt, ikm=s_ss, length=96)
41
- * 5. **VERIFY MAC FIRST**: Compare s_mac with HMAC(s_macKey, s_ciphertext)
42
- * 6. Decrypt: envelope = AES-CTR(s_cipherKey, iv=0, s_ciphertext)
43
- * 7. Parse envelope to extract certificate and message
44
- * 8. Validate certificate matches decrypted sender identity
45
- * 9. Validate certificate (expiration, signature, revocation)
46
- * 10. Self-send detection
47
- *
48
- * Security:
49
- * - MAC is verified BEFORE decryption at each stage
50
- * - Sender identity from Stage 1 is verified against certificate
27
+ * Unseal a V2 multi-recipient Sealed Sender message.
51
28
  *
52
- * @param options Unsealing options
29
+ * @param options V2 unsealing options
53
30
  * @returns Decrypted content with validated sender certificate
54
31
  * @throws Error (generic) if any validation fails
55
32
  *
56
- * @see https://signal.org/blog/sealed-sender/
33
+ * @example
34
+ * ```typescript
35
+ * const content = await unseal({
36
+ * sealedMessage: receivedMessage,
37
+ * recipientIdentityPrivate: myPrivateKey,
38
+ * recipientIdentityPublic: myPublicKey,
39
+ * recipientServiceId: 'my-user-id',
40
+ * recipientDeviceId: 1,
41
+ * trustRoots: [trustRootPublicKey],
42
+ * });
43
+ * ```
57
44
  */
58
- export declare function unseal(options: UnsealOptions): Promise<UnidentifiedSenderMessageContent>;
45
+ export declare function unseal(options: UnsealOptions): Promise<SealedSenderMessageContent>;
59
46
  //# sourceMappingURL=decryption.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"decryption.d.ts","sourceRoot":"","sources":["../../../../internal/protocol/sealed-sender/decryption.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,gCAAgC,EAAqB,MAAM,SAAS,CAAC;AAyBlG,oDAAoD;AACpD,OAAO,EAAE,CAAC;AAwGV;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,wBAAsB,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,gCAAgC,CAAC,CA4K9F"}
1
+ {"version":3,"file":"decryption.d.ts","sourceRoot":"","sources":["../../../../internal/protocol/sealed-sender/decryption.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,0BAA0B,EAAqB,MAAM,SAAS,CAAC;AA2B5F,oDAAoD;AACpD,OAAO,EAAE,CAAC;AA6EV;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAsB,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAwIxF"}
@@ -1,53 +1,36 @@
1
1
  /**
2
- * Envelope Decryption (Unseal) for Sealed Sender
2
+ * Sealed Sender V2 Multi-Recipient Decryption
3
3
  *
4
- * Implements two decryption stages:
5
- * - Stage 1 (Ephemeral): Decrypts sender's identity public key
6
- * - Stage 2 (Sender): Decrypts certificate + message
4
+ * Implements V2 multi-recipient sealed-sender decryption.
7
5
  *
8
- * Security:
9
- * - MAC is verified BEFORE decryption at each stage (critical for security)
10
- * - All errors use generic messages to prevent fingerprinting
11
- * - Constant-time MAC comparison prevents timing attacks
12
- * - Sender identity is verified against certificate
6
+ * Algorithm:
7
+ * 1. Parse version, find my recipient entry by serviceId
8
+ * 2. M = apply_agreement_xor(recipient_identity, ephemeral_pub, encrypted_key)
9
+ * 3. Verify E_pub matches X25519(derive_private_from_M(M)) - authenticates XOR decryption
10
+ * 4. K = HKDF(M, LABEL_K)
11
+ * 5. usmc = AES-GCM-SIV.Decrypt(K, nonce=zeros, ciphertext) - authenticated decryption
12
+ * 6. Parse certificate from usmc to get sender identity
13
+ * 7. Verify auth_tag using identity-to-identity ECDH (binds sender to message)
14
+ * 8. Validate sender certificate, return content
15
+ *
16
+ * IMPORTANT: Auth tag is verified AFTER decryption because it requires sender identity
17
+ * from the certificate. This is safe because:
18
+ * - AES-GCM-SIV provides authenticated encryption
19
+ * - Ephemeral key consistency check authenticates the XOR decryption
20
+ * - Auth tag binds the sender's identity to the message
13
21
  *
14
- * @see https://signal.org/blog/sealed-sender/
15
22
  */
16
- import { SEALED_SENDER_SALT, MAC_BYTES, isSealedSenderContentType, } from './types.js';
23
+ import { SEALED_SENDER_V2_UUID_VERSION, SEALED_SENDER_V2_SERVICE_ID_VERSION, isSealedSenderContentType, } from './types.js';
17
24
  import { deserializeSenderCertificate, validateSenderCertificate } from './certificate.js';
18
- import { computeSharedSecret, hkdf, aesCtrDecrypt, hmac, bytesToBase64, base64ToBytes, stringToBytes, concatBytes, constantTimeEqual, secureZeroBytes, } from '../../crypto/index.js';
19
- import { decodeVarint as decodeVarintObj } from '../../encoding/proto/primitives.js';
25
+ import { deriveEphemeralPrivateFromM, deriveCipherKeyFromM, applyAgreementXor, computeAuthenticationTag, verifyAuthenticationTag, } from './multi-recipient-crypto.js';
26
+ import { bytesToBase64, base64ToBytes, constantTimeEqual, secureZeroBytes, aesGcmSivDecryptZeroNonce, } from '../../crypto/index.js';
27
+ import { decodeVarint } from '../../encoding/proto/primitives.js';
20
28
  import { x25519 } from '@noble/curves/ed25519.js';
21
29
  const GENERIC_ERROR = 'Sealed sender verification failed';
22
30
  /** contentType(1) + certLen varint(1) + msgLen varint(1) + contentHint(1). */
23
31
  const MIN_ENVELOPE_BYTES = 4;
24
32
  /**
25
- * Decode a varint from bytes, returning a tuple for sealed sender call sites.
26
- *
27
- * Wraps the shared decodeVarint primitive with:
28
- * - Tuple return format [value, bytesRead] (matching sealed sender convention)
29
- * - Generic error message (no information leakage)
30
- *
31
- * @param bytes Bytes to decode from
32
- * @param offset Starting offset
33
- * @returns [value, bytesRead]
34
- */
35
- function decodeVarint(bytes, offset) {
36
- try {
37
- const { value, bytesRead } = decodeVarintObj(bytes, offset);
38
- return [value, bytesRead];
39
- }
40
- catch {
41
- throw new Error(GENERIC_ERROR);
42
- }
43
- }
44
- /**
45
- * Parse the decrypted envelope to extract certificate and message.
46
- *
47
- * Format: contentType(1) || varint(certLen) || certBytes || varint(msgLen) || msgBytes || contentHint
48
- *
49
- * @param envelope Decrypted envelope bytes
50
- * @returns Parsed envelope components
33
+ * Parse decrypted envelope to extract certificate and message.
51
34
  */
52
35
  function parseEnvelope(envelope) {
53
36
  // Smallest well-formed envelope: type, two zero-length varints, hint.
@@ -61,12 +44,11 @@ function parseEnvelope(envelope) {
61
44
  }
62
45
  let offset = 1;
63
46
  // Read certificate length
64
- const [certLen, certLenBytes] = decodeVarint(envelope, offset);
47
+ const { value: certLen, bytesRead: certLenBytes } = decodeVarint(envelope, offset);
65
48
  if (certLenBytes === 0) {
66
49
  throw new Error(GENERIC_ERROR);
67
50
  }
68
51
  offset += certLenBytes;
69
- // Validate we have enough bytes for certificate
70
52
  if (offset + certLen > envelope.length) {
71
53
  throw new Error(GENERIC_ERROR);
72
54
  }
@@ -74,12 +56,11 @@ function parseEnvelope(envelope) {
74
56
  const certBytes = envelope.slice(offset, offset + certLen);
75
57
  offset += certLen;
76
58
  // Read message length
77
- const [msgLen, msgLenBytes] = decodeVarint(envelope, offset);
59
+ const { value: msgLen, bytesRead: msgLenBytes } = decodeVarint(envelope, offset);
78
60
  if (msgLenBytes === 0) {
79
61
  throw new Error(GENERIC_ERROR);
80
62
  }
81
63
  offset += msgLenBytes;
82
- // Validate we have enough bytes for message
83
64
  if (offset + msgLen > envelope.length) {
84
65
  throw new Error(GENERIC_ERROR);
85
66
  }
@@ -103,171 +84,103 @@ function parseEnvelope(envelope) {
103
84
  return { certificate, message, contentType, contentHint };
104
85
  }
105
86
  /**
106
- * Unseal a Sealed Sender message using two-stage decryption.
107
- *
108
- * Reveals the sender identity only after authenticating the encrypted
109
- * certificate.
110
- *
111
- * Two-Stage Algorithm:
87
+ * Unseal a V2 multi-recipient Sealed Sender message.
112
88
  *
113
- * Stage 1 - Ephemeral Layer (verify and decrypt sender identity):
114
- * 1. Extract ephemeralPublic, encryptedStatic, encryptedMessage
115
- * 2. Parse encryptedStatic: e_ciphertext (32 bytes) || e_mac (10 bytes)
116
- * 3. Compute shared secret: e_ss = X25519(recipientIdentityPrivate, ephemeralPublic)
117
- * 4. Build salt: e_salt = "UnidentifiedDelivery" || recipientIdentityPublic || ephemeralPublic
118
- * 5. Derive keys: e_material = HKDF(salt=e_salt, ikm=e_ss, length=96)
119
- * 6. **VERIFY MAC FIRST**: Compare e_mac with HMAC(e_macKey, e_ciphertext)
120
- * 7. Decrypt: senderIdentityPublic = AES-CTR(e_cipherKey, iv=0, e_ciphertext)
121
- *
122
- * Stage 2 - Sender Layer (verify and decrypt message):
123
- * 1. Parse encryptedMessage: s_ciphertext || s_mac (10 bytes)
124
- * 2. Compute shared secret: s_ss = X25519(recipientIdentityPrivate, senderIdentityPublic)
125
- * 3. Build salt: s_salt = e_chain || e_ciphertext || e_mac
126
- * 4. Derive keys: s_material = HKDF(salt=s_salt, ikm=s_ss, length=96)
127
- * 5. **VERIFY MAC FIRST**: Compare s_mac with HMAC(s_macKey, s_ciphertext)
128
- * 6. Decrypt: envelope = AES-CTR(s_cipherKey, iv=0, s_ciphertext)
129
- * 7. Parse envelope to extract certificate and message
130
- * 8. Validate certificate matches decrypted sender identity
131
- * 9. Validate certificate (expiration, signature, revocation)
132
- * 10. Self-send detection
133
- *
134
- * Security:
135
- * - MAC is verified BEFORE decryption at each stage
136
- * - Sender identity from Stage 1 is verified against certificate
137
- *
138
- * @param options Unsealing options
89
+ * @param options V2 unsealing options
139
90
  * @returns Decrypted content with validated sender certificate
140
91
  * @throws Error (generic) if any validation fails
141
92
  *
142
- * @see https://signal.org/blog/sealed-sender/
93
+ * @example
94
+ * ```typescript
95
+ * const content = await unseal({
96
+ * sealedMessage: receivedMessage,
97
+ * recipientIdentityPrivate: myPrivateKey,
98
+ * recipientIdentityPublic: myPublicKey,
99
+ * recipientServiceId: 'my-user-id',
100
+ * recipientDeviceId: 1,
101
+ * trustRoots: [trustRootPublicKey],
102
+ * });
103
+ * ```
143
104
  */
144
105
  export async function unseal(options) {
145
- const { sealedMessage, recipientIdentityPrivate, trustRoots, currentTime } = options;
146
- // Arrays to track for secure zeroing
106
+ const { sealedMessage, recipientIdentityPrivate, recipientIdentityPublic, recipientServiceId, recipientDeviceId, trustRoots, currentTime, certificatePolicy, } = options;
107
+ // Track sensitive material for secure zeroing
147
108
  const toZero = [];
148
- // Step 1: Verify version
149
- // Signal Protocol version format: (requiredVersion << 4) | currentVersion
150
- const requiredVersion = (sealedMessage.version >> 4) & 0x0f;
151
- const currentVersion = sealedMessage.version & 0x0f;
152
- // We support messages that require version 1 or lower
153
- if (requiredVersion > 1) {
109
+ // ========================================================================
110
+ // Step 1: Validate version and find my recipient entry
111
+ // ========================================================================
112
+ const version = sealedMessage.version;
113
+ if (version !== SEALED_SENDER_V2_UUID_VERSION &&
114
+ version !== SEALED_SENDER_V2_SERVICE_ID_VERSION) {
154
115
  throw new Error(GENERIC_ERROR);
155
116
  }
156
- // Current version must also be valid (1 for now)
157
- if (currentVersion < 1) {
117
+ // Find my recipient entry
118
+ const myRecipient = sealedMessage.recipients.find((r) => r.serviceId === recipientServiceId && r.deviceId === recipientDeviceId);
119
+ if (!myRecipient) {
158
120
  throw new Error(GENERIC_ERROR);
159
121
  }
160
122
  try {
161
- // ========================================================================
162
- // Stage 1: Ephemeral Layer
163
- // Decrypt sender's identity public key
164
- // ========================================================================
165
- // Step 1.1: Extract components
166
- const ephemeralPublicBytes = base64ToBytes(sealedMessage.ephemeralPublic);
167
- const encryptedStatic = base64ToBytes(sealedMessage.encryptedStatic);
168
- const encryptedMessage = base64ToBytes(sealedMessage.encryptedMessage);
169
- // Validate ephemeral key size
170
- if (ephemeralPublicBytes.length !== 32) {
171
- throw new Error(GENERIC_ERROR);
172
- }
173
- // Step 1.2: Parse encryptedStatic: e_ciphertext || e_mac
174
- // e_ciphertext = 32 bytes (encrypted sender identity public key)
175
- // e_mac = MAC_BYTES
176
- const expectedStaticLen = 32 + MAC_BYTES;
177
- if (encryptedStatic.length !== expectedStaticLen) {
178
- throw new Error(GENERIC_ERROR);
179
- }
180
- const e_ciphertext = encryptedStatic.slice(0, 32);
181
- const e_mac = encryptedStatic.slice(32, 32 + MAC_BYTES);
182
- // Step 1.3: Compute shared secret: e_ss = ECDH(recipientIdentityPrivate, ephemeralPublic)
183
- const recipientPrivateBase64 = bytesToBase64(recipientIdentityPrivate);
184
- const ephemeralPublicBase64 = bytesToBase64(ephemeralPublicBytes);
185
- const e_sharedSecret = await computeSharedSecret(recipientPrivateBase64, ephemeralPublicBase64);
186
- toZero.push(e_sharedSecret);
187
- // Step 1.4: build the Stage 1 salt.
188
- // We need the recipient's public key, so derive it from the private key.
189
- // Note: for X25519, we cannot easily derive public from private without the
190
- // curve library.
191
- // The recipient should provide their public key. For now, we will need to
192
- // add it to options.
193
- // WORKAROUND: use the certificate's expected sender identity to validate
194
- // later.
195
- const saltPrefix = stringToBytes(SEALED_SENDER_SALT);
196
- // Derive recipient's public key from private key for salt computation.
197
- // The reference implementation uses the recipient's stored public key. We derive it from the private key.
198
- const recipientIdentityPublic = x25519.getPublicKey(recipientIdentityPrivate);
199
- const e_salt = concatBytes(saltPrefix, recipientIdentityPublic, ephemeralPublicBytes);
200
- // Step 1.5: Derive ephemeral keys (96 bytes: cipher + mac + chain)
201
- const e_material = await hkdf(e_sharedSecret, e_salt, new Uint8Array(0), 96);
202
- toZero.push(e_material);
203
- const e_cipherKey = e_material.slice(0, 32);
204
- const e_macKey = e_material.slice(32, 64);
205
- const e_chain = e_material.slice(64, 96);
206
- toZero.push(e_cipherKey, e_macKey, e_chain);
207
- // Step 1.6: **VERIFY MAC FIRST** (critical for security)
208
- const expectedE_mac_full = await hmac(e_macKey, e_ciphertext);
209
- const expectedE_mac = expectedE_mac_full.slice(0, MAC_BYTES);
210
- if (!constantTimeEqual(e_mac, expectedE_mac)) {
123
+ const ephemeralPublic = base64ToBytes(sealedMessage.ephemeralPublic);
124
+ const encryptedMessageKey = base64ToBytes(myRecipient.encryptedMessageKey);
125
+ const receivedAuthTag = base64ToBytes(myRecipient.authenticationTag);
126
+ const messageCiphertext = base64ToBytes(sealedMessage.messageCiphertext);
127
+ if (ephemeralPublic.length !== 32) {
211
128
  throw new Error(GENERIC_ERROR);
212
129
  }
213
- // Step 1.7: Decrypt sender's identity public key
214
- const e_iv = new Uint8Array(16); // All zeros
215
- const senderIdentityPublic = aesCtrDecrypt(e_cipherKey, e_iv, e_ciphertext);
216
- // Validate sender identity key size
217
- if (senderIdentityPublic.length !== 32) {
130
+ // ========================================================================
131
+ // Step 2: Decrypt M using XOR (ephemeral-to-identity ECDH)
132
+ // ========================================================================
133
+ const M = await applyAgreementXor(recipientIdentityPrivate, ephemeralPublic, ephemeralPublic, // For receiving: both ECDH target and IKM are the ephemeral key
134
+ 'receiving', encryptedMessageKey);
135
+ toZero.push(M);
136
+ // ========================================================================
137
+ // Step 3: Verify ephemeral public key consistency
138
+ // This authenticates the XOR decryption - if M was wrong, derived key will not match
139
+ // ========================================================================
140
+ const derivedEphemeralPrivate = await deriveEphemeralPrivateFromM(M);
141
+ toZero.push(derivedEphemeralPrivate);
142
+ const derivedEphemeralPublic = x25519.getPublicKey(derivedEphemeralPrivate);
143
+ if (!constantTimeEqual(ephemeralPublic, derivedEphemeralPublic)) {
218
144
  throw new Error(GENERIC_ERROR);
219
145
  }
220
146
  // ========================================================================
221
- // Stage 2: Sender Layer
222
- // Decrypt certificate + message
147
+ // Step 4: Derive cipher key K from M
223
148
  // ========================================================================
224
- // Step 2.1: Parse encryptedMessage: s_ciphertext || s_mac
225
- if (encryptedMessage.length < MAC_BYTES) {
226
- throw new Error(GENERIC_ERROR);
149
+ const cipherKey = await deriveCipherKeyFromM(M);
150
+ toZero.push(cipherKey);
151
+ // ========================================================================
152
+ // Step 5: Decrypt message with AES-GCM-SIV (provides authenticated decryption)
153
+ // ========================================================================
154
+ let envelope;
155
+ try {
156
+ envelope = aesGcmSivDecryptZeroNonce(cipherKey, messageCiphertext);
227
157
  }
228
- const s_ciphertext = encryptedMessage.slice(0, -MAC_BYTES);
229
- const s_mac = encryptedMessage.slice(-MAC_BYTES);
230
- // Step 2.2: Compute shared secret: s_ss = ECDH(recipientIdentityPrivate, senderIdentityPublic)
231
- const senderIdentityPublicBase64 = bytesToBase64(senderIdentityPublic);
232
- const s_sharedSecret = await computeSharedSecret(recipientPrivateBase64, senderIdentityPublicBase64);
233
- toZero.push(s_sharedSecret);
234
- // Step 2.3: Build salt for Stage 2 (chained from Stage 1)
235
- // s_salt = e_chain || e_ciphertext || e_mac
236
- const s_salt = concatBytes(e_chain, e_ciphertext, e_mac);
237
- // Step 2.4: Derive sender keys (96 bytes: discard + cipher + mac)
238
- // The first 32 bytes of the 96-byte derivation are domain-separated discard
239
- // material. Cipher and MAC keys occupy the remaining bytes.
240
- const s_material = await hkdf(s_sharedSecret, s_salt, new Uint8Array(0), 96);
241
- toZero.push(s_material);
242
- // s_material[0:32] discarded (mirrors EphemeralKeys structure)
243
- const s_cipherKey = s_material.slice(32, 64);
244
- const s_macKey = s_material.slice(64, 96);
245
- toZero.push(s_cipherKey, s_macKey);
246
- // Step 2.5: **VERIFY MAC FIRST** (critical for security)
247
- const expectedS_mac_full = await hmac(s_macKey, s_ciphertext);
248
- const expectedS_mac = expectedS_mac_full.slice(0, MAC_BYTES);
249
- if (!constantTimeEqual(s_mac, expectedS_mac)) {
158
+ catch {
250
159
  throw new Error(GENERIC_ERROR);
251
160
  }
252
- // Step 2.6: Decrypt envelope
253
- const s_iv = new Uint8Array(16); // All zeros
254
- const envelope = aesCtrDecrypt(s_cipherKey, s_iv, s_ciphertext);
255
- // Step 2.7: Parse envelope
161
+ // ========================================================================
162
+ // Step 6: Parse envelope to get sender certificate
163
+ // ========================================================================
256
164
  const { certificate, message, contentType, contentHint } = parseEnvelope(envelope);
257
- // Step 2.8: Validate sender identity matches certificate
258
- const certIdentityKey = base64ToBytes(certificate.senderIdentityKey);
259
- if (!constantTimeEqual(senderIdentityPublic, certIdentityKey)) {
165
+ // ========================================================================
166
+ // Step 7: Verify authentication tag (identity-to-identity ECDH).
167
+ // Now that we have the sender's identity from the certificate, verify the
168
+ // auth tag. This binds the sender's identity to the message.
169
+ // ========================================================================
170
+ const senderIdentityPublic = base64ToBytes(certificate.senderIdentityKey);
171
+ const expectedAuthTag = await computeAuthenticationTag(recipientIdentityPrivate, recipientIdentityPublic, senderIdentityPublic, ephemeralPublic, encryptedMessageKey, 'receiving');
172
+ if (!verifyAuthenticationTag(expectedAuthTag, receivedAuthTag)) {
260
173
  throw new Error(GENERIC_ERROR);
261
174
  }
262
- // Step 2.9: Validate certificate
263
- await validateSenderCertificate(certificate, trustRoots, currentTime);
264
- // Step 2.10: Self-send detection (SPEC Section 6.6)
265
- // Reject messages where sender === recipient (replay attack prevention)
266
- if (certificate.senderUuid === options.recipientUuid &&
267
- certificate.senderDeviceId === options.recipientDeviceId) {
175
+ // ========================================================================
176
+ // Step 8: Validate sender certificate
177
+ // ========================================================================
178
+ await validateSenderCertificate(certificate, trustRoots, currentTime, certificatePolicy);
179
+ // Self-send detection
180
+ if (certificate.senderUuid === recipientServiceId &&
181
+ certificate.senderDeviceId === recipientDeviceId) {
268
182
  throw new Error(GENERIC_ERROR);
269
183
  }
270
- // Step 2.11: Return validated content
271
184
  return {
272
185
  senderCertificate: certificate,
273
186
  signalProtocolMessage: bytesToBase64(message),