11package com .genexus .cryptography .asymmetric ;
22
3- import java .io .ByteArrayInputStream ;
4- import java .io .InputStream ;
5-
6- import org .bouncycastle .crypto .Digest ;
7- import org .bouncycastle .crypto .Signer ;
8- import org .bouncycastle .crypto .params .AsymmetricKeyParameter ;
9- import org .bouncycastle .util .encoders .Base64 ;
10-
113import com .genexus .cryptography .asymmetric .utils .AsymmetricSigningAlgorithm ;
124import com .genexus .cryptography .commons .AsymmetricSignerObject ;
135import com .genexus .cryptography .hash .Hashing ;
1911import com .genexus .securityapicommons .keys .CertificateX509 ;
2012import com .genexus .securityapicommons .keys .PrivateKeyManager ;
2113import com .genexus .securityapicommons .utils .SecurityUtils ;
14+ import org .apache .logging .log4j .LogManager ;
15+ import org .apache .logging .log4j .Logger ;
16+ import org .bouncycastle .crypto .Digest ;
17+ import org .bouncycastle .crypto .Signer ;
18+ import org .bouncycastle .crypto .params .AsymmetricKeyParameter ;
19+ import org .bouncycastle .util .encoders .Base64 ;
20+
21+ import java .io .ByteArrayInputStream ;
22+ import java .io .InputStream ;
2223
2324public class AsymmetricSigner extends AsymmetricSignerObject {
2425
25- /**
26- * AsymmetricSigner class constructor
27- */
26+ private static final Logger logger = LogManager .getLogger (AsymmetricSigner .class );
27+
2828 public AsymmetricSigner () {
2929 super ();
3030 }
@@ -33,170 +33,175 @@ public AsymmetricSigner() {
3333
3434 @ Override
3535 public String doSign (PrivateKeyManager key , String hashAlgorithm , String plainText ) {
36+ logger .debug ("doSign" );
3637 this .error .cleanError ();
37-
38- /******* INPUT VERIFICATION - BEGIN *******/
39- SecurityUtils .validateObjectInput ("key" , key , this .error );
40- SecurityUtils .validateStringInput ("hashAlgorithm" , hashAlgorithm , this .error );
41- SecurityUtils .validateStringInput ("plainText" , plainText , this .error );
38+ // INPUT VERIFICATION - BEGIN
39+ SecurityUtils .validateObjectInput (String .valueOf (AsymmetricSigner .class ), "doSign" , "key" , key , this .error );
40+ SecurityUtils .validateStringInput (String .valueOf (AsymmetricSigner .class ), "doSign" , "hashAlgorithm" , hashAlgorithm , this .error );
41+ SecurityUtils .validateStringInput (String .valueOf (AsymmetricSigner .class ), "doSign" , "plainText" , plainText , this .error );
4242 if (this .hasError ()) {
4343 return "" ;
4444 }
4545 ;
46- /******* INPUT VERIFICATION - END *******/
46+ // INPUT VERIFICATION - END
4747
4848 EncodingUtil eu = new EncodingUtil ();
4949 byte [] inputText = eu .getBytes (plainText );
5050 if (eu .hasError ()) {
5151 this .error = eu .getError ();
5252 return "" ;
5353 }
54- String result = "" ;
54+
5555 try (InputStream inputStream = new ByteArrayInputStream (inputText )) {
56- result = sign (key , hashAlgorithm , inputStream );
56+ return sign (key , hashAlgorithm , inputStream );
5757 } catch (Exception e ) {
5858 error .setError ("AS001" , e .getMessage ());
59+ logger .error ("doSign" , e );
60+ return "" ;
5961 }
60- return result ;
6162 }
6263
6364 @ Override
6465 public String doSignFile (PrivateKeyManager key , String hashAlgorithm , String path ) {
66+ logger .debug ("doSignFile" );
6567 this .error .cleanError ();
66-
67- /******* INPUT VERIFICATION - BEGIN *******/
68- SecurityUtils .validateObjectInput ("key" , key , this .error );
69- SecurityUtils .validateStringInput ("hashAlgorithm" , hashAlgorithm , this .error );
70- SecurityUtils .validateStringInput ("path" , path , this .error );
68+ // INPUT VERIFICATION - BEGIN
69+ SecurityUtils .validateObjectInput (String .valueOf (AsymmetricSigner .class ), "doSignFile" , "key" , key , this .error );
70+ SecurityUtils .validateStringInput (String .valueOf (AsymmetricSigner .class ), "doSignFile" , "hashAlgorithm" , hashAlgorithm , this .error );
71+ SecurityUtils .validateStringInput (String .valueOf (AsymmetricSigner .class ), "doSignFile" , "path" , path , this .error );
7172 if (this .hasError ()) {
7273 return "" ;
7374 }
74- /******* INPUT VERIFICATION - END *******/
75+ // INPUT VERIFICATION - END
7576
76- String result = "" ;
7777 try (InputStream input = SecurityUtils .getFileStream (path , this .error )) {
7878 if (this .hasError ()) {
7979 return "" ;
8080 }
8181
82- result = sign (key , hashAlgorithm , input );
82+ return sign (key , hashAlgorithm , input );
8383 } catch (Exception e ) {
8484 error .setError ("AS002" , e .getMessage ());
85+ logger .error ("doSignFile" , e );
86+ return "" ;
8587 }
86- return result ;
8788 }
8889
8990 @ Override
9091 public boolean doVerify (CertificateX509 cert , String plainText , String signature ) {
92+ logger .debug ("doVerify" );
9193 this .error .cleanError ();
92-
93- /******* INPUT VERIFICATION - BEGIN *******/
94- SecurityUtils .validateObjectInput ("cert" , cert , this .error );
95- SecurityUtils .validateStringInput ("plainText" , plainText , this .error );
96- SecurityUtils .validateStringInput ("signature" , signature , this .error );
94+ // INPUT VERIFICATION - BEGIN
95+ SecurityUtils .validateObjectInput (String .valueOf (AsymmetricSigner .class ), "doVerify" , "cert" , cert , this .error );
96+ SecurityUtils .validateStringInput (String .valueOf (AsymmetricSigner .class ), "doVerify" , "plainText" , plainText , this .error );
97+ SecurityUtils .validateStringInput (String .valueOf (AsymmetricSigner .class ), "doVerify" , "signature" , signature , this .error );
9798 if (this .hasError ()) {
9899 return false ;
99100 }
100- /******* INPUT VERIFICATION - END *******/
101+ // INPUT VERIFICATION - END
101102
102103 EncodingUtil eu = new EncodingUtil ();
103104 byte [] inputText = eu .getBytes (plainText );
104105 if (eu .hasError ()) {
105106 this .error = eu .getError ();
106107 return false ;
107108 }
108- boolean result = false ;
109+
109110 try (InputStream inputStream = new ByteArrayInputStream (inputText )) {
110- result = verify (cert , inputStream , signature , null );
111+ return verify (cert , inputStream , signature , null );
111112 } catch (Exception e ) {
112113 error .setError ("AS003" , e .getMessage ());
114+ logger .error ("doVerify" , e );
115+ return false ;
113116 }
114- return result ;
115117 }
116118
117119 @ Override
118120 public boolean doVerifyWithPublicKey (PublicKey key , String plainText , String signature , String hash ) {
121+ logger .debug ("doVerifyWithPublicKey" );
119122 this .error .cleanError ();
120-
121- /******* INPUT VERIFICATION - BEGIN *******/
122- SecurityUtils .validateObjectInput ("key" , key , this .error );
123- SecurityUtils .validateStringInput ("plainText" , plainText , this .error );
124- SecurityUtils .validateStringInput ("signature" , signature , this .error );
125- SecurityUtils .validateStringInput ("hashAlgorithm" , hash , this .error );
123+ // INPUT VERIFICATION - BEGIN
124+ SecurityUtils .validateObjectInput (String .valueOf (AsymmetricSigner .class ), "doVerifyWithPublicKey" , "key" , key , this .error );
125+ SecurityUtils .validateStringInput (String .valueOf (AsymmetricSigner .class ), "doVerifyWithPublicKey" , "plainText" , plainText , this .error );
126+ SecurityUtils .validateStringInput (String .valueOf (AsymmetricSigner .class ), "doVerifyWithPublicKey" , "signature" , signature , this .error );
127+ SecurityUtils .validateStringInput (String .valueOf (AsymmetricSigner .class ), "doVerifyWithPublicKey" , "hashAlgorithm" , hash , this .error );
126128 if (this .hasError ()) {
127129 return false ;
128130 }
129- /******* INPUT VERIFICATION - END *******/
131+ // INPUT VERIFICATION - END
130132
131133 EncodingUtil eu = new EncodingUtil ();
132134 byte [] inputText = eu .getBytes (plainText );
133135 if (eu .hasError ()) {
134136 this .error = eu .getError ();
135137 return false ;
136138 }
137- boolean result = false ;
139+
138140 try (InputStream inputStream = new ByteArrayInputStream (inputText )) {
139- result = verify (key , inputStream , signature , hash );
141+ return verify (key , inputStream , signature , hash );
140142 } catch (Exception e ) {
141143 error .setError ("AS003" , e .getMessage ());
144+ logger .error ("doVerifyWithPublicKey" , e );
145+ return false ;
142146 }
143- return result ;
144147 }
145148
146149 @ Override
147150 public boolean doVerifyFile (CertificateX509 cert , String path , String signature ) {
151+ logger .debug ("doVerifyFile" );
148152 this .error .cleanError ();
149-
150- /******* INPUT VERIFICATION - BEGIN *******/
151- SecurityUtils .validateObjectInput ("cert" , cert , this .error );
152- SecurityUtils .validateStringInput ("path" , path , this .error );
153- SecurityUtils .validateStringInput ("signature" , signature , this .error );
153+ // INPUT VERIFICATION - BEGIN
154+ SecurityUtils .validateObjectInput (String .valueOf (AsymmetricSigner .class ), "doVerifyFile" , "cert" , cert , this .error );
155+ SecurityUtils .validateStringInput (String .valueOf (AsymmetricSigner .class ), "doVerifyFile" , "path" , path , this .error );
156+ SecurityUtils .validateStringInput (String .valueOf (AsymmetricSigner .class ), "doVerifyFile" , "signature" , signature , this .error );
154157 if (this .hasError ()) {
155158 return false ;
156159 }
157- /******* INPUT VERIFICATION - END *******/
160+ // INPUT VERIFICATION - END
158161
159- boolean result = false ;
160162 try (InputStream input = SecurityUtils .getFileStream (path , this .error )) {
161163 if (this .hasError ()) {
162164 return false ;
163165 }
164- result = verify (cert , input , signature , null );
166+ return verify (cert , input , signature , null );
165167 } catch (Exception e ) {
166168 error .setError ("AS004" , e .getMessage ());
169+ logger .error ("doVerifyFile" , e );
170+ return false ;
167171 }
168- return result ;
169172 }
170173
171174 @ Override
172175 public boolean doVerifyFileWithPublicKey (PublicKey key , String path , String signature , String hash ) {
176+ logger .debug ("doVerifyFileWithPublicKey" );
173177 this .error .cleanError ();
174-
175- /******* INPUT VERIFICATION - BEGIN *******/
176- SecurityUtils .validateObjectInput ("key" , key , this .error );
177- SecurityUtils .validateStringInput ("path" , path , this .error );
178- SecurityUtils .validateStringInput ("signature" , signature , this .error );
179- SecurityUtils .validateStringInput ("hashAlgorithm" , hash , this .error );
178+ // INPUT VERIFICATION - BEGIN
179+ SecurityUtils .validateObjectInput (String .valueOf (AsymmetricSigner .class ), "doVerifyFileWithPublicKey" , "key" , key , this .error );
180+ SecurityUtils .validateStringInput (String .valueOf (AsymmetricSigner .class ), "doVerifyFileWithPublicKey" , "path" , path , this .error );
181+ SecurityUtils .validateStringInput (String .valueOf (AsymmetricSigner .class ), "doVerifyFileWithPublicKey" , "signature" , signature , this .error );
182+ SecurityUtils .validateStringInput (String .valueOf (AsymmetricSigner .class ), "doVerifyFileWithPublicKey" , "hashAlgorithm" , hash , this .error );
180183 if (this .hasError ()) {
181184 return false ;
182185 }
183- /******* INPUT VERIFICATION - END *******/
186+ // INPUT VERIFICATION - END
187+
184188
185- boolean result = false ;
186189 try (InputStream input = SecurityUtils .getFileStream (path , this .error )) {
187190 if (this .hasError ()) {
188191 return false ;
189192 }
190- result = verify (key , input , signature , hash );
193+ return verify (key , input , signature , hash );
191194 } catch (Exception e ) {
192195 error .setError ("AS004" , e .getMessage ());
196+ logger .error ("doVerifyFileWithPublicKey" , e );
197+ return false ;
193198 }
194- return result ;
195199 }
196200
197201 /******** EXTERNAL OBJECT PUBLIC METHODS - END ********/
198202
199203 private String sign (PrivateKey key , String hashAlgorithm , InputStream input ) {
204+ logger .debug ("sign" );
200205 PrivateKeyManager keyMan = (PrivateKeyManager ) key ;
201206 if (keyMan .hasError ()) {
202207 this .error = keyMan .getError ();
@@ -213,24 +218,19 @@ private String sign(PrivateKey key, String hashAlgorithm, InputStream input) {
213218 setUpSigner (signer , input , keyMan .getAsymmetricKeyParameter (), true );
214219 if (this .hasError ())
215220 return "" ;
216- byte [] outputBytes = null ;
221+
217222 try {
218- outputBytes = signer .generateSignature ();
223+ byte [] outputBytes = signer .generateSignature ();
224+ return Base64 .toBase64String (outputBytes );
219225 } catch (Exception e ) {
220226 error .setError ("AS005" , e .getMessage ());
227+ logger .error ("sign" , e );
221228 return "" ;
222229 }
223- String result = "" ;
224- try {
225- result = Base64 .toBase64String (outputBytes );
226- } catch (Exception e ) {
227- error .setError ("AS006" , e .getMessage ());
228- return "" ;
229- }
230- return result ;
231230 }
232231
233232 private boolean verify (Key key , InputStream input , String signature , String hash ) {
233+ logger .debug ("verify" );
234234 PublicKey cert = null ;
235235 boolean isKey = false ;
236236 if (hash == null ) {
@@ -264,40 +264,30 @@ private boolean verify(Key key, InputStream input, String signature, String hash
264264 setUpSigner (signer , input , cert .getAsymmetricKeyParameter (), false );
265265 if (this .hasError ())
266266 return false ;
267- byte [] signatureBytes = null ;
267+
268268 try {
269- signatureBytes = Base64 .decode (signature );
269+ byte [] signatureBytes = Base64 .decode (signature );
270+ return signer .verifySignature (signatureBytes );
270271 } catch (Exception e ) {
271272 error .setError ("AS007" , e .getMessage ());
273+ logger .error ("verify" , e );
272274 return false ;
273275 }
274- boolean result = false ;
275- try {
276- result = signer .verifySignature (signatureBytes );
277- } catch (Exception e ) {
278- error .setError ("AS008" , e .getMessage ());
279- return false ;
280- }
281- return result ;
282-
283276 }
284277
285278 private void setUpSigner (Signer signer , InputStream input , AsymmetricKeyParameter asymmetricKeyParameter ,
286279 boolean toSign ) {
287- try {
288- signer .init (toSign , asymmetricKeyParameter );
289- } catch (Exception e ) {
290- error .setError ("AS009" , e .getMessage ());
291- return ;
292- }
280+ logger .debug ("setUpSigner" );
293281 byte [] buffer = new byte [8192 ];
294282 int n ;
295283 try {
284+ signer .init (toSign , asymmetricKeyParameter );
296285 while ((n = input .read (buffer )) > 0 ) {
297286 signer .update (buffer , 0 , n );
298287 }
299288 } catch (Exception e ) {
300- error .setError ("AS010" , e .getMessage ());
289+ error .setError ("AS009" , e .getMessage ());
290+ logger .error ("setUpSigner" , e );
301291 return ;
302292 }
303293 }
0 commit comments