Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.

Commit 62add05

Browse files
authored
Public key (#97)
* Add public key data type * Update jar versions to 18.2.0 * Update jackson libraries to version 2.14.1 * Update commons-net to version 3.9.0 * Add remove method on sftpclient * Fix error codes * Fix generic * GeneXusFtps client remove function implementation * Update version for v118u3 to 18.3.0
1 parent 4cd6e69 commit 62add05

File tree

17 files changed

+1009
-575
lines changed

17 files changed

+1009
-575
lines changed

GeneXusCryptography/src/main/java/com/genexus/cryptography/asymmetric/AsymmetricCipher.java

Lines changed: 94 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.UnsupportedEncodingException;
44

5+
import org.bouncycastle.cert.X509CertificateHolder;
56
import org.bouncycastle.crypto.AsymmetricBlockCipher;
67
import org.bouncycastle.crypto.BufferedAsymmetricBlockCipher;
78
import org.bouncycastle.crypto.Digest;
@@ -18,10 +19,13 @@
1819
import com.genexus.cryptography.commons.AsymmetricCipherObject;
1920
import com.genexus.cryptography.hash.Hashing;
2021
import com.genexus.cryptography.hash.utils.HashAlgorithm;
22+
import com.genexus.securityapicommons.commons.Certificate;
2123
import com.genexus.securityapicommons.commons.Key;
24+
import com.genexus.securityapicommons.commons.PublicKey;
2225
import com.genexus.securityapicommons.config.EncodingUtil;
2326
import com.genexus.securityapicommons.keys.CertificateX509;
2427
import com.genexus.securityapicommons.keys.PrivateKeyManager;
28+
import com.genexus.securityapicommons.utils.SecurityUtils;
2529

2630
/**
2731
* @author sgrampone
@@ -41,37 +45,106 @@ public AsymmetricCipher() {
4145
@Override
4246
public String doEncrypt_WithPrivateKey(String hashAlgorithm, String asymmetricEncryptionPadding, PrivateKeyManager key, String plainText) {
4347

48+
this.error.cleanError();
49+
/******* INPUT VERIFICATION - BEGIN *******/
50+
SecurityUtils.validateObjectInput("hashAlgorithm", hashAlgorithm, this.error);
51+
SecurityUtils.validateStringInput("asymmetricEncryptionPadding", asymmetricEncryptionPadding, this.error);
52+
SecurityUtils.validateStringInput("plainText", plainText, this.error);
53+
SecurityUtils.validateObjectInput("key", key, this.error);
4454
if (this.hasError()) {
4555
return "";
4656
}
47-
return doEncryptInternal(hashAlgorithm, asymmetricEncryptionPadding, key, true, plainText);
57+
58+
/******* INPUT VERIFICATION - END *******/
59+
60+
return doEncryptInternal(hashAlgorithm, asymmetricEncryptionPadding, key, true, plainText, false);
4861
}
4962

5063
@Override
51-
public String doEncrypt_WithPublicKey(String hashAlgorithm, String asymmetricEncryptionPadding, CertificateX509 certificate, String plainText) {
64+
public String doEncrypt_WithPublicKey(String hashAlgorithm, String asymmetricEncryptionPadding, PublicKey key, String plainText) {
65+
66+
this.error.cleanError();
67+
/******* INPUT VERIFICATION - BEGIN *******/
68+
SecurityUtils.validateObjectInput("hashAlgorithm", hashAlgorithm, this.error);
69+
SecurityUtils.validateStringInput("asymmetricEncryptionPadding", asymmetricEncryptionPadding, this.error);
70+
SecurityUtils.validateStringInput("plainText", plainText, this.error);
71+
SecurityUtils.validateObjectInput("key", key, this.error);
72+
if (this.hasError()) {
73+
return "";
74+
}
75+
76+
/******* INPUT VERIFICATION - END *******/
5277

78+
return doEncryptInternal(hashAlgorithm, asymmetricEncryptionPadding, key, false, plainText, true);
79+
}
80+
81+
@Override
82+
public String doEncrypt_WithCertificate(String hashAlgorithm, String asymmetricEncryptionPadding, Certificate certificate, String plainText) {
83+
84+
/******* INPUT VERIFICATION - BEGIN *******/
85+
SecurityUtils.validateObjectInput("hashAlgorithm", hashAlgorithm, this.error);
86+
SecurityUtils.validateStringInput("asymmetricEncryptionPadding", asymmetricEncryptionPadding, this.error);
87+
SecurityUtils.validateStringInput("plainText", plainText, this.error);
88+
SecurityUtils.validateObjectInput("certificate", certificate, this.error);
5389
if (this.hasError()) {
5490
return "";
5591
}
56-
return doEncryptInternal(hashAlgorithm, asymmetricEncryptionPadding, certificate, false, plainText);
92+
93+
/******* INPUT VERIFICATION - END *******/
94+
95+
return doEncryptInternal(hashAlgorithm, asymmetricEncryptionPadding, certificate, false, plainText, false);
5796
}
97+
5898

5999
@Override
60100
public String doDecrypt_WithPrivateKey(String hashAlgorithm, String asymmetricEncryptionPadding, PrivateKeyManager key, String encryptedInput) {
61-
101+
102+
/******* INPUT VERIFICATION - BEGIN *******/
103+
SecurityUtils.validateObjectInput("hashAlgorithm", hashAlgorithm, this.error);
104+
SecurityUtils.validateStringInput("asymmetricEncryptionPadding", asymmetricEncryptionPadding, this.error);
105+
SecurityUtils.validateStringInput("encryptedInput", encryptedInput, this.error);
106+
SecurityUtils.validateObjectInput("key", key, this.error);
62107
if (this.hasError()) {
63108
return "";
64109
}
65-
return doDecryptInternal(hashAlgorithm, asymmetricEncryptionPadding, key, true, encryptedInput);
110+
111+
/******* INPUT VERIFICATION - END *******/
112+
113+
return doDecryptInternal(hashAlgorithm, asymmetricEncryptionPadding, key, true, encryptedInput, false);
66114
}
67115

68116
@Override
69-
public String doDecrypt_WithPublicKey(String hashAlgorithm, String asymmetricEncryptionPadding, CertificateX509 certificate, String encryptedInput) {
117+
public String doDecrypt_WithPublicKey(String hashAlgorithm, String asymmetricEncryptionPadding, PublicKey key, String encryptedInput) {
118+
119+
/******* INPUT VERIFICATION - BEGIN *******/
120+
SecurityUtils.validateObjectInput("hashAlgorithm", hashAlgorithm, this.error);
121+
SecurityUtils.validateStringInput("asymmetricEncryptionPadding", asymmetricEncryptionPadding, this.error);
122+
SecurityUtils.validateStringInput("encryptedInput", encryptedInput, this.error);
123+
SecurityUtils.validateObjectInput("key", key, this.error);
124+
if (this.hasError()) {
125+
return "";
126+
}
127+
128+
/******* INPUT VERIFICATION - END *******/
70129

130+
return doDecryptInternal(hashAlgorithm, asymmetricEncryptionPadding, key, false, encryptedInput, true);
131+
}
132+
133+
@Override
134+
public String doDecrypt_WithCertificate(String hashAlgorithm, String asymmetricEncryptionPadding, Certificate certificate, String encryptedInput) {
135+
136+
/******* INPUT VERIFICATION - BEGIN *******/
137+
SecurityUtils.validateObjectInput("hashAlgorithm", hashAlgorithm, this.error);
138+
SecurityUtils.validateStringInput("asymmetricEncryptionPadding", asymmetricEncryptionPadding, this.error);
139+
SecurityUtils.validateStringInput("encryptedInput", encryptedInput, this.error);
140+
SecurityUtils.validateObjectInput("certificate", certificate, this.error);
71141
if (this.hasError()) {
72142
return "";
73143
}
74-
return doDecryptInternal(hashAlgorithm, asymmetricEncryptionPadding, certificate, false, encryptedInput);
144+
145+
/******* INPUT VERIFICATION - END *******/
146+
147+
return doDecryptInternal(hashAlgorithm, asymmetricEncryptionPadding, certificate, false, encryptedInput, false);
75148
}
76149

77150
/******** EXTERNAL OBJECT PUBLIC METHODS - END ********/
@@ -96,9 +169,8 @@ public String doDecrypt_WithPublicKey(String hashAlgorithm, String asymmetricEnc
96169
* @return String Base64 encrypted plainText text
97170
*/
98171
private String doEncryptInternal(String hashAlgorithm, String asymmetricEncryptionPadding, Key key, boolean isPrivate,
99-
String plainText) {
172+
String plainText, boolean isPublicKey) {
100173
error.cleanError();
101-
102174
HashAlgorithm hash = HashAlgorithm.getHashAlgorithm(hashAlgorithm, this.error);
103175
AsymmetricEncryptionPadding padding = AsymmetricEncryptionPadding
104176
.getAsymmetricEncryptionPadding(asymmetricEncryptionPadding, this.error);
@@ -114,21 +186,21 @@ private String doEncryptInternal(String hashAlgorithm, String asymmetricEncrypti
114186
this.error = keyMan.getError();
115187
return "";
116188
}
117-
asymmetricEncryptionAlgorithm = keyMan.getPrivateKeyAlgorithm();
189+
asymmetricEncryptionAlgorithm = keyMan.getAlgorithm();
118190

119-
asymKey = keyMan.getPrivateKeyParameterForEncryption();
191+
asymKey = keyMan.getAsymmetricKeyParameter();
120192
if (keyMan.hasError()) {
121193
this.error = keyMan.getError();
122194
return "";
123195
}
124196
} else {
125-
CertificateX509 cert = (CertificateX509) key;
126-
if (!cert.Inicialized() || cert.hasError()) {
197+
PublicKey cert = isPublicKey ? (PublicKey)key: (CertificateX509) key;
198+
if (cert.hasError()) {
127199
this.error = cert.getError();
128200
return "";
129201
}
130-
asymmetricEncryptionAlgorithm = cert.getPublicKeyAlgorithm();
131-
asymKey = cert.getPublicKeyParameterForEncryption();
202+
asymmetricEncryptionAlgorithm = cert.getAlgorithm();
203+
asymKey = cert.getAsymmetricKeyParameter();
132204
if (cert.hasError()) {
133205
this.error = cert.getError();
134206
return "";
@@ -167,7 +239,7 @@ private String doEncryptInternal(String hashAlgorithm, String asymmetricEncrypti
167239
* @return String UTF-8 decypted encryptedInput text
168240
*/
169241
private String doDecryptInternal(String hashAlgorithm, String asymmetricEncryptionPadding, Key key, boolean isPrivate,
170-
String encryptedInput) {
242+
String encryptedInput, boolean isPublicKey) {
171243
this.error.cleanError();
172244
HashAlgorithm hash = HashAlgorithm.getHashAlgorithm(hashAlgorithm, this.error);
173245
AsymmetricEncryptionPadding padding = AsymmetricEncryptionPadding
@@ -185,21 +257,21 @@ private String doDecryptInternal(String hashAlgorithm, String asymmetricEncrypti
185257
this.error = keyMan.getError();
186258
return "";
187259
}
188-
asymmetricEncryptionAlgorithm = keyMan.getPrivateKeyAlgorithm();
260+
asymmetricEncryptionAlgorithm = keyMan.getAlgorithm();
189261

190-
asymKey = keyMan.getPrivateKeyParameterForEncryption();
262+
asymKey = keyMan.getAsymmetricKeyParameter();
191263
if (keyMan.hasError()) {
192264
this.error = keyMan.getError();
193265
return "";
194266
}
195267
} else {
196-
CertificateX509 cert = (CertificateX509) key;
197-
if (!cert.Inicialized() || cert.hasError()) {
268+
PublicKey cert = isPublicKey ? (PublicKey) key: (CertificateX509) key;
269+
if (cert.hasError()) {
198270
this.error = cert.getError();
199271
return "";
200272
}
201-
asymmetricEncryptionAlgorithm = cert.getPublicKeyAlgorithm();
202-
asymKey = cert.getPublicKeyParameterForEncryption();
273+
asymmetricEncryptionAlgorithm = cert.getAlgorithm();
274+
asymKey = cert.getAsymmetricKeyParameter();
203275
if (cert.hasError()) {
204276
this.error = cert.getError();
205277
return "";

0 commit comments

Comments
 (0)