internal CombinedResult(DecryptionResult decrst, VerificationResult verrst)
 {
     this.decryptrst = decrst;
     this.verifyrst  = verrst;
 }
 public DecryptionFailedException(DecryptionResult rst)
     : base()
 {
     this.DecryptionResult = rst;
 }
 public BadPassphraseException(DecryptionResult rst)
     : base()
 {
     this.DecryptionResult = rst;
 }
 internal CombinedResult(DecryptionResult decrst, VerificationResult verrst)
 {
     this.decryptrst = decrst;
     this.verifyrst = verrst;
 }
        public CombinedResult DecryptAndVerify(GpgmeData cipher, GpgmeData plain)
        {
            if (IsValid)
            {
                if (cipher == null)
                {
                    throw new ArgumentNullException("Source data buffer must be supplied.");
                }
                if (!(cipher.IsValid))
                {
                    throw new InvalidDataBufferException("The specified source data buffer is invalid.");
                }

                if (plain == null)
                {
                    throw new ArgumentNullException("Destination data buffer must be supplied.");
                }
                if (!(plain.IsValid))
                {
                    throw new InvalidDataBufferException("The specified destination data buffer is invalid.");
                }

                lock (CtxLock)
                {
#if (VERBOSE_DEBUG)
                    DebugOutput("gpgme_op_decrypt_verify(..) START");
#endif
                    int err = libgpgme.gpgme_op_decrypt_verify(
                        CtxPtr,
                        cipher.dataPtr,
                        plain.dataPtr);
#if (VERBOSE_DEBUG)
                    DebugOutput("gpgme_op_decrypt_verify(..) DONE");
#endif
                    gpg_err_code_t errcode = libgpgerror.gpg_err_code(err);

                    switch (errcode)
                    {
                    case gpg_err_code_t.GPG_ERR_NO_ERROR:
                        break;

                    case gpg_err_code_t.GPG_ERR_NO_DATA:
                        // no encrypted data found - maybe it is only signed.
                        break;

                    case gpg_err_code_t.GPG_ERR_INV_VALUE:
                        throw new InvalidPtrException("Either the context, cipher text or plain text pointer is invalid.");
                    }

                    DecryptionResult decRst = null;

                    IntPtr rstPtr = libgpgme.gpgme_op_decrypt_result(CtxPtr);
                    if (rstPtr != IntPtr.Zero)
                    {
                        decRst = new DecryptionResult(rstPtr);
                    }

                    switch (errcode)
                    {
                    case gpg_err_code_t.GPG_ERR_NO_ERROR:
                        break;

                    case gpg_err_code_t.GPG_ERR_DECRYPT_FAILED:
                        if (decRst == null)
                        {
                            throw new DecryptionFailedException("An invalid cipher text has been supplied.");
                        }
                        else
                        {
                            throw new DecryptionFailedException(decRst);
                        }

                    case gpg_err_code_t.GPG_ERR_BAD_PASSPHRASE:
                        throw new BadPassphraseException(decRst);

                    default:
                        throw new GeneralErrorException("An unexpected error occurred. "
                                                        + errcode.ToString());
                    }

                    /* If decryption failed, verification cannot be proceeded */
                    VerificationResult verRst = null;

                    rstPtr = IntPtr.Zero;
                    rstPtr = libgpgme.gpgme_op_verify_result(CtxPtr);
                    if (rstPtr != IntPtr.Zero)
                    {
                        verRst = new VerificationResult(rstPtr);
                    }

                    GC.KeepAlive(cipher);
                    GC.KeepAlive(plain);

                    return(new CombinedResult(decRst, verRst));
                }
            }
            else
            {
                throw new InvalidContextException();
            }
        }
        public DecryptionResult Decrypt(GpgmeData cipher, GpgmeData plain)
        {
            if (IsValid)
            {
                if (cipher == null)
                {
                    throw new ArgumentNullException("Source data buffer must be supplied.");
                }
                if (!(cipher.IsValid))
                {
                    throw new InvalidDataBufferException("The specified source data buffer is invalid.");
                }

                if (plain == null)
                {
                    throw new ArgumentNullException("Destination data buffer must be supplied.");
                }
                if (!(plain.IsValid))
                {
                    throw new InvalidDataBufferException("The specified destination data buffer is invalid.");
                }

                lock (CtxLock)
                {
#if (VERBOSE_DEBUG)
                    DebugOutput("gpgme_op_decrypt(..) START");
#endif
                    int err = libgpgme.gpgme_op_decrypt(
                        CtxPtr,
                        cipher.dataPtr,
                        plain.dataPtr);
#if (VERBOSE_DEBUG)
                    DebugOutput("gpgme_op_decrypt(..) DONE");
#endif
                    gpg_err_code_t errcode = libgpgerror.gpg_err_code(err);

                    switch (errcode)
                    {
                    case gpg_err_code_t.GPG_ERR_NO_ERROR:
                        break;

                    case gpg_err_code_t.GPG_ERR_NO_DATA:
                        throw new NoDataException("The cipher does not contain any data to decrypt or is corrupt.");

                    case gpg_err_code_t.GPG_ERR_INV_VALUE:
                        throw new InvalidPtrException("Either the context, cipher text or plain text pointer is invalid.");
                    }

                    DecryptionResult decRst = null;

                    IntPtr rstPtr = libgpgme.gpgme_op_decrypt_result(CtxPtr);
                    if (rstPtr != IntPtr.Zero)
                    {
                        decRst = new DecryptionResult(rstPtr);
                    }

                    switch (errcode)
                    {
                    case gpg_err_code_t.GPG_ERR_NO_ERROR:
                        break;

                    case gpg_err_code_t.GPG_ERR_DECRYPT_FAILED:
                        if (decRst == null)
                        {
                            throw new DecryptionFailedException("An invalid cipher text has been supplied.");
                        }
                        else
                        {
                            throw new DecryptionFailedException(decRst);
                        }

                    case gpg_err_code_t.GPG_ERR_BAD_PASSPHRASE:
                        throw new BadPassphraseException(decRst);

                    default:
                        throw new GeneralErrorException("An unexpected error occurred. " + errcode.ToString());
                    }

                    GC.KeepAlive(cipher);
                    GC.KeepAlive(plain);

                    return(decRst);;
                }
            }
            else
            {
                throw new InvalidContextException();
            }
        }
Esempio n. 7
0
 public DecryptionFailedException(DecryptionResult rst) : base()
 {
     this.DecryptionResult = rst;
 }
Esempio n. 8
0
 public BadPassphraseException(DecryptionResult rst)
     : base()
 {
     this.DecryptionResult = rst;
 }