Can an SSL certificate be on a single line in a file (no line breaks)?

SSL certificates by default have line breaks after 67 characters. I'm trying to create SSL certificate files using Chef. Essentially I want to create the entire certificate file from a string variable without any line breaks. I've tried this a few times to no avail (Apache complains about not being able to find certificate).

I don't see why line breaks in an SSL cert would be necessary. Any ideas if it's possible to have a cert without any line breaks in the file?


Solution 1:

No, the certificate won't be handled properly without the line breaks - software will fail to parse it.

If you're trying to pass it in a string, why not just include them in it? (\n)

Solution 2:

I found this script handful in doing that.

awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}'  ca.pem

This will take a standard PEM file on multiple lines, replace the "hidden" newline characters with \n, and print the output as a single line.

For example, this will make the following PEM file...

-----BEGIN CERTIFICATE-----
MIIIPDCCBySgAwIBAgIQB2XGTnTlkdaAOcoqhHVj8DANBgkqhkiG9w0BAQsFADBw
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMS8wLQYDVQQDEyZEaWdpQ2VydCBTSEEyIEhpZ2ggQXNz
dXJhbmNlIFNlcnZlciBDQTAeFw0xODEwMDUwMDAwMDBaFw0xOTA4MTQxMjAwMDBa
MGoxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJOWTERMA8GA1UEBxMITmV3IFlvcmsx
HTAbBgNVBAoTFFN0YWNrIEV4Y2hhbmdlLCBJbmMuMRwwGgYDVQQDDBMqLnN0YWNr
ZXhjaGFuZ2UuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9Kk6
NXUP1ocXt89mT1cIxadBhzCL0YTqP01/DSotUHRzV70qOCT7AdMT0K1Ji6rFyapz
RiqUIhAkhEseXRp0MNr21fSUw4VoCb+ImO6gnIlzolkhrpK6Iy34KyU3zyt8XYD+
mY3i8GjPZOysRJNLy3ouQBmzuOmU/2FomnmiEGF02mVgb3ev8Prcnt7dCiF7liBI
sd3zkPeXvTVYcVcb/MBrFEzc4FuIu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nAoZBaHR0cDovL2NhY2VydHMuZGlnaWNlcnQuY29tL0RpZ2lDZXJ0U0hBMkhpZ2hB\nc3N1cmFuY2VTZXJ2ZXJDQS5jcnQwDAYDVR0TAQH/BAIwADCCAQYGCisGAQQB1nkC\nBAIEgfcEgfQA8gB3AKS5CZC0GFgUh7sTosxncAo8NZgE+RvfuON3zQ7IDdwQAAAB\nZkIJ+SMAAAQDAEgwRgIhAPCadxv47cB4QOOvN9s/R23EepEbSI4/MuAdf5vKeU79\nAiEA2gZ3O8nzuUVanRW9fg6mgfsL08Nn/ZGm73O/F3IGX2UAdwCHdb/nWXz4jEOZ\nX73zbv9WjUdWNv9KtWDBtOr/XqCDDwAAAWZCCfoGAAAEAwBIMEYCIQDQjfmVBq+/\ny2gBK/eE9x6jz9hTZ5tIfhkStS85fMAxegIhAMAuNkyuO4t06EiEgMWialJZuAnk\nQw29GceIBG8r1Ap3MA0GCSqGSIb3DQEBCwUAA4IBAQAAk873/+2QswKfJSQn+iZe\nZc8uiGg99pmd008E2cmGErqNzPclK9INbPjwxl9zIgTcXpF/UtBVVS1Z7Xo83qfs\nGMPdMzYt3F+hQpQYLhlGF+5Jf2x6Zb1zjT/aM3GMdGi+6OPV+YHl/wgUe45N6kRu\nDZnVL167+W3l2nD+mShO/7xqwHiZuz0GHyBHRp5i43blH0vg67sJ8guN81papupY\n2v78FcvR8j0ELfgyehtWpjF3vzKSq/rY2sMXTYzSPqMeksseHNhSMYU6Ww9h9pyM\naVnw9vahqf7nKHHcC2VRTUgkQfn9yDmmBOo0nQ8Xgfpd65/PaxVfBnuKfEkXBfpM\n-----END CERTIFICATE-----\n

I found this useful when trying to upload an SSL certificate via the Rancher REST API, and couldn't get the encoding correct.