Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
p11_generate_rsa.c
Go to the documentation of this file.
1/*
2 * Copyright 2015-2018 Yubico AB
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifdef NDEBUG
18#undef NDEBUG
19#endif
20#include <assert.h>
21#include <dlfcn.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <pkcs11.h>
27
28int main(int argc, char *argv[]) {
29 if (argc != 2) {
30 fprintf(stderr, "usage: p11 /path/to/yubihsm_pkcs11/module\n");
31 exit(EXIT_FAILURE);
32 }
33
34 CK_C_GetFunctionList fn;
35 void *handle = dlopen(argv[1], RTLD_NOW | RTLD_GLOBAL);
36 assert(handle != NULL);
37
38 *(void **) (&fn) = dlsym(handle, "C_GetFunctionList");
39 assert(fn != NULL);
40
42 CK_RV rv = fn(&p11);
43 assert(rv == CKR_OK);
44
46 assert(rv == CKR_OK);
47
48 CK_SESSION_HANDLE session;
50 &session);
51 assert(rv == CKR_OK);
52
53 const char *password = "0001password";
55 (CK_ULONG) strlen(password));
56 assert(rv == CKR_OK);
57
59 CK_ULONG modulus = 2048;
60 CK_BYTE exponent[] = {0x00, 0x1, 0x0, 0x1}; // 65537
61 CK_BYTE id[] = {0};
62 CK_BBOOL ck_true = CK_TRUE;
63 CK_BBOOL ck_false = CK_FALSE;
64
65 CK_ATTRIBUTE publicKeyTemplate[] = {
66 {CKA_ENCRYPT, &ck_true, sizeof(ck_true)},
67 {CKA_DECRYPT, &ck_false, sizeof(ck_false)},
68 {CKA_SIGN, &ck_false, sizeof(ck_false)},
69 {CKA_VERIFY, &ck_true, sizeof(ck_true)},
70 {CKA_WRAP, &ck_true, sizeof(ck_true)},
71 {CKA_UNWRAP, &ck_false, sizeof(ck_false)},
72 {CKA_TOKEN, &ck_true, sizeof(ck_true)},
73 {CKA_PRIVATE, &ck_true, sizeof(ck_true)},
74 {CKA_EXTRACTABLE, &ck_true, sizeof(ck_true)},
75 {CKA_MODIFIABLE, &ck_false, sizeof(ck_false)},
76 {CKA_COPYABLE, &ck_false, sizeof(ck_false)},
77 {CKA_DESTROYABLE, &ck_true, sizeof(ck_true)},
78 {CKA_ID, id, sizeof(id)},
79 {CKA_MODULUS_BITS, &modulus, sizeof(modulus)},
80 {CKA_PUBLIC_EXPONENT, exponent, sizeof(exponent)},
81 };
82 CK_ULONG publicKeyAttributeCount =
83 sizeof(publicKeyTemplate) / sizeof(publicKeyTemplate[0]);
84
85 CK_ATTRIBUTE privateKeyTemplate[] = {
86 {CKA_ENCRYPT, &ck_false, sizeof(ck_false)},
87 {CKA_DECRYPT, &ck_true, sizeof(ck_true)},
88 {CKA_SIGN, &ck_true, sizeof(ck_true)},
89 {CKA_VERIFY, &ck_false, sizeof(ck_false)},
90 {CKA_WRAP, &ck_false, sizeof(ck_false)},
91 {CKA_UNWRAP, &ck_true, sizeof(ck_true)},
92 {CKA_TOKEN, &ck_true, sizeof(ck_true)},
93 {CKA_PRIVATE, &ck_true, sizeof(ck_true)},
94 {CKA_EXTRACTABLE, &ck_true, sizeof(ck_true)},
95 {CKA_MODIFIABLE, &ck_false, sizeof(ck_false)},
96 {CKA_COPYABLE, &ck_false, sizeof(ck_false)},
97 {CKA_DESTROYABLE, &ck_true, sizeof(ck_true)},
98 {CKA_ID, id, sizeof(id)},
99 };
100 CK_ULONG privateKeyAttributeCount =
101 sizeof(privateKeyTemplate) / sizeof(privateKeyTemplate[0]);
102
103 CK_OBJECT_HANDLE publicKey, privateKey;
104 rv =
105 p11->C_GenerateKeyPair(session, &mechanism, publicKeyTemplate,
106 publicKeyAttributeCount, privateKeyTemplate,
107 privateKeyAttributeCount, &publicKey, &privateKey);
108 assert(rv == CKR_OK);
109
111 assert(rv == CKR_OK);
112
113 rv = p11->C_Finalize(NULL);
114 assert(rv == CKR_OK);
115
116 return 0;
117}
uint64_t id
Definition code_cache.cpp:0
CK_SESSION_HANDLE session
CK_FUNCTION_LIST_PTR p11
char ** argv
#define CKA_TOKEN
Definition pkcs11.h:364
#define CK_FALSE
Definition pkcs11.h:1202
#define CKA_ID
Definition pkcs11.h:385
unsigned long int CK_ULONG
Definition pkcs11.h:1194
#define CKA_PUBLIC_EXPONENT
Definition pkcs11.h:400
#define CKA_MODIFIABLE
Definition pkcs11.h:420
#define CKM_RSA_PKCS_KEY_PAIR_GEN
Definition pkcs11.h:469
#define CKR_OK
Definition pkcs11.h:1092
#define CKF_RW_SESSION
Definition pkcs11.h:293
#define CKU_USER
Definition pkcs11.h:275
unsigned char CK_BYTE
Definition pkcs11.h:1190
#define CKA_PRIVATE
Definition pkcs11.h:365
#define NULL_PTR
Definition pkcs11.h:1257
#define CKA_SIGN
Definition pkcs11.h:391
#define CK_TRUE
Definition pkcs11.h:1203
#define CKA_VERIFY
Definition pkcs11.h:393
#define CKF_SERIAL_SESSION
Definition pkcs11.h:294
#define CKA_COPYABLE
Definition pkcs11.h:421
#define CKA_UNWRAP
Definition pkcs11.h:390
unsigned char CK_BBOOL
Definition pkcs11.h:1193
#define CKA_DECRYPT
Definition pkcs11.h:388
#define CKA_DESTROYABLE
Definition pkcs11.h:422
#define CKA_MODULUS_BITS
Definition pkcs11.h:399
#define CKA_EXTRACTABLE
Definition pkcs11.h:415
#define CKA_ENCRYPT
Definition pkcs11.h:387
CK_UTF8CHAR * CK_UTF8CHAR_PTR
Definition pkcs11.h:1198
#define CKA_WRAP
Definition pkcs11.h:389
CK_C_OpenSession C_OpenSession
Definition pkcs11.h:1017
CK_C_Login C_Login
Definition pkcs11.h:1023
CK_C_GenerateKeyPair C_GenerateKeyPair
Definition pkcs11.h:1064
CK_C_Initialize C_Initialize
Definition pkcs11.h:1005
CK_C_Logout C_Logout
Definition pkcs11.h:1024
CK_C_Finalize C_Finalize
Definition pkcs11.h:1006
CK_RV rv