| 
									
										
										
										
											2018-12-16 21:19:20 -07:00
										 |  |  | // Copyright 2018 AJ ONeal. All rights reserved
 | 
					
						
							|  |  |  | /* This Source Code Form is subject to the terms of the Mozilla Public | 
					
						
							|  |  |  |  * License, v. 2.0. If a copy of the MPL was not distributed with this | 
					
						
							|  |  |  |  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 | 
					
						
							| 
									
										
										
										
											2018-07-04 00:10:43 -06:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | -----BEGIN CERTIFICATE-----LF | 
					
						
							|  |  |  | xxxLF | 
					
						
							|  |  |  | yyyLF | 
					
						
							|  |  |  | -----END CERTIFICATE-----LF | 
					
						
							|  |  |  | LF | 
					
						
							|  |  |  | -----BEGIN CERTIFICATE-----LF | 
					
						
							|  |  |  | xxxLF | 
					
						
							|  |  |  | yyyLF | 
					
						
							|  |  |  | -----END CERTIFICATE-----LF | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Rules | 
					
						
							|  |  |  |   * Only Unix LF (\n) Line endings | 
					
						
							|  |  |  |   * Each PEM's lines are separated with \n | 
					
						
							|  |  |  |   * Each PEM ends with \n | 
					
						
							|  |  |  |   * Each PEM is separated with a \n (just like commas separating an array) | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://github.com/certbot/certbot/issues/5721#issuecomment-402362709
 | 
					
						
							| 
									
										
										
										
											2019-06-13 01:55:25 -06:00
										 |  |  | var expected = '----\nxxxx\nyyyy\n----\n\n----\nxxxx\nyyyy\n----\n'; | 
					
						
							| 
									
										
										
										
											2018-07-04 00:10:43 -06:00
										 |  |  | var tests = [ | 
					
						
							| 
									
										
										
										
											2019-06-13 01:55:25 -06:00
										 |  |  | 	'----\r\nxxxx\r\nyyyy\r\n----\r\n\r\n----\r\nxxxx\r\nyyyy\r\n----\r\n', | 
					
						
							|  |  |  | 	'----\r\nxxxx\r\nyyyy\r\n----\r\n----\r\nxxxx\r\nyyyy\r\n----\r\n', | 
					
						
							|  |  |  | 	'----\nxxxx\nyyyy\n----\n\n----\r\nxxxx\r\nyyyy\r\n----', | 
					
						
							|  |  |  | 	'----\nxxxx\nyyyy\n----\n----\r\nxxxx\r\nyyyy\r\n----', | 
					
						
							|  |  |  | 	'----\nxxxx\nyyyy\n----\n----\nxxxx\nyyyy\n----', | 
					
						
							|  |  |  | 	'----\nxxxx\nyyyy\n----\n----\nxxxx\nyyyy\n----\n', | 
					
						
							|  |  |  | 	'----\nxxxx\nyyyy\n----\n\n----\nxxxx\nyyyy\n----\n', | 
					
						
							|  |  |  | 	'----\nxxxx\nyyyy\n----\r\n----\nxxxx\ryyyy\n----\n' | 
					
						
							| 
									
										
										
										
											2018-07-04 00:10:43 -06:00
										 |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function formatPemChain(str) { | 
					
						
							| 
									
										
										
										
											2019-06-13 01:55:25 -06:00
										 |  |  | 	return ( | 
					
						
							|  |  |  | 		str | 
					
						
							|  |  |  | 			.trim() | 
					
						
							|  |  |  | 			.replace(/[\r\n]+/g, '\n') | 
					
						
							|  |  |  | 			.replace(/\-\n\-/g, '-\n\n-') + '\n' | 
					
						
							|  |  |  | 	); | 
					
						
							| 
									
										
										
										
											2018-07-04 00:10:43 -06:00
										 |  |  | } | 
					
						
							|  |  |  | function splitPemChain(str) { | 
					
						
							| 
									
										
										
										
											2019-06-13 01:55:25 -06:00
										 |  |  | 	return str | 
					
						
							|  |  |  | 		.trim() | 
					
						
							|  |  |  | 		.split(/[\r\n]{2,}/g) | 
					
						
							|  |  |  | 		.map(function(str) { | 
					
						
							|  |  |  | 			return str + '\n'; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-07-04 00:10:43 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-13 01:55:25 -06:00
										 |  |  | tests.forEach(function(str) { | 
					
						
							|  |  |  | 	var actual = formatPemChain(str); | 
					
						
							|  |  |  | 	if (expected !== actual) { | 
					
						
							|  |  |  | 		console.error('input:   ', JSON.stringify(str)); | 
					
						
							|  |  |  | 		console.error('expected:', JSON.stringify(expected)); | 
					
						
							|  |  |  | 		console.error('actual:  ', JSON.stringify(actual)); | 
					
						
							|  |  |  | 		throw new Error('did not pass'); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-04 00:10:43 -06:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if ( | 
					
						
							| 
									
										
										
										
											2019-06-13 01:55:25 -06:00
										 |  |  | 	'----\nxxxx\nyyyy\n----\n' !== | 
					
						
							|  |  |  | 	formatPemChain('\n\n----\r\nxxxx\r\nyyyy\r\n----\n\n') | 
					
						
							| 
									
										
										
										
											2018-07-04 00:10:43 -06:00
										 |  |  | ) { | 
					
						
							| 
									
										
										
										
											2019-06-13 01:55:25 -06:00
										 |  |  | 	throw new Error('Not proper for single cert in chain'); | 
					
						
							| 
									
										
										
										
											2018-07-04 00:10:43 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if ( | 
					
						
							| 
									
										
										
										
											2019-06-13 01:55:25 -06:00
										 |  |  | 	'--B--\nxxxx\nyyyy\n--E--\n\n--B--\nxxxx\nyyyy\n--E--\n\n--B--\nxxxx\nyyyy\n--E--\n' !== | 
					
						
							|  |  |  | 	formatPemChain( | 
					
						
							|  |  |  | 		'\n\n\n--B--\nxxxx\nyyyy\n--E--\n\n\n\n--B--\nxxxx\nyyyy\n--E--\n\n\n--B--\nxxxx\nyyyy\n--E--\n\n\n' | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2018-07-04 00:10:43 -06:00
										 |  |  | ) { | 
					
						
							| 
									
										
										
										
											2019-06-13 01:55:25 -06:00
										 |  |  | 	throw new Error('Not proper for three certs in chain'); | 
					
						
							| 
									
										
										
										
											2018-07-04 00:10:43 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | splitPemChain( | 
					
						
							| 
									
										
										
										
											2019-06-13 01:55:25 -06:00
										 |  |  | 	'--B--\nxxxx\nyyyy\n--E--\n\n--B--\nxxxx\nyyyy\n--E--\n\n--B--\nxxxx\nyyyy\n--E--\n' | 
					
						
							|  |  |  | ).forEach(function(str) { | 
					
						
							|  |  |  | 	if ('--B--\nxxxx\nyyyy\n--E--\n' !== str) { | 
					
						
							|  |  |  | 		throw new Error('bad thingy'); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-04 00:10:43 -06:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | console.info('PASS'); |