API C/C++
HSM Dinamo
Cargando...
Buscando...
No se han encontrado entradas
export_import_tr31.c

Ejemplo de exportación e importación en formato TR-31.

Véase la nota sobre ejemplos.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <dinamo.h> /* header do Dinamo */
#define HOST_ADDR "127.0.0.1"
#define USER_ID "master"
#define USER_PWD "12345678"
int main()
{
int nRet = 0;
struct AUTH_PWD authPwd;
HSESSIONCTX hSession = NULL;
HKEYCTX hKbpk = NULL;
HKEYCTX hKey = NULL;
char szKbpk[] = "kbpk";
char szKey[] = "key";
char szImpKey[] = "impKey";
//Inicializa as bibliotecas do Dinamo
nRet = DInitialize(0);
if (nRet) {
printf("Falha na funcao: DInitialize \nCodigo de erro: %d\n", nRet);
goto clean;
}
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");
/*
Cria chaves de teste
*/
nRet = DGenerateKey(hSession, szKbpk, ALG_AES_256, EXPORTABLE_KEY, &hKbpk);
if (nRet)
{
printf("Falha na funcao: DGenerateKey \nCodigo de erro: %d\n", nRet);
goto clean;
}
nRet = DGenerateKey(hSession, szKey, ALG_AES_128, EXPORTABLE_KEY, &hKey);
if (nRet)
{
printf("Falha na funcao: DGenerateKey \nCodigo de erro: %d\n", nRet);
goto clean;
}
/*
* Exporta uma chave no formato TR-31.
*/
BYTE *pbKeyBlock = NULL;
DWORD dwKeyBlockLen = 0;
nRet = DEFTExportTR31(hSession, szKbpk, szKey, NULL,
EFT_ME_TR31_EXP_AUTO, NULL,&dwKeyBlockLen, 0);
pbKeyBlock = malloc(dwKeyBlockLen);
if(pbKeyBlock == NULL)
{
printf("Falha ao alocar memoria\n");
goto clean;
}
nRet = DEFTExportTR31(hSession, szKbpk, szKey, NULL,
EFT_ME_TR31_EXP_AUTO, pbKeyBlock, &dwKeyBlockLen, 0);
if (nRet) {
printf("Falha na funcao: DEFTExportTR31 \nCodigo de erro: %d\n", nRet);
goto clean;
}
printf("Chave exportada com sucesso.\n");
/*
* Importa uma chave no formato TR-31.
*/
nRet = DEFTImportTR31(hSession, szKbpk, szImpKey, EXPORTABLE_KEY, pbKeyBlock,
dwKeyBlockLen, 0);
if (nRet)
{
printf("Falha na funcao: DEFTImportTR31 \nCodigo de erro: %d\n", nRet);
goto clean;
}
printf("Chave importada com sucesso.\n");
clean:
if (hSession) {
DCloseSession(&hSession, 0);
printf("Sessao encerrada.\n");
}
free(pbKeyBlock);
printf("Bibliotecas finalizada.\n");
return nRet;
}
Application Programming Interface (API) do HSM Dinamo.
void * HSESSIONCTX
Definição dinamo.h:68
#define DEFAULT_PORT
Definição dinamo.h:1949
#define ALG_AES_256
Definição dinamo.h:1029
unsigned char BYTE
Definição dinamo.h:45
#define EFT_ME_TR31_EXP_USAGE_AUTO
Definição dinamo.h:1594
unsigned int DWORD
Definição dinamo.h:46
#define EFT_ME_TR31_EXP_AUTO
Definição dinamo.h:1648
#define ENCRYPTED_CONN
Definição dinamo.h:585
#define SS_USER_PWD
Definição dinamo.h:576
#define EFT_ME_TR31_EXP_MODE_AUTO
Definição dinamo.h:1634
#define ALG_AES_128
Definição dinamo.h:1027
void * HKEYCTX
Definição dinamo.h:70
#define EXPORTABLE_KEY
Definição dinamo.h:1423
int AAP_API DEFTImportTR31(HSESSIONCTX hSession, const char *szKBPK, const char *szKey, DWORD dwKeyAttributes, BYTE *pbKeyBlock, DWORD dwKeyBlockLen, DWORD dwParam)
int AAP_API DEFTExportTR31(HSESSIONCTX hSession, const char *szKBPK, const char *szKey, void *pvReserved, WORD wUsage, BYTE bMode, BYTE bExport, BYTE *pbOutBlock, DWORD *pdwOutBlockLen, DWORD dwParam)
int AAP_API DGenerateKey(HSESSIONCTX hSession, char *szKeyId, int nAlgId, DWORD dwFlags, HKEYCTX *phKey)
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()
Definição dinamo.h:3090
int nPort
Definição dinamo.h:3092
char szUserId[MAX_USR_LEN]
Definição dinamo.h:3093
char szAddr[MAX_ADDR_LEN]
Definição dinamo.h:3091
char szPassword[MAX_USR_PWD]
Definição dinamo.h:3094