1"use strict"; 2Object.defineProperty(exports, "__esModule", { value: true }); 3exports.fromDER = exports.toDER = void 0; 4/* 5Copyright 2022 The Sigstore Authors. 6 7Licensed under the Apache License, Version 2.0 (the "License"); 8you may not use this file except in compliance with the License. 9You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13Unless required by applicable law or agreed to in writing, software 14distributed under the License is distributed on an "AS IS" BASIS, 15WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16See the License for the specific language governing permissions and 17limitations under the License. 18*/ 19const PEM_HEADER = /-----BEGIN (.*)-----/; 20const PEM_FOOTER = /-----END (.*)-----/; 21function toDER(certificate) { 22 let der = ''; 23 certificate.split('\n').forEach((line) => { 24 if (line.match(PEM_HEADER) || line.match(PEM_FOOTER)) { 25 return; 26 } 27 der += line; 28 }); 29 return Buffer.from(der, 'base64'); 30} 31exports.toDER = toDER; 32// Translates a DER-encoded buffer into a PEM-encoded string. Standard PEM 33// encoding dictates that each certificate should have a trailing newline after 34// the footer. 35function fromDER(certificate, type = 'CERTIFICATE') { 36 // Base64-encode the certificate. 37 const der = certificate.toString('base64'); 38 // Split the certificate into lines of 64 characters. 39 const lines = der.match(/.{1,64}/g) || ''; 40 return [`-----BEGIN ${type}-----`, ...lines, `-----END ${type}-----`] 41 .join('\n') 42 .concat('\n'); 43} 44exports.fromDER = fromDER; 45