xref: /minix/external/bsd/bind/dist/lib/isc/pk11_result.c (revision bb9622b5)
1 /*	$NetBSD: pk11_result.c,v 1.1.1.3 2014/12/10 03:34:43 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2014  Internet Systems Consortium, Inc. ("ISC")
5  *
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.
9  *
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.
17  */
18 
19 #include <config.h>
20 #include <stddef.h>
21 
22 #include <isc/once.h>
23 #include <isc/msgcat.h>
24 #include <isc/util.h>
25 
26 #include <pk11/result.h>
27 
28 LIBISC_EXTERNAL_DATA isc_msgcat_t *		pk11_msgcat = NULL;
29 
30 static isc_once_t		msgcat_once = ISC_ONCE_INIT;
31 
32 static const char *text[PK11_R_NRESULTS] = {
33 	"PKCS#11 initialization failed",		/*%< 0 */
34 	"no PKCS#11 provider",				/*%< 1 */
35 	"PKCS#11 provider has no random service",	/*%< 2 */
36 	"PKCS#11 provider has no digest service",	/*%< 3 */
37 	"PKCS#11 provider has no AES service",		/*%< 4 */
38 };
39 
40 #define PK11_RESULT_RESULTSET			2
41 
42 static isc_once_t		once = ISC_ONCE_INIT;
43 
44 static void
45 open_msgcat(void) {
46 	isc_msgcat_open("libpk11.cat", &pk11_msgcat);
47 }
48 
49 void
50 pk11_initmsgcat(void) {
51 
52 	/*
53 	 * Initialize the PKCS#11 support's message catalog,
54 	 * pk11_msgcat, if it has not already been initialized.
55 	 */
56 
57 	RUNTIME_CHECK(isc_once_do(&msgcat_once, open_msgcat) == ISC_R_SUCCESS);
58 }
59 
60 static void
61 initialize_action(void) {
62 	isc_result_t result;
63 
64 	result = isc_result_register(ISC_RESULTCLASS_PK11, PK11_R_NRESULTS,
65 				     text, pk11_msgcat, PK11_RESULT_RESULTSET);
66 	if (result != ISC_R_SUCCESS)
67 		UNEXPECTED_ERROR(__FILE__, __LINE__,
68 				 "isc_result_register() failed: %u", result);
69 }
70 
71 static void
72 initialize(void) {
73 	pk11_initmsgcat();
74 	RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
75 }
76 
77 const char *
78 pk11_result_totext(isc_result_t result) {
79 	initialize();
80 
81 	return (isc_result_totext(result));
82 }
83 
84 void
85 pk11_result_register(void) {
86 	initialize();
87 }
88