summaryrefslogtreecommitdiffhomepage
path: root/packages/server/src/helpers/convertCertBufferToPEM.ts
blob: b6949c408849ed94e4f23fdf20a63de29d142db7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import base64url from 'base64url';
import type { Base64URLString } from '@simplewebauthn/typescript-types';

/**
 * Convert buffer to an OpenSSL-compatible PEM text format.
 */
export function convertCertBufferToPEM(certBuffer: Buffer | Base64URLString): string {
  let b64cert: string;

  /**
   * Get certBuffer to a base64 representation
   */
  if (typeof certBuffer === 'string') {
    b64cert = base64url.toBase64(certBuffer);
  } else {
    b64cert = certBuffer.toString('base64');
  }

  let PEMKey = '';
  for (let i = 0; i < Math.ceil(b64cert.length / 64); i += 1) {
    const start = 64 * i;

    PEMKey += `${b64cert.substr(start, 64)}\n`;
  }

  PEMKey = `-----BEGIN CERTIFICATE-----\n${PEMKey}-----END CERTIFICATE-----\n`;

  return PEMKey;
}