Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I import an RSA private key in PEM format for use with WebCrypto?

I'm trying to use WebCrypto to sign a token with RSA-PSS, but I keep getting the error:

DataError: Data provided to an operation does not meet requirements

at crypto.subtle.importKey.

This is my JavaScript code:

function signToken(token, key) {
    crypto.subtle.importKey(
        'pkcs8',
        PEM2Binary(key),
        {
            name: 'RSA-PSS',
            hash: { name: 'SHA-256' },
        },
        false,
        ['sign']
    ).then(function(privKey){
        crypto.subtle.sign(
            'RSA-PSS',
            privKey,
            new TextEncoder().encode(token)
        ).then(function(signedToken){
            msg = JSON.stringify({ intent: 'authenticate-token', signedToken: signedToken });
            socket.send(msg);
        })
    }).catch(function(error){
        console.error(error);
    })
}

function PEM2Binary(pem) {
    var encoded = '';
    var lines = pem.split('\n');
    for (var i = 0; i < lines.length; i++) {
        if (lines[i].indexOf('-----') < 0) {
            encoded += lines[i];
        }
    }
    var byteStr = atob(encoded);
    var bytes = new Uint8Array(byteStr.length);
    for (var i = 0; i < byteStr.length; i++) {
        bytes[i] = byteStr.charCodeAt(i);
    }
    return bytes.buffer;
}

And the sample private key I'm using:

-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA3d+Y/kWq4nNrJd0dgktQ7lfDUtEIxcs10I8+xPEUhpvacotB
o787dDP4ZFYhRQf0WMjQYJwEna/NsrPuY2fgFWIBlUdpkj1SR00FBxHvnRlzP1Sg
cYnJlqh7/A26tM51nVkMtf/4NtYr+jFvRmB5759U1GRBFwZYsoWQRQlQKh5MAk89
KRc/TvrXNzam39GcLvSYz9uN0p2p2Mrofjgv42m53AqC9LOrQbwDX7Dxl0n4z3EI
Z2Ycx33QZVYiLPH1GHTJ8+JuKjqY0ovu6lghGN0y2xR+sqWNvgyW7xlDWZ6Vwyxs
K5SCIc48VwiiAyGZGhcCZloMlO5S/dutycgHhQIDAQABAoIBAQDXfef2bmu+bSNQ
LyYN+mCsXQkUUnoWwXuPCNGKLiwlYRIV1jL2ezGfdy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h2bnEGZZ9wBffnhv1rjfNh6PbrcSdSKOznPEMpvw/pOB
wBnu6RWZ9mtWWIdDmYEJDqzrV7qp2w2x2foLc4oyCUKbUbQ/DvTYFQ==
-----END RSA PRIVATE KEY-----

Thanks in advance for any help.

like image 844
Benjy Wiener Avatar asked Dec 24 '22 06:12

Benjy Wiener


1 Answers

The header -----BEGIN RSA PRIVATE KEY----- in the PEM file is reserved to PKCS#1 keys, but WebCryptoApi does not support pkcs1, so you need to convert the key from PKCS#1 to PKCS#8 using a tool like openssl.

# openssl pkcs8 -topk8 -inform PEM -outform PEM -nocrypt -in pkcs1.key -out pkcs8.key
like image 115
pedrofb Avatar answered Dec 25 '22 23:12

pedrofb