interop.html 4.04 KB
Newer Older
Daniel Hofmann's avatar
Daniel Hofmann committed
1
<!DOCTYPE html>
Daniel Hofmann's avatar
Daniel Hofmann committed
2
<html lang="en">
Daniel Hofmann's avatar
Daniel Hofmann committed
3
4
5
6
    <head>
        <meta charset="utf-8">
        <title>AES (in CBC mode) language interop tester</title>

7
        <!-- TODO(daniel): host locally; jquery v1 for included IE support.. --!>
Daniel Hofmann's avatar
Daniel Hofmann committed
8
        <script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
9
        <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
Daniel Hofmann's avatar
Daniel Hofmann committed
10
11
    </head>
    <body>
12

Daniel Hofmann's avatar
Daniel Hofmann committed
13
14
15
16
17
18
19
        <h3>AES-128 CBC, PKCS#7, nosalt</h3>
        <hr>
        <div>
            <input id="plaintext" type="text" name="plaintext" autofocus="autofocus" placeholder="My Secret..">
            <p id="ciphertext"></p>
            <p id="key"></p>
        </div>
20
21
22
23
24
25
26
27
28
29

        <h3>Assertion Handling</h3>
        <hr>
        <div>
            <button id="get-btn">Perform GET Request</button>
            <p id="get-rv">..</p>
            <button id="post-btn">Perform POST Request</button>
            <p id="post-rv">..</p>
        </div>

Daniel Hofmann's avatar
Daniel Hofmann committed
30
31
    </body>
    <script>
32
33
34
35
36
        /* notes:
            - this script block does not (yet?) use and therefore depend on jQuery for modularity reasons
            - TODO: refactor this poc into a encrypt() function; remove console debugging
         */

Daniel Hofmann's avatar
Daniel Hofmann committed
37
38
        "use strict";

39
        var bs = 16;  // aes block size is 16 bytes
Daniel Hofmann's avatar
Daniel Hofmann committed
40

41
42
43
        document.getElementById('plaintext').onkeypress = function(e) {
          var event = e || window.event;
          var charCode = event.which || event.keyCode;
44

45
46
          if (charCode == '13') {  // enter pressed
            var plaintext = document.getElementById("plaintext").value;
Daniel Hofmann's avatar
Daniel Hofmann committed
47

48
            // note: no need for a key derivation function, we're _generating_ a key from scratch _every time_
49
50
            var key = CryptoJS.lib.WordArray.random(bs);
            var iv = CryptoJS.lib.WordArray.random(bs);
Daniel Hofmann's avatar
Daniel Hofmann committed
51

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
            var opts = {iv: iv, keySize: bs, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7};
            var enc = CryptoJS.AES.encrypt(plaintext, key, opts);

            console.log('ciphertext: ' + enc.ciphertext.toString(CryptoJS.enc.Hex));
            console.log('key: ' + enc.key.toString(CryptoJS.enc.Hex));
            console.log('iv: ' + enc.iv.toString(CryptoJS.enc.Hex));

            /* note:
                - no need for urlencode, hex is perfectly fine for urls
                - decoding: unhex 'out', first 16 bytes are iv, rest is ciphertext
                - or: split 'out', unhex first 32 bytes for iv, unhex rest for ciphertext
             */
            var out = enc.iv.toString(CryptoJS.enc.Hex) + enc.ciphertext.toString(CryptoJS.enc.Hex);

            document.getElementById("ciphertext").textContent = 'iv + ciphertext: ' + out;
            document.getElementById("key").textContent = 'key: ' + enc.key.toString(CryptoJS.enc.Hex);
          }
        }
Daniel Hofmann's avatar
Daniel Hofmann committed
70
    </script>
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

    <script>
        /* notes:
            - full blown jQuery solution to keep my sanity; GET/POST assertion poc
            - use local server for testing, same origin policy forbids requests otherwise
            - ./runserver.py (and check source)
            - TODO: chain post(encrypt(get()))
         */
        "use strict";

        var from = "http://127.0.0.1:8080/assertion";
        var to = "http://127.0.0.1:8080/upload";
        /*
        var from = "https://saml-delegation.data.kit.edu/sd/ecp.py";
        var to = "https://saml-delegation.data.kit.edu/sd/upload.py";
        */

        $(document).ready(function() {

            $("#get-btn").click(function() {
                $("#get-rv").text("..");  // clear old state

                $.get(from, function(data, stat){
                    $("#get-rv").text(stat);
                });
            });

            $("#post-btn").click(function() {
                var assertion = {role: 'dummy'};  // XXX: aes-cbc-128-pkcs#7-nosalt(get(assertion))
                $("#post-rv").text("..");  // clear old state

                $.post(to, assertion, function(data, stat){
                    $("#post-rv").text(stat);
                });
            });

        });

    </script>
Daniel Hofmann's avatar
Daniel Hofmann committed
110
111
</html>
<!-- vim: set tabstop=4 shiftwidth=4 expandtab: --!>