Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Open
tennalian opened this issue Oct 8, 2024 · 0 comments

Comments

@tennalian
Copy link

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

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

No branches or pull requests

1 participant