SSL 证书默认在 67 个字符后有换行符。我正在尝试使用 Chef 创建 SSL 证书文件。本质上,我想从字符串变量创建整个证书文件,没有任何换行符。我试过几次,但无济于事(Apache 抱怨找不到证书)。
我不明白 SSL 证书中为什么需要换行符。您知道是否有可能在文件中生成一个不带换行符的证书吗?
答案1
不,如果没有换行符,证书将无法被正确处理 - 软件将无法解析它。
如果您尝试将其传递到字符串中,为什么不直接将它们包含在其中?(\n
)
答案2
我发现这个脚本对于制作文件很方便。
awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}' ca.pem
这将获取多行标准 PEM 文件,用 替换“隐藏”换行符\n
,并将输出打印为单行。
例如,这将生成以下 PEM 文件...
-----BEGIN CERTIFICATE-----
MIIIPDCCBySgAwIBAgIQB2XGTnTlkdaAOcoqhHVj8DANBgkqhkiG9w0BAQsFADBw
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
d3cuZGlnaWNlcnQuY29tMS8wLQYDVQQDEyZEaWdpQ2VydCBTSEEyIEhpZ2ggQXNz
dXJhbmNlIFNlcnZlciBDQTAeFw0xODEwMDUwMDAwMDBaFw0xOTA4MTQxMjAwMDBa
MGoxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJOWTERMA8GA1UEBxMITmV3IFlvcmsx
HTAbBgNVBAoTFFN0YWNrIEV4Y2hhbmdlLCBJbmMuMRwwGgYDVQQDDBMqLnN0YWNr
ZXhjaGFuZ2UuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA9Kk6
NXUP1ocXt89mT1cIxadBhzCL0YTqP01/DSotUHRzV70qOCT7AdMT0K1Ji6rFyapz
RiqUIhAkhEseXRp0MNr21fSUw4VoCb+ImO6gnIlzolkhrpK6Iy34KyU3zyt8XYD+
mY3i8GjPZOysRJNLy3ouQBmzuOmU/2FomnmiEGF02mVgb3ev8Prcnt7dCiF7liBI
sd3zkPeXvTVYcVcb/MBrFEzc4Fu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EFBQcw\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
当我尝试通过 Rancher REST API 上传 SSL 证书但无法获得正确的编码时,我发现这很有用。
答案3
答案4
行长以及换行符是由于证书文件使用了 64 位编码造成的:例如,请参阅此维基百科文章,http://en.wikipedia.org/wiki/Base64
将换行符(\n)放入字符串变量中。