+ private static SignalProfile decryptProfile(SignalServiceProfile encryptedProfile, ProfileKey profileKey) throws IOException {
+ ProfileCipher profileCipher = new ProfileCipher(profileKey);
+ try {
+ return new SignalProfile(
+ encryptedProfile.getIdentityKey(),
+ encryptedProfile.getName() == null ? null : new String(profileCipher.decryptName(Base64.decode(encryptedProfile.getName()))),
+ encryptedProfile.getAvatar(),
+ encryptedProfile.getUnidentifiedAccess() == null || !profileCipher.verifyUnidentifiedAccess(Base64.decode(encryptedProfile.getUnidentifiedAccess())) ? null : encryptedProfile.getUnidentifiedAccess(),
+ encryptedProfile.isUnrestrictedUnidentifiedAccess()
+ );
+ } catch (InvalidCiphertextException e) {
+ return null;
+ }
+ }
+
+ private byte[] getTargetUnidentifiedAccessKey(SignalServiceAddress recipient) throws IOException {
+ ContactInfo contact = account.getContactStore().getContact(recipient.getNumber().get());
+ if (contact == null || contact.profileKey == null) {
+ return null;
+ }
+ ProfileKey theirProfileKey;
+ try {
+ theirProfileKey = new ProfileKey(Base64.decode(contact.profileKey));
+ } catch (InvalidInputException e) {
+ throw new AssertionError(e);
+ }
+ SignalProfile targetProfile = decryptProfile(getRecipientProfile(recipient, Optional.absent()), theirProfileKey);
+
+ if (targetProfile == null || targetProfile.getUnidentifiedAccess() == null) {
+ return null;
+ }
+
+ if (targetProfile.isUnrestrictedUnidentifiedAccess()) {
+ return KeyUtils.createUnrestrictedUnidentifiedAccess();
+ }
+
+ return UnidentifiedAccess.deriveAccessKeyFrom(theirProfileKey);