Giter VIP home page Giter VIP logo

js-jose's Introduction

JS JOSE (DEPRECATED)

This repository is DEPRECATED.

It will be placed in an archived state on February 27th, 2023.

What should I use instead?

We recommend users of square/js-jose migrate to panva/jose:

https://github.com/panva/jose

This library is available in NPM and actively maintained. No support, security fixes or updates will be delivered to the Square repository.

js-jose's People

Contributors

alokmenghrajani avatar beyondlogical avatar bmish avatar cpu avatar csstaub avatar dependabot-preview[bot] avatar devblakes avatar devinrhode2 avatar dgalling avatar dkowis avatar emerose avatar l-ko avatar mbyczkowski avatar mcpherrinm avatar microshine avatar myrual avatar rapropos avatar rmhrisk avatar sevki avatar tangzhen avatar zamicol avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-jose's Issues

How to use it with backend-generated key?

Is that possible to import and use public part of RSA key which made on a backend.
I mean keys from ssh-keygen or openssl.

I tried to import key with base64 in n-component but got an error (something about "leading zero in JWK")

Generate private & public key?

I'm searching for a library to encrypt, decrypt, sign and verify messages and tested cryptico (can't sign/verify without encrypt / decrypt) and jsencrypt (signature returns "false" every time, bug?).

Looks like js-jose is a newer and active maintained lib. With some more documentation (usage, some more examples?) and the feature to generate keys it could be a replacement?

integrate react native with js-jose

Hi,
I write an application that receives data in JSON format and I want to validate the data source before the application starts to process the data.
currently the data is stored in Amazon and the application is written in React Native (version 0.45.1).

I wonder:

Is it possible to integrate jose package with react native?
If yes, how?
In the 1st stage I will have static data in Amazon, so I wonder how I can sign the data with jose
Thanks

Looking for complete example for enc+sign and verify+dec

We're looking for a library able to

  • generate client-side the pairs of asymmetric keys for new clients (We of course store only public key, the private one will be saved into 3rd party single installations configuration)

  • given some data, we must both encrypt content and sign using asymmetric keys;

  • when receive a JWT we must verify signature, and then decrypt.

I am not able to figure how to implement all these steps using your library (or companion projects if needed). Could you point me to a starting point to experiment with your project, please?

Square/js-jose future

@dkowis thanks for taking this on and investigating this issue! I've spent some time looking into this problem, but haven't quite got time recently to focus on it.

You've mentioned quite a few issues, so here's what I've been thinking (and I should probably open it up as an issue, so we can track these):

  • I'd like to get rid off global var references and make modules as self-contained as possible.
  • It would be great if we migrated the entire project to TypeScript.
  • Improve unit and integration tests.
    the project is in 0.x release cycle, so we can create breaking changes and call it 0.3 without breaking SemVer.
  • I'd like to clean-up Travis CI and build releases on CI, not on devs' laptops.
  • I'd like to release this package under @square namespace (so maybe @square/jose?)
  • I'd like to review the API, get some feedback and make changes that we can then call as 1.0.

If you'd like to tackle any of these (however big or small), I'd be happy to chat and review the code.

Algorithm support feature detection

What would the best way to detect algorithm support in browsers?

In our case, we want to use the algorithm A256CBC-HS512 but we found that it is not supported in IE11, so in IE11 we would fall back to the default. Unfortunately, IE hangs when you try to encrypt with A256CBC-HS512 so we can't even use try/catch.

At the moment we are looking at UA-sniffing - I really want to use feature detection if possible. Any ideas?

NPM for Node

Current jose-jwe-jws in npm registry has no Node support and outdated.
Can we add

"main": "dist/jose-commonjs.js"

#35
to package.json, so we'll be able to load package as a module dependency?

JWS- Verify failed with RSA public key but working with private key

I tried one of your example for JWS signing and verification.

I'm able to perform Sign with private key properly.
But I get below issue while Verify with RSA public key

Uncaught (in promise) TypeError: Cannot read property 'then' of undefined
at WebCryptographer.verify (jose-jwe-webcryptographer.js:316)
at jose-jws-verify.js:223
at Array.forEach ()
at JoseJWS.Verifier.verify (jose-jws-verify.js:183)

Code snippet -

var cryptographer = new Jose.WebCryptographer();
cryptographer.setContentSignAlgorithm("RS256");
var signer = new JoseJWS.Signer(cryptographer);
signer.addSigner(signer_rsa_key).then(function() {
        var message = document.getElementById("jws_text").value;
        var verifier = new JoseJWS.Verifier(cryptographer, message);
        verifier.addRecipient(rsa_key1).then(function () {
                verifier.verify().then(function (verified) {
                        document.getElementById("jws_text").value = 'Verified: ' + JSON.stringify(verified, null, 4);
                        }).catch(function (err) {
                            error.textContent = "verification failed: " + err;
                            }) });});

}

var rsa_key1 =
{
"n": "00:c2:4b:af:0f:2d:2b:ad:36:72:a7:91:0f:ee:30:a0:95:d5:3a:46:82:86:96:7e:42:c6:fe:8f:20:97:af:49:f6:48:a3:91:53:ac:2e:e6:ec:9a:9a:e0:0a:fb:1c:db:44:40:5b:8c:fc:d5:1c:cb:b6:9b:60:c0:a8:ac:06:f1:6b:29:5e:2f:7b:09:d9:93:32:da:3f:db:53:9c:2e:ea:3b:41:7f:6b:c9:7b:88:9f:2e:c5:dd:42:1e:7f:8f:04:f6:60:3c:fe:43:6d:32:10:ce:8d:99:cb:76:f7:10:97:05:af:28:1e:39:0f:78:35:50:7b:8e:28:22:a4:7d:11:51:22:d1:0e:ab:6b:6f:96:cb:cf:7d:eb:c6:aa:a2:6a:2e:97:2a:93:af:a5:89:e6:c8:bc:9f:fd:85:2b:0f:b4:c0:e4:ca:b5:a7:9a:01:05:81:93:6b:f5:8d:1c:f7:f3:77:0e:6e:53:34:92:0f:48:21:34:33:44:14:5e:4a:00:41:3a:7d:cb:38:82:c1:65:e0:79:ea:a1:05:84:b2:6e:40:19:77:1a:0e:38:4b:28:1f:34:b5:cb:ac:c5:2f:58:51:d7:ec:a8:08:0e:7c:c0:20:c1:5e:a1:4d:b1:30:17:63:0e:e7:58:8e:7f:6e:9f:a4:77:8b:1e:a2:d2:2e:1b:e9",
"e": 65537
};

rsa_key as per jose-jws-example.html but I have passed only n and e as I get these values only from RSA public key export.

Please guide me if I miss any to make it work.

Support JWS

The library looks great, I was looking to see if anyone had made a library that supports both JWS and JWE based on webcrypto and your library is the closest match I found. Do you have plans to add JWS? The title of the library is such it seems it might be in your roadmap.

DataError: The JWK member "e" could not be base64url decoded or contained padding

Hi,

I tried to encrypt a message using this package and I first test it by the example in Readme. I imported to my Angular project's AppComponent with

import { Jose } from 'jose-jwe-jws';
command. After that my code looks like this:

const base_64n = btoa(`c2:4b:af:0f:2d:2b:ad:36:72:a7:91:0f:ee:30:a0:95:d5:3a:46:82:86:96:7e:42:c6:fe:8f:
20:97:af:49:f6:48:a3:91:53:ac:2e:e6:ec:9a:9a:e0:0a:fb:1c:db:44:40:5b:8c:fc:d5:1c:cb:b6:9b:60:c0:a8:ac:06:f1:
6b:29:5e:2f:7b:09:d9:93:32:da:3f:db:53:9c:2e:ea:3b:41:7f:6b:c9:7b:88:9f:2e:c5:dd:42:1e:7f:8f:04:f6:60:3c:fe:
43:6d:32:10:ce:8d:99:cb:76:f7:10:97:05:af:28:1e:39:0f:78:35:50:7b:8e:28:22:a4:7d:11:51:22:d1:0e:ab:6b:6f:
96:cb:cf:7d:eb:c6:aa:a2:6a:2e:97:2a:93:af:a5:89:e6:c8:bc:9f:fd:85:2b:0f:b4:c0:e4:ca:b5:a7:9a:01:05:81:93:6b:
f5:8d:1c:f7:f3:77:0e:6e:53:34:92:0f:48:21:34:33:44:14:5e:4a:00:41:3a:7d:cb:38:82:c1:65:e0:79:ea:a1:05:84:b2:
6e:40:19:77:1a:0e:38:4b:28:1f:34:b5:cb:ac:c5:2f:58:51:d7:ec:a8:08:0e:7c:c0:20:c1:5e:a1:4d:b1:30:17:63:0e:e7:
58:8e:7f:6e:9f:a4:77:8b:1e:a2:d2:2e:1b:e9`);

    const base64e = btoa(`65537`);

    const cryptographer = new Jose.WebCryptographer();

    const rsa_key = Jose.Utils.importRsaPublicKey({
      n: base_64n,
      e: base64e,
      kty: 'RSA'
    }, 'RSA-OAEP');
    const encrypter = new JoseJWE.Encrypter(cryptographer, rsa_key);
    encrypter.encrypt('hello world').then(function (result) {
      console.log(result);
    });

However, when I run my project, I receive the following error message:

DataError: The JWK member "e" could not be base64url decoded or contained padding

Any help will be appreciated!

Fix tests

Chrome's error when decryption fails is now "OperationError" instead of "OperationError: ".

Which is why the tests fail in some browsers.

How can I set content encryption algorithm while using jose-jwe-jws.d.ts

In jose-jwe-jws.d.ts file, setContentEncryptionAlgorithm is not exposed for WebCryptographer. As of now its taking A256GCM as default value and since this method is not exposed in ts file I am unable to set content encryption algorithm which I need to use.
Can you please let me know how to set content encryption algorithm when I use jose-jwe-jws.d.ts.

Can't use npm package in Typescript and Webpack project

Hi,
I'm trying to use the library in my project, based on Typescript targeting es6 and using commonjs modules. The final output is produced using Webpack and targeting the web platform.

I've installed the dependency with npm:

npm install --save jose-jwe-jws

and try to reference it in my code:

import {Jose} from "jose-jwe-jws";
...
let cryptographer = new Jose.WebCryptographer();
...

Everything compiles good, but when I run the result of webpack in my page, I've got an error:

Uncaught ReferenceError: Jose is not defined

I've got the same error trying to run tests with Mocha... :-(
What I'm missing?

Thank You

TypeError: Argument 3 of SubtleCrypto.wrapKey does not implement interface CryptoKey.

When I try implement this in an Ionic 2 environment, I get the following error:

Error
`

TypeError: Argument 3 of SubtleCrypto.wrapKey does not implement interface CryptoKey.

`
Install

npm install https://github.com/square/js-jose.git --save

Code

import * as jose from "jose-jwe-jws";

    private cryptographer: any = null;
    private rsa_key: any = null;
    private encrypter: any = null;

    constructor() {
        this.initEncryption();
    }

      private encrypt(value: string): Promise<string> {
        this.encrypter.encrypt(value).then(function (result) {
            console.log(result);
        }).catch(function (err) {
            console.error(err);
        });
      }

    private initEncryption(): void {
        this.cryptographer = new jose.Jose.WebCryptographer();
        this.rsa_key = {
            // n = modulus
            "n": "00:c2:4b:af:0f:2d:2b:ad:36:72:a7:91:0f:ee:30:a0:95:d5:3a:46:82:86:96:7e:42:c6:fe:8f:20:97:af:49:f6:48:a3:91:53:ac:2e:e6:ec:9a:9a:e0:0a:fb:1c:db:44:40:5b:8c:fc:d5:1c:cb:b6:9b:60:c0:a8:ac:06:f1:6b:29:5e:2f:7b:09:d9:93:32:da:3f:db:53:9c:2e:ea:3b:41:7f:6b:c9:7b:88:9f:2e:c5:dd:42:1e:7f:8f:04:f6:60:3c:fe:43:6d:32:10:ce:8d:99:cb:76:f7:10:97:05:af:28:1e:39:0f:78:35:50:7b:8e:28:22:a4:7d:11:51:22:d1:0e:ab:6b:6f:96:cb:cf:7d:eb:c6:aa:a2:6a:2e:97:2a:93:af:a5:89:e6:c8:bc:9f:fd:85:2b:0f:b4:c0:e4:ca:b5:a7:9a:01:05:81:93:6b:f5:8d:1c:f7:f3:77:0e:6e:53:34:92:0f:48:21:34:33:44:14:5e:4a:00:41:3a:7d:cb:38:82:c1:65:e0:79:ea:a1:05:84:b2:6e:40:19:77:1a:0e:38:4b:28:1f:34:b5:cb:ac:c5:2f:58:51:d7:ec:a8:08:0e:7c:c0:20:c1:5e:a1:4d:b1:30:17:63:0e:e7:58:8e:7f:6e:9f:a4:77:8b:1e:a2:d2:2e:1b:e9",

            // e = publicExponent
            "e": 65537,

            // d = privateExponent
            "d": "37:b6:4b:f4:26:17:a8:0b:3c:c5:1f:ab:59:b9:47:d2:ae:d9:8e:ee:4e:79:48:ab:0d:34:61:06:0f:78:8b:d4:ba:ef:6b:f4:7a:22:d8:c4:6f:70:89:5d:9c:b3:a1:8b:e8:88:57:dd:07:9e:c2:2b:12:52:a3:eb:b9:a8:24:01:7e:53:2b:7a:34:50:d7:0c:75:d8:69:a3:87:dd:4b:fc:c1:c3:2f:bd:0e:57:16:8d:ea:de:8e:de:ff:e4:9a:9f:aa:e8:d2:5f:b3:27:ef:f9:ca:50:97:2e:fd:99:1c:34:dd:0c:bb:dd:d0:b9:bf:4f:dc:9d:de:94:50:66:2c:58:7e:c2:31:8b:41:56:49:6a:e6:11:14:53:a1:45:0d:15:8b:26:79:0f:c9:dc:ac:dc:c7:bc:55:2c:96:ed:a7:29:09:04:ee:00:74:60:e1:bc:97:7b:0a:b6:f2:83:82:79:65:e0:aa:88:9f:90:b0:0d:76:4d:3c:08:7e:a5:05:19:d4:8b:54:d3:f1:c1:a3:e3:a5:1e:aa:d6:c4:94:ad:6c:b3:8f:85:06:8a:6f:52:f8:a3:c3:e0:8d:67:35:2f:d4:18:fc:70:f4:71:bf:18:88:d6:a7:b7:04:8e:d3:06:ca:83:c3:2d:21:98:65:c9:41:2c:77:bf:4c:7c:8c:2c:01",

            // p = prime1
            "p": "00:fa:d6:06:46:5c:04:70:e6:ec:47:02:96:02:a5:e2:41:9d:bd:7b:97:28:a4:c5:3b:b5:9b:0a:6b:7d:b6:44:8a:28:1e:d1:ef:cb:44:ef:eb:4d:08:74:80:f5:cf:3b:b7:40:10:60:c9:18:1e:a5:76:4b:41:37:06:b2:71:03:60:25:77:db:d0:b2:21:dc:b0:32:90:a2:10:9a:d5:e6:e3:11:42:a1:9a:7a:26:3c:d3:12:56:db:25:07:69:be:ae:2c:b9:33:6c:29:e3:65:b9:5b:05:84:05:e6:da:c4:f4:3f:ab:84:60:6e:f0:5f:ba:a8:98:8f:72:2c:c8:40:d1",

            // q = prime2
            "q": "00:c6:4b:ac:fe:40:1c:dc:6c:78:07:cc:3e:db:4e:d5:d0:17:3b:8f:04:f0:ae:c4:22:0d:8b:0a:4d:0f:9e:fe:c7:e6:38:b5:53:ba:a9:e8:f0:47:28:14:25:95:6a:79:ab:db:86:97:82:c5:1e:bd:80:a5:aa:a2:b7:a5:c7:48:17:c4:d9:c7:4f:50:2a:69:67:15:4c:0b:f5:e6:fb:20:23:5d:ea:ae:6c:c6:74:ba:cc:f8:06:2b:41:1f:b6:3f:2a:93:fa:f9:e1:ee:93:c3:92:ad:49:c7:8f:db:72:ff:6b:f0:f0:d6:2f:83:ce:1c:82:16:89:57:01:9f:49:2f:99",

            // dp = exponent1
            "dp": "57:d4:c1:75:b9:9a:c4:7d:d7:96:35:cd:99:37:c4:b5:fd:29:f0:30:c9:c6:88:59:94:09:a9:e8:61:a8:84:ef:6b:84:ff:35:dc:13:53:7f:2d:06:1c:e5:5b:2d:29:57:cd:52:ee:d0:fb:65:1f:c3:00:2e:e1:b9:b2:99:e7:f8:ae:a5:fd:8e:62:11:81:59:21:1b:8b:e4:0c:93:81:b9:58:bd:e0:20:5b:4d:30:57:28:40:c9:93:79:b9:09:4f:ab:d1:5d:b4:2e:26:b5:e3:e5:7f:54:ef:4c:1a:a6:84:70:16:fa:cf:59:89:49:bb:ee:75:1d:25:79:90:d5:41",

            // dq = exponent2
            "dq": "00:ab:eb:a8:8c:b7:21:4e:aa:6c:56:b6:6a:38:d1:dc:e6:91:7d:fd:bd:96:be:af:25:a7:00:49:6a:0e:85:16:f8:51:4e:11:48:0a:aa:8d:5e:e5:12:86:85:1f:4a:35:3b:1f:15:4d:fe:fe:d0:6c:14:41:8d:f3:8d:ad:99:5d:93:de:03:c2:9d:ad:2f:58:3b:1b:67:d7:66:d7:60:1a:b9:0f:10:0d:32:19:cd:d2:b7:2a:c2:8e:75:e3:fc:aa:3f:4c:15:68:d8:cd:74:27:37:e0:2d:fb:6b:6a:24:05:f7:9b:e9:f2:89:37:89:57:86:21:eb:e9:17:6a:f6:94:e1",

            // qi = coefficient
            "qi": "0a:ed:5f:30:67:d5:e5:6e:4a:7a:35:49:fe:16:2f:1e:91:2b:39:c3:01:d3:d4:c0:4d:b3:fc:08:b0:66:e9:44:10:9e:5b:5a:ea:83:a5:9c:95:7a:58:70:35:28:e5:4d:ba:19:de:0d:66:f9:db:5c:f6:5b:24:27:9d:0b:2d:44:40:eb:33:3a:19:e2:1d:c0:b0:16:99:d1:c1:52:84:02:d6:67:06:32:f8:4d:cb:42:9f:7c:8a:e0:ad:df:40:6f:e4:8c:f6:f6:9e:1d:bd:43:e3:38:91:a2:d0:9e:60:ff:9d:8c:fb:72:5b:df:95:30:17:d2:f2:cb:7d:92:56:0a"
        };
        this.encrypter = new jose.JoseJWE.Encrypter(this.cryptographer, this.rsa_key);
    }

[Q] Can I use JWK as a singleton?

const promise = jose.JWK.asKey({
kty: "oct",
k: jose.util.base64url.encode(secret, "utf8"),
alg: 'A256KW',
enc: 'A256CBC-HS512'
})
promise.then(function (key) {
JWK = key;
JWE = jose.JWE.createDecrypt(JWK);
});

Can I continue to use the JWK after making it into a singletone?
Or do I have to make a new one every time?

Encrypt in JOSE JS decrypt in JAVA

I'm trying to let JOSE JS interact with Java (more specifically the Nimbus JOSE JWT library), using RSA-OAEP-256 (key encryption) combined with AES256GCM (message encryption).
Using a server side (nimbus) generated key pair I can encrypt and decrypt server side fine.
Using the same keypair in JOSE JS also works fine as long as I stay client side
Now the use case is to

  • let the server provide the public RSA key
  • then the JOSE JS lib encrypt a message using this public key (on the fly creating an AES key to encrypt the message)
  • the resulting JOSE JS cypher is handed to to server
  • On the server I extract the 5 components (header, cek, vector, cypher and tag) from the JOSE JS cypher and feed this to the nimbus library to decrypt using the private key.
  • All seams fine until AES decrypting the cypher (so the RSA decripting of the cek succeeded). The error I get is: "AES/GCM/NoPadding decryption failed: Tag mismatch!" signed int comparison of the JOSE JS cyper client and server sided is perfect, so no info seems to be lost along the way.

Any idea what I am doing wrong, or could you reccommend a server side lib that has proven to work with JOSE JS?

Regards and I like your lib.

Working with webpack

I understand that js-jose will not work out of the box with node.js, but even when deploying to a browser where WebCrypto does exist, the way the distribution file is wrapped causes problems when the application is packaged with webpack.

I tried various ways of tweaking with loaders, but was unable to come up with a configuration solution that just worked. Would you consider accepting a PR that builds a CommonJS module version of jose.js alongside that file in the dist directory? Webpack users could then just reference that without having to jump through hoops surrounding the Jose, JoseJWE and JoseJWS objects being created off window instead of off the reference variable in an import or require statement.

All that is really required is to drop the banner and footer options in the concat task, I believe.

Fail to decrypt KeyEncryptionAlgorithm=dir and ContentEncryptionAlgorithm=A256CBC-HS512

When using dir + A256CBC-HS512 the error "encryptPlainText: incorrect cek length" is displayed (webpack: //Jose/./lib/jose-jwe-webcryptographer.js line 351)

Example code

        var cryptographer = new Jose.WebCryptographer();
        cryptographer.setKeyEncryptionAlgorithm("dir");
        cryptographer.setContentEncryptionAlgorithm("A256CBC-HS512");

        var textContentValidate = "{\"msg\":\"Hello!\"}";
        const rawKey = new Uint8Array([149, 16, 219, 76, 93, 182, 49, 142, 195, 199, 133, 107, 155, 55, 196, 133, 104, 104, 110, 173, 52, 122, 72, 75, 42, 93, 5, 50, 194, 38, 112, 178]);//equals 9510db4c5db6318ec3c7856b9b37c48568686ead347a484b2a5d0532c22670b2
        crypto.subtle.importKey("raw", rawKey, {name: "AES-CBC"}, true, ["decrypt"])
                .then(function (shared_key) {
                    console.log("shared_key",shared_key);
                    var decrypter = new Jose.JoseJWE.Decrypter(cryptographer, shared_key);
                                     //eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIiwidHlwIjoiSldUIn0 === {"alg":"dir","enc":"A256CBC-HS512","typ":"JWT"}
                    decrypter.decrypt("eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIiwidHlwIjoiSldUIn0..gQeIvPewcObpQMMt_s8coQ.pJ_cHEywvpysTyzKGtpBr5Va2hCN3HwrrUcjgVRkU18.tfZg60gQZZjB_W7ZFFKSUxwyazZNFaUIFQIT-qFr5Gk")
                            .then(function (decrypted_plain_text) {
                                if (decrypted_plain_text !== textContentValidate) {
                                    console.log("decryption failed!");
                                } else {
                                    console.log("decryption ok: " + decrypted_plain_text);
                                }
                            }).catch(function (err) {
                        console.log(err);
                    });
                });

Console.log

shared_key CryptoKey {type: "secret", extractable: true, algorithm: {…}, usages: Array(2)}
		type: "secret"
		extractable: true
		algorithm: {name: "AES-CBC", length: 256}
		usages: (2) ["decrypt"]
		__proto__: CryptoKey

Error: encryptPlainText: incorrect cek length
    at jose-jwe-webcryptographer.js:358
    at async Promise.all (index 0)

Import key ok, but validation in splitKey not ok.

Did I make a mistake at some point? Or is there any other way to decrypt this JWE(dir/A256CBC-HS512)?

Automatically generate caniuse()

Jose.caniuse() is a function which has been manually written to check if a given browser/nodejs version implements the right set of features. We should probably automatically generate this function using static analysis. Such a automation can probably benefit other javascript projects.

Type Error when importing as Commonjs package

Type error when creating a WebCryptographer object
when importing js-jose as a commonJs package
in both a Webpack and Nodejs projects.

Seems that many features of js-jose depend on being executed in the Global namespace (such as being included in a Script tag) which causes it to break when imported as a package.

This prevents users of the js-jose from practising good dependency management.

import jsjose from 'jose-jwe-jws';
const webCryptographer = jsjose.Jose.WebCryptographer();
TypeError: this.setKeyEncryptionAlgorithm is not a function
    at Object.o [as WebCryptographer] (test/js-jose.spec.js:1:2407)
    at http://0.0.0.0:9883test/js-jose.spec.js:8:22366
    at Context.it (test/js-jose.spec.js:8:22430)

crypto is not defined

I'm using jose-jwefrom nodejs application. I've got this error on jose-jwe module initialization.
I see it can be fixed by setting global variable crypto.
Maybe it would be better to check if crypto is defined before to use it. This will allow to use setCrypto after module initialization (no need to set global before importconstruction).

example

import * as jose from "jose-jwe";
import * as CryptoOssl from "node-webcrypto-ossl";

jose.setCrypto(new CryptoOssl());

If I set crypto to global before to import jose-jwe, I've got eslint error

eslint: Import in body of module; reorder to top. (import/first)

Publish to NPM

Are there any plans to publish this library to NPM for use in Node.js applications?

Decryption problem

I encrypt a string with "JS Jose". Is there a way to use C# to decrypt the JWT?

The key used for encryption is the RAS public key.

Thanks!!!

decryptCiphertext: MAC failed

Not sure if i'm doing something incorrect but i tried to do

import Jose from 'jose-jwe/dist/jose-commonjs';
const cryptographer = new Jose.Jose.WebCryptographer();
cryptographer.setContentEncryptionAlgorithm('A128CBC-HS256');

function createKeypair() {
    if (!window.crypto || !window.crypto.subtle) {
        return Promise.reject(new Error('crypto.subtle not available'));
    }
    const options = {
        name: 'RSA-OAEP',
        modulusLength: 2048,
        publicExponent: new Uint8Array([1, 0, 1]),
        hash: {name: 'SHA-1'}
    };
    return window.crypto.subtle.generateKey(options, true, ['encrypt', 'decrypt'])
        .then((keypair) => Promise.all([
            window.crypto.subtle.exportKey('jwk', keypair.publicKey),
            window.crypto.subtle.exportKey('jwk', keypair.privateKey)
        ]))
        .then((keypair) => ({
            publicKey: keypair[0],
            privateKey: keypair[1]
        }));
}

function encrypt(jwk, str) {
    const cryptoKeyPromise = Jose.Jose.Utils.importRsaPublicKey(jwk, 'RSA-OAEP');
    const encrypter = new Jose.JoseJWE.Encrypter(cryptographer, cryptoKeyPromise);
    return encrypter.encrypt(str);
}

function decrypt(jwk, str) {
    const cryptoKeyPromise = Jose.Jose.Utils.importRsaPrivateKey(jwk, 'RSA-OAEP');
    const decrypter = new Jose.JoseJWE.Decrypter(cryptographer, cryptoKeyPromise);
    return decrypter.decrypt(str);
}

createKeypair()
    .then((keypair) => {
        return encrypt(keypair.publicKey, 'this is a test')
            .then((encrypted) => decrypt(keypair.privateKey, encrypted));
    })
    .then((decrypted) => console.log(decrypted))
    .catch((err) => {
        console.error(err);
    });

But i get a decryptCiphertext: MAC failed error

Browser compatible implementation of Chacha20/Poly1305, X25519 and ED25519

I'm currently working on a project that requires Chacha20/Poly1305 & X25519 for JWE, and ED25519 for JWS for all data sent to the backend, from a browser.

Currently it's possible to use all of these on Node, because its Crypto module contains an implementation of them.

However, I need it in the Browser.
I'm thinking of integrating a pure JavaScript implementation of Chacha20/Poly1305 X25519 and ED25519 into js-jose, for this purpose.
To be compliant with the following RFCs:
RFC7539
RFC8439
RFC8037

My question is, would you be interested in a PR? or is the focus on WebCrypto only?

Thanks,
Mischa

JWT decoding

Perhaps this would be considered out of scope for the project, if so, please feel free to ignore. I'm not entirely sure where the best place to add/expose this functionality would be, but given the base64url decoding in Utils, it would be pretty trivial to add a utility function to decode the claims of a JWT. I have a use case where the client receives a JWT signed by a server, and it is expected to return said JWT on successive API requests. The client doesn't necessarily have to verify the signature on the JWT (although there are certainly situations where it would want to). It does need to unpack it and see what's inside, however. I had previously been using auth0/jwt-decode for this, but was thinking that if this functionality was rolled into js-jose, I could drop a dependency.

It should be as simple (error-checking aside) as something like:

decodeJwt(jwt:string):string {
  let parts = jwt.split('.');
  let json = Jose.Utils.Base64Url.decode(parts[1]);
  let rv = JSON.parse(json);
  return rv;
}

Unicode support

A round-trip through Utils.arrayFromString and Utils.stringFromArray mangles strings containing non-ASCII Unicode characters. A similar problem exists with Base64Url encoding/decoding, as it uses atob/btoa internally. As a result, encrypting/decrypting strings containing non-ASCII characters is broken. A PR is incoming to address this.

could not decrypt using crypto.subtle generated RSA cryptoKey

I tried to using the window.crypto.subtle.generateKey to generate RSA keypair. After generate, the JWE encryption failed at JWE decryption.
window.crypto.subtle .generateKey( { name: "RSA-OAEP", // Consider using a 4096-bit key for systems that require long-term security modulusLength: 2048, publicExponent: new Uint8Array([1, 0, 1]), hash: "SHA-256" }, true, ["wrapKey", "unwrapKey"] ) .then(keyPair => { var cryptographer = new Jose.WebCryptographer(); cryptographer.setContentEncryptionAlgorithm("A256CBC-HS512"); var public_rsa_key = keyPair.publicKey; var private_rsa_key = keyPair.privateKey; console.log(private_rsa_key); var encrypter = new Jose.JoseJWE.Encrypter(cryptographer, public_rsa_key); encrypter .encrypt(plaintext.textContent) .then(function(result) { ciphertext.textContent = result; var decrypter = new Jose.JoseJWE.Decrypter( cryptographer, private_rsa_key ); decrypter .decrypt(result) .then(function(decrypted_plain_text) { if (decrypted_plain_text != plaintext.textContent) { error.textContent = "decryption failed!"; } }) .catch(function(err) { error.textContent = err; }); }) .catch(function(err) { error.textContent = err; }); });

Javascript runtime error: 'Jose' is undefined in IE

Hi,

I am using Jose to encrypt the json data purpose. I have referred the following plugins in my code

<script src="@Url.Content("~/Content/External/Scripts/joseEncryption/promiz.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Content/External/Scripts/joseEncryption/webcrypto-shim.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Content/External/Scripts/joseEncryption/jose.js")" type="text/javascript"></script>

My code is:

crypto = new Jose.WebCryptographer();
            crypto.setKeyEncryptionAlgorithm("RSA-OAEP-256");
            crypto.setContentEncryptionAlgorithm("A128CBC-HS256");
            rsa_key = Jose.Utils.importRsaPublicKey(rsa_key, "RSA-OAEP-256");
            encrypt = new JoseJWE.Encrypter(crypto , rsa_key);

The above code is working fine in chrome browser but I am getting this error Javascript runtime error: 'Jose' is undefined in IE 11 browser.

Am I missing anything or do I need to include any additional script reference to overcome this issue.

Thanks in advance

TypeScript support

I have TypeScript typings for js-jose, if you are interested in adding them. All that is required is to add one file and add a typings stanza to package.json, and TypeScript users will be able to seamlessly access the library with type checking. As it stands now, the JsonWebKey type will not resolve, because it is not yet defined in the official library. I have a PR pending that addresses this that needs to be resolved first.

If you have zero interest in going down this road at all, simply close out this issue and I will investigate alternative ways of distributing the typings via the typings registry.

NPM (nodejs) module not working

There are a few problems, just want to collect them here:

  • There is a separate dist/ directory with a checked-in build artifact from the other files, making it hard to contribute and potentially setting up difficulties (#3 should fix this)
  • There is no index.js and no "main" defined in package.json (this means to include it, you have to awkwardly do require('./node_modules/jose-jwe/dist/jose-jwt.js')
  • There are no module exports defined
  • Unit tests are only in-browser; no npm test command
  • The code uses btoa() which doesn't exist in node

How do you pronounce jose?

Am I the only one curious? Can we throw that in the readme? Hope it's not the Spanish pronunciation, that'd be a bit confusing.

JWT ES512

Hey guys,

I need a big help creating a JWT using ES512 as per screenshot below. I could not find an example on the README. Anyone could help me with some guidance pretty please?

image

Adding support for ECC

With Chrome now following suit and implementing ECDSA and ECDH, is it time to add support for these algorithms to js-jose?

Also, I require the use of a few algos and curves that are not specified in the WebCrypto spec.
How do we feel about adding support for non-standard algos and curves via optional imports? (eg. Ellipticjs)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.