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 ERRORS_H_
54 #define ERRORS_H_
55 
56 #include <errno.h>
57 
58 #ifndef __printflike
59 #define __printflike(n, m)	__attribute__((format(printf,n,m)))
60 #endif
61 
62 /** error codes */
63 /* Remember to add names to map in errors.c */
64 typedef enum {
65 	PGP_E_OK = 0x0000,	/* no error */
66 	PGP_E_FAIL = 0x0001,	/* general error */
67 	PGP_E_SYSTEM_ERROR = 0x0002,	/* system error, look at errno for
68 					 * details */
69 	PGP_E_UNIMPLEMENTED = 0x0003,	/* feature not yet implemented */
70 
71 	/* reader errors */
72 	PGP_E_R = 0x1000,	/* general reader error */
73 	PGP_E_R_READ_FAILED = PGP_E_R + 1,
74 	PGP_E_R_EARLY_EOF = PGP_E_R + 2,
75 	PGP_E_R_BAD_FORMAT = PGP_E_R + 3,	/* For example, malformed
76 						 * armour */
77 	PGP_E_R_UNSUPPORTED = PGP_E_R + 4,
78 	PGP_E_R_UNCONSUMED_DATA = PGP_E_R + 5,
79 
80 	/* writer errors */
81 	PGP_E_W = 0x2000,	/* general writer error */
82 	PGP_E_W_WRITE_FAILED = PGP_E_W + 1,
83 	PGP_E_W_WRITE_TOO_SHORT = PGP_E_W + 2,
84 
85 	/* parser errors */
86 	PGP_E_P = 0x3000,	/* general parser error */
87 	PGP_E_P_NOT_ENOUGH_DATA = PGP_E_P + 1,
88 	PGP_E_P_UNKNOWN_TAG = PGP_E_P + 2,
89 	PGP_E_P_PACKET_CONSUMED = PGP_E_P + 3,
90 	PGP_E_P_MPI_FORMAT_ERROR = PGP_E_P + 4,
91 	PGP_E_P_PACKET_NOT_CONSUMED = PGP_E_P + 5,
92 	PGP_E_P_DECOMPRESSION_ERROR = PGP_E_P + 6,
93 	PGP_E_P_NO_USERID = PGP_E_P + 7,
94 
95 	/* creator errors */
96 	PGP_E_C = 0x4000,	/* general creator error */
97 
98 	/* validation errors */
99 	PGP_E_V = 0x5000,	/* general validation error */
100 	PGP_E_V_BAD_SIGNATURE = PGP_E_V + 1,
101 	PGP_E_V_NO_SIGNATURE = PGP_E_V + 2,
102 	PGP_E_V_UNKNOWN_SIGNER = PGP_E_V + 3,
103 	PGP_E_V_BAD_HASH = PGP_E_V + 4,
104 
105 	/* Algorithm support errors */
106 	PGP_E_ALG = 0x6000,	/* general algorithm error */
107 	PGP_E_ALG_UNSUPPORTED_SYMMETRIC_ALG = PGP_E_ALG + 1,
108 	PGP_E_ALG_UNSUPPORTED_PUBLIC_KEY_ALG = PGP_E_ALG + 2,
109 	PGP_E_ALG_UNSUPPORTED_SIGNATURE_ALG = PGP_E_ALG + 3,
110 	PGP_E_ALG_UNSUPPORTED_HASH_ALG = PGP_E_ALG + 4,
111 	PGP_E_ALG_UNSUPPORTED_COMPRESS_ALG = PGP_E_ALG + 5,
112 
113 	/* Protocol errors */
114 	PGP_E_PROTO = 0x7000,	/* general protocol error */
115 	PGP_E_PROTO_BAD_SYMMETRIC_DECRYPT = PGP_E_PROTO + 2,
116 	PGP_E_PROTO_UNKNOWN_SS = PGP_E_PROTO + 3,
117 	PGP_E_PROTO_CRITICAL_SS_IGNORED = PGP_E_PROTO + 4,
118 	PGP_E_PROTO_BAD_PUBLIC_KEY_VRSN = PGP_E_PROTO + 5,
119 	PGP_E_PROTO_BAD_SIGNATURE_VRSN = PGP_E_PROTO + 6,
120 	PGP_E_PROTO_BAD_ONE_PASS_SIG_VRSN = PGP_E_PROTO + 7,
121 	PGP_E_PROTO_BAD_PKSK_VRSN = PGP_E_PROTO + 8,
122 	PGP_E_PROTO_DECRYPTED_MSG_WRONG_LEN = PGP_E_PROTO + 9,
123 	PGP_E_PROTO_BAD_SK_CHECKSUM = PGP_E_PROTO + 10
124 } pgp_errcode_t;
125 
126 /** one entry in a linked list of errors */
127 typedef struct pgp_error {
128 	pgp_errcode_t		errcode;
129 	int			sys_errno;	/* irrelevent unless errcode ==
130 					 * PGP_E_SYSTEM_ERROR */
131 	char			*comment;
132 	const char		*file;
133 	int			 line;
134 	struct pgp_error	*next;
135 } pgp_error_t;
136 
137 const char     *pgp_errcode(const pgp_errcode_t);
138 
139 void
140 pgp_push_error(pgp_error_t **, pgp_errcode_t,
141 		int,
142 		const char *, int, const char *,...) __printflike(6, 7);
143 void pgp_print_error(pgp_error_t *);
144 void pgp_print_errors(pgp_error_t *);
145 void pgp_free_errors(pgp_error_t *);
146 int  pgp_has_error(pgp_error_t *, pgp_errcode_t);
147 
148 #define PGP_SYSTEM_ERROR_1(err,code,sys,fmt,arg)	do {		\
149 	pgp_push_error(err,PGP_E_SYSTEM_ERROR,errno,__FILE__,__LINE__,sys);\
150 	pgp_push_error(err,code,0,__FILE__,__LINE__,fmt,arg);		\
151 } while(/*CONSTCOND*/0)
152 
153 #define PGP_MEMORY_ERROR(err) {						\
154 	fprintf(stderr, "Memory error\n");				\
155 }				/* \todo placeholder for better error
156 				 * handling */
157 #define PGP_ERROR_1(err,code,fmt,arg)	do {				\
158 	pgp_push_error(err,code,0,__FILE__,__LINE__,fmt,arg);		\
159 } while(/*CONSTCOND*/0)
160 #define PGP_ERROR_2(err,code,fmt,arg,arg2)	do {			\
161 	pgp_push_error(err,code,0,__FILE__,__LINE__,fmt,arg,arg2);	\
162 } while(/*CONSTCOND*/0)
163 #define PGP_ERROR_3(err,code,fmt,arg,arg2,arg3)	do {			\
164 	pgp_push_error(err,code,0,__FILE__,__LINE__,fmt,arg,arg2,arg3);	\
165 } while(/*CONSTCOND*/0)
166 #define PGP_ERROR_4(err,code,fmt,arg,arg2,arg3,arg4)	do {		\
167 	pgp_push_error(err,code,0,__FILE__,__LINE__,fmt,arg,arg2,arg3,arg4); \
168 } while(/*CONSTCOND*/0)
169 
170 #endif /* ERRORS_H_ */
171