Example #1
0
        /// <summary>
        /// Starts an explicit encryption context.
        /// </summary>
        /// <param name="key">The key id.</param>
        /// <param name="encryptionAlgorithm">The encryption algorithm.</param>
        /// <param name="message">The BSON message.</param>
        /// <returns>A encryption context. </returns>
        public CryptContext StartExplicitEncryptionContextWithKeyId(byte[] keyId, EncryptionAlgorithm encryptionAlgorithm, byte[] message)
        {
            ContextSafeHandle handle = Library.mongocrypt_ctx_new(_handle);

            unsafe
            {
                fixed(byte *p = keyId)
                {
                    IntPtr ptr = (IntPtr)p;

                    using (PinnedBinary pinned = new PinnedBinary(ptr, (uint)keyId.Length))
                    {
                        handle.Check(_status, Library.mongocrypt_ctx_setopt_key_id(handle, pinned.Handle));
                    }
                }
            }

            handle.Check(_status, Library.mongocrypt_ctx_setopt_algorithm(handle, Helpers.EncryptionAlgorithmToString(encryptionAlgorithm), -1));

            unsafe
            {
                fixed(byte *p = message)
                {
                    IntPtr ptr = (IntPtr)p;

                    using (PinnedBinary pinned = new PinnedBinary(ptr, (uint)message.Length))
                    {
                        handle.Check(_status, Library.mongocrypt_ctx_explicit_encrypt_init(handle, pinned.Handle));
                    }
                }
            }

            return(new CryptContext(handle));
        }
Example #2
0
        /// <summary>
        /// Starts the encryption context.
        /// </summary>
        /// <param name="db">The database of the collection.</param>
        /// <param name="command">The command.</param>
        /// <returns>A encryption context.</returns>
        public CryptContext StartEncryptionContext(string db, byte[] command)
        {
            ContextSafeHandle handle = Library.mongocrypt_ctx_new(_handle);

            IntPtr stringPointer = (IntPtr)Marshal.StringToHGlobalAnsi(db);

            try
            {
                unsafe
                {
                    fixed(byte *c = command)
                    {
                        var commandPtr = (IntPtr)c;

                        using (var pinnedCommand = new PinnedBinary(commandPtr, (uint)command.Length))
                        {
                            // Let mongocrypt run strlen
                            handle.Check(_status, Library.mongocrypt_ctx_encrypt_init(handle, stringPointer, -1, pinnedCommand.Handle));
                        }
                    }
                }
            }
            finally
            {
                Marshal.FreeHGlobal(stringPointer);
            }

            return(new CryptContext(handle));
        }
Example #3
0
        /// <summary>
        /// Starts the create data key context.
        /// </summary>
        /// <param name="keyId">The key identifier.</param>
        /// <returns>A crypt context for creating a data key</returns>
        public CryptContext StartCreateDataKeyContext(KmsKeyId keyId)
        {
            ContextSafeHandle handle = Library.mongocrypt_ctx_new(_handle);

            keyId.SetCredentials(handle, _status);

            handle.Check(_status, Library.mongocrypt_ctx_datakey_init(handle));

            return(new CryptContext(handle));
        }
Example #4
0
        internal void SetCredentials(ContextSafeHandle context, Status status)
        {
            unsafe
            {
                fixed(byte *p = _dataKeyOptionsBytes)
                {
                    IntPtr ptr = (IntPtr)p;

                    using (PinnedBinary pinned = new PinnedBinary(ptr, (uint)_dataKeyOptionsBytes.Length))
                    {
                        context.Check(status, Library.mongocrypt_ctx_setopt_key_encryption_key(context, pinned.Handle));
                    }
                }
            }
            SetAlternateKeyNames(context, status);
        }
Example #5
0
        internal static void SetAlternateKeyNames(this IKmsKeyId kmsKeyId, ContextSafeHandle context, Status status)
        {
            foreach (var alternateKeyName in kmsKeyId.AlternateKeyNameBsonDocuments)
            {
                unsafe
                {
                    fixed(byte *p = alternateKeyName)
                    {
                        IntPtr ptr = (IntPtr)p;

                        using (PinnedBinary pinned = new PinnedBinary(ptr, (uint)alternateKeyName.Length))
                        {
                            context.Check(status, Library.mongocrypt_ctx_setopt_key_alt_name(context, pinned.Handle));
                        }
                    }
                }
            }
        }
Example #6
0
        // internal methods
        internal void SetAlternateKeyNames(ContextSafeHandle context, Status status)
        {
            foreach (var alternateKeyNameBytes in _alternateKeyNameBytes)
            {
                unsafe
                {
                    fixed(byte *p = alternateKeyNameBytes)
                    {
                        IntPtr ptr = (IntPtr)p;

                        using (PinnedBinary pinned = new PinnedBinary(ptr, (uint)alternateKeyNameBytes.Length))
                        {
                            context.Check(status, Library.mongocrypt_ctx_setopt_key_alt_name(context, pinned.Handle));
                        }
                    }
                }
            }
        }
Example #7
0
        /// <inheritdoc />
        void IInternalKmsKeyId.SetCredentials(ContextSafeHandle context, Status status)
        {
            IntPtr regionPointer = (IntPtr)Marshal.StringToHGlobalAnsi(Region);

            try
            {
                IntPtr customerMasterKeyPointer = (IntPtr)Marshal.StringToHGlobalAnsi(CustomerMasterKey);
                try
                {
                    // Let mongocrypt run strlen
                    context.Check(
                        status,
                        Library.mongocrypt_ctx_setopt_masterkey_aws(context, regionPointer, -1, customerMasterKeyPointer, -1));
                }
                finally
                {
                    Marshal.FreeHGlobal(customerMasterKeyPointer);
                }
            }
            finally
            {
                Marshal.FreeHGlobal(regionPointer);
            }

            if (Endpoint != null)
            {
                IntPtr endPointKeyPointer = (IntPtr)Marshal.StringToHGlobalAnsi(Endpoint);
                try
                {
                    // Let mongocrypt run strlen
                    context.Check(
                        status,
                        Library.mongocrypt_ctx_setopt_masterkey_aws_endpoint(context, endPointKeyPointer, -1));
                }
                finally
                {
                    Marshal.FreeHGlobal(endPointKeyPointer);
                }
            }

            ((IInternalKmsKeyId)this).SetAlternateKeyNames(context, status);
        }
Example #8
0
        /// <summary>
        /// Starts an explicit decryption context.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <returns>A encryption context</returns>
        public CryptContext StartExplicitDecryptionContext(byte[] buffer)
        {
            ContextSafeHandle handle = Library.mongocrypt_ctx_new(_handle);

            unsafe
            {
                fixed(byte *p = buffer)
                {
                    IntPtr ptr = (IntPtr)p;

                    using (PinnedBinary pinned = new PinnedBinary(ptr, (uint)buffer.Length))
                    {
                        // Let mongocrypt run strlen
                        handle.Check(_status, Library.mongocrypt_ctx_explicit_decrypt_init(handle, pinned.Handle));
                    }
                }
            }

            return(new CryptContext(handle));
        }
Example #9
0
        /// <summary>
        /// Starts the decryption context.
        /// </summary>
        /// <param name="buffer">The bson document to decrypt.</param>
        /// <returns>A decryption context</returns>
        public CryptContext StartDecryptionContext(byte[] buffer)
        {
            ContextSafeHandle handle = Library.mongocrypt_ctx_new(_handle);

            GCHandle gch = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            unsafe
            {
                fixed(byte *p = buffer)
                {
                    IntPtr ptr = (IntPtr)p;

                    using (PinnedBinary pinned = new PinnedBinary(ptr, (uint)buffer.Length))
                    {
                        handle.Check(_status, Library.mongocrypt_ctx_decrypt_init(handle, pinned.Handle));
                    }
                }
            }

            return(new CryptContext(handle));
        }
Example #10
0
 /// <inheritdoc />
 void IInternalKmsKeyId.SetAlternateKeyNames(ContextSafeHandle context, Status status)
 {
     this.SetAlternateKeyNames(context, status);
 }
Example #11
0
 /// <inheritdoc />
 void IInternalKmsKeyId.SetCredentials(ContextSafeHandle context, Status status)
 {
     context.Check(status, Library.mongocrypt_ctx_setopt_masterkey_local(context));
     ((IInternalKmsKeyId)this).SetAlternateKeyNames(context, status);
 }
Example #12
0
 internal CryptContext(ContextSafeHandle handle)
 {
     _handle = handle;
     _status = new Status();
 }