etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / bin / tests / pkcs11 / pkcs11-md5sum.c
blob44e0f86f73fae5fc20f3cf3ec3c75bd0a9ea1699
1 /* $NetBSD: pkcs11-md5sum.c,v 1.1.1.4 2014/12/10 03:34:28 christos Exp $ */
3 /*
4 * Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
20 * Portions copyright (c) 2008 Nominet UK. All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
24 * are met:
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 /* Id */
46 * pkcs11-md5sum
48 * Prints the MD5 checksum of the standard input, using the PKCS#11 device.
50 * Usage:
51 * pkcs11-md5sum [-m module] [-s $slot] [-n] [-p $pin]
52 * -m: PKCS#11 provider module. This must be the full
53 * path to a shared library object implementing the
54 * PKCS#11 API for a device.
55 * -s: Slot
56 * -p: PIN
57 * -n: don't log in to the PKCS#11 device
60 /*! \file */
62 #include <config.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <fcntl.h>
67 #include <errno.h>
68 #include <string.h>
69 #include <sys/types.h>
71 #include <isc/commandline.h>
72 #include <isc/result.h>
73 #include <isc/types.h>
75 #include <pk11/pk11.h>
76 #include <pk11/result.h>
78 #if !(defined(HAVE_GETPASSPHRASE) || (defined (__SVR4) && defined (__sun)))
79 #define getpassphrase(x) getpass(x)
80 #endif
82 #define BLOCKSIZE 32768
84 char buffer[BLOCKSIZE + 72];
85 char digest[16];
87 int
88 main(int argc, char *argv[]) {
89 isc_result_t result;
90 CK_RV rv;
91 CK_SLOT_ID slot = 0;
92 CK_SESSION_HANDLE hSession;
93 CK_MECHANISM mech = { CKM_MD5, NULL, 0 };
94 CK_ULONG len;
95 pk11_context_t pctx;
96 pk11_optype_t op_type = OP_DIGEST;
97 char *lib_name = NULL;
98 char *pin = NULL;
99 int error = 0;
100 isc_boolean_t logon = ISC_TRUE;
101 int c, errflg = 0;
102 size_t sum = 0;
103 unsigned int i;
105 while ((c = isc_commandline_parse(argc, argv, ":m:s:np:")) != -1) {
106 switch (c) {
107 case 'm':
108 lib_name = isc_commandline_argument;
109 break;
110 case 's':
111 slot = atoi(isc_commandline_argument);
112 op_type = OP_ANY;
113 break;
114 case 'n':
115 logon = ISC_FALSE;
116 break;
117 case 'p':
118 pin = isc_commandline_argument;
119 break;
120 case ':':
121 fprintf(stderr,
122 "Option -%c requires an operand\n",
123 isc_commandline_option);
124 errflg++;
125 break;
126 case '?':
127 default:
128 fprintf(stderr, "Unrecognised option: -%c\n",
129 isc_commandline_option);
130 errflg++;
134 if (errflg) {
135 fprintf(stderr, "Usage:\n");
136 fprintf(stderr,
137 "\tpkcs11-md5sum [-m module] [-s slot] [-n|-p pin]\n");
138 exit(1);
141 pk11_result_register();
143 /* Initialize the CRYPTOKI library */
144 if (lib_name != NULL)
145 pk11_set_lib_name(lib_name);
147 if (logon && pin == NULL)
148 pin = getpassphrase("Enter Pin: ");
150 result = pk11_get_session(&pctx, op_type, ISC_FALSE, ISC_FALSE, logon,
151 (const char *) pin, slot);
152 if ((result != ISC_R_SUCCESS) &&
153 (result != PK11_R_NORANDOMSERVICE) &&
154 (result != PK11_R_NOAESSERVICE)) {
155 fprintf(stderr, "Error initializing PKCS#11: %s\n",
156 isc_result_totext(result));
157 exit(1);
160 if (pin != NULL)
161 memset(pin, 0, strlen((char *)pin));
163 hSession = pctx.session;
165 rv = pkcs_C_DigestInit(hSession, &mech);
166 if (rv != CKR_OK) {
167 fprintf(stderr, "C_DigestInit: Error = 0x%.8lX\n", rv);
168 error = 1;
169 goto exit_session;
172 for (;;) {
173 size_t n;
175 for (;;) {
176 n = fread(buffer + sum, 1, BLOCKSIZE - sum, stdin);
177 sum += n;
178 if (sum == BLOCKSIZE)
179 break;
180 if (n == 0) {
181 if (ferror(stdin)) {
182 fprintf(stderr, "fread failed\n");
183 error = 1;
184 goto exit_session;
186 goto partial_block;
188 if (feof(stdin))
189 goto partial_block;
192 rv = pkcs_C_DigestUpdate(hSession, (CK_BYTE_PTR) buffer,
193 (CK_ULONG) BLOCKSIZE);
194 if (rv != CKR_OK) {
195 fprintf(stderr,
196 "C_DigestUpdate: Error = 0x%.8lX\n",
197 rv);
198 error = 1;
199 goto exit_session;
203 partial_block:
204 if (sum > 0) {
205 rv = pkcs_C_DigestUpdate(hSession, (CK_BYTE_PTR) buffer,
206 (CK_ULONG) sum);
207 if (rv != CKR_OK) {
208 fprintf(stderr,
209 "C_DigestUpdate: Error = 0x%.8lX\n",
210 rv);
211 error = 1;
212 goto exit_session;
216 len = 16;
217 rv = pkcs_C_DigestFinal(hSession, (CK_BYTE_PTR) digest, &len);
218 if (rv != CKR_OK) {
219 fprintf(stderr, "C_DigestFinal: Error = 0x%.8lX\n", rv);
220 error = 1;
221 goto exit_session;
223 if (len != 16) {
224 fprintf(stderr, "C_DigestFinal: bad length = %lu\n", len);
225 error = 1;
228 for (i = 0; i < 16; i++)
229 printf("%02x", digest[i] & 0xff);
230 printf("\n");
232 exit_session:
233 pk11_return_session(&pctx);
234 (void) pk11_finalize();
236 exit(error);