ignore some files
[tridge/bind9.git] / contrib / pkcs11-keygen / set_key_id.c
1 /* set_key_id [-s slot] [-p $pin] -n $keytag {-i $id | -l $label} */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <fcntl.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <sys/types.h>
10 #ifndef OPENCRYPTOKI
11 #include <security/cryptoki.h>
12 #include <security/pkcs11.h>
13 #else
14 #include <opencryptoki/pkcs11.h>
15 #endif
16
17 int
18 main(int argc, char *argv[])
19 {
20     CK_RV rv;
21     CK_SLOT_ID slot = 0;
22     CK_SESSION_HANDLE hSession;
23     CK_UTF8CHAR *pin = NULL;
24     CK_BYTE old_id[2], new_id[2];
25     CK_OBJECT_HANDLE akey;
26     int error = 0;
27     int i = 0;
28     int c, errflg = 0;
29     char *label = NULL;
30     CK_ULONG ulObjectCount;
31     int oid = 0, nid = 0;
32     CK_ATTRIBUTE search_template[] = {
33         {CKA_ID, &old_id, sizeof(old_id)}
34     };
35     extern char *optarg;
36     extern int optopt;
37
38     while ((c = getopt(argc, argv, ":s:i:n:l:p:")) != -1) {
39         switch (c) {
40         case 's':
41             slot = atoi(optarg);
42             break;
43         case 'i':
44             oid = atoi(optarg);
45             oid &= 0xffff;
46             old_id[0] = (oid >> 8) & 0xff;
47             old_id[1] = oid & 0xff;
48             break;
49         case 'n':
50             nid = atoi(optarg);
51             nid &= 0xffff;
52             new_id[0] = (nid >> 8) & 0xff;
53             new_id[1] = nid & 0xff;
54             break;
55         case 'l':
56             label = optarg;
57             break;
58         case 'p':
59             pin = (CK_UTF8CHAR *)optarg;
60             break;
61         case ':':
62             fprintf(stderr, "Option -%c requires an operand\n", optopt);
63             errflg++;
64             break;
65         case '?':
66         default:
67             fprintf(stderr, "Unrecognised option: -%c\n", optopt);
68             errflg++;
69         }
70     }
71     if ((errflg) || (!nid) || ((!oid) && (!label))) {
72         fprintf(stderr,
73                 "usage: set_key_id [-s slot] [-p pin] -n new_id "
74                 "{ -i old_id | -l label }\n");
75         exit(1);
76     }
77     if (!label)
78         printf("old %i new %i\n", oid, nid);
79     else {
80         printf("label %s new %i\n", label, nid);
81         search_template[0].type = CKA_LABEL;
82         search_template[0].pValue = label;
83         search_template[0].ulValueLen = strlen(label);
84     }
85
86     /* Initialize the CRYPTOKI library */
87     rv = C_Initialize(NULL_PTR);
88     if (rv != CKR_OK) {
89         fprintf(stderr, "C_Initialize: Error = 0x%.8X\n", rv);
90         exit(1);
91     }
92
93     /* Open a session on the slot found */
94     rv = C_OpenSession(slot, CKF_RW_SESSION+CKF_SERIAL_SESSION,
95                        NULL_PTR, NULL_PTR, &hSession);
96     if (rv != CKR_OK) {
97         fprintf(stderr, "C_OpenSession: Error = 0x%.8X\n", rv);
98         error = 1;
99         goto exit_program;
100     }
101
102     /* Login to the Token (Keystore) */
103     if (!pin)
104 #ifndef OPENCRYPTOKI
105         pin = (CK_UTF8CHAR *)getpassphrase("Enter Pin: ");
106 #else
107         pin = (CK_UTF8CHAR *)getpass("Enter Pin: ");
108 #endif
109     rv = C_Login(hSession, CKU_USER, pin, strlen((char *)pin));
110     memset(pin, 0, strlen((char *)pin));
111     if (rv != CKR_OK) {
112         fprintf(stderr, "C_Login: Error = 0x%.8X\n", rv);
113         error = 1;
114         goto exit_session;
115     }
116
117     rv = C_FindObjectsInit(hSession, search_template, 1); 
118     if (rv != CKR_OK) {
119         fprintf(stderr, "C_FindObjectsInit: Error = 0x%.8X\n", rv);
120         error = 1;
121         goto exit_session;
122     }
123     
124     ulObjectCount = 1;
125     while(ulObjectCount) {
126         rv = C_FindObjects(hSession, &akey, 1, &ulObjectCount);
127         if (rv != CKR_OK) {
128             fprintf(stderr, "C_FindObjects: Error = 0x%.8X\n", rv);
129             error = 1;
130             goto exit_search;
131         } else if (ulObjectCount) {       
132             /* Set update template. */
133             CK_ATTRIBUTE new_template[] = {
134                 {CKA_ID, &new_id, sizeof(new_id)}
135             };
136
137             rv = C_SetAttributeValue(hSession, akey, new_template, 1);
138             if (rv != CKR_OK) {
139                 fprintf(stderr, "C_SetAttributeValue: rv = 0x%.8X\n", rv);
140                 error = 1;
141             }
142         }
143     }
144
145  exit_search:
146     rv = C_FindObjectsFinal(hSession);
147     if (rv != CKR_OK) {
148         fprintf(stderr, "C_FindObjectsFinal: Error = 0x%.8X\n", rv);
149         error = 1;
150     }
151
152  exit_session:
153     (void) C_CloseSession(hSession);
154
155  exit_program:
156     (void) C_Finalize(NULL_PTR);
157
158     exit(error);
159 }