45

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?

James
  • 103
  • 6
wrangler
  • 2,920
  • 5
  • 23
  • 20

6 Answers6

30

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)

Shane Madden
  • 112,982
  • 12
  • 174
  • 248
30

I found this script handy in making the file.

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/2FomnmiEGF02mVgb3ev8Prc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ly9vY3NwLmRpZ2ljZXJ0LmNvbTBNBggrBgEFBQcw\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.

Rohit Gupta
  • 134
  • 1
  • 2
  • 12
8

It's a standard, and you should follow.

The RSA standards (e.g. RFC 2459) only define a binary representation for keys. In practice, like OpenPGP keys (RFC 4880), they are often encoded in base 64 using the otherwise obsolete PEM standards (RFC 1421).

The PEM printable encoding section says:

To represent the encapsulated text of a PEM message, the encoding function's output is delimited into text lines (using local conventions), with each line except the last containing exactly 64 printable characters and the final line containing 64 or fewer printable characters.

johnnychen
  • 81
  • 1
  • 3
  • 2
    2459 is long obsolete (by 3280 in 2002 and 5280 in 2008) but this particular point (using ASN.1 DER which is binary) hasn't changed. OTOH [7468](https://tools.ietf.org/html/rfc7468) more recently (2015) makes official the de-facto PEM-style formats for several things including certs. – dave_thompson_085 Nov 24 '17 at 04:39
2

The line length, and so the line breaks, are due to 64-bit encoding used in the certificate files: see this Wikipedia article, for instance, http://en.wikipedia.org/wiki/Base64

Put newline characters (\n) into your string variable, instead.

ramruma
  • 2,730
  • 1
  • 14
  • 8
  • 2
    Sorry but the line length of 64 printable characters has nothing to do why the base64 encoding. – hoang Aug 10 '20 at 09:01
1

If -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- lines are not required for the CLI tool or API being used to pass the certificate, then just use:

awk 'NR>2 { sub(/\r/, ""); printf "%s\\n",last} { last=$0 }' crt.pem
Exadra37
  • 103
  • 4
Tombart
  • 2,013
  • 3
  • 27
  • 47
0

You can convert a one-line certificate string by adding delimiters and wrapping at character 67.

(
  echo "-----BEGIN CERTIFICATE-----"; 
  echo $CERTIFICATE | sed -e "s/.\{67\}/&\n/g"; 
  echo "-----END CERTIFICATE-----";
) > certificate.pem
Ryan
  • 105
  • 6