summaryrefslogtreecommitdiff
path: root/c_src/pkcs11.c
blob: df89ad11588f45feebde9e98027983495429cddd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * Copyright (c) 2015, NORDUnet A/S.
 * See LICENSE for licensing information.
 */

#include <stdio.h>
#include <err.h>
#include <stdlib.h>
#include <string.h>

#include <dlfcn.h>

#include "hsmhelper.h"

void *pkcs11_library;

static CK_FUNCTION_LIST_PTR pk;

void
init(char *library_path)
{
    CK_RV result;

    pkcs11_library = dlopen(library_path, RTLD_NOW);
    CK_C_GetFunctionList C_GetFunctionList = dlsym(pkcs11_library, "C_GetFunctionList");
    result = C_GetFunctionList(&pk);
    if (result != CKR_OK) {
        errx(1, "C_GetFunctionList error: %lu", result);
    }

    result = pk->C_Initialize(NULL);
    if (result != CKR_OK) {
        errx(1, "C_Initialize error: %lu", result);
    }
}

void
finalize(CK_SLOT_ID slotID)
{
    CK_RV result;

    result = pk->C_CloseAllSessions(slotID);
    if (result != CKR_OK) {
        errx(1, "C_CloseAllSessions error: %lu", result);
    }

    result = pk->C_Finalize(NULL);
    if (result != CKR_OK) {
        errx(1, "C_Finalize error: %lu", result);
    }
}


CK_SESSION_HANDLE
open_session(CK_SLOT_ID slotID)
{
    CK_RV result;
    CK_SESSION_HANDLE hSession;

    result = pk->C_OpenSession (slotID, CKF_SERIAL_SESSION, NULL, NULL, &hSession);
    if (result != CKR_OK) {
        errx(1, "C_OpenSession error: %lu", result);
    }

    return hSession;
}

void
login(CK_SESSION_HANDLE hSession, char *pin)
{
    CK_RV result;

    result = pk->C_Login(hSession, CKU_USER, (CK_UTF8CHAR_PTR)pin, strlen(pin));
    if (result != CKR_OK) {
        errx(1, "C_Login error: %lu", result);
    }
}

CK_OBJECT_HANDLE
find_key(CK_SESSION_HANDLE hSession, CK_OBJECT_CLASS class, char *label)
{
    CK_RV result;

    CK_ATTRIBUTE template[2];
    template[0].type = CKA_CLASS;
    template[0].pValue = &class;
    template[0].ulValueLen = sizeof(class);

    template[1].type = CKA_LABEL;
    template[1].pValue = label;
    template[1].ulValueLen = strlen(label);

    CK_ULONG ulCount = 2;

    result = pk->C_FindObjectsInit(hSession,
                                   template,
                                   ulCount
        ) ;
    if (result != CKR_OK) {
        errx(1, "C_FindObjectsInit error: %lu", result);
    }

    CK_OBJECT_HANDLE hObject = 0;
    CK_ULONG ulMaxObjectCount = 1;
    CK_ULONG ulObjectCount;

    result = pk->C_FindObjects(hSession, &hObject, ulMaxObjectCount,
                               &ulObjectCount);
    if (result != CKR_OK) {
        errx(1, "C_FindObjects error: %lu", result);
    }

    if (ulObjectCount < 1) {
        errx(1, "could not find key: %s", label);
    }

    result = pk->C_FindObjectsFinal (hSession);
    if (result != CKR_OK) {
        errx(1, "C_FindObjectsFinal error: %lu", result);
    }

    return hObject;
}

void
sign(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hKey,
     CK_BYTE_PTR pData, CK_ULONG ulDataLen,
     CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen,
     CK_MECHANISM_TYPE mechanism_type)
{
    CK_RV result;

    CK_MECHANISM mechanism;
    mechanism.mechanism = mechanism_type;
    mechanism.pParameter = NULL;
    mechanism.ulParameterLen = 0;

    result = pk->C_SignInit(hSession,
                            &mechanism,
                            hKey);
    if (result != CKR_OK) {
        errx(1, "C_SignInit error: %lu", result);
    }

    result = pk->C_Sign(hSession,
                        pData,
                        ulDataLen,
                        pSignature,
                        pulSignatureLen);

    if (result != CKR_OK) {
        errx(1, "C_Sign error: %lu", result);
    }

}