Skip to content
This repository was archived by the owner on Oct 14, 2021. It is now read-only.

Commit 87863ec

Browse files
authored
Fix exception management (#39)
1 parent 77e3a91 commit 87863ec

File tree

7 files changed

+184
-35
lines changed

7 files changed

+184
-35
lines changed

dotnet/dotnetframework/GeneXusCryptography/Mac/Cmac.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ public string calculate(string plainText, string key, string algorithm, int macS
5050
this.error.setError("CM002", "The mac length must be less or equal than the algorithm block size.");
5151
return "";
5252
}
53-
byte[] byteKey = Hex.Decode(key);
53+
byte[] byteKey = SecurityUtils.GetHexa(key, "CM003", this.error);
54+
if (this.HasError())
55+
{
56+
return "";
57+
}
58+
5459
EncodingUtil eu = new EncodingUtil();
5560
byte[] byteInput = eu.getBytes(plainText);
5661

@@ -65,7 +70,14 @@ public string calculate(string plainText, string key, string algorithm, int macS
6570
{
6671
mac = new CMac(blockCipher);
6772
}
68-
mac.Init(parms);
73+
try
74+
{
75+
mac.Init(parms);
76+
}catch(Exception e)
77+
{
78+
this.error.setError("CM004", e.Message);
79+
return "";
80+
}
6981
byte[] resBytes = new byte[mac.GetMacSize()];
7082
mac.BlockUpdate(byteInput, 0, byteInput.Length);
7183
mac.DoFinal(resBytes, 0);

dotnet/dotnetframework/GeneXusCryptography/Mac/Hmac.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ public Hmac() : base()
3131
[SecuritySafeCritical]
3232
public string calculate(string plainText, string password, string algorithm)
3333
{
34-
byte[] pass = Hex.Decode(password);
34+
byte[] pass = SecurityUtils.GetHexa(password, "HS002", this.error);
35+
if (this.HasError())
36+
{
37+
return "";
38+
}
39+
3540
EncodingUtil eu = new EncodingUtil();
3641
byte[] inputBytes = eu.getBytes(plainText);
3742
if (this.HasError())
@@ -46,7 +51,14 @@ public string calculate(string plainText, string password, string algorithm)
4651
}
4752
IDigest digest = hash.createHash(alg);
4853
HMac engine = new HMac(digest);
49-
engine.Init(new KeyParameter(pass));
54+
try
55+
{
56+
engine.Init(new KeyParameter(pass));
57+
}catch(Exception e)
58+
{
59+
this.error.setError("HS003", e.Message);
60+
return "";
61+
}
5062
byte[] resBytes = new byte[engine.GetMacSize()];
5163
engine.BlockUpdate(inputBytes, 0, inputBytes.Length);
5264
engine.DoFinal(resBytes, 0);

dotnet/dotnetframework/GeneXusCryptography/Symmetric/SymmetricBlockCipher.cs

Lines changed: 81 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using SecurityAPICommons.Commons;
1212
using GeneXusCryptography.SymmetricUtils;
1313
using SecurityAPICommons.Config;
14+
using SecurityAPICommons.Utils;
1415

1516
namespace GeneXusCryptography.Symmetric
1617
{
@@ -64,10 +65,24 @@ public string DoAEADEncrypt(string symmetricBlockAlgorithm, string symmetricBloc
6465
{
6566
return "";
6667
}
67-
KeyParameter keyParam = new KeyParameter(Hex.Decode(key));
68-
byte[] nonceBytes = Hex.Decode(nonce);
68+
byte[] nonceBytes = SecurityUtils.GetHexa(nonce, "SB024", this.error);
69+
byte[] keyBytes = SecurityUtils.GetHexa(key, "SB024", this.error);
70+
if(this.HasError())
71+
{
72+
return "";
73+
}
74+
75+
KeyParameter keyParam = new KeyParameter(keyBytes);
76+
6977
AeadParameters AEADparams = new AeadParameters(keyParam, macSize, nonceBytes);
70-
bbc.Init(true, AEADparams);
78+
try
79+
{
80+
bbc.Init(true, AEADparams);
81+
}catch(Exception e)
82+
{
83+
this.error.setError("SB029", e.Message);
84+
return "";
85+
}
7186
EncodingUtil eu = new EncodingUtil();
7287
byte[] inputBytes = eu.getBytes(plainText);
7388
if (eu.GetError().existsError())
@@ -123,11 +138,23 @@ public string DoAEADDecrypt(string symmetricBlockAlgorithm, string symmetricBloc
123138
{
124139
return "";
125140
}
126-
127-
KeyParameter keyParam = new KeyParameter(Hex.Decode(key));
128-
byte[] nonceBytes = Hex.Decode(nonce);
141+
byte[] nonceBytes = SecurityUtils.GetHexa(nonce, "SB025", this.error);
142+
byte[] keyBytes = SecurityUtils.GetHexa(key, "SB025", this.error);
143+
if(this.HasError())
144+
{
145+
return "";
146+
}
147+
KeyParameter keyParam = new KeyParameter(keyBytes);
148+
129149
AeadParameters AEADparams = new AeadParameters(keyParam, macSize, nonceBytes);
130-
bbc.Init(false, AEADparams);
150+
try
151+
{
152+
bbc.Init(false, AEADparams);
153+
}catch(Exception e)
154+
{
155+
this.error.setError("SB030", e.Message);
156+
return "";
157+
}
131158
byte[] out2 = Base64.Decode(encryptedInput);
132159
byte[] comparisonBytes = new byte[bbc.GetOutputSize(out2.Length)];
133160
int length = bbc.ProcessBytes(out2, 0, out2.Length, comparisonBytes, 0);
@@ -176,17 +203,35 @@ public string DoEncrypt(string symmetricBlockAlgorithm, string symmetricBlockMod
176203
{
177204
return "";
178205
}
179-
180-
KeyParameter keyParam = new KeyParameter(Hex.Decode(key));
206+
byte[] byteIV = SecurityUtils.GetHexa(IV, "SB022", this.error);
207+
byte[] byteKey = SecurityUtils.GetHexa(key, "SB022", this.error);
208+
if (this.HasError())
209+
{
210+
return "";
211+
}
212+
KeyParameter keyParam = new KeyParameter(byteKey);
181213

182214
if (SymmetricBlockMode.ECB != mode && SymmetricBlockMode.OPENPGPCFB != mode)
183215
{
184-
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, Hex.Decode(IV));
185-
bbc.Init(true, keyParamWithIV);
216+
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, byteIV);
217+
try{
218+
bbc.Init(true, keyParamWithIV);
219+
}catch(Exception e)
220+
{
221+
this.error.setError("SB025", e.Message);
222+
return "";
223+
}
186224
}
187225
else
188226
{
189-
bbc.Init(true, keyParam);
227+
try
228+
{
229+
bbc.Init(true, keyParam);
230+
}catch(Exception e)
231+
{
232+
this.error.setError("SB026", e.Message);
233+
return "";
234+
}
190235
}
191236

192237
EncodingUtil eu = new EncodingUtil();
@@ -244,16 +289,36 @@ public string DoDecrypt(string symmetricBlockAlgorithm, string symmetricBlockMod
244289
{
245290
return "";
246291
}
292+
byte[] bytesKey = SecurityUtils.GetHexa(key, "SB023", this.error);
293+
byte[] bytesIV = SecurityUtils.GetHexa(IV, "SB023", this.error);
294+
if (this.HasError())
295+
{
296+
return "";
297+
}
247298

248-
KeyParameter keyParam = new KeyParameter(Hex.Decode(key));
299+
KeyParameter keyParam = new KeyParameter(bytesKey);
249300
if (SymmetricBlockMode.ECB != mode && SymmetricBlockMode.OPENPGPCFB != mode)
250301
{
251-
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, Hex.Decode(IV));
252-
bbc.Init(false, keyParamWithIV);
302+
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, bytesIV);
303+
try
304+
{
305+
bbc.Init(false, keyParamWithIV);
306+
}catch(Exception e)
307+
{
308+
this.error.setError("SB027", e.Message);
309+
return "";
310+
}
253311
}
254312
else
255313
{
256-
bbc.Init(false, keyParam);
314+
try
315+
{
316+
bbc.Init(false, keyParam);
317+
}catch(Exception e)
318+
{
319+
this.error.setError("SB028", e.Message);
320+
return "";
321+
}
257322
}
258323

259324
byte[] out2 = Base64.Decode(encryptedInput);

dotnet/dotnetframework/GeneXusCryptography/Symmetric/SymmetricStreamCipher.cs

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System;
1212

1313
using System.Security;
14+
using SecurityAPICommons.Utils;
1415

1516
namespace GeneXusCryptography.Symmetric
1617
{
@@ -56,20 +57,40 @@ public string DoEncrypt(string symmetricStreamAlgorithm, string key, string IV,
5657
{
5758
return "";
5859
}
59-
/* KeyParameter keyParam = new KeyParameter(Hex.Decode(key));
60+
/* KeyParameter keyParam = new KeyParameter(Hex.Decode(key));
6061
engine.Init(true, keyParam);*/
61-
KeyParameter keyParam = new KeyParameter(Hex.Decode(key));
62+
byte[] keyBytes = SecurityUtils.GetHexa(key, "SS007", this.error);
63+
byte[] ivBytes = SecurityUtils.GetHexa(IV, "SS007", this.error);
64+
if (this.HasError())
65+
{
66+
return "";
67+
}
68+
KeyParameter keyParam = new KeyParameter(keyBytes);
6269
if (SymmetricStreamAlgorithmUtils.usesIV(algorithm, this.GetError()))
6370
{
6471
if (!this.GetError().existsError())
6572
{
66-
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, Hex.Decode(IV));
67-
engine.Init(false, keyParamWithIV);
73+
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, ivBytes);
74+
try
75+
{
76+
engine.Init(false, keyParamWithIV);
77+
}catch(Exception e)
78+
{
79+
this.error.setError("SS008", e.Message);
80+
return "";
81+
}
6882
}
6983
}
7084
else
7185
{
72-
engine.Init(false, keyParam);
86+
try
87+
{
88+
engine.Init(false, keyParam);
89+
}catch(Exception e)
90+
{
91+
this.error.setError("SS009", e.Message);
92+
return "";
93+
}
7394
}
7495
EncodingUtil eu = new EncodingUtil();
7596
byte[] input = eu.getBytes(plainText);
@@ -115,20 +136,40 @@ public string DoDecrypt(string symmetricStreamAlgorithm, string key, string IV,
115136
return "";
116137
}
117138

118-
/* KeyParameter keyParam = new KeyParameter(Hex.Decode(key));
139+
/* KeyParameter keyParam = new KeyParameter(Hex.Decode(key));
119140
engine.Init(false, keyParam);*/
120-
KeyParameter keyParam = new KeyParameter(Hex.Decode(key));
141+
byte[] keyBytes = SecurityUtils.GetHexa(key, "SS010", this.error);
142+
byte[] ivBytes = SecurityUtils.GetHexa(IV, "SS010", this.error);
143+
if (this.HasError())
144+
{
145+
return "";
146+
}
147+
KeyParameter keyParam = new KeyParameter(keyBytes);
121148
if (SymmetricStreamAlgorithmUtils.usesIV(algorithm, this.GetError()))
122149
{
123150
if (!this.GetError().existsError())
124151
{
125-
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, Hex.Decode(IV));
126-
engine.Init(false, keyParamWithIV);
152+
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, ivBytes);
153+
try
154+
{
155+
engine.Init(false, keyParamWithIV);
156+
}catch(Exception e)
157+
{
158+
this.error.setError("SS011", e.Message);
159+
return "";
160+
}
127161
}
128162
}
129163
else
130164
{
131-
engine.Init(false, keyParam);
165+
try
166+
{
167+
engine.Init(false, keyParam);
168+
}catch(Exception e)
169+
{
170+
this.error.setError("SS012", e.Message);
171+
return "";
172+
}
132173
}
133174
byte[] input = Base64.Decode(encryptedInput);
134175
byte[] output = new byte[input.Length];

dotnet/dotnetframework/GeneXusJWT/Commons/JWTOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public void SetSecret(string value)
6565
{
6666
secret = Hex.Decode(value);
6767
}
68-
catch (Exception)
68+
catch (Exception e)
6969
{
70-
this.error.setError("OP001", "Hexadecimal value expected");
70+
this.error.setError("OP001", e.Message);
7171
secret = null;
7272
}
7373

dotnet/dotnetframework/SecurityAPICommons/Keys/PrivateKeyManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public bool LoadPKCS12(String privateKeyPath, String alias, String password)
5656
{
5757
loadKeyFromFile(privateKeyPath, alias, password);
5858
}
59-
catch (Exception)
59+
catch (Exception e)
6060
{
61-
61+
this.error.setError("PK018", e.Message);
6262
return false;
6363
}
6464
if (this.HasError())

dotnet/dotnetframework/SecurityAPICommons/Utils/SecurityUtils.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
using Org.BouncyCastle.Utilities.Encoders;
3+
using SecurityAPICommons.Commons;
4+
using System;
25
using System.Security;
36

47
namespace SecurityAPICommons.Utils
@@ -54,5 +57,21 @@ public static string getFileExtension(string path)
5457
}
5558
return path.Substring(lastIndexOf);
5659
}
57-
}
60+
61+
[SecuritySafeCritical]
62+
public static byte[] GetHexa(string hex, string code, Error error)
63+
{
64+
byte[] output;
65+
try
66+
{
67+
output = Hex.Decode(hex);
68+
}
69+
catch (Exception e)
70+
{
71+
error.setError(code, e.Message);
72+
return null;
73+
}
74+
return output;
75+
}
76+
}
5877
}

0 commit comments

Comments
 (0)