1 | /* |
---|
2 | * bbftpd/bbftpd_crypt.c |
---|
3 | * Copyright (C) 1999, 2000, 2001, 2002 IN2P3, CNRS |
---|
4 | * bbftp@in2p3.fr |
---|
5 | * http://doc.in2p3.fr/bbftp |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
20 | */ |
---|
21 | |
---|
22 | /**************************************************************************** |
---|
23 | |
---|
24 | |
---|
25 | |
---|
26 | bbftpd_crypt.c v 1.4.0 2000/03/22 |
---|
27 | v 1.6.1 2000/03/28 - Portage to OSF1 |
---|
28 | v 1.8.0 2000/04/14 - Introduce RSA Cryptage |
---|
29 | v 1.8.2 2000/04/17 - Portage to OSF1 |
---|
30 | v 1.8.4 2000/04/21 - Random seed done in do_deamon.c |
---|
31 | v 1.8.7 2000/05/24 - Modify headers |
---|
32 | v 1.8.10 2000/08/11 - Portage to Linux |
---|
33 | v 1.9.0 2000/08/18 - Use configure to help portage |
---|
34 | v 2.0.0 2000/12/18 - Use incontrolsock and outcontrolsock |
---|
35 | v 2.0.1 2001/04/23 - Correct indentation |
---|
36 | v 2.1.0 2001/06/11 - Change file name |
---|
37 | |
---|
38 | *****************************************************************************/ |
---|
39 | #include <bbftpd.h> |
---|
40 | |
---|
41 | #include <errno.h> |
---|
42 | #include <sys/types.h> |
---|
43 | #include <sys/socket.h> |
---|
44 | #include <netinet/in.h> |
---|
45 | #include <bbftpd_private_log.h> |
---|
46 | #if TIME_WITH_SYS_TIME |
---|
47 | # include <sys/time.h> |
---|
48 | # include <time.h> |
---|
49 | #else |
---|
50 | # if HAVE_SYS_TIME_H |
---|
51 | # include <sys/time.h> |
---|
52 | # else |
---|
53 | # include <time.h> |
---|
54 | # endif |
---|
55 | #endif |
---|
56 | |
---|
57 | #include <common.h> |
---|
58 | #include <daemon.h> |
---|
59 | #include <structures.h> |
---|
60 | #include <openssl/rsa.h> |
---|
61 | #include <openssl/err.h> |
---|
62 | #include <openssl/rand.h> |
---|
63 | |
---|
64 | #include <daemon_proto.h> |
---|
65 | |
---|
66 | extern int outcontrolsock ; |
---|
67 | extern int sendcontrolto ; |
---|
68 | extern RSA *myrsa ; |
---|
69 | |
---|
70 | void sendcrypt() |
---|
71 | { |
---|
72 | struct message *mess ; |
---|
73 | struct mess_sec *msg_sec ; |
---|
74 | char buf[MAXMESSLEN] ; |
---|
75 | unsigned char pubkey[NBITSINKEY] ; |
---|
76 | unsigned char pubexponent[NBITSINKEY] ; |
---|
77 | int lenkey ; |
---|
78 | int lenexpo ; |
---|
79 | |
---|
80 | /* |
---|
81 | ** Ask for the private and public Key |
---|
82 | */ |
---|
83 | if ( (myrsa = RSA_generate_key(NBITSINKEY,3,NULL,NULL)) == NULL) { |
---|
84 | bbftpd_log(BBFTPD_ERR,"%s",ERR_error_string(ERR_get_error(),NULL) ) ; |
---|
85 | exit(1) ; |
---|
86 | } |
---|
87 | /* |
---|
88 | ** Now extract the public key in order to send it |
---|
89 | */ |
---|
90 | lenkey = BN_bn2mpi(myrsa->n,pubkey) ; |
---|
91 | lenexpo = BN_bn2mpi(myrsa->e,pubexponent) ; |
---|
92 | mess = (struct message *) buf ; |
---|
93 | mess->code = MSG_CRYPT ; |
---|
94 | #ifndef WORDS_BIGENDIAN |
---|
95 | mess->msglen = ntohl(CRYPTMESSLEN+lenkey+lenexpo) ; |
---|
96 | #else |
---|
97 | mess->msglen = CRYPTMESSLEN+lenkey+lenexpo ; |
---|
98 | #endif |
---|
99 | if (writemessage(outcontrolsock,buf,MINMESSLEN,sendcontrolto) < 0 ) { |
---|
100 | bbftpd_log(BBFTPD_ERR,"Error on sendcrypt 1") ; |
---|
101 | exit(1) ; |
---|
102 | } |
---|
103 | msg_sec = (struct mess_sec *) buf ; |
---|
104 | msg_sec->crtype = CRYPT_RSA_PKCS1_OAEP_PADDING ; |
---|
105 | #ifndef WORDS_BIGENDIAN |
---|
106 | msg_sec->pubkeylen = ntohl(lenkey) ; |
---|
107 | msg_sec->expolen = ntohl(lenexpo) ; |
---|
108 | #else |
---|
109 | msg_sec->pubkeylen = lenkey ; |
---|
110 | msg_sec->expolen = lenexpo ; |
---|
111 | #endif |
---|
112 | if (writemessage(outcontrolsock,buf,CRYPTMESSLEN,sendcontrolto) < 0 ) { |
---|
113 | bbftpd_log(BBFTPD_ERR,"Error on sendcrypt 2") ; |
---|
114 | exit(1) ; |
---|
115 | } |
---|
116 | /* |
---|
117 | ** Send Key and exponent |
---|
118 | */ |
---|
119 | if (writemessage(outcontrolsock,pubkey,lenkey,sendcontrolto) < 0 ) { |
---|
120 | bbftpd_log(BBFTPD_ERR,"Error on sendcrypt pubkey") ; |
---|
121 | exit(1) ; |
---|
122 | } |
---|
123 | if (writemessage(outcontrolsock,pubexponent,lenexpo,sendcontrolto) < 0 ) { |
---|
124 | bbftpd_log(BBFTPD_ERR,"Error on sendcrypt pubexponent") ; |
---|
125 | exit(1) ; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | int decodersapass(char *buffer, char *username, char *password) |
---|
130 | { |
---|
131 | struct mess_rsa *msg_rsa ; |
---|
132 | int lenuser ; |
---|
133 | int lenpass ; |
---|
134 | |
---|
135 | msg_rsa = (struct mess_rsa *) buffer ; |
---|
136 | |
---|
137 | #ifndef WORDS_BIGENDIAN |
---|
138 | msg_rsa->numuser = ntohl(msg_rsa->numuser) ; |
---|
139 | msg_rsa->numpass = ntohl(msg_rsa->numpass) ; |
---|
140 | #endif |
---|
141 | lenuser = RSA_private_decrypt(msg_rsa->numuser,msg_rsa->cryptuser,(unsigned char *)username,myrsa,RSA_PKCS1_OAEP_PADDING) ; |
---|
142 | username[lenuser] = '\0' ; |
---|
143 | lenpass = RSA_private_decrypt(msg_rsa->numpass,msg_rsa->cryptpass,(unsigned char *)password,myrsa,RSA_PKCS1_OAEP_PADDING) ; |
---|
144 | password[lenpass] = '\0' ; |
---|
145 | |
---|
146 | return 0 ; |
---|
147 | } |
---|