~ubuntu-branches/ubuntu/lucid/bind9/lucid

« back to all changes in this revision

Viewing changes to contrib/pkcs11-keygen/genkey.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2010-03-01 20:51:23 UTC
  • mfrom: (1.6.4 upstream) (10.1.5 experimental)
  • Revision ID: james.westby@ubuntu.com-20100301205123-kslpwaipx72vq1o1
Tags: 1:9.7.0.dfsg-1~build1
upload of -1 to lucid, LP#530107

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* genkey - pkcs11 rsa key generator
2
 
 *
3
 
 * create RSASHA1 key in the keystore of an SCA6000
4
 
 * The calculation of key tag is left to the script
5
 
 * that converts the key into a DNSKEY RR and inserts 
6
 
 * it into a zone file.
7
 
 *
8
 
 * usage:
9
 
 * genkey [-P] [-s slot] -b keysize -l label [-p pin] 
10
 
 *
11
 
 */
12
 
 
13
 
#include <stdio.h>
14
 
#include <stdlib.h>
15
 
#include <unistd.h>
16
 
#include <fcntl.h>
17
 
#include <errno.h>
18
 
#include <string.h>
19
 
#include <sys/types.h>
20
 
#ifndef OPENCRYPTOKI
21
 
#include <security/cryptoki.h>
22
 
#include <security/pkcs11.h>
23
 
#else
24
 
#include <opencryptoki/pkcs11.h>
25
 
#endif
26
 
 
27
 
/* Define static key template values */
28
 
static CK_BBOOL truevalue = TRUE;
29
 
static CK_BBOOL falsevalue = FALSE;
30
 
 
31
 
int
32
 
main(int argc, char *argv[])
33
 
{
34
 
    CK_RV rv;
35
 
    CK_SLOT_ID slot = 0;
36
 
    CK_MECHANISM genmech;
37
 
    CK_SESSION_HANDLE hSession;
38
 
    CK_UTF8CHAR *pin = NULL;
39
 
    CK_ULONG modulusbits = 0;
40
 
    CK_CHAR *label = NULL;
41
 
    CK_OBJECT_HANDLE privatekey, publickey;
42
 
    CK_BYTE public_exponent[3];
43
 
    int error = 0;
44
 
    int i = 0;
45
 
    int c, errflg = 0;
46
 
    int hide = 1;
47
 
    CK_ULONG ulObjectCount;
48
 
    /* Set search template */
49
 
    CK_ATTRIBUTE search_template[] = {
50
 
        {CKA_LABEL, NULL_PTR, 0}
51
 
    };
52
 
    CK_ATTRIBUTE publickey_template[] = {
53
 
        {CKA_LABEL, NULL_PTR, 0},
54
 
        {CKA_VERIFY, &truevalue, sizeof (truevalue)},
55
 
        {CKA_TOKEN, &truevalue, sizeof (truevalue)},
56
 
        {CKA_MODULUS_BITS, &modulusbits, sizeof (modulusbits)},
57
 
        {CKA_PUBLIC_EXPONENT, &public_exponent, sizeof (public_exponent)}
58
 
    };
59
 
    CK_ATTRIBUTE privatekey_template[] = {
60
 
        {CKA_LABEL, NULL_PTR, 0},
61
 
        {CKA_SIGN, &truevalue, sizeof (truevalue)},
62
 
        {CKA_TOKEN, &truevalue, sizeof (truevalue)},
63
 
        {CKA_PRIVATE, &truevalue, sizeof (truevalue)},
64
 
        {CKA_SENSITIVE, &truevalue, sizeof (truevalue)},
65
 
        {CKA_EXTRACTABLE, &falsevalue, sizeof (falsevalue)}
66
 
    };
67
 
    extern char *optarg;
68
 
    extern int optopt;
69
 
    
70
 
    while ((c = getopt(argc, argv, ":Ps:b:i:l:p:")) != -1) {
71
 
        switch (c) {
72
 
        case 'P':
73
 
            hide = 0;
74
 
            break;
75
 
        case 's':
76
 
            slot = atoi(optarg);
77
 
            break;
78
 
        case 'b':
79
 
            modulusbits = atoi(optarg);
80
 
            break;
81
 
        case 'l':
82
 
            label = (CK_CHAR *)optarg;
83
 
            break;
84
 
        case 'p':
85
 
            pin = (CK_UTF8CHAR *)optarg;
86
 
            break;
87
 
        case ':':
88
 
            fprintf(stderr, "Option -%c requires an operand\n", optopt);
89
 
            errflg++;
90
 
            break;
91
 
        case '?':
92
 
        default:
93
 
            fprintf(stderr, "Unrecognised option: -%c\n", optopt);
94
 
            errflg++;
95
 
        }
96
 
    }
97
 
    if ((errflg) || (!modulusbits) || (!label)) {
98
 
        fprintf(stderr,
99
 
                "usage: genkey [-P] [-s slot] -b keysize -l label [-p pin]\n");
100
 
        exit(2);
101
 
    }
102
 
    
103
 
    search_template[0].pValue = label;
104
 
    search_template[0].ulValueLen = strlen((char *)label);
105
 
    publickey_template[0].pValue = label;
106
 
    publickey_template[0].ulValueLen = strlen((char *)label);
107
 
    privatekey_template[0].pValue = label;
108
 
    privatekey_template[0].ulValueLen = strlen((char *)label);
109
 
 
110
 
    /* Set public exponent to 65537 */
111
 
    public_exponent[0] = 0x01;
112
 
    public_exponent[1] = 0x00;
113
 
    public_exponent[2] = 0x01;
114
 
 
115
 
    /* Set up mechanism for generating key pair */
116
 
    genmech.mechanism = CKM_RSA_PKCS_KEY_PAIR_GEN;
117
 
    genmech.pParameter = NULL_PTR;
118
 
    genmech.ulParameterLen = 0;
119
 
 
120
 
    /* Initialize the CRYPTOKI library */
121
 
    rv = C_Initialize(NULL_PTR);
122
 
 
123
 
    if (rv != CKR_OK) {
124
 
        fprintf(stderr, "C_Initialize: Error = 0x%.8X\n", rv);
125
 
        exit(1);
126
 
    }
127
 
 
128
 
    /* Open a session on the slot found */
129
 
    rv = C_OpenSession(slot, CKF_RW_SESSION+CKF_SERIAL_SESSION,
130
 
                       NULL_PTR, NULL_PTR, &hSession);
131
 
 
132
 
    if (rv != CKR_OK) {
133
 
        fprintf(stderr, "C_OpenSession: Error = 0x%.8X\n", rv);
134
 
        error = 1;
135
 
        goto exit_program;
136
 
    }
137
 
 
138
 
    /* Login to the Token (Keystore) */
139
 
    if (!pin)
140
 
#ifndef OPENCRYPTOKI
141
 
        pin = (CK_UTF8CHAR *)getpassphrase("Enter Pin: ");
142
 
#else
143
 
        pin = (CK_UTF8CHAR *)getpass("Enter Pin: ");
144
 
#endif
145
 
    rv = C_Login(hSession, CKU_USER, pin, strlen((char *)pin));
146
 
    memset(pin, 0, strlen((char *)pin));
147
 
    if (rv != CKR_OK) {
148
 
        fprintf(stderr, "C_Login: Error = 0x%.8X\n", rv);
149
 
        error = 1;
150
 
        goto exit_session;
151
 
    }
152
 
   
153
 
    /* check if a key with the same id already exists */
154
 
    rv = C_FindObjectsInit(hSession, search_template, 1); 
155
 
    if (rv != CKR_OK) {
156
 
        fprintf(stderr, "C_FindObjectsInit: Error = 0x%.8X\n", rv);
157
 
        error = 1;
158
 
        goto exit_session;
159
 
    }
160
 
    rv = C_FindObjects(hSession, &privatekey, 1, &ulObjectCount);
161
 
    if (rv != CKR_OK) {
162
 
        fprintf(stderr, "C_FindObjects: Error = 0x%.8X\n", rv);
163
 
        error = 1;
164
 
        goto exit_search;
165
 
    }
166
 
    if (ulObjectCount != 0) {
167
 
        fprintf(stderr, "Key already exists.\n");
168
 
        error = 1;
169
 
        goto exit_search;
170
 
    }
171
 
    
172
 
    /* Set attributes if the key is not to be hidden */
173
 
    if (!hide) {
174
 
        privatekey_template[4].pValue = &falsevalue;
175
 
        privatekey_template[5].pValue = &truevalue;
176
 
    }
177
 
 
178
 
    /* Generate Key pair for signing/verifying */
179
 
    rv = C_GenerateKeyPair(hSession, &genmech, publickey_template,
180
 
                           (sizeof (publickey_template) /
181
 
                            sizeof (CK_ATTRIBUTE)),
182
 
                           privatekey_template,
183
 
                           (sizeof (privatekey_template) /
184
 
                            sizeof (CK_ATTRIBUTE)),
185
 
                           &publickey, &privatekey);
186
 
        
187
 
    if (rv != CKR_OK) {
188
 
        fprintf(stderr, "C_GenerateKeyPair: Error = 0x%.8X\n", rv);
189
 
        error = 1;
190
 
    }
191
 
    
192
 
 exit_search:
193
 
    rv = C_FindObjectsFinal(hSession);
194
 
    if (rv != CKR_OK) {
195
 
        fprintf(stderr, "C_FindObjectsFinal: Error = 0x%.8X\n", rv);
196
 
        error = 1;
197
 
    }
198
 
 
199
 
 exit_session:
200
 
    (void) C_CloseSession(hSession);
201
 
 
202
 
 exit_program:
203
 
    (void) C_Finalize(NULL_PTR);
204
 
 
205
 
    exit(error);
206
 
}