Exemplo n.º 1
0
        // jsc should translate Tasks to Promises if defined on Native apis?
        public static Task <bool> verifyAsync(
            this SubtleCrypto that,

            CryptoKey publicKey, byte[] signature, byte[] data)
        {
            // Z:\jsc.svn\examples\javascript\crypto\ClientVerifiedWebServiceAuthority\ClientVerifiedWebServiceAuthority\Application.cs
            var x = new TaskCompletionSource <bool>();

            Native.crypto.subtle.verify(
                new
            {
                name = "RSASSA-PKCS1-v1_5",
            },
                publicKey, //from generateKey or importKey above
                signature, //ArrayBuffer of the signature
                //Encoding.UTF8.GetBytes(x.value) //ArrayBuffer of the data
                data       //ArrayBuffer of the data

                ).then(

                onSuccess: verified =>
            {
                x.SetResult(verified);
            }
                );

            return(x.Task);
        }
Exemplo n.º 2
0
        public static Task <byte[]> decryptAsync(
            this SubtleCrypto that,

            object algorithm, CryptoKey key, byte[] data
            )
        {
            Console.WriteLine(
                "enter decryptAsync"
                );

            var x       = new TaskCompletionSource <byte[]>();
            var promise = that.decrypt(algorithm, key, data);

            // android webview wont like .catch
            promise.@catch(
                err =>
            {    // X:\jsc.svn\examples\javascript\Test\TestWebCryptoKeyExport\TestWebCryptoKeyExport\Application.cs
                 // setexception?

                Console.WriteLine(
                    "decryptAsync " + new { err }
                    );
            }
                );

            // tested by?
            promise.then(
                z => { x.SetResult(z); }
                );

            return(x.Task);
        }
Exemplo n.º 3
0
        // X:\jsc.svn\examples\javascript\Test\TestWebCryptoEncryption\TestWebCryptoEncryption\Application.cs
        public static Task <byte[]> encryptAsync(
            this SubtleCrypto that,

            object algorithm, CryptoKey key, byte[] data
            )
        {
            var x       = new TaskCompletionSource <byte[]>();
            var promise = that.encrypt(algorithm, key, data);

            promise.then(
                z => { x.SetResult(z); }
                );

            return(x.Task);
        }
Exemplo n.º 4
0
        // what an ugly name. keep it?
        public static Task <JsonWebKey> exportJSONWebKeyAsync(
            this SubtleCrypto that,

            CryptoKey key
            )
        {
            // X:\jsc.svn\examples\javascript\Test\TestWebCryptoKeyExport\TestWebCryptoKeyExport\Application.cs

            var x       = new TaskCompletionSource <JsonWebKey>();
            var promise = that.exportKey("jwk", key);

            promise.then(
                z => { x.SetResult((JsonWebKey)z); }
                );

            return(x.Task);
        }
Exemplo n.º 5
0
        public static Task <byte[]> digestAsync(
            this SubtleCrypto that,

            object algorithm,
            byte[] data
            )
        {
            // X:\jsc.svn\examples\javascript\async\AsyncWorkerSourceSHA1\AsyncWorkerSourceSHA1\Application.cs
            // X:\jsc.svn\examples\javascript\Test\TestWebCryptoSHA1\TestWebCryptoSHA1\Application.cs

            var x       = new TaskCompletionSource <byte[]>();
            var promise = that.digest(algorithm, data);

            promise.then(
                z => { x.SetResult(z); }
                );

            return(x.Task);
        }
Exemplo n.º 6
0
        // where to put the async definitions?
        // keep the original callback/Promise api also visible?

        // tested by
        // X:\jsc.svn\examples\javascript\async\Test\TestWebCryptoAsync\TestWebCryptoAsync\Application.cs

        //[Obsolete("workaround until jsc implicitly turns Promis into Task for return values.")]
        public static Task <KeyPair> generateKeyAsync(
            this SubtleCrypto that,

            object algorithm,
            bool extractable,
            string[] keyUsages
            )
        {
            var x = new TaskCompletionSource <KeyPair>();

            // Error	1	Keyword 'this' is not valid in a static property, static method, or static field initializer	X:\jsc.svn\core\ScriptCoreLib\JavaScript\DOM\SubtleCrypto.cs	59	13	ScriptCoreLib
            //this.generateKey();

            var promise = that.generateKey(algorithm, extractable, keyUsages);

            // we are taking a delegate of a BCL function, and then converting it to IFunction! nice.

            return(promise.AsTask());
        }
Exemplo n.º 7
0
        // Z:\jsc.svn\examples\javascript\crypto\ClientVerifiedWebServiceAuthority\ClientVerifiedWebServiceAuthority\Application.cs
        // haha. long name eh.
        public static Task <CryptoKey> importRSAPublicKeyForVerificationAsync(
            this SubtleCrypto that,

            byte[] PublicKeyModulus,
            byte[] PublicKeyExponent
            )
        {
            // X:\jsc.svn\examples\javascript\async\AsyncWorkerSourceSHA1\AsyncWorkerSourceSHA1\Application.cs
            // X:\jsc.svn\examples\javascript\Test\TestWebCryptoSHA1\TestWebCryptoSHA1\Application.cs

            var x = new TaskCompletionSource <CryptoKey>();


            #region keyData
            // make URL friendly:
            var m64padding = Convert.ToBase64String(PublicKeyModulus);
            var m64        = m64padding;
            while (m64.EndsWith("=="))
            {
                m64 = m64.Substring(0, m64.Length - 2);
            }
            // http://stackoverflow.com/questions/4492426/remove-trailing-when-base64-encoding
            m64 = m64.Replace("+", "-").Replace("/", "_").Replace("=+", "");

            var e64 = Convert.ToBase64String(PublicKeyExponent);
            //var e64 = e64padding;
            //while (e64.EndsWith("=="))
            //    e64 = e64.Substring(0, e64.Length - 2);
            //// http://stackoverflow.com/questions/4492426/remove-trailing-when-base64-encoding
            //e64 = e64.Replace("+", "-").Replace("/", "_").Replace("=+", "");

            var keyData = new
            {
                //alg = "RS256",
                alg = "RS1",
                //alg = "RSA-OAEP",
                e   = e64,
                ext = false,
                kty = "RSA",
                n   = m64
            };
            #endregion

            var algorithm = new
            {
                name = "RSASSA-PKCS1-v1_5",
                //name = "RSA-OAEP",
                //hash = new { name = "SHA-256" },
                hash = new { name = "SHA-1" }

                //modulusLength = 2048,
                //publicExponent,
            };

            Console.WriteLine(new { keyData });
            Console.WriteLine(new { algorithm });

            var p = Native.crypto.subtle.importKey(
                format: "jwk",
                keyData: keyData,
                algorithm: algorithm,
                extractable: false,
                keyUsages: new[] {      // "encrypt",
                // onError { z = SyntaxError: Cannot create a key using the specified key usages. }
                "verify"
            }
                );

            p.then(
                onSuccess: publicKey =>
            {
                x.SetResult(publicKey);

                // onSuccess {{ z = [object CryptoKey], ElapsedMilliseconds = 9278 }}


                //new IHTMLPre { "onSuccess " + new { publicKey, sw.ElapsedMilliseconds } }.AttachToDocument();

                //new IHTMLButton { "encrypt for server" }.AttachToDocument().onclick +=
                //async delegate
                //{
                //    // Man in the middle?
                //    // layered security
                //    var data = Encoding.UTF8.GetBytes("hello from client");
                //    var esw = Stopwatch.StartNew();

                //    var ebytes = await Native.crypto.subtle.encryptAsync(algorithm, z, data);
                //    new IHTMLPre { "encryptAsync " + new { esw.ElapsedMilliseconds } }.AttachToDocument();

                //    await UploadEncryptedString(
                //        ebytes
                //    );
                //};

                // onSuccess { z = [object CryptoKey], ElapsedMilliseconds = 0 }


                //Native.crypto.subtle.verify(
                //     new
                //     {
                //         name = "RSASSA-PKCS1-v1_5",
                //     },
                //    publicKey, //from generateKey or importKey above
                //    x.signature, //ArrayBuffer of the signature
                //    Encoding.UTF8.GetBytes(x.value) //ArrayBuffer of the data

                //).then(

                //onSuccess: verified =>
                //{
                //    new IHTMLPre {
                //            "verify " +
                //                new {
                //               verified}
                //            }.AttachToDocument();
                //}
                //);
            },

                onError: importKeyError =>
            {
                // onError { z = DataError: The JWK member "n" could not be base64url decoded or contained padding }

                //new IHTMLPre {
                //            "onError " +
                //                new {
                //               z}
                //            }.AttachToDocument();

                //x.SetException

                //{ importKeyError = DataError: The JWK member "e" could not be base64url decoded or contained padding }

                Console.WriteLine(new { importKeyError });

                x.SetResult(null);
            }

                );



            return(x.Task);
        }