source: TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/bbftpd_crypt.c @ 773

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/bbftpd_crypt.c@773
Revision 773, 4.5 KB checked in by spascoe, 14 years ago (diff)

Initial import of bbftp source

Line 
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 <errno.h>
40#include <sys/types.h>
41#include <sys/socket.h>
42#include <netinet/in.h>
43#include <syslog.h>
44#if TIME_WITH_SYS_TIME
45# include <sys/time.h>
46# include <time.h>
47#else
48# if HAVE_SYS_TIME_H
49#  include <sys/time.h>
50# else
51#  include <time.h>
52# endif
53#endif
54
55#include <bbftpd.h>
56#include <common.h>
57#include <daemon.h>
58#include <structures.h>
59#include <openssl/rsa.h>
60#include <openssl/err.h>
61#include <openssl/rand.h>
62
63extern int  outcontrolsock ;
64extern  int     sendcontrolto ;
65extern RSA  *myrsa ;
66
67void sendcrypt() 
68{
69    struct message    *mess ;
70    struct mess_sec    *msg_sec ;
71    char    buf[MAXMESSLEN] ;
72    unsigned char    pubkey[NBITSINKEY] ;
73    unsigned char    pubexponent[NBITSINKEY] ;
74    int        lenkey ;
75    int        lenexpo ;
76   
77    /*
78    ** Ask for the private and public Key
79    */
80    if ( (myrsa = RSA_generate_key(NBITSINKEY,3,NULL,NULL)) == NULL) {
81        syslog(BBFTPD_ERR,"%s",ERR_error_string(ERR_get_error(),NULL) ) ;
82        exit(1) ;
83    }
84    /*
85    ** Now extract the public key in order to send it
86    */
87    lenkey  = BN_bn2mpi(myrsa->n,pubkey) ;
88    lenexpo = BN_bn2mpi(myrsa->e,pubexponent) ;
89    mess = (struct message *) buf ;
90    mess->code = MSG_CRYPT ;
91#ifndef WORDS_BIGENDIAN
92    mess->msglen = ntohl(CRYPTMESSLEN+lenkey+lenexpo) ;
93#else
94    mess->msglen = CRYPTMESSLEN+lenkey+lenexpo ;
95#endif
96    if (writemessage(outcontrolsock,buf,MINMESSLEN,sendcontrolto) < 0 ) {
97        syslog(BBFTPD_ERR,"Error on sendcrypt 1") ;
98        exit(1) ;
99    }
100    msg_sec  = (struct mess_sec    *) buf ;
101    msg_sec->crtype  = CRYPT_RSA_PKCS1_OAEP_PADDING ;
102#ifndef WORDS_BIGENDIAN
103    msg_sec->pubkeylen  = ntohl(lenkey) ;
104    msg_sec->expolen  = ntohl(lenexpo) ;
105#else
106    msg_sec->pubkeylen  = lenkey ;
107    msg_sec->expolen  = lenexpo ;
108#endif
109    if (writemessage(outcontrolsock,buf,CRYPTMESSLEN,sendcontrolto) < 0 ) {
110        syslog(BBFTPD_ERR,"Error on sendcrypt 2") ;
111        exit(1) ;
112    }
113    /*
114    ** Send Key and exponent
115    */
116    if (writemessage(outcontrolsock,pubkey,lenkey,sendcontrolto) < 0 ) {
117        syslog(BBFTPD_ERR,"Error on sendcrypt pubkey") ;
118        exit(1) ;
119    }
120    if (writemessage(outcontrolsock,pubexponent,lenexpo,sendcontrolto) < 0 ) {
121        syslog(BBFTPD_ERR,"Error on sendcrypt pubexponent") ;
122        exit(1) ;
123    }
124}
125
126int decodersapass(char *buffer, char *username, char *password) 
127{
128    struct mess_rsa *msg_rsa ;
129    int    lenuser ;
130    int lenpass ;
131   
132    msg_rsa = (struct mess_rsa *) buffer ;
133
134#ifndef WORDS_BIGENDIAN
135    msg_rsa->numuser = ntohl(msg_rsa->numuser) ;
136    msg_rsa->numpass = ntohl(msg_rsa->numpass) ;
137#endif
138    lenuser = RSA_private_decrypt(msg_rsa->numuser,msg_rsa->cryptuser,(unsigned char *)username,myrsa,RSA_PKCS1_OAEP_PADDING) ;
139    username[lenuser] = '\0' ;
140    lenpass = RSA_private_decrypt(msg_rsa->numpass,msg_rsa->cryptpass,(unsigned char *)password,myrsa,RSA_PKCS1_OAEP_PADDING) ;
141    password[lenpass] = '\0' ;
142   
143    return 0 ;
144}
Note: See TracBrowser for help on using the repository browser.