Skip to content

Commit

Permalink
Support for passing encryption key.
Browse files Browse the repository at this point in the history
  • Loading branch information
cincuranet committed Aug 29, 2017
1 parent a27f27e commit 2891bad
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.Net.Sockets;
using System.Text;
using FirebirdSql.Data.Client.Managed.Version11;
using FirebirdSql.Data.Client.Managed.Version13;
using FirebirdSql.Data.Common;

namespace FirebirdSql.Data.Client.Managed
Expand Down Expand Up @@ -369,6 +370,9 @@ public static IResponse ProcessOperation(int operation, XdrStream xdr)
case IscCodes.op_trusted_auth:
return new AuthResponse(xdr.ReadBuffer());

case IscCodes.op_crypt_key_callback:
return new CryptKeyCallbackReponse(xdr.ReadBuffer());

default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public void Dispose()

#region Attach/Detach Methods

public virtual void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database)
public virtual void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
{
try
{
Expand Down Expand Up @@ -208,7 +208,7 @@ protected void AfterAttachActions()
_serverVersion = GetServerVersion();
}

public virtual void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database)
public virtual void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
{
throw new NotSupportedException("Trusted Auth isn't supported on < FB2.1.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public override StatementBase CreateStatement(TransactionBase transaction)
#endregion

#region Trusted Auth
public override void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database)
public override void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Firebird ADO.NET Data provider for .NET and Mono
*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* http://www.firebirdsql.org/index.php?op=doc&id=idpl
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* Copyright (c) 2017 Jiri Cincura ([email protected])
* All Rights Reserved.
*
*/

using System;
using FirebirdSql.Data.Client.Managed;

namespace FirebirdSql.Data.Client.Managed.Version13
{
internal class CryptKeyCallbackReponse : IResponse
{
public byte[] Data { get; }

public CryptKeyCallbackReponse(byte[] data)
{
Data = data;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,36 @@ public GdsDatabase(GdsConnection connection)
: base(connection)
{ }

public override void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
{
try
{
SendAttachToBuffer(dpb, database);
XdrStream.Flush();
var response = ReadResponse();
while (response is CryptKeyCallbackReponse cryptResponse)
{
XdrStream.Write(IscCodes.op_crypt_key_callback);
XdrStream.WriteBuffer(cryptKey);
XdrStream.Flush();
response = ReadResponse();
}
ProcessAttachResponse(response as GenericResponse);
}
catch (IscException)
{
SafelyDetach();
throw;
}
catch (IOException ex)
{
SafelyDetach();
throw IscException.ForErrorCode(IscCodes.isc_net_write_err, ex);
}

AfterAttachActions();
}

protected override void SendAttachToBuffer(DatabaseParameterBuffer dpb, string database)
{
XdrStream.Write(IscCodes.op_attach);
Expand All @@ -63,9 +93,9 @@ protected override void SendCreateToBuffer(DatabaseParameterBuffer dpb, string d
XdrStream.WriteBuffer(dpb.ToArray());
}

public override void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database)
public override void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
{
Attach(dpb, dataSource, port, database);
Attach(dpb, dataSource, port, database, cryptKey);
}

#region Override Statement Creation Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,12 @@ public void CancelEvents(RemoteEvent events)

#region Methods

public void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database)
public void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
{
// ICryptKeyCallbackImpl would have to be passed from C# for 'cryptKey' passing
if (cryptKey?.Length > 0)
throw new NotSupportedException("Passing Encryption Key isn't, yet, supported on Firebird Embedded.");

byte[] databaseBuffer = Encoding2.Default.GetBytes(database);

ClearStatusVector();
Expand All @@ -221,9 +225,9 @@ public void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, str
_serverVersion = GetServerVersion();
}

public void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database)
public void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
{
throw new NotSupportedException("Trusted Auth isn't supported on Embedded Firebird.");
throw new NotSupportedException("Trusted Auth isn't supported on Firebird Embedded.");
}

public void Detach()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ internal interface IDatabase : IDisposable
short Dialect { get; set; }
bool HasRemoteEventSupport { get; }

void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database);
void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database);
void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey);
void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey);
void Detach();

void CreateDatabase(DatabaseParameterBuffer dpb, string dataSource, int port, string database);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void CreateDatabase(DatabaseParameterBuffer dpb)
public void DropDatabase()
{
IDatabase db = ClientFactory.CreateDatabase(_options);
db.Attach(BuildDpb(db, _options), _options.DataSource, _options.Port, _options.Database);
db.Attach(BuildDpb(db, _options), _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
db.DropDatabase();
}

Expand All @@ -159,11 +159,11 @@ public void Connect()

if (string.IsNullOrEmpty(_options.UserID) && string.IsNullOrEmpty(_options.Password))
{
_db.AttachWithTrustedAuth(dpb, _options.DataSource, _options.Port, _options.Database);
_db.AttachWithTrustedAuth(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
}
else
{
_db.Attach(dpb, _options.DataSource, _options.Port, _options.Database);
_db.Attach(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
}
}
catch (IscException ex)
Expand Down
Loading

0 comments on commit 2891bad

Please sign in to comment.