Skip to content

Commit 964797f

Browse files
iroquetaBeta Bot
authored andcommitted
Cherry pick branch 'genexuslabs:removeKeysFromAndroid' into beta
1 parent ef5e9ff commit 964797f

File tree

3 files changed

+93
-235
lines changed

3 files changed

+93
-235
lines changed

android/src/main/java/com/genexus/internet/HttpContext.java

Lines changed: 2 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,22 @@
1111

1212
import com.artech.base.services.AndroidContext;
1313
import com.genexus.util.Codecs;
14-
import com.genexus.util.Encryption;
1514

1615
import org.json.JSONArray;
1716
import org.json.JSONException;
1817
import org.json.JSONObject;
1918

2019
public abstract class HttpContext extends HttpAjaxContext implements IHttpContext
2120
{
22-
private static String GX_AJAX_REQUEST_HEADER = "GxAjaxRequest";
23-
2421
protected boolean PortletMode = false;
25-
protected boolean AjaxCallMode = false;
26-
protected boolean AjaxEventMode = false;
27-
protected boolean FullAjaxMode = false;
2822
public boolean drawingGrid = false;
2923

3024
public void setPortletMode()
3125
{ PortletMode = true; }
3226

33-
public void setAjaxCallMode()
34-
{ AjaxCallMode = true; }
35-
36-
public void setFullAjaxMode()
37-
{ FullAjaxMode = true; }
38-
39-
public void setAjaxEventMode()
40-
{ AjaxEventMode = true; }
41-
4227
public boolean isPortletMode()
4328
{ return PortletMode; }
4429

45-
public boolean isAjaxCallMode()
46-
{ return AjaxCallMode; }
47-
48-
public boolean isAjaxEventMode()
49-
{ return AjaxEventMode; }
50-
51-
public boolean isFullAjaxMode()
52-
{ return FullAjaxMode; }
53-
54-
public boolean isAjaxRequest()
55-
{ return isAjaxCallMode() || isAjaxEventMode() || isPortletMode() || isFullAjaxMode(); }
56-
57-
5830
public byte wbGlbDoneStart = 0;
5931
//nSOAPErr
6032
public HttpResponse GX_webresponse;
@@ -385,7 +357,7 @@ public void windowClosed()
385357

386358
public void pushCurrentUrl()
387359
{
388-
if (getRequestMethod().equals("GET") && !isAjaxRequest())
360+
if (getRequestMethod().equals("GET"))
389361
{
390362
String sUrl = getRequestNavUrl().trim();
391363
String topUrl = getNavigationHelper().peekUrl(sUrl);
@@ -409,106 +381,9 @@ public void printReportAtClient(String reportFile, String printerRule)
409381
{
410382
addPrintReportCommand(getResource(reportFile), printerRule);
411383
}
412-
413-
public boolean isGxAjaxRequest()
414-
{
415-
if (this.isMultipartContent())
416-
{
417-
return true;
418-
}
419-
// String gxHeader = getRequest().getHeader(GX_AJAX_REQUEST_HEADER);
420-
// if (gxHeader != null && gxHeader.trim().length() > 0)
421-
// {
422-
// return true;
423-
// }
424-
return false;
425-
}
426-
427-
private String getAjaxEncryptionKey()
428-
{
429-
if(getSessionValue(Encryption.AJAX_ENCRYPTION_KEY) == null)
430-
{
431-
if (!recoverEncryptionKey())
432-
{
433-
webPutSessionValue(Encryption.AJAX_ENCRYPTION_KEY, Encryption.getRijndaelKey());
434-
}
435-
}
436-
return (String)getSessionValue(Encryption.AJAX_ENCRYPTION_KEY);
437-
}
438-
439-
private boolean recoverEncryptionKey()
440-
{
441-
if (getSessionValue(Encryption.AJAX_ENCRYPTION_KEY) == null)
442-
{
443-
// String clientKey = getRequest().getHeader(Encryption.AJAX_SECURITY_TOKEN);
444-
// if (clientKey != null && clientKey.trim().length() > 0)
445-
// {
446-
// boolean candecrypt[]=new boolean[1];
447-
// clientKey = Encryption.decryptRijndael(clientKey, Encryption.GX_AJAX_PRIVATE_KEY, candecrypt);
448-
// if (candecrypt[0])
449-
// {
450-
// webPutSessionValue(Encryption.AJAX_ENCRYPTION_KEY, clientKey);
451-
// return true;
452-
// }else
453-
// {
454-
// return false;
455-
// }
456-
// }
457-
}
458-
return false;
459-
}
460-
461-
public String DecryptAjaxCall(String encrypted)
462-
{
463-
validEncryptedParm = false;
464-
if (isGxAjaxRequest())
465-
{
466-
String key = getAjaxEncryptionKey();
467-
boolean candecrypt[] = new boolean[1];
468-
String decrypted = Encryption.decryptRijndael(encrypted, key, candecrypt);
469-
validEncryptedParm = candecrypt[0];
470-
if (!validEncryptedParm)
471-
{
472-
sendResponseStatus(403, "Forbidden action");
473-
return "";
474-
}
475-
if (validEncryptedParm && !getRequestMethod().equalsIgnoreCase("post"))
476-
{
477-
setQueryString(decrypted);
478-
decrypted = GetNextPar();
479-
}
480-
return decrypted;
481-
}
482-
return encrypted;
483-
}
484-
485-
public boolean IsValidAjaxCall()
486-
{
487-
return IsValidAjaxCall(true);
488-
}
489-
490-
public boolean IsValidAjaxCall(boolean insideAjaxCall)
491-
{
492-
if (insideAjaxCall && !validEncryptedParm)
493-
{
494-
sendResponseStatus(403, "Forbidden action");
495-
return false;
496-
}
497-
else if (!insideAjaxCall && isGxAjaxRequest())
498-
{
499-
sendResponseStatus(440, "Session timeout");
500-
return false;
501-
}
502-
return true;
503-
}
504384

505385
public void sendResponseStatus(int statusCode, String statusDescription)
506386
{
507-
//getResponse().setStatus(statusCode);
508-
//try { getResponse().sendError(statusCode, statusDescription); }
509-
//catch(Exception e) {}
510-
//setAjaxCallMode();
511-
//disableOutput();
512387
}
513388

514389
private void sendReferer()
@@ -578,28 +453,11 @@ public String decrypt64(String value, String key)
578453
}
579454
return sRet;
580455
}
581-
582-
public void SendAjaxEncryptionKey()
583-
{
584-
if(!encryptionKeySended)
585-
{
586-
String key = getAjaxEncryptionKey();
587-
ajax_rsp_assign_hidden(Encryption.AJAX_ENCRYPTION_KEY, key);
588-
ajax_rsp_assign_hidden(Encryption.AJAX_ENCRYPTION_IV, Encryption.GX_AJAX_PRIVATE_IV);
589-
590-
try
591-
{
592-
ajax_rsp_assign_hidden(Encryption.AJAX_SECURITY_TOKEN, Encryption.encryptRijndael(key, Encryption.GX_AJAX_PRIVATE_KEY));
593-
}
594-
catch(Exception exc) {}
595-
encryptionKeySended = true;
596-
}
597-
}
598456

599457
public void SendServerCommands()
600458
{
601459
try {
602-
if (!isAjaxRequest() && commands.getCount() > 0)
460+
if (commands.getCount() > 0)
603461
{
604462
HiddenValues.put("GX_SRV_COMMANDS", commands.getJSONArray());
605463
}

common/src/main/java/com/genexus/util/Encryption.java

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,10 @@
33
import java.security.InvalidKeyException;
44
import com.genexus.CommonUtil;
55
import com.genexus.common.interfaces.SpecificImplementation;
6-
import java.nio.charset.StandardCharsets;
76

87
import com.genexus.diagnostics.core.ILogger;
98
import com.genexus.diagnostics.core.LogManager;
109
import org.apache.commons.codec.binary.Base64;
11-
import org.bouncycastle.crypto.BlockCipher;
12-
import org.bouncycastle.crypto.BufferedBlockCipher;
13-
import org.bouncycastle.crypto.DataLengthException;
14-
import org.bouncycastle.crypto.InvalidCipherTextException;
15-
import org.bouncycastle.crypto.engines.RijndaelEngine;
16-
import org.bouncycastle.crypto.modes.CBCBlockCipher;
17-
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
18-
import org.bouncycastle.crypto.paddings.ZeroBytePadding;
19-
import org.bouncycastle.crypto.params.KeyParameter;
20-
import org.bouncycastle.crypto.params.ParametersWithIV;
2110
import org.bouncycastle.util.encoders.Hex;
2211

2312
import java.io.UnsupportedEncodingException;
@@ -29,8 +18,6 @@ public class Encryption
2918
public static String AJAX_ENCRYPTION_KEY = "GX_AJAX_KEY";
3019
public static String AJAX_ENCRYPTION_IV = "GX_AJAX_IV";
3120
public static String AJAX_SECURITY_TOKEN = "AJAX_SECURITY_TOKEN";
32-
public static String GX_AJAX_PRIVATE_KEY = "595D54FF4A612E69FF4F3FFFFF0B01FF";
33-
public static String GX_AJAX_PRIVATE_IV = "8722E2EA52FD44F599D35D1534485D8E";
3421
private static int[] VALID_KEY_LENGHT_IN_BYTES = new int[]{32, 48, 64};
3522

3623
static public class InvalidGXKeyException extends RuntimeException
@@ -344,77 +331,4 @@ public void nextBytes2(byte[] out)
344331
}
345332
}
346333
}
347-
348-
public static String getRijndaelKey()
349-
{
350-
SecureRandom rdm = new SecureRandom();
351-
byte[] bytes = new byte[16];
352-
rdm.nextBytes(bytes);
353-
StringBuffer buffer = new StringBuffer(32);
354-
for (int i = 0; i < 16; i++)
355-
{
356-
buffer.append(CommonUtil.padl(Integer.toHexString((int)bytes[i]), 2, "0"));
357-
}
358-
return buffer.toString().toUpperCase();
359-
}
360-
361-
public static String decryptRijndael(String ivEncrypted, String key, boolean[] candecrypt) {
362-
363-
try {
364-
candecrypt[0] = false;
365-
String encrypted = ivEncrypted.length() >= GX_AJAX_PRIVATE_IV.length() ? ivEncrypted.substring(GX_AJAX_PRIVATE_IV.length()) : ivEncrypted;
366-
byte[] inputBytes = Hex.decode(encrypted.trim().getBytes());
367-
byte[] outputBytes;
368-
String decrypted = "";
369-
if (inputBytes != null) {
370-
try {
371-
outputBytes = aesCipher(inputBytes, false, key, GX_AJAX_PRIVATE_IV);
372-
} catch (DataLengthException | IllegalStateException | InvalidCipherTextException e) {
373-
return ivEncrypted;
374-
}
375-
376-
String result = new String(outputBytes, StandardCharsets.US_ASCII).replaceAll("[\ufffd]", "");
377-
if (result != null) {
378-
candecrypt[0] = true;
379-
decrypted = result.trim();
380-
}
381-
}
382-
return decrypted;
383-
}catch(Exception ex){
384-
return ivEncrypted;
385-
}
386-
}
387-
388-
public static String encryptRijndael(String plainText, String key) {
389-
byte[] inputBytes = plainText.trim().getBytes(StandardCharsets.US_ASCII);
390-
byte[] outputBytes;
391-
try {
392-
outputBytes = aesCipher(inputBytes, true, key, GX_AJAX_PRIVATE_IV);
393-
} catch (DataLengthException | IllegalStateException | InvalidCipherTextException e) {
394-
logger.error("encryptRijndael error", e);
395-
return "";
396-
}
397-
return Hex.toHexString(outputBytes);
398-
}
399-
400-
401-
private static byte[] aesCipher(byte[] inputBytes, boolean init, String key, String iv)
402-
throws DataLengthException, IllegalStateException, InvalidCipherTextException {
403-
byte[] byteKey = Hex.decode(key);
404-
byte[] byteIV = Hex.decode(iv);
405-
KeyParameter keyParam = new KeyParameter(byteKey);
406-
ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, byteIV);
407-
408-
BlockCipher engineWithMode = new CBCBlockCipher(new RijndaelEngine());
409-
410-
BufferedBlockCipher bbc = new PaddedBufferedBlockCipher(engineWithMode, new ZeroBytePadding());
411-
bbc.init(init, keyParamWithIV);
412-
byte[] outputBytes = new byte[bbc.getOutputSize(inputBytes.length)];
413-
if (inputBytes != null) {
414-
int length = bbc.processBytes(inputBytes, 0, inputBytes.length, outputBytes, 0);
415-
bbc.doFinal(outputBytes, length);
416-
417-
}
418-
return outputBytes;
419-
}
420334
}

0 commit comments

Comments
 (0)