Search in sources :

Example 1 with OlmException

use of org.matrix.olm.OlmException in project matrix-android-sdk by matrix-org.

the class MediaScanRestClient method scanEncryptedFile.

/**
 * Scan an encrypted file.
 *
 * @param encryptedMediaScanBody the encryption information required to decrypt the content before scanning it.
 * @param callback               on success callback containing a MediaScanResult object
 */
public void scanEncryptedFile(final EncryptedMediaScanBody encryptedMediaScanBody, final ApiCallback<MediaScanResult> callback) {
    // Encrypt encryptedMediaScanBody if the server support it
    getServerPublicKey(new SimpleApiCallback<String>(callback) {

        @Override
        public void onSuccess(String serverPublicKey) {
            Call<MediaScanResult> request;
            // Encrypt the data, if antivirus server supports it
            if (!TextUtils.isEmpty(serverPublicKey)) {
                try {
                    OlmPkEncryption olmPkEncryption = new OlmPkEncryption();
                    olmPkEncryption.setRecipientKey(serverPublicKey);
                    String data = JsonUtility.getCanonicalizedJsonString(encryptedMediaScanBody);
                    OlmPkMessage message = olmPkEncryption.encrypt(data);
                    EncryptedMediaScanEncryptedBody encryptedMediaScanEncryptedBody = new EncryptedMediaScanEncryptedBody();
                    encryptedMediaScanEncryptedBody.encryptedBodyFileInfo = new EncryptedBodyFileInfo(message);
                    request = mApi.scanEncrypted(encryptedMediaScanEncryptedBody);
                } catch (OlmException e) {
                    // should not happen. Send the error to the caller
                    request = null;
                    callback.onUnexpectedError(e);
                }
            } else {
                // No public key on this server, do not encrypt data
                request = mApi.scanEncrypted(encryptedMediaScanBody);
            }
            if (request != null) {
                request.enqueue(new RestAdapterCallback<>("scanEncryptedFile", null, new SimpleApiCallback<MediaScanResult>(callback) {

                    @Override
                    public void onSuccess(MediaScanResult scanResult) {
                        callback.onSuccess(scanResult);
                    }

                    @Override
                    public void onMatrixError(MatrixError e) {
                        // Check whether the provided encrypted_body could not be decrypted.
                        if (e.mStatus == HttpURLConnection.HTTP_FORBIDDEN) {
                            MediaScanError mcsError;
                            try {
                                mcsError = JsonUtils.getGson(false).fromJson(e.mErrorBodyAsString, MediaScanError.class);
                            } catch (Exception exc) {
                                mcsError = null;
                            }
                            if (mcsError != null && MediaScanError.MCS_BAD_DECRYPTION.equals(mcsError.reason)) {
                                // The client should request again the public key of the server.
                                resetServerPublicKey();
                            }
                        }
                        super.onMatrixError(e);
                    }
                }, null));
            }
        }
    });
}
Also used : Call(retrofit2.Call) EncryptedBodyFileInfo(org.matrix.androidsdk.crypto.model.crypto.EncryptedBodyFileInfo) MediaScanError(org.matrix.androidsdk.rest.model.MediaScanError) RestAdapterCallback(org.matrix.androidsdk.rest.callback.RestAdapterCallback) OlmPkMessage(org.matrix.olm.OlmPkMessage) EncryptedMediaScanEncryptedBody(org.matrix.androidsdk.rest.model.EncryptedMediaScanEncryptedBody) OlmException(org.matrix.olm.OlmException) OlmException(org.matrix.olm.OlmException) MediaScanResult(org.matrix.androidsdk.rest.model.MediaScanResult) MatrixError(org.matrix.androidsdk.core.model.MatrixError) OlmPkEncryption(org.matrix.olm.OlmPkEncryption)

Aggregations

MatrixError (org.matrix.androidsdk.core.model.MatrixError)1 EncryptedBodyFileInfo (org.matrix.androidsdk.crypto.model.crypto.EncryptedBodyFileInfo)1 RestAdapterCallback (org.matrix.androidsdk.rest.callback.RestAdapterCallback)1 EncryptedMediaScanEncryptedBody (org.matrix.androidsdk.rest.model.EncryptedMediaScanEncryptedBody)1 MediaScanError (org.matrix.androidsdk.rest.model.MediaScanError)1 MediaScanResult (org.matrix.androidsdk.rest.model.MediaScanResult)1 OlmException (org.matrix.olm.OlmException)1 OlmPkEncryption (org.matrix.olm.OlmPkEncryption)1 OlmPkMessage (org.matrix.olm.OlmPkMessage)1 Call (retrofit2.Call)1