選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

pkitool 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #!/bin/sh
  2. # OpenVPN -- An application to securely tunnel IP networks
  3. # over a single TCP/UDP port, with support for SSL/TLS-based
  4. # session authentication and key exchange,
  5. # packet encryption, packet authentication, and
  6. # packet compression.
  7. #
  8. # Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License version 2
  12. # as published by the Free Software Foundation.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program (see the file COPYING included with this
  21. # distribution); if not, write to the Free Software Foundation, Inc.,
  22. # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. # pkitool is a front-end for the openssl tool.
  24. # Calling scripts can set the certificate organizational
  25. # unit with the KEY_OU environmental variable.
  26. # Calling scripts can also set the KEY_NAME environmental
  27. # variable to set the "name" X509 subject field.
  28. PROGNAME=pkitool
  29. VERSION=2.0
  30. DEBUG=0
  31. die()
  32. {
  33. local m="$1"
  34. echo "$m" >&2
  35. exit 1
  36. }
  37. need_vars()
  38. {
  39. echo ' Please edit the vars script to reflect your configuration,'
  40. echo ' then source it with "source ./vars".'
  41. echo ' Next, to start with a fresh PKI configuration and to delete any'
  42. echo ' previous certificates and keys, run "./clean-all".'
  43. echo " Finally, you can run this tool ($PROGNAME) to build certificates/keys."
  44. }
  45. usage()
  46. {
  47. echo "$PROGNAME $VERSION"
  48. echo "Usage: $PROGNAME [options...] [common-name]"
  49. echo "Options:"
  50. echo " --batch : batch mode (default)"
  51. echo " --keysize : Set keysize"
  52. echo " size : size (default=1024)"
  53. echo " --interact : interactive mode"
  54. echo " --server : build server cert"
  55. echo " --initca : build root CA"
  56. echo " --inter : build intermediate CA"
  57. echo " --pass : encrypt private key with password"
  58. echo " --csr : only generate a CSR, do not sign"
  59. echo " --sign : sign an existing CSR"
  60. echo " --pkcs12 : generate a combined PKCS#12 file"
  61. echo " --pkcs11 : generate certificate on PKCS#11 token"
  62. echo " lib : PKCS#11 library"
  63. echo " slot : PKCS#11 slot"
  64. echo " id : PKCS#11 object id (hex string)"
  65. echo " label : PKCS#11 object label"
  66. echo "Standalone options:"
  67. echo " --pkcs11-slots : list PKCS#11 slots"
  68. echo " lib : PKCS#11 library"
  69. echo " --pkcs11-objects : list PKCS#11 token objects"
  70. echo " lib : PKCS#11 library"
  71. echo " slot : PKCS#11 slot"
  72. echo " --pkcs11-init : initialize PKCS#11 token DANGEROUS!!!"
  73. echo " lib : PKCS#11 library"
  74. echo " slot : PKCS#11 slot"
  75. echo " label : PKCS#11 token label"
  76. echo "Notes:"
  77. need_vars
  78. echo " In order to use PKCS#11 interface you must have opensc-0.10.0 or higher."
  79. echo "Generated files and corresponding OpenVPN directives:"
  80. echo '(Files will be placed in the $KEY_DIR directory, defined in ./vars)'
  81. echo " ca.crt -> root certificate (--ca)"
  82. echo " ca.key -> root key, keep secure (not directly used by OpenVPN)"
  83. echo " .crt files -> client/server certificates (--cert)"
  84. echo " .key files -> private keys, keep secure (--key)"
  85. echo " .csr files -> certificate signing request (not directly used by OpenVPN)"
  86. echo " dh1024.pem or dh2048.pem -> Diffie Hellman parameters (--dh)"
  87. echo "Examples:"
  88. echo " $PROGNAME --initca -> Build root certificate"
  89. echo " $PROGNAME --initca --pass -> Build root certificate with password-protected key"
  90. echo " $PROGNAME --server server1 -> Build \"server1\" certificate/key"
  91. echo " $PROGNAME client1 -> Build \"client1\" certificate/key"
  92. echo " $PROGNAME --pass client2 -> Build password-protected \"client2\" certificate/key"
  93. echo " $PROGNAME --pkcs12 client3 -> Build \"client3\" certificate/key in PKCS#12 format"
  94. echo " $PROGNAME --csr client4 -> Build \"client4\" CSR to be signed by another CA"
  95. echo " $PROGNAME --sign client4 -> Sign \"client4\" CSR"
  96. echo " $PROGNAME --inter interca -> Build an intermediate key-signing certificate/key"
  97. echo " Also see ./inherit-inter script."
  98. echo " $PROGNAME --pkcs11 /usr/lib/pkcs11/lib1 0 010203 \"client5 id\" client5"
  99. echo " -> Build \"client5\" certificate/key in PKCS#11 token"
  100. echo "Typical usage for initial PKI setup. Build myserver, client1, and client2 cert/keys."
  101. echo "Protect client2 key with a password. Build DH parms. Generated files in ./keys :"
  102. echo " [edit vars with your site-specific info]"
  103. echo " source ./vars"
  104. echo " ./clean-all"
  105. echo " ./build-dh -> takes a long time, consider backgrounding"
  106. echo " ./$PROGNAME --initca"
  107. echo " ./$PROGNAME --server myserver"
  108. echo " ./$PROGNAME client1"
  109. echo " ./$PROGNAME --pass client2"
  110. echo "Typical usage for adding client cert to existing PKI:"
  111. echo " source ./vars"
  112. echo " ./$PROGNAME client-new"
  113. }
  114. # Set tool defaults
  115. [ -n "$OPENSSL" ] || export OPENSSL="openssl"
  116. [ -n "$PKCS11TOOL" ] || export PKCS11TOOL="pkcs11-tool"
  117. [ -n "$GREP" ] || export GREP="grep"
  118. # Set defaults
  119. DO_REQ="1"
  120. REQ_EXT=""
  121. DO_CA="1"
  122. CA_EXT=""
  123. DO_P12="0"
  124. DO_P11="0"
  125. DO_ROOT="0"
  126. NODES_REQ="-nodes"
  127. NODES_P12=""
  128. BATCH="-batch"
  129. CA="ca"
  130. # must be set or errors of openssl.cnf
  131. PKCS11_MODULE_PATH="dummy"
  132. PKCS11_PIN="dummy"
  133. # Process options
  134. while [ $# -gt 0 ]; do
  135. case "$1" in
  136. --keysize ) KEY_SIZE=$2
  137. shift;;
  138. --server ) REQ_EXT="$REQ_EXT -extensions server"
  139. CA_EXT="$CA_EXT -extensions server" ;;
  140. --batch ) BATCH="-batch" ;;
  141. --interact ) BATCH="" ;;
  142. --inter ) CA_EXT="$CA_EXT -extensions v3_ca" ;;
  143. --initca ) DO_ROOT="1" ;;
  144. --pass ) NODES_REQ="" ;;
  145. --csr ) DO_CA="0" ;;
  146. --sign ) DO_REQ="0" ;;
  147. --pkcs12 ) DO_P12="1" ;;
  148. --pkcs11 ) DO_P11="1"
  149. PKCS11_MODULE_PATH="$2"
  150. PKCS11_SLOT="$3"
  151. PKCS11_ID="$4"
  152. PKCS11_LABEL="$5"
  153. shift 4;;
  154. # standalone
  155. --pkcs11-init)
  156. PKCS11_MODULE_PATH="$2"
  157. PKCS11_SLOT="$3"
  158. PKCS11_LABEL="$4"
  159. if [ -z "$PKCS11_LABEL" ]; then
  160. die "Please specify library name, slot and label"
  161. fi
  162. $PKCS11TOOL --module "$PKCS11_MODULE_PATH" --init-token --slot "$PKCS11_SLOT" \
  163. --label "$PKCS11_LABEL" &&
  164. $PKCS11TOOL --module "$PKCS11_MODULE_PATH" --init-pin --slot "$PKCS11_SLOT"
  165. exit $?;;
  166. --pkcs11-slots)
  167. PKCS11_MODULE_PATH="$2"
  168. if [ -z "$PKCS11_MODULE_PATH" ]; then
  169. die "Please specify library name"
  170. fi
  171. $PKCS11TOOL --module "$PKCS11_MODULE_PATH" --list-slots
  172. exit 0;;
  173. --pkcs11-objects)
  174. PKCS11_MODULE_PATH="$2"
  175. PKCS11_SLOT="$3"
  176. if [ -z "$PKCS11_SLOT" ]; then
  177. die "Please specify library name and slot"
  178. fi
  179. $PKCS11TOOL --module "$PKCS11_MODULE_PATH" --list-objects --login --slot "$PKCS11_SLOT"
  180. exit 0;;
  181. # errors
  182. --* ) die "$PROGNAME: unknown option: $1" ;;
  183. * ) break ;;
  184. esac
  185. shift
  186. done
  187. if ! [ -z "$BATCH" ]; then
  188. if $OPENSSL version | grep 0.9.6 > /dev/null; then
  189. die "Batch mode is unsupported in openssl<0.9.7"
  190. fi
  191. fi
  192. if [ $DO_P12 -eq 1 -a $DO_P11 -eq 1 ]; then
  193. die "PKCS#11 and PKCS#12 cannot be specified together"
  194. fi
  195. if [ $DO_P11 -eq 1 ]; then
  196. if ! grep "^pkcs11.*=" "$KEY_CONFIG" > /dev/null; then
  197. die "Please edit $KEY_CONFIG and setup PKCS#11 engine"
  198. fi
  199. fi
  200. # If we are generating pkcs12, only encrypt the final step
  201. if [ $DO_P12 -eq 1 ]; then
  202. NODES_P12="$NODES_REQ"
  203. NODES_REQ="-nodes"
  204. fi
  205. if [ $DO_P11 -eq 1 ]; then
  206. if [ -z "$PKCS11_LABEL" ]; then
  207. die "PKCS#11 arguments incomplete"
  208. fi
  209. fi
  210. # If undefined, set default key expiration intervals
  211. if [ -z "$KEY_EXPIRE" ]; then
  212. KEY_EXPIRE=3650
  213. fi
  214. if [ -z "$CA_EXPIRE" ]; then
  215. CA_EXPIRE=3650
  216. fi
  217. # Set organizational unit to empty string if undefined
  218. if [ -z "$KEY_OU" ]; then
  219. KEY_OU=""
  220. fi
  221. # Set X509 Name string to empty string if undefined
  222. if [ -z "$KEY_NAME" ]; then
  223. KEY_NAME=""
  224. fi
  225. # Set KEY_CN, FN
  226. if [ $DO_ROOT -eq 1 ]; then
  227. if [ -z "$KEY_CN" ]; then
  228. if [ "$1" ]; then
  229. KEY_CN="$1"
  230. elif [ "$KEY_ORG" ]; then
  231. KEY_CN="$KEY_ORG CA"
  232. fi
  233. fi
  234. if [ $BATCH ] && [ "$KEY_CN" ]; then
  235. echo "Using CA Common Name:" "$KEY_CN"
  236. fi
  237. FN="$KEY_CN"
  238. elif [ $BATCH ] && [ "$KEY_CN" ]; then
  239. echo "Using Common Name:" "$KEY_CN"
  240. FN="$KEY_CN"
  241. if [ "$1" ]; then
  242. FN="$1"
  243. fi
  244. else
  245. if [ $# -ne 1 ]; then
  246. usage
  247. exit 1
  248. else
  249. KEY_CN="$1"
  250. fi
  251. FN="$KEY_CN"
  252. fi
  253. export CA_EXPIRE KEY_EXPIRE KEY_OU KEY_NAME KEY_CN PKCS11_MODULE_PATH PKCS11_PIN
  254. # Show parameters (debugging)
  255. if [ $DEBUG -eq 1 ]; then
  256. echo DO_REQ $DO_REQ
  257. echo REQ_EXT $REQ_EXT
  258. echo DO_CA $DO_CA
  259. echo CA_EXT $CA_EXT
  260. echo NODES_REQ $NODES_REQ
  261. echo NODES_P12 $NODES_P12
  262. echo DO_P12 $DO_P12
  263. echo KEY_CN $KEY_CN
  264. echo BATCH $BATCH
  265. echo DO_ROOT $DO_ROOT
  266. echo KEY_EXPIRE $KEY_EXPIRE
  267. echo CA_EXPIRE $CA_EXPIRE
  268. echo KEY_OU $KEY_OU
  269. echo KEY_NAME $KEY_NAME
  270. echo DO_P11 $DO_P11
  271. echo PKCS11_MODULE_PATH $PKCS11_MODULE_PATH
  272. echo PKCS11_SLOT $PKCS11_SLOT
  273. echo PKCS11_ID $PKCS11_ID
  274. echo PKCS11_LABEL $PKCS11_LABEL
  275. fi
  276. # Make sure ./vars was sourced beforehand
  277. if [ -d "$KEY_DIR" ] && [ "$KEY_CONFIG" ]; then
  278. cd "$KEY_DIR"
  279. # Make sure $KEY_CONFIG points to the correct version
  280. # of openssl.cnf
  281. if $GREP -i 'easy-rsa version 2\.[0-9]' "$KEY_CONFIG" >/dev/null; then
  282. :
  283. else
  284. echo "$PROGNAME: KEY_CONFIG (set by the ./vars script) is pointing to the wrong"
  285. echo "version of openssl.cnf: $KEY_CONFIG"
  286. echo "The correct version should have a comment that says: easy-rsa version 2.x";
  287. exit 1;
  288. fi
  289. # Build root CA
  290. if [ $DO_ROOT -eq 1 ]; then
  291. $OPENSSL req $BATCH -days $CA_EXPIRE $NODES_REQ -new -newkey rsa:$KEY_SIZE -sha1 \
  292. -x509 -keyout "$CA.key" -out "$CA.crt" -config "$KEY_CONFIG" && \
  293. chmod 0600 "$CA.key"
  294. else
  295. # Make sure CA key/cert is available
  296. if [ $DO_CA -eq 1 ] || [ $DO_P12 -eq 1 ]; then
  297. if [ ! -r "$CA.crt" ] || [ ! -r "$CA.key" ]; then
  298. echo "$PROGNAME: Need a readable $CA.crt and $CA.key in $KEY_DIR"
  299. echo "Try $PROGNAME --initca to build a root certificate/key."
  300. exit 1
  301. fi
  302. fi
  303. # Generate key for PKCS#11 token
  304. PKCS11_ARGS=
  305. if [ $DO_P11 -eq 1 ]; then
  306. stty -echo
  307. echo -n "User PIN: "
  308. read -r PKCS11_PIN
  309. stty echo
  310. export PKCS11_PIN
  311. echo "Generating key pair on PKCS#11 token..."
  312. $PKCS11TOOL --module "$PKCS11_MODULE_PATH" --keypairgen \
  313. --login --pin "$PKCS11_PIN" \
  314. --key-type rsa:1024 \
  315. --slot "$PKCS11_SLOT" --id "$PKCS11_ID" --label "$PKCS11_LABEL" || exit 1
  316. PKCS11_ARGS="-engine pkcs11 -keyform engine -key $PKCS11_SLOT:$PKCS11_ID"
  317. fi
  318. # Build cert/key
  319. ( [ $DO_REQ -eq 0 ] || $OPENSSL req $BATCH -days $KEY_EXPIRE $NODES_REQ -new -newkey rsa:$KEY_SIZE \
  320. -keyout "$FN.key" -out "$FN.csr" $REQ_EXT -config "$KEY_CONFIG" $PKCS11_ARGS ) && \
  321. ( [ $DO_CA -eq 0 ] || $OPENSSL ca $BATCH -days $KEY_EXPIRE -out "$FN.crt" \
  322. -in "$FN.csr" $CA_EXT -md sha1 -config "$KEY_CONFIG" ) && \
  323. ( [ $DO_P12 -eq 0 ] || $OPENSSL pkcs12 -export -inkey "$FN.key" \
  324. -in "$FN.crt" -certfile "$CA.crt" -out "$FN.p12" $NODES_P12 ) && \
  325. ( [ $DO_CA -eq 0 -o $DO_P11 -eq 1 ] || chmod 0600 "$FN.key" ) && \
  326. ( [ $DO_P12 -eq 0 ] || chmod 0600 "$FN.p12" )
  327. # Load certificate into PKCS#11 token
  328. if [ $DO_P11 -eq 1 ]; then
  329. $OPENSSL x509 -in "$FN.crt" -inform PEM -out "$FN.crt.der" -outform DER && \
  330. $PKCS11TOOL --module "$PKCS11_MODULE_PATH" --write-object "$FN.crt.der" --type cert \
  331. --login --pin "$PKCS11_PIN" \
  332. --slot "$PKCS11_SLOT" --id "$PKCS11_ID" --label "$PKCS11_LABEL"
  333. [ -e "$FN.crt.der" ]; rm "$FN.crt.der"
  334. fi
  335. fi
  336. # Need definitions
  337. else
  338. need_vars
  339. fi