C/C++ API
HSM Dinamo
Loading...
Looking for...
No entries found
user_otp.c

Example of using OTP to authenticate HSM users.

See Note on examples.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <dinamo.h>
/* Connection parameters */
#define HSM_USR_ADM "master"
#define HSM_USR "user"
#define HSM_IP "127.0.0.1"
#define HSM_PWD "12345678"
int NewHsmUser(HSESSIONCTX hSession, const char *szUser, const char *szPwd)
{
int nRet = 0;
struct USER_INFO stUserInfo = { 0 };
/* Fills in the user structure */
strncpy(stUserInfo. szUserId, szUser, sizeof(stUserInfo.szUserId));
strncpy(stUserInfo. szPassword, szPwd, sizeof(stUserInfo.szPassword));
/* Creates a new user in HSM */
nRet = DCreateUser(hSession, stUserInfo);
if (nRet)
{
printf("DCreateUser : Failed! %d.\n", nRet);
return nRet;
}
return 0;
}
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 };
const char cszOTP[] = "992271";
/*
* Seed used to generate the HOTP Blob.
*
* This seed can be transformed from binary to Base32
* and imported into the client (Google Authenticator, for example).
*
*/
BYTE pbOtpKey[20] =
{
0xD5, 0x17, 0xED, 0x40, 0x1D, 0xF3, 0x03, 0x38, 0x37, 0xE0, 0x8B, 0x62, 0x55, 0xBE, 0xDB, 0xF9,
0x52, 0x0E, 0xF8, 0x8E,
};
/*
Fills in the structure of the administrator user
*/
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;
/*
Connect with a user with admin permissions
to associate an otp token with an ordinary user.
*/
nRet = DOpenSession(&hSessionAdm,
(BYTE *) &stAuthInfoAdm,
sizeof(struct AUTH_PWD_EX),
if(nRet)
{
printf("DOpenSession (adm) : Failed! %d.\n", nRet);
goto clean;
}
/* Creates an ordinary test user */
nRet = NewHsmUser(hSessionAdm, HSM_USR, HSM_PWD);
if (nRet)
{
printf("NewHsmUser : Failed! %d.\n", nRet);
goto clean;
}
/* Associates the token with the common user. */
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;
}
/*
Fills in the structure of the average user.
*/
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 *)cszOTP;
stAuthInfoUser.nStrongAuthLen = (int)sizeof(cszOTP) - 1;
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-synchronizes the common user's OTP token.
Use when the HOTP(event) token is not synchronized.
2 consecutive OTPs are passed for the HSM to adjust the event window
*/
nRet = DOATHResync( hSessionAdm,
HSM_USR,
"758993",
"864532",
0);
if (nRet)
{
printf("DOATHResync : Failed! %d.\n", nRet);
goto clean;
}
/*
Disassociates the token from the ordinary user.
*/
nRet = DUnassignToken( hSessionAdm,
HSM_USR);
if (nRet)
{
printf("DUnassignToken : Failed! %d.\n", nRet);
goto clean;
}
clean:
DCloseSession(&hSessionUser, 0);
// Remove o usuário criado
if (hSessionAdm != NULL)
{
DRemoveUser(hSessionAdm, HSM_USR);
}
DCloseSession(&hSessionAdm, 0);
return nRet;
}
HSM Application Programming Interface (API) Dinamo.
void * HSESSIONCTX
Definition dinamo.h:68
#define DEFAULT_PORT
Definition dinamo.h:1948
#define CACHE_BYPASS
Definition dinamo.h:589
#define SA_AUTH_NONE
Definition dinamo.h:594
#define SA_AUTH_OTP
Definition dinamo.h:595
unsigned char BYTE
Definition dinamo.h:45
#define LB_BYPASS
Definition dinamo.h:588
#define OATH_SA_v1_HOTP_DYN_TRUNC_OFF
Definition dinamo.h:853
#define ENCRYPTED_CONN
Definition dinamo.h:585
#define OATH_SA_v1_type_SHA1
Definition dinamo.h:852
#define SS_USR_PWD_EX
Definition dinamo.h:579
#define AT_OATH_TOKEN
Definition dinamo.h:844
int AAP_API DOpenSession(HSESSIONCTX *phSession, DWORD dwParam, BYTE *pbData, DWORD dwDataLen, DWORD dwFlags)
int AAP_API DCloseSession(HSESSIONCTX *phSession, DWORD dwFlags)
int AAP_API DInitialize(DWORD dwReserved)
int AAP_API DFinalize()
int AAP_API DRemoveUser(HSESSIONCTX hSession, char *szUserId)
int AAP_API DUnassignToken(const HSESSIONCTX hSession, const DWORD dwParam, const char *szUserId)
int AAP_API DAssignToken(const HSESSIONCTX hSession, const char *szUserId, const DWORD dwParam, BYTE *pbData, const DWORD dwDataLen)
int AAP_API DCreateUser(HSESSIONCTX hSession, struct USER_INFO userInfo)
int AAP_API DOATHResync(const HSESSIONCTX hSession, char *szUser, char *szOTP1, char *szOTP2, DWORD dwParam)
Definition dinamo.h:3101
int nPort
Definition dinamo.h:3103
char szUserId[MAX_USR_LEN]
Definition dinamo.h:3104
DWORD dwAuthType
Definition dinamo.h:3106
char szAddr[MAX_ADDR_LEN]
Definition dinamo.h:3102
char szPassword[MAX_USR_PWD]
Definition dinamo.h:3105
int nStrongAuthLen
Definition dinamo.h:3112
BYTE * pbStrongAuth
Definition dinamo.h:3111
Definition dinamo.h:2806
BYTE key_len
Definition dinamo.h:2811
BYTE key[MAX_OATH_HMAC_LEN]
Definition dinamo.h:2812
BYTE type
Definition dinamo.h:2807
BYTE truncation_offset
Definition dinamo.h:2813
Definition dinamo.h:3167
char szPassword[MAX_USR_PWD+1]
Definition dinamo.h:3169
char szUserId[MAX_USR_LEN+1]
Definition dinamo.h:3168