Signal协议是一种基于双棘轮技术的前向保密协议。可应用于在同步和异步这两种消息传递环境。
接口库开源地址:https://github.com/signalapp/libsignal-protocol-java
预共享密钥
Signal协议中应用了预共享密钥(PreKey)概念。预共享密钥包括一个公钥和一个对应的惟一ID。
准备数据时,客户端生成一个已签名的预共享密钥,以及一组未签名的预共享密钥的列表,并将它们全部传输到服务器上存储,以供查询。预共享密钥也可以签名。
会话
Signal协议是面向会话的。客户端建立一个“会话”,用于后续的加密/解密操作。一旦建立了一个会话,通信过程中就不必再拆掉它。
会话可以通过以下三种方式建立:
基于预共享密钥包(PreKeyBundles):预向某收件人发送消息的客户端,可以通过从服务器上检索该收件人的预共享密钥包来建立会话。
预共享密钥消息(PreKeySignalMessages):客户端可以从收件人接收预共享密钥消息并使用它建立会话。
密钥交换消息(KeyExchangeMessages):两个客户端可以交换密钥交换消息以建立会话。
状态
已建立的会话封装了两个客户端之间的许多状态。这些状态与会话同生命周期,保存在记录中:
身份信息状态:客户端需要维护自己的身份密钥对以及从其他客户端接收的身份密钥的状态;
预共享密钥状态:客户端需要维护生成的预共享密钥的状态;
已签名预共享密钥状态:客户端需要维护其已签名预密钥的状态;
会话状态:客户端需要维护其已建立会话的状态。
接口库使用
准备数据
准备数据时,libsignal客户端需要生成其身份认证密钥、注册id和预共享密钥。以下代码是以bob端为例:
// 构建bob的数据内容
int bobId = 0x10203040;
// bob 的地址
SignalProtocolAddress bobAddress = new SignalProtocolAddress("BobName", bobId);
// IKP-B,身份认证密钥对
IdentityKeyPair bobIdentityKeyPair = KeyHelper.generateIdentityKeyPair();
// 注册编号,
int bobRegistrationId = KeyHelper.generateRegistrationId(false);
// 预共享密钥
List
// 已签名预共享密钥SPK-B ID
int bobSignedPreKeyId = 1;
// 已签名预共享密钥
SignedPreKeyRecord bobSignedPreKey = KeyHelper.generateSignedPreKey(bobIdentityKeyPair, bobSignedPreKeyId);
// 获取到的,bob的某一个预共享的密钥包
// 一次性预共享密钥OPK-B ID
int bobPreKeyId = 1;
// 一次性预共享密钥OPK-B公钥
ECPublicKey bobPreKeyPublic = bobPreKeys.get(bobPreKeyId).getKeyPair().getPublicKey();
// 已签名预共享密钥SPK-B公钥
ECPublicKey bobSignedPreKeyPublic = bobSignedPreKey.getKeyPair().getPublicKey();
// 预共享密钥签名Sig(IK-B,Encode(SPK-B))
byte[] bobSignedPreKeySignature = Curve.calculateSignature(bobIdentityKeyPair.getPrivateKey(),
bobSignedPreKeyPublic.serialize());
// 身份认证公钥IK-B
IdentityKey bobIdentityKey = bobIdentityKeyPair.getPublicKey();
// 组建预共享密钥包
PreKeyBundle bobProKeyBundle = new PreKeyBundle(bobRegistrationId, bobRegistrationId, bobPreKeyId,bobPreKeyPublic, bobSignedPreKeyId, bobSignedPreKeyPublic, bobSignedPreKeySignature, bobIdentityKey);
//发送预共享密钥包到服务器...
建立会话
应用接口库的客户端需要实现四个接口:IdentityKeyStore、PreKeyStore、SignedPreKeyStore和SessionStore。它们将用于管理身份认证密钥、预共享密钥、已签名预共享密钥和会话状态的加载和存储。
通过这些构建会话就相当简单:
// IKP-A,身份认证密钥对
IdentityKeyPair aliceIdentityKeyPair = KeyHelper.generateIdentityKeyPair();
// 注册编号,
int aliceRegistrationId = KeyHelper.generateRegistrationId(false);
// 会话存储
SessionStore aliceSessionStore = new InMemorySessionStore();
// 预共享存储
PreKeyStore alicePreKeyStore = new InMemoryPreKeyStore();
// 已签名预共享密钥
SignedPreKeyStore aliceSignedPreKeyStore = new InMemorySignedPreKeyStore();
// 身份认证密钥对
IdentityKeyStore aliceIdentityStore = new InMemoryIdentityKeyStore(aliceIdentityKeyPair, aliceRegistrationId);
// 创建会话处理器
SessionBuilder aliceSessionBuilder = new SessionBuilder(aliceSessionStore, alicePreKeyStore,
aliceSignedPreKeyStore, aliceIdentityStore, bobAddress);
// 从服务器获取bob的预共享密钥包,初始化会话
aliceSessionBuilder.process(bobProKeyBundle);
//创建消息加解密器
SessionCipher sessionCipher = new SessionCipher(aliceSessionStore, alicePreKeyStore, aliceSignedPreKeyStore,aliceIdentityStore, bobAddress);
// 组建初始消息
CiphertextMessage message = sessionCipher.encrypt("Hello world!".getBytes("UTF-8"));
//将消息内容发送到bob...
https://www.imdb.com/list/ls080192921/?mode=desktop
https://www.imdb.com/list/ls080192997/?mode=desktop
https://www.imdb.com/list/ls080192807/?mode=desktop
https://www.imdb.com/list/ls080192852/?mode=desktop
https://www.imdb.com/list/ls080192813/?mode=desktop
https://www.imdb.com/list/ls080192860/?mode=desktop
https://www.imdb.com/list/ls080192823/?mode=desktop
https://www.imdb.com/list/ls080192898/?mode=desktop
https://www.imdb.com/list/ls080194001/?mode=desktop
https://www.imdb.com/list/ls080194076/?mode=desktop
https://www.imdb.com/list/ls080194036/?mode=desktop
https://www.imdb.com/list/ls080194044/?mode=desktop
https://www.imdb.com/list/ls080194082/?mode=desktop
https://www.imdb.com/list/ls080194556/?mode=desktop
https://www.imdb.com/list/ls080194519/?mode=desktop
https://www.imdb.com/list/ls080194528/?mode=desktop
https://www.imdb.com/list/ls080194599/?mode=desktop
https://www.imdb.com/list/ls080194706/?mode=desktop
https://www.imdb.com/list/ls080194771/?mode=desktop
https://www.imdb.com/list/ls080194712/?mode=desktop
https://www.imdb.com/list/ls080194764/?mode=desktop
https://www.imdb.com/list/ls080194748/?mode=desktop
https://www.imdb.com/list/ls080194786/?mode=desktop
https://www.imdb.com/list/ls080194151/?mode=desktop
https://www.imdb.com/list/ls080194111/?mode=desktop
https://www.imdb.com/list/ls080194161/?mode=desktop
https://www.imdb.com/list/ls080194143/?mode=desktop
https://www.imdb.com/list/ls080194180/?mode=desktop
https://www.imdb.com/list/ls080194357/?mode=desktop
https://www.imdb.com/list/ls080194372/?mode=desktop
https://www.imdb.com/list/ls080194368/?mode=desktop
https://www.imdb.com/list/ls080194342/?mode=desktop
https://www.imdb.com/list/ls080194386/?mode=desktop
https://www.imdb.com/list/ls080194651/?mode=desktop
https://www.imdb.com/list/ls080194612/?mode=desktop
https://www.imdb.com/list/ls080194660/?mode=desktop
https://www.imdb.com/list/ls080194629/?mode=desktop
https://www.imdb.com/list/ls080194696/?mode=desktop
https://www.imdb.com/list/ls080194682/?mode=desktop
https://www.imdb.com/list/ls080194277/?mode=desktop
https://www.imdb.com/list/ls080194237/?mode=desktop
https://www.imdb.com/list/ls080194223/?mode=desktop
https://www.imdb.com/list/ls080194249/?mode=desktop
https://www.imdb.com/list/ls080194406/?mode=desktop
https://www.imdb.com/list/ls080194476/?mode=desktop
https://www.imdb.com/list/ls080194430/?mode=desktop
https://www.imdb.com/list/ls080194434/?mode=desktop
https://www.imdb.com/list/ls080194426/?mode=desktop
https://www.imdb.com/list/ls080194497/?mode=desktop
https://www.imdb.com/list/ls080194486/?mode=desktop
https://www.imdb.com/list/ls080194956/?mode=desktop
https://www.imdb.com/list/ls080194913/?mode=desktop
https://www.imdb.com/list/ls080194938/?mode=desktop
https://www.imdb.com/list/ls080194928/?mode=desktop
https://www.imdb.com/list/ls080194993/?mode=desktop
https://www.imdb.com/list/ls080194806/?mode=desktop
https://www.imdb.com/list/ls080194810/?mode=desktop
https://www.imdb.com/list/ls080194820/?mode=desktop
https://www.imdb.com/list/ls080194848/?mode=desktop
https://www.imdb.com/list/ls080194899/?mode=desktop