You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

677 lines
23 KiB

2 years ago
2 years ago
  1. //
  2. // PEPSession.m
  3. // PEPObjCAdapterFramework
  4. //
  5. // Created by Dirk Zimmermann on 17.07.20.
  6. // Copyright © 2020 pp. All rights reserved.
  7. //
  8. #import "PEPSession.h"
  9. #import "PEPMessage.h"
  10. #import "PEPEngineTypes.h"
  11. #import "PEPInternalSession.h"
  12. #import "PEPInternalSession+TKA.h"
  13. #import "NSNumber+PEPRating.h"
  14. #import "PEPIdentity.h"
  15. #import "PEPSessionProvider.h"
  16. #import "PEPInternalConstants.h"
  17. #import <PEPObjCTypeUtils.h>
  18. static dispatch_queue_t queue;
  19. @implementation PEPSession
  20. + (void)initialize
  21. {
  22. if (self == [PEPSession class]) {
  23. queue = dispatch_queue_create("security.pep.PEPAsyncSession.queue", DISPATCH_QUEUE_SERIAL);
  24. }
  25. }
  26. + (void)cleanup
  27. {
  28. [PEPSessionProvider cleanup];
  29. }
  30. - (void)decryptMessage:(PEPMessage *)message
  31. flags:(PEPDecryptFlags)flags
  32. extraKeys:(PEPStringList * _Nullable)extraKeys
  33. errorCallback:(void (^)(NSError *error))errorCallback
  34. successCallback:(void (^)(PEPMessage *srcMessage,
  35. PEPMessage *dstMessage,
  36. PEPStringList *keyList,
  37. PEPDecryptFlags flags,
  38. BOOL isFormerlyEncryptedReuploadedMessage))successCallback
  39. {
  40. dispatch_async(queue, ^{
  41. PEPMessage *theMessage = [[PEPMessage alloc] initWithMessage:message];
  42. PEPDecryptFlags theFlags = flags;
  43. PEPStringList *theExtraKeys = extraKeys;
  44. PEPStatus status;
  45. NSError *error = nil;
  46. PEPMessage *newMessage = [[PEPSessionProvider session] decryptMessage:theMessage
  47. flags:&theFlags
  48. extraKeys:&theExtraKeys
  49. status:&status
  50. error:&error];
  51. if (newMessage) {
  52. // See IOS-2414 for details
  53. BOOL isFormerlyEncryptedReuploadedMessage = (status == PEPStatusUnencrypted) && newMessage.rating >= PEPRatingUnreliable;
  54. successCallback(theMessage,
  55. newMessage,
  56. theExtraKeys,
  57. theFlags,
  58. isFormerlyEncryptedReuploadedMessage);
  59. } else {
  60. errorCallback(error);
  61. }
  62. });
  63. }
  64. - (void)reEvaluateMessage:(PEPMessage *)message
  65. xKeyList:(PEPStringList *_Nullable)xKeyList
  66. originalRating:(PEPRating)originalRating
  67. errorCallback:(void (^)(NSError *error))errorCallback
  68. successCallback:(void (^)(PEPRating rating))successCallback
  69. {
  70. dispatch_async(queue, ^{
  71. PEPRating theRating = originalRating;
  72. NSError *error = nil;
  73. BOOL result = [[PEPSessionProvider session]
  74. reEvaluateMessage:message
  75. xKeyList:xKeyList
  76. rating:&theRating
  77. status:nil
  78. error:&error];
  79. if (result) {
  80. successCallback(theRating);
  81. } else {
  82. errorCallback(error);
  83. }
  84. });
  85. }
  86. - (void)encryptMessage:(PEPMessage *)message
  87. extraKeys:(PEPStringList * _Nullable)extraKeys
  88. encFormat:(PEPEncFormat)encFormat
  89. errorCallback:(void (^)(NSError *error))errorCallback
  90. successCallback:(void (^)(PEPMessage *srcMessage,
  91. PEPMessage *destMessage))successCallback
  92. {
  93. dispatch_async(queue, ^{
  94. PEPMessage *theMessage = [[PEPMessage alloc] initWithMessage:message];
  95. NSError *error = nil;
  96. PEPMessage *destMessage = [[PEPSessionProvider session]
  97. encryptMessage:theMessage
  98. extraKeys:extraKeys
  99. encFormat:encFormat
  100. status:nil
  101. error:&error];
  102. if (destMessage) {
  103. successCallback(theMessage, destMessage);
  104. } else {
  105. errorCallback(error);
  106. }
  107. });
  108. }
  109. - (void)encryptMessage:(PEPMessage *)message
  110. extraKeys:(PEPStringList * _Nullable)extraKeys
  111. errorCallback:(void (^)(NSError *error))errorCallback
  112. successCallback:(void (^)(PEPMessage *srcMessage,
  113. PEPMessage *destMessage))successCallback
  114. {
  115. dispatch_async(queue, ^{
  116. PEPMessage *theMessage = [[PEPMessage alloc] initWithMessage:message];
  117. NSError *error = nil;
  118. PEPMessage *destMessage = [[PEPSessionProvider session]
  119. encryptMessage:theMessage
  120. extraKeys:extraKeys
  121. status:nil
  122. error:&error];
  123. if (destMessage) {
  124. successCallback(theMessage, destMessage);
  125. } else {
  126. errorCallback(error);
  127. }
  128. });
  129. }
  130. - (void)encryptMessage:(PEPMessage *)message
  131. forSelf:(PEPIdentity *)ownIdentity
  132. extraKeys:(PEPStringList * _Nullable)extraKeys
  133. errorCallback:(void (^)(NSError *error))errorCallback
  134. successCallback:(void (^)(PEPMessage *srcMessage,
  135. PEPMessage *destMessage))successCallback
  136. {
  137. dispatch_async(queue, ^{
  138. PEPMessage *theMessage = [[PEPMessage alloc] initWithMessage:message];
  139. NSError *error = nil;
  140. PEPMessage *destMessage = [[PEPSessionProvider session]
  141. encryptMessage:theMessage
  142. forSelf:ownIdentity
  143. extraKeys:extraKeys
  144. status:nil
  145. error:&error];
  146. if (destMessage) {
  147. successCallback(theMessage, destMessage);
  148. } else {
  149. errorCallback(error);
  150. }
  151. });
  152. }
  153. - (void)encryptMessage:(PEPMessage *)message
  154. toFpr:(NSString *)toFpr
  155. encFormat:(PEPEncFormat)encFormat
  156. flags:(PEPDecryptFlags)flags
  157. errorCallback:(void (^)(NSError *error))errorCallback
  158. successCallback:(void (^)(PEPMessage *srcMessage,
  159. PEPMessage *destMessage))successCallback
  160. {
  161. dispatch_async(queue, ^{
  162. PEPMessage *theMessage = [[PEPMessage alloc] initWithMessage:message];
  163. NSError *error = nil;
  164. PEPMessage *destMessage = [[PEPSessionProvider session]
  165. encryptMessage:theMessage
  166. toFpr:toFpr
  167. encFormat:encFormat
  168. flags:flags
  169. status:nil
  170. error:&error];
  171. if (destMessage) {
  172. successCallback(theMessage, destMessage);
  173. } else {
  174. errorCallback(error);
  175. }
  176. });
  177. }
  178. - (void)outgoingRatingForMessage:(PEPMessage *)theMessage
  179. errorCallback:(void (^)(NSError *error))errorCallback
  180. successCallback:(void (^)(PEPRating rating))successCallback
  181. {
  182. dispatch_async(queue, ^{
  183. NSError *error = nil;
  184. NSNumber *ratingNum = [[PEPSessionProvider session]
  185. outgoingRatingForMessage:theMessage
  186. error:&error];
  187. if (ratingNum != nil) {
  188. successCallback(ratingNum.pEpRating);
  189. } else {
  190. errorCallback(error);
  191. }
  192. });
  193. }
  194. - (void)ratingForIdentity:(PEPIdentity *)identity
  195. errorCallback:(void (^)(NSError *error))errorCallback
  196. successCallback:(void (^)(PEPRating rating))successCallback
  197. {
  198. dispatch_async(queue, ^{
  199. NSError *error = nil;
  200. NSNumber *ratingNum = [[PEPSessionProvider session]
  201. ratingForIdentity:identity
  202. error:&error];
  203. if (ratingNum != nil) {
  204. successCallback(ratingNum.pEpRating);
  205. } else {
  206. errorCallback(error);
  207. }
  208. });
  209. }
  210. - (void)trustwordsForFingerprint:(NSString *)fingerprint
  211. languageID:(NSString *)languageID
  212. shortened:(BOOL)shortened
  213. errorCallback:(void (^)(NSError *error))errorCallback
  214. successCallback:(void (^)(NSArray<NSString *> *trustwords))successCallback
  215. {
  216. dispatch_async(queue, ^{
  217. NSError *error = nil;
  218. NSArray *trustwords = [[PEPSessionProvider session]
  219. trustwordsForFingerprint:fingerprint
  220. languageID:languageID
  221. shortened:shortened
  222. error:&error];
  223. if (!error) {
  224. successCallback(trustwords);
  225. } else {
  226. errorCallback(error);
  227. }
  228. });
  229. }
  230. - (void)mySelf:(PEPIdentity *)identity
  231. errorCallback:(void (^)(NSError *error))errorCallback
  232. successCallback:(void (^)(PEPIdentity *identity))successCallback
  233. {
  234. __block PEPIdentity *theIdentity = [[PEPIdentity alloc] initWithIdentity:identity];
  235. dispatch_async(queue, ^{
  236. NSError *error = nil;
  237. BOOL success = [[PEPSessionProvider session] mySelf:theIdentity error:&error];
  238. if (success) {
  239. successCallback(theIdentity);
  240. } else {
  241. errorCallback(error);
  242. }
  243. });
  244. }
  245. - (void)updateIdentity:(PEPIdentity *)identity
  246. errorCallback:(void (^)(NSError *error))errorCallback
  247. successCallback:(void (^)(PEPIdentity *identity))successCallback
  248. {
  249. __block PEPIdentity *theIdentity = [[PEPIdentity alloc] initWithIdentity:identity];
  250. dispatch_async(queue, ^{
  251. NSError *error = nil;
  252. BOOL success = [[PEPSessionProvider session] updateIdentity:theIdentity error:&error];
  253. if (success) {
  254. successCallback(theIdentity);
  255. } else {
  256. errorCallback(error);
  257. }
  258. });
  259. }
  260. - (void)trustPersonalKey:(PEPIdentity *)identity
  261. errorCallback:(void (^)(NSError *error))errorCallback
  262. successCallback:(void (^)(void))successCallback
  263. {
  264. dispatch_async(queue, ^{
  265. NSError *error = nil;
  266. BOOL success = [[PEPSessionProvider session] trustPersonalKey:identity error:&error];
  267. if (success) {
  268. successCallback();
  269. } else {
  270. errorCallback(error);
  271. }
  272. });
  273. }
  274. - (void)keyMistrusted:(PEPIdentity *)identity
  275. errorCallback:(void (^)(NSError *error))errorCallback
  276. successCallback:(void (^)(void))successCallback
  277. {
  278. dispatch_async(queue, ^{
  279. NSError *error = nil;
  280. BOOL success = [[PEPSessionProvider session] keyMistrusted:identity error:&error];
  281. if (success) {
  282. successCallback();
  283. } else {
  284. errorCallback(error);
  285. }
  286. });
  287. }
  288. - (void)keyResetTrust:(PEPIdentity *)identity
  289. errorCallback:(void (^)(NSError *error))errorCallback
  290. successCallback:(void (^)(void))successCallback
  291. {
  292. dispatch_async(queue, ^{
  293. NSError *error = nil;
  294. BOOL success = [[PEPSessionProvider session] keyResetTrust:identity error:&error];
  295. if (success) {
  296. successCallback();
  297. } else {
  298. errorCallback(error);
  299. }
  300. });
  301. }
  302. - (void)enableSyncForIdentity:(PEPIdentity *)identity
  303. errorCallback:(void (^)(NSError *error))errorCallback
  304. successCallback:(void (^)(void))successCallback
  305. {
  306. dispatch_async(queue, ^{
  307. NSError *error = nil;
  308. BOOL success = [[PEPSessionProvider session] enableSyncForIdentity:identity error:&error];
  309. if (success) {
  310. successCallback();
  311. } else {
  312. errorCallback(error);
  313. }
  314. });
  315. }
  316. - (void)disableSyncForIdentity:(PEPIdentity *)identity
  317. errorCallback:(void (^)(NSError *error))errorCallback
  318. successCallback:(void (^)(void))successCallback
  319. {
  320. dispatch_async(queue, ^{
  321. NSError *error = nil;
  322. BOOL success = [[PEPSessionProvider session] disableSyncForIdentity:identity error:&error];
  323. if (success) {
  324. successCallback();
  325. } else {
  326. errorCallback(error);
  327. }
  328. });
  329. }
  330. - (void)importKey:(NSString *)keydata
  331. errorCallback:(void (^)(NSError *error))errorCallback
  332. successCallback:(void (^)(NSArray<PEPIdentity *> *identities))successCallback
  333. {
  334. dispatch_async(queue, ^{
  335. NSError *error = nil;
  336. NSArray *identities = [[PEPSessionProvider session] importKey:keydata error:&error];
  337. if (identities) {
  338. successCallback(identities);
  339. } else {
  340. errorCallback(error);
  341. }
  342. });
  343. }
  344. - (void)logTitle:(NSString *)title
  345. entity:(NSString *)entity
  346. description:(NSString * _Nullable)description
  347. comment:(NSString * _Nullable)comment
  348. errorCallback:(void (^)(NSError *error))errorCallback
  349. successCallback:(void (^)(void))successCallback
  350. {
  351. dispatch_async(queue, ^{
  352. NSError *error = nil;
  353. BOOL success = [[PEPSessionProvider session] logTitle:title
  354. entity:entity
  355. description:description
  356. comment:comment
  357. error:&error];
  358. if (success) {
  359. successCallback();
  360. } else {
  361. errorCallback(error);
  362. }
  363. });
  364. }
  365. - (void)getLog:(void (^)(NSError *error))errorCallback
  366. successCallback:(void (^)(NSString *log))successCallback
  367. {
  368. dispatch_async(queue, ^{
  369. NSError *error = nil;
  370. NSString *log = [[PEPSessionProvider session] getLogWithError:&error];
  371. if (log) {
  372. successCallback(log);
  373. } else {
  374. errorCallback(error);
  375. }
  376. });
  377. }
  378. - (void)getTrustwordsIdentity1:(PEPIdentity *)identity1
  379. identity2:(PEPIdentity *)identity2
  380. language:(NSString * _Nullable)language
  381. full:(BOOL)full
  382. errorCallback:(void (^)(NSError *error))errorCallback
  383. successCallback:(void (^)(NSString *trustwords))successCallback
  384. {
  385. dispatch_async(queue, ^{
  386. NSError *error = nil;
  387. NSString *trustwords = [[PEPSessionProvider session] getTrustwordsIdentity1:identity1
  388. identity2:identity2
  389. language:language
  390. full:full
  391. error:&error];
  392. if (trustwords) {
  393. successCallback(trustwords);
  394. } else {
  395. errorCallback(error);
  396. }
  397. });
  398. }
  399. - (void)getTrustwordsFpr1:(NSString *)fpr1
  400. fpr2:(NSString *)fpr2
  401. language:(NSString * _Nullable)language
  402. full:(BOOL)full
  403. errorCallback:(void (^)(NSError *error))errorCallback
  404. successCallback:(void (^)(NSString *trustwords))successCallback
  405. {
  406. dispatch_async(queue, ^{
  407. NSError *error = nil;
  408. NSString *trustwords = [[PEPSessionProvider session] getTrustwordsFpr1:fpr1
  409. fpr2:fpr2
  410. language:language
  411. full:full
  412. error:&error];
  413. if (trustwords) {
  414. successCallback(trustwords);
  415. } else {
  416. errorCallback(error);
  417. }
  418. });
  419. }
  420. - (void)languageList:(void (^)(NSError *error))errorCallback
  421. successCallback:(void (^)(NSArray<PEPLanguage *> *languages))successCallback
  422. {
  423. dispatch_async(queue, ^{
  424. NSError *error = nil;
  425. NSArray *languages = [[PEPSessionProvider session] languageListWithError:&error];
  426. if (languages) {
  427. successCallback(languages);
  428. } else {
  429. errorCallback(error);
  430. }
  431. });
  432. }
  433. - (void)isPEPUser:(PEPIdentity *)identity
  434. errorCallback:(void (^)(NSError *error))errorCallback
  435. successCallback:(void (^)(BOOL enabled))successCallback
  436. {
  437. dispatch_async(queue, ^{
  438. NSError *error = nil;
  439. NSNumber *ispEpUserNum = [[PEPSessionProvider session] isPEPUser:identity error:&error];
  440. if (ispEpUserNum != nil) {
  441. successCallback(ispEpUserNum.boolValue);
  442. } else {
  443. errorCallback(error);
  444. }
  445. });
  446. }
  447. - (void)setOwnKey:(PEPIdentity *)identity
  448. fingerprint:(NSString *)fingerprint
  449. errorCallback:(void (^)(NSError *error))errorCallback
  450. successCallback:(void (^)(void))successCallback
  451. {
  452. dispatch_async(queue, ^{
  453. NSError *error = nil;
  454. BOOL success = [[PEPSessionProvider session] setOwnKey:identity
  455. fingerprint:fingerprint
  456. error:&error];
  457. if (success) {
  458. successCallback();
  459. } else {
  460. errorCallback(error);
  461. }
  462. });
  463. }
  464. - (void)deliverHandshakeResult:(PEPSyncHandshakeResult)result
  465. identitiesSharing:(NSArray<PEPIdentity *> * _Nullable)identitiesSharing
  466. errorCallback:(void (^)(NSError *error))errorCallback
  467. successCallback:(void (^)(void))successCallback
  468. {
  469. dispatch_async(queue, ^{
  470. NSError *error = nil;
  471. BOOL success = [[PEPSessionProvider session] deliverHandshakeResult:result
  472. identitiesSharing:identitiesSharing
  473. error:&error];
  474. if (success) {
  475. successCallback();
  476. } else {
  477. errorCallback(error);
  478. }
  479. });
  480. }
  481. - (void)trustOwnKeyIdentity:(PEPIdentity *)identity
  482. errorCallback:(void (^)(NSError *error))errorCallback
  483. successCallback:(void (^)(void))successCallback
  484. {
  485. dispatch_async(queue, ^{
  486. NSError *error = nil;
  487. BOOL success = [[PEPSessionProvider session] trustOwnKeyIdentity:identity error:&error];
  488. if (success) {
  489. successCallback();
  490. } else {
  491. errorCallback(error);
  492. }
  493. });
  494. }
  495. - (void)keyReset:(PEPIdentity *)identity
  496. fingerprint:(NSString * _Nullable)fingerprint
  497. errorCallback:(void (^)(NSError *error))errorCallback
  498. successCallback:(void (^)(void))successCallback
  499. {
  500. dispatch_async(queue, ^{
  501. NSError *error = nil;
  502. BOOL success = [[PEPSessionProvider session] keyReset:identity
  503. fingerprint:fingerprint
  504. error:&error];
  505. if (success) {
  506. successCallback();
  507. } else {
  508. errorCallback(error);
  509. }
  510. });
  511. }
  512. - (void)leaveDeviceGroup:(void (^)(NSError *error))errorCallback
  513. successCallback:(void (^)(void))successCallback
  514. {
  515. dispatch_async(queue, ^{
  516. NSError *error = nil;
  517. BOOL success = [[PEPSessionProvider session] leaveDeviceGroup:&error];
  518. if (success) {
  519. successCallback();
  520. } else {
  521. errorCallback(error);
  522. }
  523. });
  524. }
  525. - (void)keyResetAllOwnKeys:(void (^)(NSError *error))errorCallback
  526. successCallback:(void (^)(void))successCallback
  527. {
  528. dispatch_async(queue, ^{
  529. NSError *error = nil;
  530. BOOL success = [[PEPSessionProvider session] keyResetAllOwnKeysError:&error];
  531. if (success) {
  532. successCallback();
  533. } else {
  534. errorCallback(error);
  535. }
  536. });
  537. }
  538. // MARK: - Configuration
  539. - (void)configurePassiveModeEnabled:(BOOL)enabled
  540. {
  541. return [[PEPSessionProvider session] configurePassiveModeEnabled:enabled];
  542. }
  543. - (BOOL)configurePassphrase:(NSString * _Nonnull)passphrase
  544. error:(NSError * _Nullable __autoreleasing * _Nullable)error {
  545. PEPInternalSession *session = [PEPSessionProvider session];
  546. if (session == nil) {
  547. if (error) {
  548. *error = [PEPStatusNSErrorUtil errorWithPEPStatus:PEPStatusUnknownError];
  549. }
  550. return NO;
  551. }
  552. return [session configurePassphrase:passphrase error:error];
  553. }
  554. // MARK: - Methods that can be executed syncronously
  555. - (PEPRating)ratingFromString:(NSString * _Nonnull)string
  556. {
  557. PEPInternalSession *session = [PEPSessionProvider session];
  558. if (session == nil) {
  559. return PEPRatingUndefined;
  560. }
  561. return [session ratingFromString:string];
  562. }
  563. - (NSString * _Nonnull)stringFromRating:(PEPRating)rating
  564. {
  565. PEPInternalSession *session = [PEPSessionProvider session];
  566. if (session == nil) {
  567. return kUndefined;
  568. }
  569. return [session stringFromRating:rating];
  570. }
  571. - (PEPColor)colorFromRating:(PEPRating)rating
  572. {
  573. PEPInternalSession *session = [PEPSessionProvider session];
  574. if (session == nil) {
  575. return PEPColorNoColor;
  576. }
  577. return [session colorFromRating:rating];
  578. }
  579. - (BOOL)disableAllSyncChannels:(NSError * _Nullable * _Nullable)error
  580. {
  581. PEPInternalSession *session = [PEPSessionProvider session];
  582. if (session == nil) {
  583. if (error) {
  584. *error = [PEPStatusNSErrorUtil errorWithPEPStatus:PEPStatusUnknownError];
  585. }
  586. return NO;
  587. }
  588. return [session disableAllSyncChannels:error];
  589. }
  590. // MARK: - TKA
  591. - (void)tkaSubscribeWithKeychangeDelegate:(nullable id<PEPTKADelegate>)delegate
  592. errorCallback:(nonnull void (^)(NSError * _Nonnull))errorCallback
  593. successCallback:(nonnull void (^)(void))successCallback {
  594. dispatch_async(queue, ^{
  595. NSError *error = nil;
  596. BOOL success = [[PEPSessionProvider session] tkaSubscribeWithKeychangeDelegate:delegate
  597. error:&error];
  598. if (success) {
  599. successCallback();
  600. } else {
  601. errorCallback(error);
  602. }
  603. });
  604. }
  605. - (void)tkaRequestTempKeyForMe:(nonnull PEPIdentity *)me
  606. partner:(nonnull PEPIdentity *)partner
  607. errorCallback:(nonnull void (^)(NSError * _Nonnull))errorCallback
  608. successCallback:(nonnull void (^)(void))successCallback {
  609. dispatch_async(queue, ^{
  610. NSError *error = nil;
  611. BOOL success = [[PEPSessionProvider session] tkaRequestTempKeyForMe:me
  612. partner:partner
  613. error:&error];
  614. if (success) {
  615. successCallback();
  616. } else {
  617. errorCallback(error);
  618. }
  619. });
  620. }
  621. @end