Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Alternate WebCrypto Library? #178

Closed
apowers313 opened this issue Jun 17, 2018 · 9 comments
Closed

Alternate WebCrypto Library? #178

apowers313 opened this issue Jun 17, 2018 · 9 comments

Comments

@apowers313
Copy link
Contributor

In theory, should it be possible to use @trust/webcrypto with pkijs? If so, do you know if anyone has ever tried it and / or successfully gotten it to work?

@YuryStrozhevsky
Copy link
Collaborator

PKIjs would work with any object looks same with standard WebCrypto API object. It is the first time I heard about the package and can’t say anything about it.

@rmhrisk
Copy link
Contributor

rmhrisk commented Jun 17, 2018

@rmhrisk rmhrisk closed this as completed Jun 17, 2018
@apowers313
Copy link
Contributor Author

For future reference, replacing this code:

const WebCrypto = require("node-webcrypto-ossl");
const webcrypto = new WebCrypto();
pkijs.setEngine("newEngine", webcrypto, new CryptoEngine({
    name: "",
    crypto: webcrypto,
    subtle: webcrypto.subtle
}));

With this code

const crypto = require("@trust/webcrypto");

pkijs.setEngine("newEngine", crypto, new CryptoEngine({
    name: "",
    crypto: crypto,
    subtle: crypto.subtle
}));

Seems to work. I can verify a certificate, but I haven't tried anything more than that.

Pros:

  • no code compiling required
  • uses node's built-in OpenSSL implementation, which I'm assuming is FIPS 140-2 compliant (if not certified) and resistant to things like timing attacks

Cons:

  • less crypto support?

@YuryStrozhevsky
Copy link
Collaborator

@apowers313 In fact I myself had been working on something similar with @trust/webcrypto - having node-webcrypto-ossl compiled, especially on Windows, could be a hell sometimes. But the problem with bilt-in Node's OpenSSL is that the require('crypto') interface is very poor and could not provide all necessary for implementing WebCrypto API. Even such easy task as generating ECDSA keys could lead to a big set of tricks - there is no obvious way how to do it using Node's crypto. That is why I bet @trust/webcrypto would not be finished without compilations steps - in any case at the end they would come to it.

@YuryStrozhevsky
Copy link
Collaborator

@apowers313 Haha - just checked code: they are using this for ECDSA key generation:

let privateKey = spawnSync('openssl', ['ecparam','-name',osslCurveName.name,'-genkey','-noout']).stdout
let publicKey = spawnSync('openssl', ['ec', '-pubout'], { input: privateKey }).stdout

As I said working directly with Node's crypto would not give all possible features for making a kind of WebCrypto API.

@apowers313
Copy link
Contributor Author

Yea, I got to the point where verify fails with @trust/webcrypto and passes with node-webcrypto-ossl with the same inputs... somewhat disturbing, but I'm not going to spend more time hunting it down.

Should anyone ever stumble across this thread and want to pick up where I left off here's the data I'm seeing:

importKey()

format spki
keyData (120 bytes)
30 76 30 10 06 07 2A 86 48 CE 3D 02 01 06 05 2B
81 04 00 22 03 62 00 04 14 4A 28 FB A8 DD C6 0E
A8 52 E3 A5 A2 A3 E3 2B F9 D5 1B 26 3E 01 70 85
CF 58 B9 25 1E 0E 3D E1 49 B3 E9 B2 D7 8D DA 8B
4A 77 E0 45 F8 47 55 F1 9B DC 46 8B D9 FC 17 E4
71 2E 32 CD 3C 7B 65 90 1D D7 E2 29 AC AD F2 31
ED 07 04 22 6C 75 70 C4 E0 BB 9C D4 8B 0C 4B 5F
5A 28 91 58 48 45 2A 2C
algorithm { name: 'ECDSA', namedCurve: 'P-384' }
extractable true
keyUsages [ 'verify' ]

verify()

algorithm { name: 'ECDSA', hash: { name: 'SHA-384' } }
key CryptoKey {
type: 'public',
extractable: true,
algorithm: ECDSA { name: 'ECDSA', namedCurve: 'P-384' },
usages: [ 'verify' ] }
signature (96 bytes)
12 D5 AB 42 5D 5A FD B2 63 84 69 D4 88 B2 21 55
84 28 6D 4F 4F 3D 69 3C 01 6A 12 76 A4 3E DC 38
44 C4 53 1B 01 DD 6F 68 9F 70 36 CF A0 49 1F 61
E2 E4 7C 43 E3 DB 67 C1 9D E8 F5 C2 D8 32 CA 5B
2E EF 7A CA 2D 4B F8 C6 10 F2 F5 30 1E 55 49 A5
D2 20 C1 33 C4 AD 13 80 E2 56 CF F8 7A 31 61 A9
data (572 bytes)
30 82 02 38 A0 03 02 01 02 02 0E 46 A9 B1 93 C3
50 B8 97 C2 10 D5 58 6B 54 30 0A 06 08 2A 86 48
CE 3D 04 03 03 30 53 31 0B 30 09 06 03 55 04 06
13 02 55 53 31 16 30 14 06 03 55 04 0A 13 0D 46
49 44 4F 20 41 6C 6C 69 61 6E 63 65 31 1D 30 1B
06 03 55 04 0B 13 14 4D 65 74 61 64 61 74 61 20
54 4F 43 20 53 69 67 6E 69 6E 67 31 0D 30 0B 06
03 55 04 03 13 04 52 6F 6F 74 30 1E 17 0D 31 35
30 36 31 37 30 30 30 30 30 30 5A 17 0D 34 30 30
36 31 37 30 30 30 30 30 30 5A 30 53 31 0B 30 09
06 03 55 04 06 13 02 55 53 31 16 30 14 06 03 55
04 0A 13 0D 46 49 44 4F 20 41 6C 6C 69 61 6E 63
65 31 1D 30 1B 06 03 55 04 0B 13 14 4D 65 74 61
64 61 74 61 20 54 4F 43 20 53 69 67 6E 69 6E 67
31 0D 30 0B 06 03 55 04 03 13 04 43 41 2D 31 30
59 30 13 06 07 2A 86 48 CE 3D 02 01 06 08 2A 86
48 CE 3D 03 01 07 03 42 00 04 F6 C0 E0 0B C3 F3
05 89 7F C0 AA A9 5D F6 BD F2 33 88 A3 BF 25 F6
9C F8 C4 EC C6 0C 62 33 D3 31 17 31 7B 01 63 A9
00 87 25 38 19 19 93 D3 0A 38 B7 25 2F C7 90 58
F0 E2 A5 B0 B3 33 95 B2 8C 45 A3 81 EF 30 81 EC
30 0E 06 03 55 1D 0F 01 01 FF 04 04 03 02 01 06
30 12 06 03 55 1D 13 01 01 FF 04 08 30 06 01 01
FF 02 01 00 30 1D 06 03 55 1D 0E 04 16 04 14 69
11 5E 2D 69 64 85 9D 96 B8 D6 FF 5C CD BF 3C 75
CF 4C 18 30 1F 06 03 55 1D 23 04 18 30 16 80 14
D2 A5 1F 0B A7 F6 62 C8 40 D4 D8 BD B9 D7 8E D1
54 BB BC 46 30 35 06 03 55 1D 1F 04 2E 30 2C 30
2A A0 28 A0 26 86 24 68 74 74 70 3A 2F 2F 6D 64
73 2E 66 69 64 6F 61 6C 6C 69 61 6E 63 65 2E 6F
72 67 2F 52 6F 6F 74 2E 63 72 6C 30 4F 06 03 55
1D 20 04 48 30 46 30 44 06 0B 2B 06 01 04 01 82
E5 1C 01 03 01 30 35 30 33 06 08 2B 06 01 05 05
07 02 01 16 27 68 74 74 70 73 3A 2F 2F 6D 64 73
2E 66 69 64 6F 61 6C 6C 69 61 6E 63 65 2E 6F 72
67 2F 72 65 70 6F 73 69 74 6F 72 79
VERIFY RESULT false

(result is true with node-webcrypto-ossl)

@YuryStrozhevsky
Copy link
Collaborator

@apowers313 Tested it with CTjs - it provides incorrect results during ECDSA signature validation. So, please do not consider @trust\webcryto to be used in a real environment.

@apowers313
Copy link
Contributor Author

Yep, it's failing my tests too.

@jpoliveirauai
Copy link

It can be solved using this library:
https://www.npmjs.com/package/@peculiar/webcrypto

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants