Skip to content

Commit 3dfa93c

Browse files
committed
Support for encryption in services (DNET-821).
1 parent a2a49c0 commit 3dfa93c

File tree

12 files changed

+191
-42
lines changed

12 files changed

+191
-42
lines changed

Provider/src/FirebirdSql.Data.FirebirdClient.Tests/FbConnectionTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,17 @@ public void UseCompression(bool compression)
359359
public void PassCryptKey()
360360
{
361361
var csb = BuildConnectionStringBuilder(FbServerType, Compression);
362-
csb.CryptKey = Encoding.ASCII.GetBytes("1234567890123456");
363-
using (var conn = new FbConnection(csb.ToString()))
362+
csb.Database = "enc.fdb";
363+
void Test()
364364
{
365-
conn.Open();
365+
using (var conn = new FbConnection(csb.ToString()))
366+
{
367+
conn.Open();
368+
}
366369
}
370+
Assert.Throws<FbException>(Test);
371+
csb.CryptKey = Encoding.ASCII.GetBytes("1234567890123456");
372+
Assert.DoesNotThrow(Test);
367373
}
368374

369375
[Test, Explicit]

Provider/src/FirebirdSql.Data.FirebirdClient.Tests/FbServicesTests.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using System.IO;
2121
using System.Linq;
2222
using System.Reflection;
23+
using System.Text;
2324
using System.Text.RegularExpressions;
2425
using System.Threading;
2526
using FirebirdSql.Data.Services;
@@ -395,11 +396,27 @@ public void TraceTest()
395396
Assert.AreNotEqual(-1, sessionId);
396397
}
397398

399+
[Test, Explicit]
400+
public void StatisticsWithEncryptedTest()
401+
{
402+
var csb = BuildServicesConnectionStringBuilder(FbServerType, Compression, true);
403+
csb.Database = "enc.fdb";
404+
void Test()
405+
{
406+
var statisticalSvc = new FbStatistical(csb.ToString());
407+
statisticalSvc.ServiceOutput += ServiceOutput;
408+
statisticalSvc.Execute();
409+
}
410+
Assert.Throws<FbException>(Test);
411+
csb.CryptKey = Encoding.ASCII.GetBytes("1234567890123456");
412+
Assert.DoesNotThrow(Test);
413+
}
414+
398415
#endregion
399416

400417
#region Methods
401418

402-
void ServiceOutput(object sender, ServiceOutputEventArgs e)
419+
static void ServiceOutput(object sender, ServiceOutputEventArgs e)
403420
{
404421
var dummy = e.Message;
405422
}

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

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222
namespace FirebirdSql.Data.Client.Managed.Version10
2323
{
24-
internal sealed class GdsServiceManager : IServiceManager
24+
internal class GdsServiceManager : IServiceManager
2525
{
2626
#region Fields
2727

2828
private int _handle;
29-
private GdsDatabase _database;
3029
private GdsConnection _connection;
30+
private GdsDatabase _database;
3131

3232
#endregion
3333

@@ -43,6 +43,11 @@ public byte[] AuthData
4343
get { return _connection.AuthData; }
4444
}
4545

46+
public GdsDatabase Database
47+
{
48+
get { return _database; }
49+
}
50+
4651
#endregion
4752

4853
#region Constructors
@@ -57,22 +62,13 @@ public GdsServiceManager(GdsConnection connection)
5762

5863
#region Methods
5964

60-
public void Attach(ServiceParameterBuffer spb, string dataSource, int port, string service)
65+
public virtual void Attach(ServiceParameterBuffer spb, string dataSource, int port, string service, byte[] cryptKey)
6166
{
62-
GenericResponse response = null;
63-
6467
try
6568
{
66-
#warning Separate method for op_service_attach as for i.e. op_attach
67-
_database.XdrStream.Write(IscCodes.op_service_attach);
68-
_database.XdrStream.Write(0);
69-
_database.XdrStream.Write(service);
70-
_database.XdrStream.WriteBuffer(spb.ToArray());
69+
SendAttachToBuffer(spb, service);
7170
_database.XdrStream.Flush();
72-
73-
response = _database.ReadGenericResponse();
74-
75-
_handle = response.ObjectHandle;
71+
ProcessAttachResponse(_database.ReadGenericResponse());
7672
}
7773
catch (IOException ex)
7874
{
@@ -81,7 +77,20 @@ public void Attach(ServiceParameterBuffer spb, string dataSource, int port, stri
8177
}
8278
}
8379

84-
public void Detach()
80+
protected virtual void SendAttachToBuffer(ServiceParameterBuffer spb, string service)
81+
{
82+
_database.XdrStream.Write(IscCodes.op_service_attach);
83+
_database.XdrStream.Write(0);
84+
_database.XdrStream.Write(service);
85+
_database.XdrStream.WriteBuffer(spb.ToArray());
86+
}
87+
88+
protected virtual void ProcessAttachResponse(GenericResponse response)
89+
{
90+
_handle = response.ObjectHandle;
91+
}
92+
93+
public virtual void Detach()
8594
{
8695
try
8796
{
@@ -114,7 +123,7 @@ public void Detach()
114123
}
115124
}
116125

117-
public void Start(ServiceParameterBuffer spb)
126+
public virtual void Start(ServiceParameterBuffer spb)
118127
{
119128
try
120129
{
@@ -139,7 +148,7 @@ public void Start(ServiceParameterBuffer spb)
139148
}
140149
}
141150

142-
public void Query(ServiceParameterBuffer spb, int requestLength, byte[] requestBuffer, int bufferLength, byte[] buffer)
151+
public virtual void Query(ServiceParameterBuffer spb, int requestLength, byte[] requestBuffer, int bufferLength, byte[] buffer)
143152
{
144153
try
145154
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* The contents of this file are subject to the Initial
3+
* Developer's Public License Version 1.0 (the "License");
4+
* you may not use this file except in compliance with the
5+
* License. You may obtain a copy of the License at
6+
* https://github.com/FirebirdSQL/NETProvider/blob/master/license.txt.
7+
*
8+
* Software distributed under the License is distributed on
9+
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
10+
* express or implied. See the License for the specific
11+
* language governing rights and limitations under the License.
12+
*
13+
* All Rights Reserved.
14+
*/
15+
16+
//$Authors = Carlos Guzman Alvarez, Jiri Cincura (jiri@cincura.net)
17+
18+
namespace FirebirdSql.Data.Client.Managed.Version11
19+
{
20+
internal class GdsServiceManager : Version10.GdsServiceManager
21+
{
22+
public GdsServiceManager(GdsConnection connection)
23+
: base(connection)
24+
{ }
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* The contents of this file are subject to the Initial
3+
* Developer's Public License Version 1.0 (the "License");
4+
* you may not use this file except in compliance with the
5+
* License. You may obtain a copy of the License at
6+
* https://github.com/FirebirdSQL/NETProvider/blob/master/license.txt.
7+
*
8+
* Software distributed under the License is distributed on
9+
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
10+
* express or implied. See the License for the specific
11+
* language governing rights and limitations under the License.
12+
*
13+
* All Rights Reserved.
14+
*/
15+
16+
//$Authors = Carlos Guzman Alvarez, Jiri Cincura (jiri@cincura.net)
17+
18+
namespace FirebirdSql.Data.Client.Managed.Version12
19+
{
20+
internal class GdsServiceManager : Version11.GdsServiceManager
21+
{
22+
public GdsServiceManager(GdsConnection connection)
23+
: base(connection)
24+
{ }
25+
}
26+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public override void Attach(DatabaseParameterBuffer dpb, string dataSource, int
4242
SendAttachToBuffer(dpb, database);
4343
XdrStream.Flush();
4444
var response = ReadResponse();
45+
#warning Unification
4546
while (response is CryptKeyCallbackReponse cryptResponse)
4647
{
4748
XdrStream.Write(IscCodes.op_crypt_key_callback);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* The contents of this file are subject to the Initial
3+
* Developer's Public License Version 1.0 (the "License");
4+
* you may not use this file except in compliance with the
5+
* License. You may obtain a copy of the License at
6+
* https://github.com/FirebirdSQL/NETProvider/blob/master/license.txt.
7+
*
8+
* Software distributed under the License is distributed on
9+
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
10+
* express or implied. See the License for the specific
11+
* language governing rights and limitations under the License.
12+
*
13+
* All Rights Reserved.
14+
*/
15+
16+
//$Authors = Carlos Guzman Alvarez, Jiri Cincura (jiri@cincura.net)
17+
18+
using System.IO;
19+
using FirebirdSql.Data.Common;
20+
21+
namespace FirebirdSql.Data.Client.Managed.Version13
22+
{
23+
internal class GdsServiceManager : Version12.GdsServiceManager
24+
{
25+
public GdsServiceManager(GdsConnection connection)
26+
: base(connection)
27+
{ }
28+
29+
public override void Attach(ServiceParameterBuffer spb, string dataSource, int port, string service, byte[] cryptKey)
30+
{
31+
try
32+
{
33+
SendAttachToBuffer(spb, service);
34+
Database.XdrStream.Flush();
35+
var response = Database.ReadResponse();
36+
while (response is CryptKeyCallbackReponse cryptResponse)
37+
{
38+
Database.XdrStream.Write(IscCodes.op_crypt_key_callback);
39+
Database.XdrStream.WriteBuffer(cryptKey);
40+
Database.XdrStream.Flush();
41+
response = Database.ReadResponse();
42+
}
43+
ProcessAttachResponse(response as GenericResponse);
44+
}
45+
catch (IOException ex)
46+
{
47+
Database.Detach();
48+
throw IscException.ForErrorCode(IscCodes.isc_net_write_err, ex);
49+
}
50+
}
51+
}
52+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ public FesServiceManager(string dllName, Charset charset)
6060

6161
#region Methods
6262

63-
public void Attach(ServiceParameterBuffer spb, string dataSource, int port, string service)
63+
public void Attach(ServiceParameterBuffer spb, string dataSource, int port, string service, byte[] cryptKey)
6464
{
65+
// ICryptKeyCallbackImpl would have to be passed from C# for 'cryptKey' passing
66+
if (cryptKey?.Length > 0)
67+
throw new NotSupportedException("Passing Encryption Key isn't, yet, supported on Firebird Embedded.");
68+
6569
ClearStatusVector();
6670

6771
int svcHandle = Handle;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal interface IServiceManager
2323
{
2424
int Handle { get; }
2525

26-
void Attach(ServiceParameterBuffer spb, string dataSource, int port, string service);
26+
void Attach(ServiceParameterBuffer spb, string dataSource, int port, string service, byte[] cryptKey);
2727
void Detach();
2828
void Start(ServiceParameterBuffer spb);
2929
void Query(ServiceParameterBuffer spb, int requestLength, byte[] requestBuffer, int bufferLength, byte[] buffer);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ private static IServiceManager CreateManagedServiceManager(FbConnectionString op
8383
switch (connection.ProtocolVersion)
8484
{
8585
case IscCodes.PROTOCOL_VERSION13:
86+
return new Client.Managed.Version13.GdsServiceManager(connection);
8687
case IscCodes.PROTOCOL_VERSION12:
88+
return new Client.Managed.Version12.GdsServiceManager(connection);
8789
case IscCodes.PROTOCOL_VERSION11:
90+
return new Client.Managed.Version11.GdsServiceManager(connection);
8891
case IscCodes.PROTOCOL_VERSION10:
8992
return new Client.Managed.Version10.GdsServiceManager(connection);
9093
default:

0 commit comments

Comments
 (0)