keyaddremove.c

Exemplo de criação e remoção uma chave simétrica.

Veja Nota sobre os exemplos.
#include <windows.h>
#include <stdio.h>
#include <dinamo.h> /* header do Dinamo */
#define HOST_ADDR "10.10.4.13"
#define USER_ID "master"
#define USER_PWD "12345678"
#define KEY_ID "Nome_Da_Chave"
#define KEY_TYPE ALG_3DES_168
#define FLAGS EXPORTABLE_KEY | NO_CRYPTO
void main ()
{
int nRet;
struct AUTH_PWD authPwd;
HSESSIONCTX hSession = NULL;
HKEYCTX hKey = NULL;
//Inicializa as bibliotecas do Dinamo
nRet = DInitialize(0);
if (nRet){
printf("Falha na funcao: DInitialize \nCodigo de erro: %d\n",nRet);
exit(1);
}
printf("Bibliotecas inicializadas.\n");
//Inicializa a estrutura para conexão com o HSM
strncpy(authPwd.szAddr, HOST_ADDR, sizeof(authPwd.szAddr));
authPwd.nPort = DEFAULT_PORT;
strncpy(authPwd.szUserId, USER_ID, sizeof(authPwd.szUserId));
strncpy(authPwd.szPassword, USER_PWD, sizeof(authPwd.szPassword));
nRet = DOpenSession(&hSession, SS_USER_PWD, (BYTE *)&authPwd, sizeof(authPwd), ENCRYPTED_CONN);
if (nRet){
printf("Falha na funcao: DOpenSession \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Sessao com o Dinamo estabelecida.\n");
nRet = DGenerateKey(hSession, KEY_ID, KEY_TYPE, FLAGS, &hKey);
if (nRet){
printf("Falha na funcao: DGenerateKey \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Chave criada com sucesso.\n");
//Remove a chave do HSM
nRet = DDestroyKey(&hKey, REMOVE_FROM_HCM);
if (nRet){
printf("Falha na funcao: DDestroyKey \nCodigo de erro: %d\n",nRet);
goto clean;
}
printf("Chave removida com sucesso.\n");
clean:
if (hKey){
DDestroyKey(&hKey, 0);
printf("Contexto da chave liberado.\n");
}
if (hSession){
DCloseSession(&hSession, 0);
printf("Sessao encerrada.\n");
}
printf("Bibliotecas finalizada.\n");
}