Search in sources :

Example 6 with InvalidCertificateException

use of org.signal.libsignal.metadata.certificate.InvalidCertificateException in project Signal-Android by signalapp.

the class PushSendJob method rotateSenderCertificateIfNecessary.

protected void rotateSenderCertificateIfNecessary() throws IOException {
    try {
        Collection<CertificateType> requiredCertificateTypes = SignalStore.phoneNumberPrivacy().getRequiredCertificateTypes();
        Log.i(TAG, "Ensuring we have these certificates " + requiredCertificateTypes);
        for (CertificateType certificateType : requiredCertificateTypes) {
            byte[] certificateBytes = SignalStore.certificateValues().getUnidentifiedAccessCertificate(certificateType);
            if (certificateBytes == null) {
                throw new InvalidCertificateException(String.format("No certificate %s was present.", certificateType));
            }
            SenderCertificate certificate = new SenderCertificate(certificateBytes);
            if (System.currentTimeMillis() > (certificate.getExpiration() - CERTIFICATE_EXPIRATION_BUFFER)) {
                throw new InvalidCertificateException(String.format(Locale.US, "Certificate %s is expired, or close to it. Expires on: %d, currently: %d", certificateType, certificate.getExpiration(), System.currentTimeMillis()));
            }
            Log.d(TAG, String.format("Certificate %s is valid", certificateType));
        }
        Log.d(TAG, "All certificates are valid.");
    } catch (InvalidCertificateException e) {
        Log.w(TAG, "A certificate was invalid at send time. Fetching new ones.", e);
        if (!ApplicationDependencies.getJobManager().runSynchronously(new RotateCertificateJob(), 5000).isPresent()) {
            throw new IOException("Timeout rotating certificate");
        }
    }
}
Also used : CertificateType(org.thoughtcrime.securesms.keyvalue.CertificateType) SenderCertificate(org.signal.libsignal.metadata.certificate.SenderCertificate) IOException(java.io.IOException) InvalidCertificateException(org.signal.libsignal.metadata.certificate.InvalidCertificateException)

Aggregations

InvalidCertificateException (org.signal.libsignal.metadata.certificate.InvalidCertificateException)6 CertificateType (org.thoughtcrime.securesms.keyvalue.CertificateType)4 UnidentifiedAccess (org.whispersystems.signalservice.api.crypto.UnidentifiedAccess)4 UnidentifiedAccessPair (org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair)4 WorkerThread (androidx.annotation.WorkerThread)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 SenderCertificate (org.signal.libsignal.metadata.certificate.SenderCertificate)2 Recipient (org.thoughtcrime.securesms.recipients.Recipient)2 Optional (org.whispersystems.libsignal.util.guava.Optional)2