Commit 8aaf9533 authored by Alberto Inch's avatar Alberto Inch
Browse files

Commit inicial.

parents
## ChangePin
Librería JNI para cambiar el PIN de un Token criptográfico.
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="changepin" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/changepin" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="3" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Option createStaticLib="1" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/changepin" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="3" />
<Option compiler="gcc" />
<Option createDefFile="1" />
<Option createStaticLib="1" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
<Add option="-fPIC" />
<Add directory="/usr/lib/jvm/java-11-openjdk-amd64/include" />
<Add directory="/usr/lib/jvm/java-11-openjdk-amd64/include/linux" />
</Compiler>
<Linker>
<Add option="-fPIC" />
<Add option="-lc" />
</Linker>
<Unit filename="include/Token.h" />
<Unit filename="main.cpp" />
<Unit filename="main.h" />
<Unit filename="src/Token.cpp" />
<Extensions>
<envvars />
<code_completion />
<lib_finder disable_auto="1" />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>
#include <cstring>
#include <sstream>
#include <dlfcn.h>
#include <pkcs11-helper-1.0/pkcs11.h>
class Token
{
public:
Token(const char* lib);
CK_ULONG nroSlots();
CK_SLOT_ID_PTR slots();
CK_SESSION_HANDLE open(CK_SLOT_ID slot);
void close(CK_SESSION_HANDLE hSession);
void changePin(CK_SESSION_HANDLE hSession, const char* oldPin, const char* newPin);
virtual ~Token();
protected:
void* dlHandle;
CK_FUNCTION_LIST_PTR functionList;
char* byteToHex(CK_BYTE_PTR bytes, int l);
char* byteToChar(CK_BYTE_PTR bytes, int l);
void error(CK_RV rv);
private:
};
#include "main.h"
#include "include/Token.h"
JNIEXPORT jstring JNICALL Java_bo_firmadigital_token_ChangePinJNI_changePin
(JNIEnv* env, jobject thisObject, jstring lib, jint slot, jstring oldPin, jstring newPin) {
const char* res = "Ok";
try {
Token *token = new Token(env->GetStringUTFChars(lib, NULL));
int nroSlots = token->nroSlots();
if (nroSlots > 0) {
try {
CK_SESSION_HANDLE hSession = token->open(slot);
try {
token->changePin(hSession, env->GetStringUTFChars(oldPin, NULL), env->GetStringUTFChars(newPin, NULL));
} catch (const std::runtime_error& error) {
res = "No se pudo cambiar la contraseña.";
}
token->close(hSession);
} catch (const std::runtime_error& error) {
res = "No se encontró el slot especificado.";
}
} else {
res = "No se encontró ningún token conectado.";
}
token->~Token();
} catch (const std::runtime_error& error) {
res = "No se pudo cargar el controlador.";
}
return env->NewStringUTF(res);
}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class bo_firmadigital_token_ChangePinJNI */
#ifndef _Included_bo_firmadigital_token_ChangePinJNI
#define _Included_bo_firmadigital_token_ChangePinJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: bo_firmadigital_token_ChangePinJNI
* Method: changePin
* Signature: (Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_bo_firmadigital_token_ChangePinJNI_changePin
(JNIEnv *, jobject, jstring, jint, jstring, jstring);
#ifdef __cplusplus
}
#endif
#endif
#include "../include/Token.h"
Token::Token(const char* lib)
{
dlHandle = dlopen(lib, RTLD_LAZY);
if (!dlHandle) {
throw std::runtime_error(dlerror());
}
CK_C_GetFunctionList f_C_GetFunctionList = (CK_C_GetFunctionList)dlsym(dlHandle, "C_GetFunctionList");
if (dlerror()) {
throw std::runtime_error("Error en el controlador del token.");
}
f_C_GetFunctionList(&functionList);
CK_RV rv = functionList->C_Initialize(NULL);
error(rv);
}
CK_ULONG Token::nroSlots() {
CK_ULONG ulCount;
CK_RV rv = functionList->C_GetSlotList(TRUE, NULL, &ulCount);
error(rv);
return ulCount;
}
CK_SLOT_ID_PTR Token::slots() {
CK_ULONG ulCount;
CK_RV rv = functionList->C_GetSlotList(TRUE, NULL, &ulCount);
error(rv);
CK_SLOT_ID_PTR slotList = new CK_SLOT_ID[ulCount];
rv = functionList->C_GetSlotList(TRUE, slotList, &ulCount);
error(rv);
return slotList;
}
CK_SESSION_HANDLE Token::open(CK_SLOT_ID slot) {
CK_SESSION_HANDLE hSession = 0;
CK_RV rv = functionList->C_OpenSession(slot, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL, NULL, &hSession);
error(rv);
return hSession;
}
void Token::close(CK_SESSION_HANDLE hSession) {
CK_RV rv = functionList->C_CloseSession(hSession);
error(rv);
}
void Token::changePin(CK_SESSION_HANDLE hSession, const char* oldPin, const char* newPin) {
char oldBuf[21], newBuf[21];
strcpy(oldBuf, oldPin);
oldPin = oldBuf;
strcpy(newBuf, newPin);
newPin = newBuf;
CK_RV rv = functionList->C_SetPIN(hSession, (CK_UTF8CHAR *)oldBuf, strlen(oldBuf), (CK_UTF8CHAR *)newBuf, strlen(newBuf));
error(rv);
}
char* Token::byteToHex(CK_BYTE_PTR bytes, int l) {
std::stringstream ss;
ss << std::hex;
for (int i = 0; i < l; i++) {
ss << (int)bytes[i];
}
char* res = (char*)malloc((l * 2 + 1) * sizeof(char));
strncpy(res, ss.str().c_str(), l * 2);
res[l * 2] = 0;
return res;
}
char* Token::byteToChar(CK_BYTE_PTR bytes, int l) {
char* res = (char*)malloc((l + 1) * sizeof(char));
strncpy(res, (char*)bytes, l);
res[l] = 0;
return res;
}
void Token::error(CK_RV rv)
{
switch (rv) {
case CKR_OK:
return;
case CKR_CANCEL:
throw std::runtime_error("CKR_CANCEL");
break;
case CKR_HOST_MEMORY:
throw std::runtime_error("CKR_HOST_MEMORY");
break;
case CKR_SLOT_ID_INVALID:
throw std::runtime_error("CKR_SLOT_ID_INVALID");
break;
case CKR_GENERAL_ERROR:
throw std::runtime_error("CKR_GENERAL_ERROR");
break;
case CKR_FUNCTION_FAILED:
throw std::runtime_error("CKR_FUNCTION_FAILED");
break;
case CKR_ARGUMENTS_BAD:
throw std::runtime_error("CKR_ARGUMENTS_BAD");
break;
case CKR_NO_EVENT:
throw std::runtime_error("CKR_NO_EVENT");
break;
case CKR_NEED_TO_CREATE_THREADS:
throw std::runtime_error("CKR_NEED_TO_CREATE_THREADS");
break;
case CKR_CANT_LOCK:
throw std::runtime_error("CKR_CANT_LOCK");
break;
case CKR_ATTRIBUTE_READ_ONLY:
throw std::runtime_error("CKR_ATTRIBUTE_READ_ONLY");
break;
case CKR_ATTRIBUTE_SENSITIVE:
throw std::runtime_error("CKR_ATTRIBUTE_SENSITIVE");
break;
case CKR_ATTRIBUTE_TYPE_INVALID:
throw std::runtime_error("CKR_ATTRIBUTE_TYPE_INVALID");
break;
case CKR_ATTRIBUTE_VALUE_INVALID:
throw std::runtime_error("CKR_ATTRIBUTE_VALUE_INVALID");
break;
case CKR_DATA_INVALID:
throw std::runtime_error("CKR_DATA_INVALID");
break;
case CKR_DATA_LEN_RANGE:
throw std::runtime_error("CKR_DATA_LEN_RANGE");
break;
case CKR_DEVICE_ERROR:
throw std::runtime_error("CKR_DEVICE_ERROR");
break;
case CKR_DEVICE_MEMORY:
throw std::runtime_error("CKR_DEVICE_MEMORY");
break;
case CKR_DEVICE_REMOVED:
throw std::runtime_error("CKR_DEVICE_REMOVED");
break;
case CKR_ENCRYPTED_DATA_INVALID:
throw std::runtime_error("CKR_ENCRYPTED_DATA_INVALID");
break;
case CKR_ENCRYPTED_DATA_LEN_RANGE:
throw std::runtime_error("CKR_ENCRYPTED_DATA_LEN_RANGE");
break;
case CKR_FUNCTION_CANCELED:
throw std::runtime_error("CKR_FUNCTION_CANCELED");
break;
case CKR_FUNCTION_NOT_PARALLEL:
throw std::runtime_error("CKR_FUNCTION_NOT_PARALLEL");
break;
case CKR_FUNCTION_NOT_SUPPORTED:
throw std::runtime_error("CKR_FUNCTION_NOT_SUPPORTED");
break;
case CKR_KEY_HANDLE_INVALID:
throw std::runtime_error("CKR_KEY_HANDLE_INVALID");
break;
case CKR_KEY_SIZE_RANGE:
throw std::runtime_error("CKR_KEY_SIZE_RANGE");
break;
case CKR_KEY_TYPE_INCONSISTENT:
throw std::runtime_error("CKR_KEY_TYPE_INCONSISTENT");
break;
case CKR_KEY_NOT_NEEDED:
throw std::runtime_error("CKR_KEY_NOT_NEEDED");
break;
case CKR_KEY_CHANGED:
throw std::runtime_error("CKR_KEY_CHANGED");
break;
case CKR_KEY_NEEDED:
throw std::runtime_error("CKR_KEY_NEEDED");
break;
case CKR_KEY_INDIGESTIBLE:
throw std::runtime_error("CKR_KEY_INDIGESTIBLE");
break;
case CKR_KEY_FUNCTION_NOT_PERMITTED:
throw std::runtime_error("CKR_KEY_FUNCTION_NOT_PERMITTED");
break;
case CKR_KEY_NOT_WRAPPABLE:
throw std::runtime_error("CKR_KEY_NOT_WRAPPABLE");
break;
case CKR_KEY_UNEXTRACTABLE:
throw std::runtime_error("CKR_KEY_UNEXTRACTABLE");
break;
case CKR_MECHANISM_INVALID:
throw std::runtime_error("CKR_MECHANISM_INVALID");
break;
case CKR_MECHANISM_PARAM_INVALID:
throw std::runtime_error("CKR_MECHANISM_PARAM_INVALID");
break;
case CKR_OBJECT_HANDLE_INVALID:
throw std::runtime_error("CKR_OBJECT_HANDLE_INVALID");
break;
case CKR_OPERATION_ACTIVE:
throw std::runtime_error("CKR_OPERATION_ACTIVE");
break;
case CKR_OPERATION_NOT_INITIALIZED:
throw std::runtime_error("CKR_OPERATION_NOT_INITIALIZED");
break;
case CKR_PIN_INCORRECT:
throw std::runtime_error("CKR_PIN_INCORRECT");
break;
case CKR_PIN_INVALID:
throw std::runtime_error("CKR_PIN_INVALID");
break;
case CKR_PIN_LEN_RANGE:
throw std::runtime_error("CKR_PIN_LEN_RANGE");
break;
case CKR_PIN_EXPIRED:
throw std::runtime_error("CKR_PIN_EXPIRED");
break;
case CKR_PIN_LOCKED:
throw std::runtime_error("CKR_PIN_LOCKED");
break;
case CKR_SESSION_CLOSED:
throw std::runtime_error("CKR_SESSION_CLOSED");
break;
case CKR_SESSION_COUNT:
throw std::runtime_error("CKR_SESSION_COUNT");
break;
case CKR_SESSION_HANDLE_INVALID:
throw std::runtime_error("Sesión inválida.");
break;
case CKR_SESSION_PARALLEL_NOT_SUPPORTED:
throw std::runtime_error("CKR_SESSION_PARALLEL_NOT_SUPPORTED");
break;
case CKR_SESSION_READ_ONLY:
throw std::runtime_error("CKR_SESSION_READ_ONLY");
break;
case CKR_SESSION_EXISTS:
throw std::runtime_error("CKR_SESSION_EXISTS");
break;
case CKR_SESSION_READ_ONLY_EXISTS:
throw std::runtime_error("CKR_SESSION_READ_ONLY_EXISTS");
break;
case CKR_SESSION_READ_WRITE_SO_EXISTS:
throw std::runtime_error("CKR_SESSION_READ_WRITE_SO_EXISTS");
break;
case CKR_SIGNATURE_INVALID:
throw std::runtime_error("CKR_SIGNATURE_INVALID");
break;
case CKR_SIGNATURE_LEN_RANGE:
throw std::runtime_error("CKR_SIGNATURE_LEN_RANGE");
break;
case CKR_TEMPLATE_INCOMPLETE:
throw std::runtime_error("CKR_TEMPLATE_INCOMPLETE");
break;
case CKR_TEMPLATE_INCONSISTENT:
throw std::runtime_error("CKR_TEMPLATE_INCONSISTENT");
break;
case CKR_TOKEN_NOT_PRESENT:
throw std::runtime_error("CKR_TOKEN_NOT_PRESENT");
break;
case CKR_TOKEN_NOT_RECOGNIZED:
throw std::runtime_error("CKR_TOKEN_NOT_RECOGNIZED");
break;
case CKR_TOKEN_WRITE_PROTECTED:
throw std::runtime_error("CKR_TOKEN_WRITE_PROTECTED");
break;
case CKR_UNWRAPPING_KEY_HANDLE_INVALID:
throw std::runtime_error("CKR_UNWRAPPING_KEY_HANDLE_INVALID");
break;
case CKR_UNWRAPPING_KEY_SIZE_RANGE:
throw std::runtime_error("CKR_UNWRAPPING_KEY_SIZE_RANGE");
break;
case CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT:
throw std::runtime_error("CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT");
break;
case CKR_USER_ALREADY_LOGGED_IN:
throw std::runtime_error("CKR_USER_ALREADY_LOGGED_IN");
break;
case CKR_USER_NOT_LOGGED_IN:
throw std::runtime_error("CKR_USER_NOT_LOGGED_IN");
break;
case CKR_USER_PIN_NOT_INITIALIZED:
throw std::runtime_error("CKR_USER_PIN_NOT_INITIALIZED");
break;
case CKR_USER_TYPE_INVALID:
throw std::runtime_error("CKR_USER_TYPE_INVALID");
break;
case CKR_USER_ANOTHER_ALREADY_LOGGED_IN:
throw std::runtime_error("CKR_USER_ANOTHER_ALREADY_LOGGED_IN");
break;
case CKR_USER_TOO_MANY_TYPES:
throw std::runtime_error("CKR_USER_TOO_MANY_TYPES");
break;
case CKR_WRAPPED_KEY_INVALID:
throw std::runtime_error("CKR_WRAPPED_KEY_INVALID");
break;
case CKR_WRAPPED_KEY_LEN_RANGE:
throw std::runtime_error("CKR_WRAPPED_KEY_LEN_RANGE");
break;
case CKR_WRAPPING_KEY_HANDLE_INVALID:
throw std::runtime_error("CKR_WRAPPING_KEY_HANDLE_INVALID");
break;
case CKR_WRAPPING_KEY_SIZE_RANGE:
throw std::runtime_error("CKR_WRAPPING_KEY_SIZE_RANGE");
break;
case CKR_WRAPPING_KEY_TYPE_INCONSISTENT:
throw std::runtime_error("CKR_WRAPPING_KEY_TYPE_INCONSISTENT");
break;
case CKR_RANDOM_SEED_NOT_SUPPORTED:
throw std::runtime_error("CKR_RANDOM_SEED_NOT_SUPPORTED");
break;
case CKR_RANDOM_NO_RNG:
throw std::runtime_error("CKR_RANDOM_NO_RNG");
break;
case CKR_DOMAIN_PARAMS_INVALID:
throw std::runtime_error("CKR_DOMAIN_PARAMS_INVALID");
break;
case CKR_BUFFER_TOO_SMALL:
throw std::runtime_error("CKR_BUFFER_TOO_SMALL");
break;
case CKR_SAVED_STATE_INVALID:
throw std::runtime_error("CKR_SAVED_STATE_INVALID");
break;
case CKR_INFORMATION_SENSITIVE:
throw std::runtime_error("CKR_INFORMATION_SENSITIVE");
break;
case CKR_STATE_UNSAVEABLE:
throw std::runtime_error("CKR_STATE_UNSAVEABLE");
break;
case CKR_CRYPTOKI_NOT_INITIALIZED:
throw std::runtime_error("CKR_CRYPTOKI_NOT_INITIALIZED");
break;
case CKR_CRYPTOKI_ALREADY_INITIALIZED:
//throw std::runtime_error("La librería cryptoki ya fue inicializada.");
break;
case CKR_MUTEX_BAD:
throw std::runtime_error("CKR_MUTEX_BAD");
break;
case CKR_MUTEX_NOT_LOCKED:
throw std::runtime_error("CKR_MUTEX_NOT_LOCKED");
break;
case CKR_FUNCTION_REJECTED:
throw std::runtime_error("CKR_FUNCTION_REJECTED");
break;
case CKR_VENDOR_DEFINED:
throw std::runtime_error("CKR_VENDOR_DEFINED");
break;
default:
throw std::runtime_error("Error desconocido.");
break;
}
}
Token::~Token()
{
CK_RV rv = functionList->C_Finalize(NULL);
error(rv);
functionList = NULL;
dlclose(dlHandle);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment