Skip to content

Error: Only 8, 16, 24, or 32 bits supported: 608 in certificationRequestFromPem #1097

Open
@tennalian

Description

@tennalian

Hello!

When I create a certificate request, convert it to PEM format, and then decode it back, I get an error: Error: Only 8, 16, 24, or 32 bits supported: 608.

I'm guessing the problem is that there is a "№" character in my certificate parameters, which I think is causing problems when converting to base64 or vice versa.

Can I somehow avoid this problem?

Example:

https://stackblitz.com/edit/vitejs-vite-dmjz3f?file=main.js

    const keys = pki.rsa.generateKeyPair(2048);
    const csr = pki.createCertificationRequest();

    const subject = [
        {
            name: 'commonName',
            value: '"ITEM №1"',
        },
        {
            name: 'countryName',
            value: 'RU',
        },
        {
            name: 'localityName',
            value: 'G SAMARA',
        },
        {
            name: 'organizationName',
            value: '"ITEM №1"',
        },
        {
            name: 'stateOrProvinceName',
            value: 'SAMARSKAIA',
        },
        {
            name: 'emailAddress',
            value: '[email protected]',
        },
    ];

    const attributes = [
        {
            name: 'extensionRequest',
            extensions: [
                {
                    name: 'keyUsage',
                    digitalSignature: true,
                    nonRepudiation: true,
                    keyEncipherment: true,
                    dataEncipherment: true,
                    keyAgreement: true,
                },
                {
                    name: 'extKeyUsage',
                    clientAuth: true,
                },
            ],
        },
    ];

    csr.publicKey = keys.publicKey;
    csr.setSubject(subject);
    csr.setAttributes(attributes);
    csr.sign(keys.privateKey);

    const rsaPrivateKey = pki.privateKeyToAsn1(keys.privateKey);
    const privateKeyInfo = pki.wrapRsaPrivateKey(rsaPrivateKey);

    const output = {
        privateKey: pki.privateKeyInfoToPem(privateKeyInfo),
        publicKey: pki.publicKeyToPem(keys.publicKey),
        csrKey: pki.certificationRequestToPem(csr), // it's OK
    }

   // decode it
   pki.certificationRequestFromPem(output.csrKey) // Error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions