Skip to content

Commit 2891bad

Browse files
committed
Support for passing encryption key.
1 parent a27f27e commit 2891bad

File tree

12 files changed

+238
-187
lines changed

12 files changed

+238
-187
lines changed

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/GdsConnection.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using System.Net.Sockets;
2525
using System.Text;
2626
using FirebirdSql.Data.Client.Managed.Version11;
27+
using FirebirdSql.Data.Client.Managed.Version13;
2728
using FirebirdSql.Data.Common;
2829

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

373+
case IscCodes.op_crypt_key_callback:
374+
return new CryptKeyCallbackReponse(xdr.ReadBuffer());
375+
372376
default:
373377
return null;
374378
}

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version10/GdsDatabase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void Dispose()
164164

165165
#region Attach/Detach Methods
166166

167-
public virtual void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database)
167+
public virtual void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
168168
{
169169
try
170170
{
@@ -208,7 +208,7 @@ protected void AfterAttachActions()
208208
_serverVersion = GetServerVersion();
209209
}
210210

211-
public virtual void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database)
211+
public virtual void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
212212
{
213213
throw new NotSupportedException("Trusted Auth isn't supported on < FB2.1.");
214214
}

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version11/GdsDatabase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public override StatementBase CreateStatement(TransactionBase transaction)
6565
#endregion
6666

6767
#region Trusted Auth
68-
public override void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database)
68+
public override void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
6969
{
7070
try
7171
{
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Firebird ADO.NET Data provider for .NET and Mono
3+
*
4+
* The contents of this file are subject to the Initial
5+
* Developer's Public License Version 1.0 (the "License");
6+
* you may not use this file except in compliance with the
7+
* License. You may obtain a copy of the License at
8+
* http://www.firebirdsql.org/index.php?op=doc&id=idpl
9+
*
10+
* Software distributed under the License is distributed on
11+
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
12+
* express or implied. See the License for the specific
13+
* language governing rights and limitations under the License.
14+
*
15+
* Copyright (c) 2017 Jiri Cincura ([email protected])
16+
* All Rights Reserved.
17+
*
18+
*/
19+
20+
using System;
21+
using FirebirdSql.Data.Client.Managed;
22+
23+
namespace FirebirdSql.Data.Client.Managed.Version13
24+
{
25+
internal class CryptKeyCallbackReponse : IResponse
26+
{
27+
public byte[] Data { get; }
28+
29+
public CryptKeyCallbackReponse(byte[] data)
30+
{
31+
Data = data;
32+
}
33+
}
34+
}

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version13/GdsDatabase.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,36 @@ public GdsDatabase(GdsConnection connection)
3737
: base(connection)
3838
{ }
3939

40+
public override void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
41+
{
42+
try
43+
{
44+
SendAttachToBuffer(dpb, database);
45+
XdrStream.Flush();
46+
var response = ReadResponse();
47+
while (response is CryptKeyCallbackReponse cryptResponse)
48+
{
49+
XdrStream.Write(IscCodes.op_crypt_key_callback);
50+
XdrStream.WriteBuffer(cryptKey);
51+
XdrStream.Flush();
52+
response = ReadResponse();
53+
}
54+
ProcessAttachResponse(response as GenericResponse);
55+
}
56+
catch (IscException)
57+
{
58+
SafelyDetach();
59+
throw;
60+
}
61+
catch (IOException ex)
62+
{
63+
SafelyDetach();
64+
throw IscException.ForErrorCode(IscCodes.isc_net_write_err, ex);
65+
}
66+
67+
AfterAttachActions();
68+
}
69+
4070
protected override void SendAttachToBuffer(DatabaseParameterBuffer dpb, string database)
4171
{
4272
XdrStream.Write(IscCodes.op_attach);
@@ -63,9 +93,9 @@ protected override void SendCreateToBuffer(DatabaseParameterBuffer dpb, string d
6393
XdrStream.WriteBuffer(dpb.ToArray());
6494
}
6595

66-
public override void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database)
96+
public override void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
6797
{
68-
Attach(dpb, dataSource, port, database);
98+
Attach(dpb, dataSource, port, database, cryptKey);
6999
}
70100

71101
#region Override Statement Creation Methods

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Native/FesDatabase.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,12 @@ public void CancelEvents(RemoteEvent events)
202202

203203
#region Methods
204204

205-
public void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database)
205+
public void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
206206
{
207+
// ICryptKeyCallbackImpl would have to be passed from C# for 'cryptKey' passing
208+
if (cryptKey?.Length > 0)
209+
throw new NotSupportedException("Passing Encryption Key isn't, yet, supported on Firebird Embedded.");
210+
207211
byte[] databaseBuffer = Encoding2.Default.GetBytes(database);
208212

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

224-
public void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database)
228+
public void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey)
225229
{
226-
throw new NotSupportedException("Trusted Auth isn't supported on Embedded Firebird.");
230+
throw new NotSupportedException("Trusted Auth isn't supported on Firebird Embedded.");
227231
}
228232

229233
public void Detach()

Provider/src/FirebirdSql.Data.FirebirdClient/Common/IDatabase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ internal interface IDatabase : IDisposable
3737
short Dialect { get; set; }
3838
bool HasRemoteEventSupport { get; }
3939

40-
void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database);
41-
void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database);
40+
void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey);
41+
void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey);
4242
void Detach();
4343

4444
void CreateDatabase(DatabaseParameterBuffer dpb, string dataSource, int port, string database);

Provider/src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbConnectionInternal.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void CreateDatabase(DatabaseParameterBuffer dpb)
133133
public void DropDatabase()
134134
{
135135
IDatabase db = ClientFactory.CreateDatabase(_options);
136-
db.Attach(BuildDpb(db, _options), _options.DataSource, _options.Port, _options.Database);
136+
db.Attach(BuildDpb(db, _options), _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
137137
db.DropDatabase();
138138
}
139139

@@ -159,11 +159,11 @@ public void Connect()
159159

160160
if (string.IsNullOrEmpty(_options.UserID) && string.IsNullOrEmpty(_options.Password))
161161
{
162-
_db.AttachWithTrustedAuth(dpb, _options.DataSource, _options.Port, _options.Database);
162+
_db.AttachWithTrustedAuth(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
163163
}
164164
else
165165
{
166-
_db.Attach(dpb, _options.DataSource, _options.Port, _options.Database);
166+
_db.Attach(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
167167
}
168168
}
169169
catch (IscException ex)

0 commit comments

Comments
 (0)