xref: /netbsd-src/crypto/external/bsd/netpgp/dist/src/lib/errors.h (revision 2232f8005f71ea97271dcbe021a46395971dafe2)
1 /*-
2  * Copyright (c) 2009 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Alistair Crooks (agc@NetBSD.org)
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 /*
30  * Copyright (c) 2005-2008 Nominet UK (www.nic.uk)
31  * All rights reserved.
32  * Contributors: Ben Laurie, Rachel Willmer. The Contributors have asserted
33  * their moral rights under the UK Copyright Design and Patents Act 1988 to
34  * be recorded as the authors of this copyright work.
35  *
36  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
37  * use this file except in compliance with the License.
38  *
39  * You may obtain a copy of the License at
40  *     http://www.apache.org/licenses/LICENSE-2.0
41  *
42  * Unless required by applicable law or agreed to in writing, software
43  * distributed under the License is distributed on an "AS IS" BASIS,
44  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45  *
46  * See the License for the specific language governing permissions and
47  * limitations under the License.
48  */
49 
50 /** \file
51  */
52 
53 #ifndef OPS_ERRORS
54 #define OPS_ERRORS
55 
56 #include <errno.h>
57 
58 /** error codes */
59 /* Remember to add names to map in errors.c */
60 typedef enum {
61 	OPS_E_OK = 0x0000,	/* no error */
62 	OPS_E_FAIL = 0x0001,	/* general error */
63 	OPS_E_SYSTEM_ERROR = 0x0002,	/* system error, look at errno for
64 					 * details */
65 	OPS_E_UNIMPLEMENTED = 0x0003,	/* feature not yet implemented */
66 
67 	/* reader errors */
68 	OPS_E_R = 0x1000,	/* general reader error */
69 	OPS_E_R_READ_FAILED = OPS_E_R + 1,
70 	OPS_E_R_EARLY_EOF = OPS_E_R + 2,
71 	OPS_E_R_BAD_FORMAT = OPS_E_R + 3,	/* For example, malformed
72 						 * armour */
73 	OPS_E_R_UNSUPPORTED = OPS_E_R + 4,
74 	OPS_E_R_UNCONSUMED_DATA = OPS_E_R + 5,
75 
76 	/* writer errors */
77 	OPS_E_W = 0x2000,	/* general writer error */
78 	OPS_E_W_WRITE_FAILED = OPS_E_W + 1,
79 	OPS_E_W_WRITE_TOO_SHORT = OPS_E_W + 2,
80 
81 	/* parser errors */
82 	OPS_E_P = 0x3000,	/* general parser error */
83 	OPS_E_P_NOT_ENOUGH_DATA = OPS_E_P + 1,
84 	OPS_E_P_UNKNOWN_TAG = OPS_E_P + 2,
85 	OPS_E_P_PACKET_CONSUMED = OPS_E_P + 3,
86 	OPS_E_P_MPI_FORMAT_ERROR = OPS_E_P + 4,
87 	OPS_E_P_PACKET_NOT_CONSUMED = OPS_E_P + 5,
88 	OPS_E_P_DECOMPRESSION_ERROR = OPS_E_P + 6,
89 	OPS_E_P_NO_USERID = OPS_E_P + 7,
90 
91 	/* creator errors */
92 	OPS_E_C = 0x4000,	/* general creator error */
93 
94 	/* validation errors */
95 	OPS_E_V = 0x5000,	/* general validation error */
96 	OPS_E_V_BAD_SIGNATURE = OPS_E_V + 1,
97 	OPS_E_V_NO_SIGNATURE = OPS_E_V + 2,
98 	OPS_E_V_UNKNOWN_SIGNER = OPS_E_V + 3,
99 	OPS_E_V_BAD_HASH = OPS_E_V + 4,
100 
101 	/* Algorithm support errors */
102 	OPS_E_ALG = 0x6000,	/* general algorithm error */
103 	OPS_E_ALG_UNSUPPORTED_SYMMETRIC_ALG = OPS_E_ALG + 1,
104 	OPS_E_ALG_UNSUPPORTED_PUBLIC_KEY_ALG = OPS_E_ALG + 2,
105 	OPS_E_ALG_UNSUPPORTED_SIGNATURE_ALG = OPS_E_ALG + 3,
106 	OPS_E_ALG_UNSUPPORTED_HASH_ALG = OPS_E_ALG + 4,
107 	OPS_E_ALG_UNSUPPORTED_COMPRESS_ALG = OPS_E_ALG + 5,
108 
109 	/* Protocol errors */
110 	OPS_E_PROTO = 0x7000,	/* general protocol error */
111 	OPS_E_PROTO_BAD_SYMMETRIC_DECRYPT = OPS_E_PROTO + 2,
112 	OPS_E_PROTO_UNKNOWN_SS = OPS_E_PROTO + 3,
113 	OPS_E_PROTO_CRITICAL_SS_IGNORED = OPS_E_PROTO + 4,
114 	OPS_E_PROTO_BAD_PUBLIC_KEY_VRSN = OPS_E_PROTO + 5,
115 	OPS_E_PROTO_BAD_SIGNATURE_VRSN = OPS_E_PROTO + 6,
116 	OPS_E_PROTO_BAD_ONE_PASS_SIG_VRSN = OPS_E_PROTO + 7,
117 	OPS_E_PROTO_BAD_PKSK_VRSN = OPS_E_PROTO + 8,
118 	OPS_E_PROTO_DECRYPTED_MSG_WRONG_LEN = OPS_E_PROTO + 9,
119 	OPS_E_PROTO_BAD_SK_CHECKSUM = OPS_E_PROTO + 10
120 } __ops_errcode_t;
121 
122 /** one entry in a linked list of errors */
123 typedef struct __ops_error {
124 	__ops_errcode_t   errcode;
125 	int             sys_errno;	/* !< irrelevent unless errcode ==
126 					 * OPS_E_SYSTEM_ERROR */
127 	char           *comment;
128 	const char     *file;
129 	int             line;
130 	struct __ops_error *next;
131 }               __ops_error_t;
132 
133 const char     *__ops_errcode(const __ops_errcode_t errcode);
134 
135 void
136 __ops_push_error(__ops_error_t ** errstack, __ops_errcode_t errcode, int sys_errno,
137 	       const char *file, int line, const char *comment,...);
138 void            __ops_print_error(__ops_error_t * err);
139 void            __ops_print_errors(__ops_error_t * errstack);
140 void            __ops_free_errors(__ops_error_t * errstack);
141 int             __ops_has_error(__ops_error_t * errstack, __ops_errcode_t errcode);
142 
143 #define OPS_SYSTEM_ERROR_1(err,code,syscall,fmt,arg)	do {		\
144 	__ops_push_error(err,OPS_E_SYSTEM_ERROR,errno,__FILE__,__LINE__,syscall);\
145 	__ops_push_error(err,code,0,__FILE__,__LINE__,fmt,arg);		\
146 } while(/*CONSTCOND*/0)
147 #define OPS_MEMORY_ERROR(err) {						\
148 	fprintf(stderr, "Memory error\n");				\
149 }				/* \todo placeholder for better error
150 				 * handling */
151 #define OPS_ERROR(err,code,fmt)	do {					\
152 	__ops_push_error(err,code,0,__FILE__,__LINE__,fmt);		\
153 } while(/*CONSTCOND*/0)
154 #define OPS_ERROR_1(err,code,fmt,arg)	do {				\
155 	__ops_push_error(err,code,0,__FILE__,__LINE__,fmt,arg);		\
156 } while(/*CONSTCOND*/0)
157 #define OPS_ERROR_2(err,code,fmt,arg,arg2)	do {			\
158 	__ops_push_error(err,code,0,__FILE__,__LINE__,fmt,arg,arg2);	\
159 } while(/*CONSTCOND*/0)
160 #define OPS_ERROR_3(err,code,fmt,arg,arg2,arg3)	do {			\
161 	__ops_push_error(err,code,0,__FILE__,__LINE__,fmt,arg,arg2,arg3);	\
162 } while(/*CONSTCOND*/0)
163 #define OPS_ERROR_4(err,code,fmt,arg,arg2,arg3,arg4)	do {		\
164 	__ops_push_error(err,code,0,__FILE__,__LINE__,fmt,arg,arg2,arg3,arg4); \
165 } while(/*CONSTCOND*/0)
166 
167 #endif				/* OPS_ERRORS */
168