package/network/services/ead/src/tinysrp/tinysrp.c
/* This is called by the client with a connected socket, username, and
passphrase.  pass can be NULL in which case the user is queried. */
This is called by the client with a connected socket, username, and
passphrase.  pass can be NULL in which case the user is queried.
This is called by the client with a connected socket username and passphrase . pass can be NULL in which case the user is queried .
int tsrp_client_authenticate(int s, char *user, char *pass, TSRP_SESSION *tsrp)
{
	int i, index;
	unsigned char username[MAXUSERLEN + 1], sbuf[MAXSALTLEN];
	unsigned char msgbuf[MAXPARAMLEN + 1], bbuf[MAXPARAMLEN];
	unsigned char passbuf[128], *skey;
	struct t_client *tc;
	struct t_preconf *tcp;          
	struct t_num salt, *A, B;
	i = strlen(user);
	if (i > MAXUSERLEN) {
		i = MAXUSERLEN;
	}
	msgbuf[0] = i;
	memcpy(msgbuf + 1, user, i);
	if (send(s, msgbuf, i + 1, 0) < 0) {
		return 0;
	}
	memcpy(username, user, i);
	username[i] = '\0';
	i = recv(s, msgbuf, 2, MSG_WAITALL);
	if (i <= 0) {
		return 0;
	}
	index = msgbuf[0];
	if (index <= 0 || index > t_getprecount()) {
		return 0;
	}
	tcp = t_getpreparam(index - 1);
	salt.len = msgbuf[1];
	if (salt.len > MAXSALTLEN) {
		return 0;
	}
	salt.data = sbuf;
	i = recv(s, sbuf, salt.len, MSG_WAITALL);
	if (i <= 0) {
		return 0;
	}
	tc = t_clientopen(username, &tcp->modulus, &tcp->generator, &salt);
	if (tc == NULL) {
		return 0;
	}
	A = t_clientgenexp(tc);
	msgbuf[0] = A->len - 1;         
	memcpy(msgbuf + 1, A->data, A->len);
	if (send(s, msgbuf, A->len + 1, 0) < 0) {
		return 0;
	}
	if (pass == NULL) {
		t_getpass(passbuf, sizeof(passbuf), "Enter password:");
		pass = passbuf;
	}
	t_clientpasswd(tc, pass);
	i = recv(s, msgbuf, 1, 0);
	if (i <= 0) {
		return 0;
	}
	B.len = msgbuf[0] + 1;
	B.data = bbuf;
	i = recv(s, bbuf, B.len, MSG_WAITALL);
	if (i <= 0) {
		return 0;
	}
	skey = t_clientgetkey(tc, &B);
	if (skey == NULL) {
		return 0;
	}
	if (send(s, t_clientresponse(tc), RESPONSE_LEN, 0) < 0) {
		return 0;
	}
	i = recv(s, msgbuf, RESPONSE_LEN, MSG_WAITALL);
	if (i <= 0) {
		return 0;
	}
	if (t_clientverify(tc, msgbuf) != 0) {
		return 0;
	}
	if (tsrp) {
		memcpy(tsrp->username, username, strlen(username) + 1);
		memcpy(tsrp->key, skey, SESSION_KEY_LEN);
	}
	t_clientclose(tc);
	return 1;
}
int tsrp_client_authenticate ( int s , char * user , char * pass , TSRP_SESSION * tsrp ) { int i , index ; unsigned char username [ MAXUSERLEN + 1 ] , sbuf [ MAXSALTLEN ] ; unsigned char msgbuf [ MAXPARAMLEN + 1 ] , bbuf [ MAXPARAMLEN ] ; unsigned char passbuf [ 128 ] , * skey ; struct t_client * tc ; struct t_preconf * tcp ; struct t_num salt , * A , B ; i = strlen ( user ) ; if ( i > MAXUSERLEN ) { i = MAXUSERLEN ; } msgbuf [ 0 ] = i ; memcpy ( msgbuf + 1 , user , i ) ; if ( send ( s , msgbuf , i + 1 , 0 ) < 0 ) { return 0 ; } memcpy ( username , user , i ) ; username [ i ] = ' \0 ' ; i = recv ( s , msgbuf , 2 , MSG_WAITALL ) ; if ( i <= 0 ) { return 0 ; } index = msgbuf [ 0 ] ; if ( index <= 0 || index > t_getprecount ( ) ) { return 0 ; } tcp = t_getpreparam ( index - 1 ) ; salt . len = msgbuf [ 1 ] ; if ( salt . len > MAXSALTLEN ) { return 0 ; } salt . data = sbuf ; i = recv ( s , sbuf , salt . len , MSG_WAITALL ) ; if ( i <= 0 ) { return 0 ; } tc = t_clientopen ( username , & tcp -> modulus , & tcp -> generator , & salt ) ; if ( tc == NULL ) { return 0 ; } A = t_clientgenexp ( tc ) ; msgbuf [ 0 ] = A -> len - 1 ; memcpy ( msgbuf + 1 , A -> data , A -> len ) ; if ( send ( s , msgbuf , A -> len + 1 , 0 ) < 0 ) { return 0 ; } if ( pass == NULL ) { t_getpass ( passbuf , sizeof ( passbuf ) , " " ) ; pass = passbuf ; } t_clientpasswd ( tc , pass ) ; i = recv ( s , msgbuf , 1 , 0 ) ; if ( i <= 0 ) { return 0 ; } B . len = msgbuf [ 0 ] + 1 ; B . data = bbuf ; i = recv ( s , bbuf , B . len , MSG_WAITALL ) ; if ( i <= 0 ) { return 0 ; } skey = t_clientgetkey ( tc , & B ) ; if ( skey == NULL ) { return 0 ; } if ( send ( s , t_clientresponse ( tc ) , RESPONSE_LEN , 0 ) < 0 ) { return 0 ; } i = recv ( s , msgbuf , RESPONSE_LEN , MSG_WAITALL ) ; if ( i <= 0 ) { return 0 ; } if ( t_clientverify ( tc , msgbuf ) != 0 ) { return 0 ; } if ( tsrp ) { memcpy ( tsrp -> username , username , strlen ( username ) + 1 ) ; memcpy ( tsrp -> key , skey , SESSION_KEY_LEN ) ; } t_clientclose ( tc ) ; return 1 ; }
This is called by the client with a connected socket, username, and
passphrase.
This is called by the client with a connected socket username and passphrase .
/* @@@ should go away */ /* Send the username. */ /* Get the prime index and salt. */ /* @@@ t_clientopen() needs a variant that takes the index */ /* Calculate A and send it to the server. */ /* len is max 256 */ /* Ask the user for the passphrase. */ /* Get B from the server. */ /* Compute the session key. */ /* Send the response. */ /* Get the server's response. */ /* All done.  Now copy the key and clean up. */