2017-08-26 17:57:45 +02:00
|
|
|
//
|
2018-08-06 15:58:08 +02:00
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
2017-08-26 17:57:45 +02:00
|
|
|
//
|
2016-08-19 21:36:24 +02:00
|
|
|
|
|
|
|
#import "OWSProvisioningMessage.h"
|
|
|
|
#import "OWSProvisioningCipher.h"
|
|
|
|
#import <AxolotlKit/NSData+keyVersionByte.h>
|
2018-08-06 15:58:08 +02:00
|
|
|
#import <Curve25519Kit/Curve25519.h>
|
2016-08-19 21:36:24 +02:00
|
|
|
#import <HKDFKit/HKDFKit.h>
|
2018-08-06 15:58:08 +02:00
|
|
|
#import <SignalServiceKit/SignalServiceKit-Swift.h>
|
2016-08-19 21:36:24 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
@interface OWSProvisioningMessage ()
|
|
|
|
|
|
|
|
@property (nonatomic, readonly) NSData *myPublicKey;
|
|
|
|
@property (nonatomic, readonly) NSData *myPrivateKey;
|
|
|
|
@property (nonatomic, readonly) NSString *accountIdentifier;
|
|
|
|
@property (nonatomic, readonly) NSData *theirPublicKey;
|
2017-08-26 20:35:33 +02:00
|
|
|
@property (nonatomic, readonly) NSData *profileKey;
|
2017-10-02 22:35:24 +02:00
|
|
|
@property (nonatomic, readonly) BOOL areReadReceiptsEnabled;
|
2016-08-19 21:36:24 +02:00
|
|
|
@property (nonatomic, readonly) NSString *provisioningCode;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation OWSProvisioningMessage
|
|
|
|
|
|
|
|
- (instancetype)initWithMyPublicKey:(NSData *)myPublicKey
|
|
|
|
myPrivateKey:(NSData *)myPrivateKey
|
|
|
|
theirPublicKey:(NSData *)theirPublicKey
|
|
|
|
accountIdentifier:(NSString *)accountIdentifier
|
2017-08-26 20:35:33 +02:00
|
|
|
profileKey:(NSData *)profileKey
|
2017-10-02 22:35:24 +02:00
|
|
|
readReceiptsEnabled:(BOOL)areReadReceiptsEnabled
|
2016-08-19 21:36:24 +02:00
|
|
|
provisioningCode:(NSString *)provisioningCode
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
_myPublicKey = myPublicKey;
|
|
|
|
_myPrivateKey = myPrivateKey;
|
|
|
|
_theirPublicKey = theirPublicKey;
|
2017-08-26 20:35:33 +02:00
|
|
|
_accountIdentifier = accountIdentifier;
|
|
|
|
_profileKey = profileKey;
|
2017-10-02 22:35:24 +02:00
|
|
|
_areReadReceiptsEnabled = areReadReceiptsEnabled;
|
2016-08-19 21:36:24 +02:00
|
|
|
_provisioningCode = provisioningCode;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-08-26 17:57:45 +02:00
|
|
|
- (nullable NSData *)buildEncryptedMessageBody
|
2016-08-19 21:36:24 +02:00
|
|
|
{
|
2018-08-08 19:08:47 +02:00
|
|
|
ProvisioningProtoProvisionMessageBuilder *messageBuilder =
|
|
|
|
[[ProvisioningProtoProvisionMessageBuilder alloc] initWithIdentityKeyPublic:self.myPublicKey
|
|
|
|
identityKeyPrivate:self.myPrivateKey
|
|
|
|
number:self.accountIdentifier
|
|
|
|
provisioningCode:self.provisioningCode
|
|
|
|
userAgent:@"OWI"
|
|
|
|
profileKey:self.profileKey
|
|
|
|
readReceipts:self.areReadReceiptsEnabled];
|
2016-08-19 21:36:24 +02:00
|
|
|
|
2018-08-06 15:58:08 +02:00
|
|
|
NSError *error;
|
|
|
|
NSData *_Nullable plainTextProvisionMessage = [messageBuilder buildSerializedDataAndReturnError:&error];
|
|
|
|
if (!plainTextProvisionMessage || error) {
|
|
|
|
OWSFail(@"%@ could not serialize proto: %@.", self.logTag, error);
|
|
|
|
return nil;
|
|
|
|
}
|
2016-08-19 21:36:24 +02:00
|
|
|
|
|
|
|
OWSProvisioningCipher *cipher = [[OWSProvisioningCipher alloc] initWithTheirPublicKey:self.theirPublicKey];
|
2017-08-26 17:57:45 +02:00
|
|
|
NSData *_Nullable encryptedProvisionMessage = [cipher encrypt:plainTextProvisionMessage];
|
|
|
|
if (encryptedProvisionMessage == nil) {
|
2017-09-25 18:30:03 +02:00
|
|
|
OWSFail(@"Failed to encrypt provision message");
|
2017-08-26 17:57:45 +02:00
|
|
|
return nil;
|
|
|
|
}
|
2016-08-19 21:36:24 +02:00
|
|
|
|
|
|
|
// Note that this is a one-time-use *cipher* public key, not our Signal *identity* public key
|
2018-08-08 19:08:47 +02:00
|
|
|
ProvisioningProtoProvisionEnvelopeBuilder *envelopeBuilder =
|
|
|
|
[[ProvisioningProtoProvisionEnvelopeBuilder alloc] initWithPublicKey:[cipher.ourPublicKey prependKeyType]
|
|
|
|
body:encryptedProvisionMessage];
|
2016-08-19 21:36:24 +02:00
|
|
|
|
2018-08-06 15:58:08 +02:00
|
|
|
NSData *_Nullable envelopeData = [envelopeBuilder buildSerializedDataAndReturnError:&error];
|
|
|
|
if (!envelopeData || error) {
|
|
|
|
OWSFail(@"%@ could not serialize proto: %@.", self.logTag, error);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return envelopeData;
|
2016-08-19 21:36:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|