mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / sql / des_key_file.cc
blob6e47a04020d54b55ca3e71d53177ee6ee3424a2f
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"
17 #include <m_ctype.h>
19 #ifdef HAVE_OPENSSL
21 struct st_des_keyschedule des_keyschedule[10];
22 uint des_default_key;
24 #define des_cs &my_charset_latin1
26 /**
27 Load DES keys from plaintext file into
28 memory on MySQL server startup and on command FLUSH DES_KEY_FILE.
30 @retval
31 0 ok
32 @retval
33 1 Error
37 bool
38 load_des_key_file(const char *file_name)
40 bool result=1;
41 File file;
42 IO_CACHE io;
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)))
49 goto error;
51 bzero((char*) des_keyschedule,sizeof(struct st_des_keyschedule) * 10);
52 des_default_key=15; // Impossible key
53 for (;;)
55 char *start, *end;
56 char buf[1024], offset;
57 st_des_keyblock keyblock;
58 uint length;
60 if (!(length=my_b_gets(&io,buf,sizeof(buf)-1)))
61 break; // End of file
62 offset=buf[0];
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++) ;
68 end=buf+length;
69 for (end=strend(buf) ;
70 end > start && !my_isgraph(des_cs, end[-1]) ; end--) ;
72 if (start != end)
74 DES_cblock ivec;
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,
79 (uchar *) &keyblock,
80 ivec);
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);
91 result=0;
93 error:
94 if (file >= 0)
96 my_close(file,MYF(0));
97 end_io_cache(&io);
99 VOID(pthread_mutex_unlock(&LOCK_des_key_file));
100 DBUG_RETURN(result);
102 #endif /* HAVE_OPENSSL */