You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

438 lines
18 KiB

  1. Quick notes:
  2. --------------------------------------------
  3. [tonu@x153 mysql-4.0]$ cat /etc/my.cnf
  4. [mysqld]
  5. ssl-ca=SSL/cacert.pem
  6. ssl-cert=SSL/server-cert.pem
  7. ssl-key=SSL/server-key.pem
  8. [mysql]
  9. ssl-ca=SSL/cacert.pem
  10. ssl-cert=SSL/client-cert.pem
  11. ssl-key=SSL/client-key.pem
  12. [mysqldump]
  13. ssl-ca=SSL/cacert.pem
  14. ssl-cert=SSL/client-cert.pem
  15. ssl-key=SSL/client-key.pem
  16. [tonu@x153 mysql-4.0]$
  17. --------------------------------------------
  18. To remove passwords from keyfiles:
  19. [tonu@x153 SSL]$ openssl rsa -inform pem < server-req.pem > server-key.pem
  20. read RSA key
  21. Enter PEM pass phrase:
  22. writing RSA key
  23. [tonu@x153 SSL]$
  24. --------------------------------------------
  25. To run server:
  26. sql/mysqld --ssl-ca=SSL/cacert.pem --ssl-cert=SSL/server-cert.pem --ssl-key=SSL/server-key.pem --skip-grant --debug='d:t:O,-' > /tmp/mysqld.trace
  27. --------------------------------------------
  28. To run client:
  29. client/mysql --ssl-ca=SSL/cacert.pem --ssl-cert=SSL/server-cert.pem --ssl-key=SSL/server-key.pem --debug='d:t:O,/tmp/client.trace' -h 127.0.0.1
  30. --------------------------------------------
  31. openssl s_client -host 127.0.0.1 -port 1111 -debug -verify 1 -cert ../SSL/client-cert.pem -key ../SSL/client-key.pem -CAfile ../SSL/cacert.pem -pause -showcerts -state
  32. --------------------------------------------
  33. openssl s_server -port 1111 -cert ../SSL/server-cert.pem -key ../SSL/server-key.pem
  34. -------------------------------------------
  35. How to generate new keys:
  36. First we need the private key of the CA cert. Since we always throw
  37. away the old private key for the CA, we need to generate a totally new
  38. CA cert. Our CA cert is self signed and we will use that to sign the
  39. server and client keys. As long as we distibute the cacert.pem they can
  40. b oth be validated against that.
  41. 1) openssl genrsa 512 > cecert.pem
  42. 2) openssl req -new -x509 -nodes -md5 -days 1000 -key cacert.pem > cacert.pem
  43. We now have a cacert.pem which is the public key and a cakey.pem which is the
  44. private key of the CA.
  45. Steps to generate the server key.
  46. 3) openssl req -newkey rsa:512 -md5 -days 1000 -nodes -keyout server-key.pem > server-req.pem
  47. 4) copy ca-key.pem ca-cert.srl
  48. 5) openssl x509 -req -in server-req.pem -days 1000 -md5 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > server-cert.pem
  49. -- adding metadata to beginning
  50. 6) openssl x509 -in server-cert.pem -text > tmp.pem
  51. 7) mv tmp.pem server-cert.pem
  52. -- And almost the same for the client.
  53. 8) openssl req -newkey rsa:512 -md5 -days 1000 -nodes -keyout client-key.pem > client-req.pem
  54. 9) openssl x509 -req -in client-req.pem -days 1000 -md5 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > client-cert.pem
  55. -- adding metadata to beginning
  56. 10) openssl x509 -in client-cert.pem -text > tmp.pem
  57. 11) mv tmp.pem client-cert.pem
  58. The new certs are now generated. They can be verified against the cacert to test they are ok. This is actually what is done in the MySQL client and server.
  59. 12) openssl verify -CAfile cacert.pem server-cert.pem
  60. server-cert.pem: OK
  61. 13) openssl verify -CAfile cacert.pem client-cert.pm
  62. client-cert.pem: OK
  63. The files we add to our repository and thus distribute are
  64. * cacert.pem - CA's public key, used to verify the client/servers pblic keys
  65. * server-key.pem - servers private key
  66. * server-cert.pem - servers public key
  67. * client-key.pem - clients private key
  68. * client-cert.pem - clients public key
  69. == OLD NOTES below ==
  70. --------------------------------------------
  71. CA stuff:
  72. [tonu@x153 bin]$ pwd
  73. /usr/local/ssl/bin
  74. [tonu@x153 bin]$
  75. [tonu@x153 bin]$ ./CA.sh
  76. [tonu@x153 bin]$ ./CA.sh -h
  77. usage: CA -newcert|-newreq|-newca|-sign|-verify
  78. [tonu@x153 bin]$
  79. [root@x153 bin]# ./CA.sh -newca
  80. CA certificate filename (or enter to create)
  81. Making CA certificate ...
  82. Using configuration from /usr/lib/ssl/openssl.cnf
  83. Generating a 1024 bit RSA private key
  84. .++++++
  85. ................++++++
  86. writing new private key to './demoCA/private/./cakey.pem'
  87. Enter PEM pass phrase:
  88. Verifying password - Enter PEM pass phrase:
  89. phrase is too short, needs to be at least 4 chars
  90. Enter PEM pass phrase:
  91. Verifying password - Enter PEM pass phrase:
  92. -----
  93. You are about to be asked to enter information that will be incorporated
  94. into your certificate request.
  95. What you are about to enter is what is called a Distinguished Name or a DN.
  96. There are quite a few fields but you can leave some blank
  97. For some fields there will be a default value,
  98. If you enter '.', the field will be left blank.
  99. -----
  100. ountry Name (2 letter code) [AU]:FI
  101. State or Province Name (full name) [Some-State]:
  102. Locality Name (eg, city) []:Helsinki
  103. Organization Name (eg, company) [Internet Widgits Pty Ltd]:MySQL Finland AB
  104. Organizational Unit Name (eg, section) []:
  105. Common Name (eg, YOUR name) []:Tonu Samuel
  106. Email Address []:tonu@mysql.com
  107. [root@x153 bin]#
  108. [root@x153 bin]# ls -la demoCA/
  109. total 13
  110. drwxr-xr-x 6 root root 232 Jun 24 18:50 ./
  111. drwxr-xr-x 3 root root 2136 Jun 24 18:41 ../
  112. -rw-r--r-- 1 root root 1241 Jun 24 18:50 cacert.pem
  113. drwxr-xr-x 2 root root 48 Jun 24 18:41 certs/
  114. drwxr-xr-x 2 root root 48 Jun 24 18:41 crl/
  115. -rw-r--r-- 1 root root 0 Jun 24 18:44 index.txt
  116. drwxr-xr-x 2 root root 48 Jun 24 18:41 newcerts/
  117. drwxr-xr-x 2 root root 80 Jun 24 18:44 private/
  118. -rw-r--r-- 1 root root 3 Jun 24 18:44 serial
  119. [root@x153 bin]#
  120. [root@x153 bin]# ls -la demoCA/private/
  121. total 5
  122. drwxr-xr-x 2 root root 80 Jun 24 18:44 ./
  123. drwxr-xr-x 6 root root 232 Jun 24 18:50 ../
  124. -rw-r--r-- 1 root root 963 Jun 24 18:50 cakey.pem
  125. [root@x153 bin]#
  126. [root@x153 bin]# ./CA.sh -newreq
  127. Using configuration from /usr/lib/ssl/openssl.cnf
  128. Generating a 1024 bit RSA private key
  129. ..................++++++
  130. ........................++++++
  131. writing new private key to 'newreq.pem'
  132. Enter PEM pass phrase: <- new key password, not CA
  133. Verifying password - Enter PEM pass phrase:
  134. -----
  135. You are about to be asked to enter information that will be incorporated
  136. into your certificate request.
  137. What you are about to enter is what is called a Distinguished Name or a DN.
  138. There are quite a few fields but you can leave some blank
  139. For some fields there will be a default value,
  140. If you enter '.', the field will be left blank.
  141. -----
  142. Country Name (2 letter code) [AU]:EE
  143. State or Province Name (full name) [Some-State]:
  144. Locality Name (eg, city) []:Tallinn
  145. Organization Name (eg, company) [Internet Widgits Pty Ltd]:Noname
  146. Organizational Unit Name (eg, section) []:
  147. Common Name (eg, YOUR name) []:Mr Noname
  148. Email Address []:a@b.c
  149. Please enter the following 'extra' attributes
  150. to be sent with your certificate request
  151. A challenge password []:
  152. An optional company name []:
  153. Request (and private key) is in newreq.pem
  154. [root@x153 bin]#
  155. [root@x153 bin]# ls -la newreq.pem
  156. -rw-r--r-- 1 root root 1623 Jun 24 18:54 newreq.pem
  157. [root@x153 bin]#
  158. [root@x153 bin]# ./CA.sh -sign
  159. Using configuration from /usr/lib/ssl/openssl.cnf
  160. Enter PEM pass phrase: <- CA's one!
  161. Check that the request matches the signature
  162. Signature ok
  163. The Subjects Distinguished Name is as follows
  164. countryName :PRINTABLE:'EE'
  165. stateOrProvinceName :PRINTABLE:'Some-State'
  166. localityName :PRINTABLE:'Tallinn'
  167. organizationName :PRINTABLE:'Noname'
  168. commonName :PRINTABLE:'Mr Noname'
  169. emailAddress :IA5STRING:'a@b.c'
  170. Certificate is to be certified until Jun 24 15:50:23 2002 GMT (365 days)
  171. Sign the certificate? [y/n]:y
  172. 1 out of 1 certificate requests certified, commit? [y/n]y
  173. Write out database with 1 new entries
  174. Data Base Updated
  175. Certificate:
  176. Data:
  177. Version: 3 (0x2)
  178. Serial Number: 1 (0x1)
  179. Signature Algorithm: md5WithRSAEncryption
  180. Issuer: C=FI, ST=Some-State, L=Helsinki, O=MySQL Finland AB, CN=Tonu Samuel/Email=tonu@mysql.com
  181. Validity
  182. Not Before: Jun 24 15:50:23 2001 GMT
  183. Not After : Jun 24 15:50:23 2002 GMT
  184. Subject: C=EE, ST=Some-State, L=Tallinn, O=Noname, CN=Mr Noname/Email=a@b.c
  185. Subject Public Key Info:
  186. Public Key Algorithm: rsaEncryption
  187. RSA Public Key: (1024 bit)
  188. Modulus (1024 bit):
  189. 00:ab:3b:7d:5b:6c:93:f6:46:1a:2c:46:73:6f:89:
  190. 8a:99:bb:e9:6b:94:0d:74:aa:aa:c4:5c:a2:61:cf:
  191. 56:bb:a1:a9:5a:37:c4:4e:b2:ec:5c:18:3a:a4:8d:
  192. af:3d:23:66:7c:85:7f:d1:f2:e3:fc:16:a7:4c:a2:
  193. d6:45:06:92:75:d8:a2:3b:f9:aa:77:da:26:b9:87:
  194. e0:df:50:54:e4:36:9f:35:87:39:8e:a6:7c:3e:a8:
  195. e4:49:1a:76:c2:6f:73:0b:22:93:2a:04:67:0d:7d:
  196. ae:34:5c:fe:7c:29:b8:a2:fe:1e:ef:d1:0c:4d:dd:
  197. 5b:7a:67:b0:0a:22:88:a0:af
  198. Exponent: 65537 (0x10001)
  199. X509v3 extensions:
  200. X509v3 Basic Constraints:
  201. CA:FALSE
  202. Netscape Comment:
  203. OpenSSL Generated Certificate
  204. X509v3 Subject Key Identifier:
  205. 83:D1:0D:52:0F:DE:61:2D:A6:10:20:B8:46:0C:77:D5:D2:D0:BE:20
  206. X509v3 Authority Key Identifier:
  207. keyid:A5:0A:D6:72:B5:DF:E4:C2:2B:7B:07:5E:D3:4D:52:07:E1:83:6B:7F
  208. DirName:/C=FI/ST=Some-State/L=Helsinki/O=MySQL Finland AB/CN=Tonu Samuel/Email=tonu@mysql.com
  209. serial:00
  210. Signature Algorithm: md5WithRSAEncryption
  211. 60:85:f7:d0:54:2a:67:88:0e:37:a6:a8:8e:fd:a0:c9:a1:d7:
  212. c6:fc:4c:2e:59:8d:88:6d:69:0a:b8:b2:67:5f:81:94:39:0e:
  213. ab:67:fc:8b:62:de:85:f6:b3:8c:2d:1a:e3:dc:28:fc:f5:99:
  214. 39:f0:3d:50:ca:88:c0:8e:f8:c2:02:5d:34:19:63:9f:c4:a2:
  215. f6:a8:81:c9:8d:6d:bd:c4:42:4a:0c:49:5a:cc:24:ea:65:80:
  216. dd:79:20:89:9e:ea:6b:80:7a:86:f9:bb:6d:24:3c:80:13:5b:
  217. e6:16:fc:3d:8d:f6:16:ea:33:25:c6:90:20:81:a4:b0:15:2e:
  218. 9c:1c
  219. -----BEGIN CERTIFICATE-----
  220. MIIDfjCCAuegAwIBAgIBATANBgkqhkiG9w0BAQQFADCBhTELMAkGA1UEBhMCRkkx
  221. EzARBgNVBAgTClNvbWUtU3RhdGUxETAPBgNVBAcTCEhlbHNpbmtpMRkwFwYDVQQK
  222. ExBNeVNRTCBGaW5sYW5kIEFCMRQwEgYDVQQDEwtUb251IFNhbXVlbDEdMBsGCSqG
  223. SIb3DQEJARYOdG9udUBteXNxbC5jb20wHhcNMDEwNjI0MTU1MDIzWhcNMDIwNjI0
  224. MTU1MDIzWjBvMQswCQYDVQQGEwJFRTETMBEGA1UECBMKU29tZS1TdGF0ZTEQMA4G
  225. A1UEBxMHVGFsbGlubjEPMA0GA1UEChMGTm9uYW1lMRIwEAYDVQQDEwlNciBOb25h
  226. bWUxFDASBgkqhkiG9w0BCQEWBWFAYi5jMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB
  227. iQKBgQCrO31bbJP2RhosRnNviYqZu+lrlA10qqrEXKJhz1a7oalaN8ROsuxcGDqk
  228. ja89I2Z8hX/R8uP8FqdMotZFBpJ12KI7+ap32ia5h+DfUFTkNp81hzmOpnw+qORJ
  229. GnbCb3MLIpMqBGcNfa40XP58Kbii/h7v0QxN3Vt6Z7AKIoigrwIDAQABo4IBETCC
  230. AQ0wCQYDVR0TBAIwADAsBglghkgBhvhCAQ0EHxYdT3BlblNTTCBHZW5lcmF0ZWQg
  231. Q2VydGlmaWNhdGUwHQYDVR0OBBYEFIPRDVIP3mEtphAguEYMd9XS0L4gMIGyBgNV
  232. HSMEgaowgaeAFKUK1nK13+TCK3sHXtNNUgfhg2t/oYGLpIGIMIGFMQswCQYDVQQG
  233. EwJGSTETMBEGA1UECBMKU29tZS1TdGF0ZTERMA8GA1UEBxMISGVsc2lua2kxGTAX
  234. BgNVBAoTEE15U1FMIEZpbmxhbmQgQUIxFDASBgNVBAMTC1RvbnUgU2FtdWVsMR0w
  235. GwYJKoZIhvcNAQkBFg50b251QG15c3FsLmNvbYIBADANBgkqhkiG9w0BAQQFAAOB
  236. gQBghffQVCpniA43pqiO/aDJodfG/EwuWY2IbWkKuLJnX4GUOQ6rZ/yLYt6F9rOM
  237. LRrj3Cj89Zk58D1QyojAjvjCAl00GWOfxKL2qIHJjW29xEJKDElazCTqZYDdeSCJ
  238. nuprgHqG+bttJDyAE1vmFvw9jfYW6jMlxpAggaSwFS6cHA==
  239. -----END CERTIFICATE-----
  240. Signed certificate is in newcert.pem
  241. [root@x153 bin]# ls -la demoCA/newcerts/
  242. total 5
  243. drwxr-xr-x 2 root root 72 Jun 24 18:58 ./
  244. drwxr-xr-x 6 root root 296 Jun 24 18:58 ../
  245. -rw-r--r-- 1 root root 3533 Jun 24 18:58 01.pem
  246. [root@x153 bin]#
  247. [root@x153 mysql-4.0]# ./sql/mysqld --ssl-cert=SSL/server-cert.pem --ssl-ca=SSL/cacert.pem --ssl-ke
  248. y=SSL/server-req.pem -L /home/tonu/mysql-4.0/sql/share/english/ -u root
  249. Enter PEM pass phrase:
  250. ./sql/mysqld: ready for connections
  251. [tonu@x153 mysql-4.0]$ client/mysql --ssl-key=SSL/client-req.pem --ssl-ca=SSL/cacert.pem --ssl-cert
  252. =SSL/client-cert.pem
  253. Enter PEM pass phrase:
  254. ERROR:
  255. [tonu@x153 mysql-4.0]$
  256. -8<------------------------
  257. SSL encrypts data between MySQL server and client.
  258. You need openssl (formerly SSLeay) for MySQL SSL support. Development
  259. and testing was done on openssl version 0.9.3a
  260. To compile MySQL one must do:
  261. ./configure --with-openssl=/usr
  262. or
  263. ./configure --with-openssl=yes
  264. There are sample keys and certificates included with MySQL tarball in
  265. directory ./SSL. They are meant to be for quick start and
  266. testing only. Using them in production environment means same as not
  267. using encryption. This is because private keys are publicly
  268. accessible for everyone. You must use openssl distribution for new key
  269. and certificate generation for both client and server.
  270. ----------- for manual: ---------------------
  271. *New API calls:*
  272. mysql_ssl_set() - Set SSL properties (key, certificate,
  273. certificates authority certificate). Must be called before
  274. mysql_real_connect();
  275. mysql_ssl_clear() - Clear and free resources occupied by
  276. mysql_ssl_set() API call.
  277. char *mysql_ssl_cipher(MYSQL *) - returns cipher in use. For example
  278. "DES-CDC3-SHA" means that you have combined triple DES symmetric
  279. algorithm and SHA
  280. hashing algorithm.
  281. *New command line switches:*
  282. --ssl Use SSL for connection (automatically set with
  283. other flags. This means one can use encrypted connection without strong
  284. cryptological authentication. Normally one must use all switches
  285. together including ssl-key, ssl-cert and ssl-ca and never mind about
  286. --ssl because this is assumed by defult if any of them (--ssl-...)
  287. included.
  288. --ssl-key X509 key in PEM format (implies --ssl)
  289. --ssl-cert X509 cert in PEM format (implies --ssl)
  290. --ssl-ca CA file in PEM format (check OpenSSL docs,
  291. implies --ssl)
  292. --ssl-capath CA directory (check OpenSSL docs, implies --ssl
  293. ----------------
  294. This is about using SSL in MySQL privilege system. My idea is to make
  295. possible use of x509 certificates and keys instead of MySQL native
  296. passwords
  297. Some basic theory about crypt, SSL and x509:
  298. x509 is standard for certificates. SSL is standard for secure
  299. communication. Certificates are issued by someone anyone can trust. This
  300. trusted party is called "Certificate Authority" or "CA". This is
  301. someone, we MUST trust. Everyone must have some "fingerprint" of CA (so
  302. called "CA certificate" or "CA cert") using which one can verify
  303. authenticity of other
  304. certificates issued by this CA. CA uses his power to give certificates
  305. to persons (they can be physical (like "monty") or logical (like some
  306. process). Person is identified by "subject" like
  307. "/C=EE/ST=Harjumaa/L=Tallinn/O=MySQL client bogus certificate/CN=Tonu
  308. Samuel/Email=<EMAIL: PROTECTED>". and signed cryptologically. This sign can be
  309. verified using CA-cert. So, if we trust CA, then we can trust identity
  310. of user.
  311. There can be many CA-s (usually not but who knows). Also there can be
  312. some users we don`t trust or have different privileges. This means we
  313. must have one table to hold CA-certs and other table to hold so called
  314. "subjects" (users). I think it`s a good idea to use existing structure
  315. of host/user/db/field and add some x509 relationship. Then we can
  316. use usual simple user/host pair or x509 subject/CA pair.
  317. So I think user must grant rights using old method GRANT blabla ON
  318. blabla TO blabla IDENTIFIED BY blabla
  319. or new way:
  320. -----------8<---------------------------
  321. GRANT blabla ON blabla TO blabla
  322. IDENTIFIED BY X509 SUBJECT "/C=EE/ST=Harjumaa/L=Tallinn/O=MySQL client
  323. bogus certificate/CN=Tonu Samuel/Email=<EMAIL: PROTECTED>" AND ISSUER
  324. "/C=EE/ST=Harjumaa/L=Tallinn/O=TCX AB/CN=Tonu
  325. Samuel/Email=<EMAIL: PROTECTED>";
  326. -----------8<---------------------------
  327. Please note the difference in Subject and Issuer. This command requests
  328. user to authenticate itself with exact subject and exact certificate
  329. issuer. Next possibility is just have any certificate of some good CA:
  330. -----------8<---------------------------
  331. GRANT blabla ON blabla TO blabla IDENTIFIED BY X509 ISSUER
  332. "/C=EE/ST=Harjumaa/L=Tallinn/O=TCX
  333. AB/CN=Tonu Samuel/Email=<EMAIL: PROTECTED>";
  334. -----------8<---------------------------
  335. or if any registered CA is good enough (usual case when only one CA is
  336. registered)
  337. but we care about exact user, then something like:
  338. -----------8<---------------------------
  339. GRANT blabla ON blabla TO blabla IDENTIFIED BY X509 SUBJECT
  340. "/C=EE/ST=Harjumaa/L=Tallinn/O=MySQL client
  341. bogus certificate/CN=Tonu Samuel/Email=<EMAIL: PROTECTED>";
  342. -----------8<---------------------------
  343. And case if user must authenticate itself but we don`t care about exact
  344. person until he have some certificate issued by CA registered in our
  345. system:
  346. -----------8<---------------------------
  347. GRANT blabla ON blabla TO blabla IDENTIFIED BY X509;
  348. -----------8<---------------------------
  349. Then additionally we need one exception. Let`s assume we need SSL
  350. encryption
  351. for preventing eavesdropping but we don`t care who it is at all. We need
  352. privilege to exclude all non-SSL users but we accept anyone using SSL.
  353. How
  354. this must be done in GRANT syntax? Maybe:
  355. -----------8<---------------------------
  356. GRANT blabla ON blabla TO blabla
  357. IDENTIFIED BY blabla AND USING SSL
  358. -----------8<---------------------------
  359. But maybe we want to add in future possibility to check different
  360. algorithms and key lengths? Something like:
  361. -----------8<---------------------------
  362. GRANT blabla ON blabla TO blabla IDENTIFIED BY blabla AND USING SSL WITH
  363. CIPHER "DES-CBC3-SHA" OR "DES-CBC3-MD5"
  364. -----------8<---------------------------
  365. Also we need some command to include/exclude CA certificates. This must
  366. be some commands like INSERT/DELETE/UPDATE/REPLACE to do it.
  367. All examples is given for clarify my problem. I asking for help because
  368. I don`t know
  369. any similar command in other SQL-s.
  370. ------------8<------------------------
  371. So, at moment SSL communications is ready and working. I don`t have this
  372. command iterface at moment yet and this can be changed a lot if someone
  373. can suggest good idea or reason to change them. We are ready to listen
  374. every opinion.
  375. About Kerberos: I just don`t know much about it. I have to read this
  376. again before I can comment. I never used it itself and forgot most of
  377. theory. Sorry. Anyway now the problem/need is known and I will put
  378. thinking about this in personal TODO.