|
| 1 | +package servlets; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.PrintWriter; |
| 6 | + |
| 7 | +import javax.servlet.ServletException; |
| 8 | +import javax.servlet.http.HttpServlet; |
| 9 | +import javax.servlet.http.HttpServletRequest; |
| 10 | +import javax.servlet.http.HttpServletResponse; |
| 11 | +import javax.servlet.http.HttpSession; |
| 12 | + |
| 13 | +public class Cls extends HttpServlet |
| 14 | +{ |
| 15 | + private static org.apache.log4j.Logger log = Logger.getLogger(Register.class); |
| 16 | + |
| 17 | + // cf. https://find-sec-bugs.github.io/bugs.htm#TDES_USAGE |
| 18 | + protected void danger(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 19 | + // <expect-error> |
| 20 | + Cipher c = Cipher.getInstance("DES/ECB/PKCS5Padding"); |
| 21 | + c.init(Cipher.ENCRYPT_MODE, k, iv); |
| 22 | + byte[] cipherText = c.doFinal(plainText); |
| 23 | + } |
| 24 | + |
| 25 | + protected void danger2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 26 | + // <expect-error> |
| 27 | + Cipher c = Cipher.getInstance("DES"); |
| 28 | + c.init(Cipher.ENCRYPT_MODE, k, iv); |
| 29 | + byte[] cipherText = c.doFinal(plainText); |
| 30 | + } |
| 31 | + |
| 32 | + protected void ok(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 33 | + // <no-error> |
| 34 | + Cipher c = Cipher.getInstance("AES/GCM/NoPadding"); |
| 35 | + c.init(Cipher.ENCRYPT_MODE, k, iv); |
| 36 | + byte[] cipherText = c.doFinal(plainText); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +public class Cls extends HttpServlet |
| 41 | +{ |
| 42 | + private static org.apache.log4j.Logger log = Logger.getLogger(Register.class); |
| 43 | + |
| 44 | + // cf. https://find-sec-bugs.github.io/bugs.htm#TDES_USAGE |
| 45 | + protected void danger(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 46 | + // <expect-error> |
| 47 | + Cipher c = Cipher.getInstance("DESede/ECB/PKCS5Padding"); |
| 48 | + c.init(Cipher.ENCRYPT_MODE, k, iv); |
| 49 | + byte[] cipherText = c.doFinal(plainText); |
| 50 | + } |
| 51 | + |
| 52 | + protected void ok(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 53 | + // <no-error> |
| 54 | + Cipher c = Cipher.getInstance("AES/GCM/NoPadding"); |
| 55 | + c.init(Cipher.ENCRYPT_MODE, k, iv); |
| 56 | + byte[] cipherText = c.doFinal(plainText); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +@WebServlet(value = "/crypto-00/BenchmarkTest00019") |
| 61 | +public class BenchmarkTest00019 extends HttpServlet { |
| 62 | + |
| 63 | + private static final long serialVersionUID = 1L; |
| 64 | + |
| 65 | + @Override |
| 66 | + public void doGet(HttpServletRequest request, HttpServletResponse response) |
| 67 | + throws ServletException, IOException { |
| 68 | + doPost(request, response); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void doPost(HttpServletRequest request, HttpServletResponse response) |
| 73 | + throws ServletException, IOException { |
| 74 | + response.setContentType("text/html;charset=UTF-8"); |
| 75 | + |
| 76 | + java.io.InputStream param = request.getInputStream(); |
| 77 | + |
| 78 | + try { |
| 79 | + java.util.Properties benchmarkprops = new java.util.Properties(); |
| 80 | + benchmarkprops.load( |
| 81 | + this.getClass().getClassLoader().getResourceAsStream("benchmark.properties")); |
| 82 | + String algorithm = benchmarkprops.getProperty("cryptoAlg1", "DESede/ECB/PKCS5Padding"); |
| 83 | + javax.crypto.Cipher c = javax.crypto.Cipher.getInstance(algorithm); |
| 84 | + |
| 85 | + // Prepare the cipher to encrypt |
| 86 | + // <expect-error> |
| 87 | + javax.crypto.SecretKey key = javax.crypto.KeyGenerator.getInstance("DES").generateKey(); |
| 88 | + c.init(javax.crypto.Cipher.ENCRYPT_MODE, key); |
| 89 | + |
| 90 | + // encrypt and store the results |
| 91 | + byte[] input = {(byte) '?'}; |
| 92 | + Object inputParam = param; |
| 93 | + if (inputParam instanceof String) input = ((String) inputParam).getBytes(); |
| 94 | + if (inputParam instanceof java.io.InputStream) { |
| 95 | + byte[] strInput = new byte[1000]; |
| 96 | + int i = ((java.io.InputStream) inputParam).read(strInput); |
| 97 | + if (i == -1) { |
| 98 | + response.getWriter() |
| 99 | + .println( |
| 100 | + "This input source requires a POST, not a GET. Incompatible UI for the InputStream source."); |
| 101 | + return; |
| 102 | + } |
| 103 | + input = java.util.Arrays.copyOf(strInput, i); |
| 104 | + } |
| 105 | + byte[] result = c.doFinal(input); |
| 106 | + |
| 107 | + java.io.File fileTarget = |
| 108 | + new java.io.File( |
| 109 | + new java.io.File(org.owasp.benchmark.helpers.Utils.TESTFILES_DIR), |
| 110 | + "passwordFile.txt"); |
| 111 | + java.io.FileWriter fw = |
| 112 | + new java.io.FileWriter(fileTarget, true); // the true will append the new data |
| 113 | + fw.write( |
| 114 | + "secret_value=" |
| 115 | + + org.owasp.esapi.ESAPI.encoder().encodeForBase64(result, true) |
| 116 | + + "\n"); |
| 117 | + fw.close(); |
| 118 | + response.getWriter() |
| 119 | + .println( |
| 120 | + "Sensitive value: '" |
| 121 | + + org.owasp |
| 122 | + .esapi |
| 123 | + .ESAPI |
| 124 | + .encoder() |
| 125 | + .encodeForHTML(new String(input)) |
| 126 | + + "' encrypted and stored<br/>"); |
| 127 | + |
| 128 | + } catch (java.security.NoSuchAlgorithmException |
| 129 | + | javax.crypto.NoSuchPaddingException |
| 130 | + | javax.crypto.IllegalBlockSizeException |
| 131 | + | javax.crypto.BadPaddingException |
| 132 | + | java.security.InvalidKeyException e) { |
| 133 | + response.getWriter() |
| 134 | + .println( |
| 135 | + "Problem executing crypto - javax.crypto.Cipher.getInstance(java.lang.String,java.security.Provider) Test Case"); |
| 136 | + e.printStackTrace(response.getWriter()); |
| 137 | + throw new ServletException(e); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments