Format TLS certficiate with new line

We extract raw tls certificate and we need to pass the certificate to a program with proper format of the cert - new line characters to extract information about certificate.

Used below snippet in js and trying to get complete string but facing issues.

Any javascript expert plz advice.


var rawcert = context.getVariable("request.header.X-Tls.ssl_client_raw_cert");
var newrawcert= function(splitArr){  
var parseArr=[]; 
 splitArr = splitArr.split(' ');
  print (splitArr);  
for (var i=0; i <splitArr.length;i++){
  if (i === 0 || i ===1 || i=== splitArr.length -2 || i === splitArr.length -1)  
continue; 
 else   
parseArr.push(splitArr[i]); 
 }  
var beginStr='-----BEGIN CERTIFICATE-----';  
var endStr='-----END CERTIFICATE-----';  
parseArr.unshift(beginStr);  
parseArr.push(endStr);  
parseArr.join("\n");  
return parseArr;   
}
var result = newrawcert(rawcert);
0 4 290
4 REPLIES 4

First of all,please tell what is the usecase for extracting the information, if you are looking for 2 way SSL it can be done without extracting the certificate info.

var options = {	host: 'example.com',	
path: '/',	
headers: headers,	
method: 'GET',
	key: fs.readFileSync('resources/keys/privateKey.pem'),
	cert: fs.readFileSync('resources/keys/publicCert.pem'),	passphrase: 'secret123',	
agent: false,	rejectUnauthorized: false};
options.agent = new https.Agent(options);
var req = https.request(options, function(res) {	console.log(res.statusCode);	res.on('data', function(data){		process.stdout.write(data);	});});<br>

Let us know if this helps. if not please provide more information so community can help you.

Sorry.May be it is not clear..

Example we if we pass sampleCert as input & we need to format with new line chars as provided in formatCert.

Refer below zip with files..

cert.zip

what is the usecase of formatting the certificate content? if you modify it it won't remain valid when you send the certificate in further apis.

Good that you posted your certificates, otherwise, it would be hard to identify an issue.

There are problems with your certs as I can see now.

The sampleCert has space characher (0x20) instead of EOL (0x0A)


The formatCert contains sequence of slash, n, and new line (0x5C, 0x6E, 0x0A) instead of slash, n (\n)

You need to normalise the pem string EOLs to a 0x0a at the *point of parsing*.

Please look at this comment for details on a pem format.

https://community.apigee.com/questions/69213/retrieving-eidas-certificate-values.html?childToView=70...