docker-entrypoint.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #!/bin/bash
  2. echo "Zammad S/MIME test certificate generation"
  3. # prepare crl stuff
  4. touch /tmp/index.txt
  5. echo 1000 > /tmp/serial
  6. if [[ ! -e "$CERT_DIR/RootCA.key" ]] || [[ ! -e "$CERT_DIR/RootCA.crt" ]] || [[ -z "$SKIP_REGENERATE" ]]
  7. then
  8. export CA="RootCA"
  9. echo "Generating RootCA.key and RootCA.crt"
  10. openssl req -batch -config ca.cnf \
  11. -new -x509 -days 7300 -sha256 -extensions v3_ca -out "${CERT_DIR}/RootCA.crt" \
  12. -newkey rsa:4096 -nodes -keyout "${CERT_DIR}/RootCA.key" \
  13. -subj "/emailAddress=RootCA@example.com/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com"
  14. echo "Generating RootCA.secret"
  15. cp pass.secret $CERT_DIR/RootCA.secret
  16. unset CA
  17. fi
  18. if [[ ! -e "$CERT_DIR/IntermediateCA.key" ]] || [[ ! -e "$CERT_DIR/IntermediateCA.crt" ]] || [[ -z "$SKIP_REGENERATE" ]]
  19. then
  20. export CA="RootCA"
  21. export ICA="IntermediateCA"
  22. echo "Generating IntermediateCA.key and IntermediateCA.csr"
  23. openssl req -batch -config intermediate.cnf \
  24. -new -sha256 -out $CERT_DIR/IntermediateCA.csr \
  25. -newkey rsa:4096 -nodes -keyout $CERT_DIR/IntermediateCA.key \
  26. -subj "/emailAddress=IntermediateCA@example.com/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com"
  27. echo "Generating IntermediateCA.crt"
  28. openssl ca -batch -config ca.cnf \
  29. -extensions v3_intermediate_ca -days 7300 -notext -md sha256 \
  30. -in $CERT_DIR/IntermediateCA.csr -out $CERT_DIR/IntermediateCA.crt
  31. echo "Generating IntermediateCA.secret"
  32. cp pass.secret $CERT_DIR/IntermediateCA.secret
  33. unset CA
  34. unset ICA
  35. fi
  36. if [[ ! -e "$CERT_DIR/ChainCA.crt" ]] || [[ -z "$SKIP_REGENERATE" ]]
  37. then
  38. echo "Generating ChainCA.key and ChainCA.csr"
  39. cp $CERT_DIR/IntermediateCA.key $CERT_DIR/ChainCA.key
  40. cat $CERT_DIR/IntermediateCA.crt $CERT_DIR/RootCA.crt > $CERT_DIR/ChainCA.crt
  41. echo "Generating IntermediateCA.secret"
  42. cp pass.secret $CERT_DIR/ChainCA.secret
  43. fi
  44. for EMAIL_ADDRESS in smime1@example.com smime2@example.com smime3@example.com smimedouble@example.com CaseInsenstive@eXample.COM pgp+smime-sender@example.com pgp+smime-recipient@example.com chain@example.com
  45. do
  46. if [[ ! -e "$CERT_DIR/$EMAIL_ADDRESS.crt" ]] || [[ -z "$SKIP_REGENERATE" ]]
  47. then
  48. export CA="RootCA"
  49. export ICA="IntermediateCA"
  50. echo "Generating $EMAIL_ADDRESS.key"
  51. openssl genrsa -aes256 -passout file:pass.secret -out $CERT_DIR/${EMAIL_ADDRESS}.key 4096
  52. echo "Generating $EMAIL_ADDRESS.csr (certificate signing request)"
  53. openssl req -batch -config intermediate.cnf \
  54. -new -sha256 -out $CERT_DIR/${EMAIL_ADDRESS}.csr \
  55. -key $CERT_DIR/${EMAIL_ADDRESS}.key -passin file:pass.secret \
  56. -subj "/emailAddress=${EMAIL_ADDRESS}/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com"
  57. echo "Generating $EMAIL_ADDRESS.crt (certificate)"
  58. if [ "$EMAIL_ADDRESS" != "smimedouble@example.com" ]
  59. then
  60. SAN="email:${EMAIL_ADDRESS}"
  61. else
  62. SAN="email:smimedouble@example.com,email:smimedouble@example.de"
  63. fi
  64. export SAN
  65. openssl ca -batch -config intermediate.cnf \
  66. -extensions smime -days 7300 -notext -md sha256 \
  67. -in $CERT_DIR/${EMAIL_ADDRESS}.csr -out $CERT_DIR/${EMAIL_ADDRESS}.crt
  68. unset SAN
  69. echo "Generating $EMAIL_ADDRESS.secret"
  70. cp pass.secret $CERT_DIR/$EMAIL_ADDRESS.secret
  71. unset CA
  72. unset ICA
  73. fi
  74. done
  75. echo "Generating a combo of private key and certificate for issue #3727"
  76. if [[ ! -e "$CERT_DIR/issue_3727.key" ]] || [[ -z "$SKIP_REGENERATE" ]]
  77. then
  78. cat "$CERT_DIR/smime1@example.com.key" "$CERT_DIR/smime1@example.com.crt" > "$CERT_DIR/issue_3727.key"
  79. cp "$CERT_DIR/smime1@example.com.secret" "$CERT_DIR/issue_3727.secret"
  80. # Get SHA1 fingerprint of the certificate, in lowercase.
  81. openssl x509 -fingerprint -sha1 -noout -in "$CERT_DIR/smime1@example.com.crt" | sed -r 's/.*=([0-9A-F:]{59})/\1/' | sed 's/://g' | tr '[:upper:]' '[:lower:]' > "$CERT_DIR/issue_3727.fingerprint"
  82. fi
  83. echo "Generating expired"
  84. FAKETIME=-10y date
  85. if [[ ! -e "$CERT_DIR/ExpiredCA.key" ]] || [[ ! -e "$CERT_DIR/ExpiredCA.crt" ]] || [[ -z "$SKIP_REGENERATE" ]]
  86. then
  87. export CA="ExpiredCA"
  88. echo "Generating ExpiredCA.key and ExpiredCA.crt"
  89. FAKETIME=-10y \
  90. openssl req -batch -config ca.cnf \
  91. -new -x509 -days 365 -sha256 -extensions v3_ca -out "${CERT_DIR}/ExpiredCA.crt" \
  92. -newkey rsa:4096 -nodes -keyout "${CERT_DIR}/ExpiredCA.key" \
  93. -subj "/emailAddress=ExpiredCA@example.com/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com"
  94. echo "Generating ExpiredCA.secret"
  95. cp pass.secret $CERT_DIR/ExpiredCA.secret
  96. unset CA
  97. fi
  98. if [[ ! -e "$CERT_DIR/ExpiredIntermediateCA.key" ]] || [[ ! -e "$CERT_DIR/ExpiredIntermediateCA.crt" ]] || [[ -z "$SKIP_REGENERATE" ]]
  99. then
  100. export CA="ExpiredCA"
  101. export ICA="ExpiredIntermediateCA"
  102. echo "Generating ExpiredIntermediateCA.key and ExpiredIntermediateCA.csr"
  103. FAKETIME=-10y \
  104. openssl req -batch -config intermediate.cnf \
  105. -new -sha256 -out $CERT_DIR/ExpiredIntermediateCA.csr \
  106. -newkey rsa:4096 -nodes -keyout $CERT_DIR/ExpiredIntermediateCA.key \
  107. -subj "/emailAddress=ExpiredIntermediateCA@example.com/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com"
  108. echo "Generating ExpiredIntermediateCA.crt"
  109. FAKETIME=-10y \
  110. openssl ca -batch -config ca.cnf \
  111. -extensions v3_intermediate_ca -days 365 -notext -md sha256 \
  112. -in $CERT_DIR/ExpiredIntermediateCA.csr -out $CERT_DIR/ExpiredIntermediateCA.crt
  113. echo "Generating ExpiredIntermediateCA.secret"
  114. cp pass.secret $CERT_DIR/ExpiredIntermediateCA.secret
  115. unset CA
  116. unset ICA
  117. fi
  118. for EMAIL_ADDRESS in expiredsmime1@example.com expiredsmime2@example.com
  119. do
  120. if [[ ! -e "$CERT_DIR/$EMAIL_ADDRESS.crt" ]] || [[ -z "$SKIP_REGENERATE" ]]
  121. then
  122. export CA="ExpiredCA"
  123. export ICA="ExpiredIntermediateCA"
  124. echo "Generating $EMAIL_ADDRESS.key"
  125. FAKETIME=-10y \
  126. openssl genrsa -aes256 -passout file:pass.secret -out $CERT_DIR/$EMAIL_ADDRESS.key 4096
  127. echo "Generating $EMAIL_ADDRESS.csr (certificate signing request)"
  128. FAKETIME=-10y \
  129. openssl req -batch -config intermediate.cnf \
  130. -new -sha256 -out $CERT_DIR/${EMAIL_ADDRESS}.csr \
  131. -key $CERT_DIR/${EMAIL_ADDRESS}.key -passin file:pass.secret \
  132. -subj "/emailAddress=${EMAIL_ADDRESS}/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com"
  133. echo "Generating $EMAIL_ADDRESS.crt (certificate)"
  134. export SAN="email:${EMAIL_ADDRESS}"
  135. FAKETIME=-10y \
  136. openssl ca -batch -config intermediate.cnf \
  137. -extensions smime -days 365 -notext -md sha256 \
  138. -in $CERT_DIR/${EMAIL_ADDRESS}.csr -out $CERT_DIR/${EMAIL_ADDRESS}.crt
  139. unset SAN
  140. echo "Generating $EMAIL_ADDRESS.secret"
  141. cp pass.secret $CERT_DIR/$EMAIL_ADDRESS.secret
  142. unset CA
  143. unset ICA
  144. fi
  145. done
  146. if [[ ! -e "$CERT_DIR/SenderCA.key" ]] || [[ ! -e "$CERT_DIR/SenderCA.crt" ]] || [[ -z "$SKIP_REGENERATE" ]]
  147. then
  148. export CA="SenderCA"
  149. echo "Generating SenderCA.key and SenderCA.crt"
  150. openssl req -batch -config ca.cnf \
  151. -new -x509 -days 7300 -sha256 -extensions v3_ca -out "${CERT_DIR}/SenderCA.crt" \
  152. -newkey rsa:4096 -nodes -keyout "${CERT_DIR}/SenderCA.key" \
  153. -subj "/emailAddress=SenderCA@example.com/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com"
  154. echo "Generating SenderCA.secret"
  155. cp pass.secret $CERT_DIR/SenderCA.secret
  156. unset CA
  157. fi
  158. if [[ ! -e "$CERT_DIR/SenderIntermediateCA.key" ]] || [[ ! -e "$CERT_DIR/SenderIntermediateCA.crt" ]] || [[ -z "$SKIP_REGENERATE" ]]
  159. then
  160. export CA="SenderCA"
  161. export ICA="SenderIntermediateCA"
  162. echo "Generating SenderIntermediateCA.key and SenderIntermediateCA.csr"
  163. openssl req -batch -config intermediate.cnf \
  164. -new -sha256 -out $CERT_DIR/SenderIntermediateCA.csr \
  165. -newkey rsa:4096 -nodes -keyout $CERT_DIR/SenderIntermediateCA.key \
  166. -subj "/emailAddress=SenderIntermediateCA@example.com/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com"
  167. echo "Generating SenderIntermediateCA.crt"
  168. openssl ca -batch -config ca.cnf \
  169. -extensions v3_intermediate_ca -days 7300 -notext -md sha256 \
  170. -in $CERT_DIR/SenderIntermediateCA.csr -out $CERT_DIR/SenderIntermediateCA.crt
  171. echo "Generating SenderIntermediateCA.secret"
  172. cp pass.secret $CERT_DIR/SenderIntermediateCA.secret
  173. unset CA
  174. unset ICA
  175. fi
  176. EMAIL_ADDRESS="smime-sender-ca@example.com"
  177. if [[ ! -e "$CERT_DIR/$EMAIL_ADDRESS.crt" ]] || [[ -z "$SKIP_REGENERATE" ]]
  178. then
  179. export CA="SenderCA"
  180. export ICA="SenderIntermediateCA"
  181. echo "Generating $EMAIL_ADDRESS.key"
  182. openssl genrsa -aes256 -passout file:pass.secret -out $CERT_DIR/${EMAIL_ADDRESS}.key 4096
  183. echo "Generating $EMAIL_ADDRESS.csr (certificate signing request)"
  184. openssl req -batch -config intermediate.cnf \
  185. -new -sha256 -out $CERT_DIR/${EMAIL_ADDRESS}.csr \
  186. -key $CERT_DIR/${EMAIL_ADDRESS}.key -passin file:pass.secret \
  187. -subj "/emailAddress=${EMAIL_ADDRESS}/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com"
  188. echo "Generating $EMAIL_ADDRESS.crt (certificate)"
  189. if [ "$EMAIL_ADDRESS" != "smimedouble@example.com" ]
  190. then
  191. SAN="email:${EMAIL_ADDRESS}"
  192. else
  193. # special config that contains two email addresses in one certificate
  194. SAN="email:smimedouble@example.com,email:smimedouble.example.de"
  195. fi
  196. export SAN
  197. openssl ca -batch -config intermediate.cnf \
  198. -extensions smime -days 7300 -notext -md sha256 \
  199. -in $CERT_DIR/${EMAIL_ADDRESS}.csr -out $CERT_DIR/${EMAIL_ADDRESS}.crt
  200. unset SAN
  201. echo "Generating $EMAIL_ADDRESS.secret"
  202. cp pass.secret $CERT_DIR/$EMAIL_ADDRESS.secret
  203. unset CA
  204. unset ICA
  205. fi
  206. echo "Generating test mails"
  207. for TEST_MAIL_SIGNER in sender_is_signer,smime1@example.com sender_not_signer,smime1@example.com sender_is_signer_with_ca,smime-sender-ca@example.com
  208. do
  209. TEST_MAIL=${TEST_MAIL_SIGNER%,*}
  210. TEST_SIGNER=${TEST_MAIL_SIGNER#*,}
  211. if [[ ! -e "$CERT_DIR/$TEST_MAIL.eml" ]] || [[ -z "$SKIP_REGENERATE" ]]
  212. then
  213. if [[ ! -e "$CERT_DIR/$TEST_MAIL.eml.head.txt" ]] || [[ ! -e "$CERT_DIR/$TEST_MAIL.eml.body.txt" ]] || [[ -n "$SKIP_REGENERATE" ]]
  214. then
  215. echo "$CERT_DIR/$TEST_MAIL.eml.head.txt or $CERT_DIR/$TEST_MAIL.eml.body.txt not found, skipping..."
  216. continue
  217. fi
  218. if [[ ! -e "$CERT_DIR/$TEST_SIGNER.crt" ]] || [[ ! -e "$CERT_DIR/$TEST_SIGNER.key" ]] || [[ ! -e "$CERT_DIR/$TEST_SIGNER.secret" ]] || [[ -n "$SKIP_REGENERATE" ]]
  219. then
  220. echo "$CERT_DIR/$TEST_SIGNER.secret or $CERT_DIR/$TEST_SIGNER.secret or $CERT_DIR/$TEST_SIGNER.secret not found, skipping..."
  221. continue
  222. fi
  223. if [ $TEST_SIGNER != "smime-sender-ca@example.com" ]
  224. then
  225. CERTFILE="RootCA.crt"
  226. else
  227. CERTFILE="SenderCA.crt"
  228. fi
  229. echo "Generating $CERT_DIR/$TEST_MAIL.eml"
  230. openssl smime -sign -in "$CERT_DIR/$TEST_MAIL.eml.body.txt" -out "$CERT_DIR/$TEST_MAIL.eml" \
  231. -signer "$CERT_DIR/$TEST_SIGNER.crt" -inkey "$CERT_DIR/$TEST_SIGNER.key" \
  232. -certfile "$CERT_DIR/$CERTFILE" -text -passin "file:$CERT_DIR/$TEST_SIGNER.secret"
  233. cat "$CERT_DIR/$TEST_MAIL.eml.head.txt" "$CERT_DIR/$TEST_MAIL.eml" > /tmp/test_mail && mv /tmp/test_mail "$CERT_DIR/$TEST_MAIL.eml"
  234. fi
  235. done
  236. echo "Generating further certificates for test variation"
  237. certs=(
  238. "alice@acme.corp,true,true,false,true,alice@acme.corp+sign+encrypt,rsa"
  239. "alice@acme.corp,false,true,false,true,alice@acme.corp+encrypt,rsa"
  240. "alice@acme.corp,true,false,false,true,alice@acme.corp+sign,rsa"
  241. "alice@acme.corp,true,true,true,true,alice@acme.corp+sign+encrypt+expired,rsa"
  242. "alice@acme.corp,true,true,true,true,alice@acme.corp+sign+encrypt+ec,ec"
  243. "alice@acme.corp,true,true,true,false,alice@acme.corp+sign+encrypt+future,rsa"
  244. )
  245. # email sign encrypt expired effective filename algorithm
  246. for cert in "${certs[@]}"; do
  247. IFS=$',' read -r email sign encrypt expired effective filename algorithm <<< "$cert"
  248. if [[ -e "$CERT_DIR/$filename.crt" ]] && [[ -n "$SKIP_REGENERATE" ]]
  249. then
  250. continue
  251. fi
  252. if [ "$sign" == "true" ] && [ "$encrypt" == "true" ]
  253. then
  254. KU="nonRepudiation, digitalSignature, keyEncipherment"
  255. elif [ "$sign" == "true" ]
  256. then
  257. KU="nonRepudiation, digitalSignature"
  258. elif [ "$encrypt" == "true" ]
  259. then
  260. KU="keyEncipherment"
  261. fi
  262. FAKETIME=""
  263. if [ "$expired" == "true" ]
  264. then
  265. FAKETIME=-10y
  266. fi
  267. if [ "$effective" == "false" ]
  268. then
  269. FAKETIME=+10y
  270. fi
  271. echo "Generating $filename.key"
  272. if [ "$algorithm" == "ec" ]
  273. then
  274. openssl genpkey -algorithm EC \
  275. -pkeyopt ec_paramgen_curve:prime192v1 \
  276. -pkeyopt ec_param_enc:named_curve \
  277. -out $CERT_DIR/${filename}.key -pass file:pass.secret
  278. else
  279. openssl genrsa -aes256 -passout file:pass.secret -out $CERT_DIR/${filename}.key 4096
  280. fi
  281. echo "Generating $filename.csr (certificate signing request)"
  282. openssl req -batch -config intermediate.cnf \
  283. -new -sha256 -out $CERT_DIR/${filename}.csr \
  284. -key $CERT_DIR/${filename}.key -passin file:pass.secret \
  285. -subj "/emailAddress=${email}/C=DE/ST=Berlin/L=Berlin/O=Example Security/OU=IT Department/CN=example.com"
  286. echo "Generating $filename.crt (certificate)"
  287. export SAN="email:${email}"
  288. export KU
  289. export FAKETIME
  290. openssl ca -batch -config intermediate.cnf \
  291. -extensions smime -days 3285 -notext -md sha256 \
  292. -in $CERT_DIR/${filename}.csr -out $CERT_DIR/${filename}.crt
  293. unset SAN
  294. unset KU
  295. unset FAKETIME
  296. echo "Generating $filename.secret"
  297. cp pass.secret $CERT_DIR/$filename.secret
  298. # get rid of crl stuff because of email reusage
  299. rm -f /tmp/*
  300. touch /tmp/index.txt
  301. echo 1000 > /tmp/serial
  302. rm -f $CERT_DIR/*.pem
  303. done
  304. # cleanup serial number named certificate copies
  305. rm -f $CERT_DIR/*.pem
  306. # run command passed to docker run
  307. exec "$@"