Database
 sql >> Base de Dados >  >> RDS >> Database

Usando dados protegidos com um armazenamento de chaves personalizado do Linux


O processo para trabalhar com colunas Always Encrypted protegidas por armazenamento de chaves personalizado do Linux é:
  1. Instale o driver ODBC do SQL Server 1.10.5+ em sua máquina Linux.
  2. Configure uma fonte de dados ODBC em /etc/odbc.ini que se conecta a uma instância do SQL Server 2016+:
    [SQLSERVER_2016]
    Driver=Easysoft ODBC-SQL Server SSL
    Server=machine\sqlserver_instance
    Database=database_with_always_encrypted_data
    User=user # This can be a Windows or SQL Server login.
    Password=password
    Trusted_Connection=Yes # Set this to No for a SQL Server login
    ColumnEncryption=Enabled
  3. Nesta máquina Linux, crie dois novos arquivos:
    $ cd ~/Documents
    $ touch MyKSP.c KspApp.c
  4. Copie e cole o código do exemplo de provedor de armazenamento de chaves em MyKSP.c.
  5. Copie e cole o código do aplicativo ODBC de exemplo em KspApp.c.
  6. Abra MyKSP.c e KspApp.c em um editor de texto. Em ambos os arquivos, substitua esta linha:
    #include "msodbcsql.h"

    com:
    #include <sqlncli.h>
    Nota de 32 bits Para usar uma versão de 32 bits do aplicativo ODBC de amostra, tivemos que alterar o código em KspApp.c:
    1. Adicionamos esta função:
      {
          wchar_t c1, c2;
      
          do {
              c1 = *s1++;
              c2 = *s2++;
              if (c1 == '\0')
                  return c1 - c2;
              }
          while (c1 == c2);
          return c1 - c2;
      }

      imediatamente após esta linha:
      static int safe_wcscmp( wchar_t *s1, wchar_t *s2 )
    2. Substituímos as chamadas para wcscmp com safe_wcscmp .
  7. Compile o código e defina a permissão de execução na biblioteca e no aplicativo resultantes. Por exemplo:
    $ gcc -I/usr/local/easysoft/unixODBC/include -I/usr/local/easysoft/sqlserver/include \
    	                             -fshort-wchar -fPIC -o MyKSP.so -shared MyKSP.c
    $ gcc -I/usr/local/easysoft/unixODBC/include -I/usr/local/easysoft/sqlserver/include \
                                         -fshort-wchar -fPIC -o KspApp -fshort-wchar \
    	                             KspApp.c -lodbc -L/usr/local/easysoft/unixODBC/lib/ \
                	                     -L/usr/lib/x86_64-linux-gnu/libdl.so -ldl
    $ chmod +x MyKSP.so KspApp
  8. Execute o aplicativo (KspApp), que usa o armazenamento de chaves personalizado (MyKSP.so) para criar e preencher uma tabela com colunas AlwaysEncrypted, recupera os dados não criptografados e descarta a tabela:
    $ ./KspApp DSN=SQLSERVER_2016
    Press Enter to continue...
    
    KSP Decrypt() function called (keypath=Retrieved data: c1=1 c2=Sample data 0 for column 2
    Retrieved data: c1=12 c2=Sample data 1 for column 2
    	
  9. Prior to cleaning up the sample data, we used isql to confirm that, for an application that does not have access to the custom key vault, the data is encrypted. We turned off ColumnEncryption for this application, because unless is done the SQL Server ODBC driver will attempt to decrypt the data with a local key store, which will not work:
    /usr/local/easysoft/unixODBC/bin/isql -v -k "DRIVER={Easysoft ODBC-SQL Server SSL};Server=machine\sqlserver_instance;
                                  UID=user;PWD=password;Database=database_with_always_encrypted_data;ColumnEncryption=No"
    SQL> select top 1 c2 from CustomKSPTestTable
    +----+
    | c2 |
    +----+
    | 0104590D628739293CD8D455BD43EC59...