userOtp.c

Exemplo de utilização de OTP para autenticação de usuários do HSM.

Veja Nota sobre os exemplos.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <dinamo.h>
/* Parametros da conexao */
#define HSM_USR_ADM "master"
#define HSM_USR "user"
#define HSM_IP "10.0.62.47"
#define HSM_PWD "12345678"
int main(void)
{
int nRet = 0;
struct AUTH_PWD_EX stAuthInfoAdm = {0};
struct AUTH_PWD_EX stAuthInfoUser = { 0 };
HSESSIONCTX hSessionAdm = NULL;
HSESSIONCTX hSessionUser = NULL;
OATH_SA_v1 stTokenParam = { 0 };
/*
* Semente utilizada para gerar o Blob HOTP.
*
* Esta semente pode ser transformada de binário para Base32
* e importada no client(Google Authenticator, por exemplo).
*
*/
BYTE pbOtpKey[20] =
{
0xD5, 0x17, 0xED, 0x40, 0x1D, 0xF3, 0x03, 0x38, 0x37, 0xE0, 0x8B, 0x62, 0x55, 0xBE, 0xDB, 0xF9,
0x52, 0x0E, 0xF8, 0x8E,
};
/*
Preenche a estrutura do usuario administrador
*/
strncpy(stAuthInfoAdm.szAddr, HSM_IP, sizeof(stAuthInfoAdm.szAddr));
strncpy(stAuthInfoAdm.szUserId, HSM_USR_ADM, sizeof(stAuthInfoAdm.szUserId));
strncpy(stAuthInfoAdm.szPassword, HSM_PWD, sizeof(stAuthInfoAdm.szPassword));
stAuthInfoAdm.nPort = DEFAULT_PORT;
stAuthInfoAdm.nStrongAuthLen = 0;
stAuthInfoAdm.pbStrongAuth = NULL;
stAuthInfoAdm.dwAuthType = SA_AUTH_NONE;
/*
Conecta com um usuário com permissões de administração
para associar um token otp a um usuário comum.
*/
nRet = DOpenSession(&hSessionAdm,
(BYTE *) &stAuthInfoAdm,
sizeof(struct AUTH_PWD_EX),
if(nRet)
{
printf("DOpenSession (adm) : Failed! %d.\n", nRet);
goto clean;
}
/* Associa o token ao usuário comum. */
stTokenParam.type = OATH_SA_v1_type_SHA1;
memcpy(stTokenParam.key, pbOtpKey, sizeof(pbOtpKey));
stTokenParam.key_len = sizeof(pbOtpKey);
nRet = DAssignToken(hSessionAdm,
HSM_USR,
(BYTE *)&stTokenParam,
sizeof(stTokenParam));
if( nRet )
{
printf("DAssignToken : Failed! %d.\n", nRet);
goto clean;
}
/*
Preenche a estrutura do usuario comum.
*/
strncpy(stAuthInfoUser.szAddr, HSM_IP, sizeof(stAuthInfoUser.szAddr));
strncpy(stAuthInfoUser.szUserId, HSM_USR, sizeof(stAuthInfoUser.szUserId));
strncpy(stAuthInfoUser.szPassword, HSM_PWD, sizeof(stAuthInfoUser.szPassword));
stAuthInfoUser.nPort = DEFAULT_PORT;
stAuthInfoUser.pbStrongAuth = (BYTE*)"992271";
stAuthInfoUser.nStrongAuthLen = (int)strlen(stAuthInfoUser.pbStrongAuth);
stAuthInfoUser.dwAuthType = SA_AUTH_OTP;
nRet = DOpenSession(&hSessionUser,
(BYTE *)&stAuthInfoUser,
sizeof(struct AUTH_PWD_EX),
if (nRet)
{
printf("DOpenSession (user) : Failed! %d.\n", nRet);
goto clean;
}
/*
Re-sincroniza o token OTP do usuário comum.
Usar quando o token HOTP(evento) não estiver sincronizado.
Passa-se 2 OTPs consecutivos para que o HSM ajuste a janela de eventos
*/
nRet = DOATHResync( hSessionAdm,
HSM_USR,
"758993",
"864532",
0);
/*
Desassocia o token do usuário comum.
*/
nRet = DUnassignToken( hSessionAdm,
HSM_USR);
clean:
DCloseSession(&hSessionAdm, 0);
DCloseSession(&hSessionUser, 0);
return nRet;
}