1 /* Copyright (c) 2001-2003, 2005-2007 MySQL AB
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 #include "mysql_priv.h"
21 struct st_des_keyschedule des_keyschedule
[10];
24 #define des_cs &my_charset_latin1
27 Load DES keys from plaintext file into
28 memory on MySQL server startup and on command FLUSH DES_KEY_FILE.
38 load_des_key_file(const char *file_name
)
43 DBUG_ENTER("load_des_key_file");
44 DBUG_PRINT("enter",("name: %s",file_name
));
46 VOID(pthread_mutex_lock(&LOCK_des_key_file
));
47 if ((file
=my_open(file_name
,O_RDONLY
| O_BINARY
,MYF(MY_WME
))) < 0 ||
48 init_io_cache(&io
, file
, IO_SIZE
*2, READ_CACHE
, 0, 0, MYF(MY_WME
)))
51 bzero((char*) des_keyschedule
,sizeof(struct st_des_keyschedule
) * 10);
52 des_default_key
=15; // Impossible key
56 char buf
[1024], offset
;
57 st_des_keyblock keyblock
;
60 if (!(length
=my_b_gets(&io
,buf
,sizeof(buf
)-1)))
63 if (offset
>= '0' && offset
<= '9') // If ok key
65 offset
=(char) (offset
- '0');
66 // Remove newline and possible other control characters
67 for (start
=buf
+1 ; my_isspace(des_cs
, *start
) ; start
++) ;
69 for (end
=strend(buf
) ;
70 end
> start
&& !my_isgraph(des_cs
, end
[-1]) ; end
--) ;
75 bzero((char*) &ivec
,sizeof(ivec
));
76 // We make good 24-byte (168 bit) key from given plaintext key with MD5
77 EVP_BytesToKey(EVP_des_ede3_cbc(),EVP_md5(),NULL
,
78 (uchar
*) start
, (int) (end
-start
),1,
81 DES_set_key_unchecked(&keyblock
.key1
,&(des_keyschedule
[(int)offset
].ks1
));
82 DES_set_key_unchecked(&keyblock
.key2
,&(des_keyschedule
[(int)offset
].ks2
));
83 DES_set_key_unchecked(&keyblock
.key3
,&(des_keyschedule
[(int)offset
].ks3
));
84 if (des_default_key
== 15)
85 des_default_key
= (uint
) offset
; // use first as def.
88 else if (offset
!= '#')
89 sql_print_error("load_des_file: Found wrong key_number: %c",offset
);
96 my_close(file
,MYF(0));
99 VOID(pthread_mutex_unlock(&LOCK_des_key_file
));
102 #endif /* HAVE_OPENSSL */