Mercurial > trustbridge
comparison common/listutil.c @ 905:698b6a9bd75e
Fix coding style for C code
astyle --style=gnu --indent=spaces=2
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Wed, 13 Aug 2014 15:49:47 +0200 |
parents | 7861950f7637 |
children | edbf5e5e88f4 |
comparison
equal
deleted
inserted
replaced
904:f89b41fa7048 | 905:698b6a9bd75e |
---|---|
39 #define READ_FILE_NO_MEMORY -3 | 39 #define READ_FILE_NO_MEMORY -3 |
40 #define READ_FILE_READ_FAILED -4 | 40 #define READ_FILE_READ_FAILED -4 |
41 #define READ_FILE_INVALID_CALL -5 | 41 #define READ_FILE_INVALID_CALL -5 |
42 int | 42 int |
43 read_file(const char *file_name, char **data, size_t *size, | 43 read_file(const char *file_name, char **data, size_t *size, |
44 const size_t max_size) | 44 const size_t max_size) |
45 { | 45 { |
46 FILE *f; | 46 FILE *f; |
47 long file_size; | 47 long file_size; |
48 | 48 |
49 if (!file_name || !data || !size || !max_size) { | 49 if (!file_name || !data || !size || !max_size) |
50 return READ_FILE_INVALID_CALL; | 50 { |
51 } | 51 return READ_FILE_INVALID_CALL; |
52 | 52 } |
53 f = fopen(file_name, "rb"); | 53 |
54 if (f == NULL) | 54 f = fopen(file_name, "rb"); |
55 return READ_FILE_UNREADABLE; | 55 if (f == NULL) |
56 | 56 return READ_FILE_UNREADABLE; |
57 fseek(f, 0, SEEK_END); | 57 |
58 file_size = ftell(f); | 58 fseek(f, 0, SEEK_END); |
59 if (file_size <= 0){ | 59 file_size = ftell(f); |
60 fclose(f); | 60 if (file_size <= 0) |
61 return READ_FILE_UNREADABLE; | 61 { |
62 } | 62 fclose(f); |
63 | 63 return READ_FILE_UNREADABLE; |
64 fseek(f, 0, SEEK_SET); | 64 } |
65 | 65 |
66 if (file_size + 1 == 0) { | 66 fseek(f, 0, SEEK_SET); |
67 fclose(f); | 67 |
68 return READ_FILE_TOO_LARGE; | 68 if (file_size + 1 == 0) |
69 } | 69 { |
70 *size = (size_t) file_size; | 70 fclose(f); |
71 | 71 return READ_FILE_TOO_LARGE; |
72 if (*size > max_size) { | 72 } |
73 fclose(f); | 73 *size = (size_t) file_size; |
74 return READ_FILE_TOO_LARGE; | 74 |
75 } | 75 if (*size > max_size) |
76 | 76 { |
77 *data = (char *) malloc( *size + 1 ); | 77 fclose(f); |
78 if (*data == NULL) { | 78 return READ_FILE_TOO_LARGE; |
79 fclose(f); | 79 } |
80 return READ_FILE_NO_MEMORY; | 80 |
81 } | 81 *data = (char *) malloc( *size + 1 ); |
82 | 82 if (*data == NULL) |
83 if (fread(*data, 1, *size, f) != *size) { | 83 { |
84 free(*data); | 84 fclose(f); |
85 fclose(f); | 85 return READ_FILE_NO_MEMORY; |
86 return READ_FILE_READ_FAILED; | 86 } |
87 } | 87 |
88 | 88 if (fread(*data, 1, *size, f) != *size) |
89 fclose(f); | 89 { |
90 | 90 free(*data); |
91 (*data)[*size] = '\0'; | 91 fclose(f); |
92 | 92 return READ_FILE_READ_FAILED; |
93 return 0; | 93 } |
94 | |
95 fclose(f); | |
96 | |
97 (*data)[*size] = '\0'; | |
98 | |
99 return 0; | |
94 } | 100 } |
95 | 101 |
96 int verify_list(const char *data, const size_t size) | 102 int verify_list(const char *data, const size_t size) |
97 { | 103 { |
98 int ret = -1; | 104 int ret = -1; |
99 pk_context pub_key_ctx; | 105 pk_context pub_key_ctx; |
100 char *p; | 106 char *p; |
101 | 107 |
102 /* Modulus / 8 are the necessary bytes. */ | 108 /* Modulus / 8 are the necessary bytes. */ |
103 #ifndef TRUSTBRIDGE_RSA_KEY_SIZE | 109 #ifndef TRUSTBRIDGE_RSA_KEY_SIZE |
104 # error "Key size undefined" | 110 # error "Key size undefined" |
105 #endif | 111 #endif |
106 const size_t sig_b64_size = TRUSTBRIDGE_RSA_KEY_SIZE / 8 * 4 / 3; | 112 const size_t sig_b64_size = TRUSTBRIDGE_RSA_KEY_SIZE / 8 * 4 / 3; |
107 size_t sig_size = TRUSTBRIDGE_RSA_KEY_SIZE / 8; | 113 size_t sig_size = TRUSTBRIDGE_RSA_KEY_SIZE / 8; |
108 | 114 |
109 char signature_b64[sig_b64_size + 1]; | 115 char signature_b64[sig_b64_size + 1]; |
110 unsigned char signature[sig_size]; | 116 unsigned char signature[sig_size]; |
111 /* Hash algroithm is sha256 */ | 117 /* Hash algroithm is sha256 */ |
112 unsigned char hash[32]; | 118 unsigned char hash[32]; |
113 | 119 |
114 if (!data || !size) { | 120 if (!data || !size) |
115 return -1; | 121 { |
116 } | 122 return -1; |
117 | 123 } |
118 /* Fetch the signature from the first line od data */ | 124 |
119 p = strchr(data, '\r'); | 125 /* Fetch the signature from the first line od data */ |
120 if (p == 0 || (unsigned int)(p - (data + 2)) != sig_b64_size) { | 126 p = strchr(data, '\r'); |
121 /* printf("Invalid data. Signature might be too long.\n"); */ | 127 if (p == 0 || (unsigned int)(p - (data + 2)) != sig_b64_size) |
122 return -1; | 128 { |
123 } | 129 /* printf("Invalid data. Signature might be too long.\n"); */ |
124 strncpy(signature_b64, data + 2, sig_b64_size); | 130 return -1; |
125 signature_b64[sig_b64_size] = '\0'; | 131 } |
126 | 132 strncpy(signature_b64, data + 2, sig_b64_size); |
127 ret = base64_decode(signature, &sig_size, | 133 signature_b64[sig_b64_size] = '\0'; |
128 (unsigned char *)signature_b64, sig_b64_size); | 134 |
129 | 135 ret = base64_decode(signature, &sig_size, |
130 if (ret != 0 || sig_size != TRUSTBRIDGE_RSA_KEY_SIZE / 8) { | 136 (unsigned char *)signature_b64, sig_b64_size); |
131 /* printf("failed to decode signature\n"); */ | 137 |
132 return -1; | 138 if (ret != 0 || sig_size != TRUSTBRIDGE_RSA_KEY_SIZE / 8) |
133 } | 139 { |
134 | 140 /* printf("failed to decode signature\n"); */ |
135 /* Hash is calculated over the data without the first line. | 141 return -1; |
136 * linebreaks are \r\n so the first char of the new line is | 142 } |
137 * p+2 */ | 143 |
138 p += 2; | 144 /* Hash is calculated over the data without the first line. |
139 /* Size of the data to hash is the size - signature line | 145 * linebreaks are \r\n so the first char of the new line is |
140 * signature line is sig_b64_size - "S:" and - "\r\n" so -4*/ | 146 * p+2 */ |
141 sha256((unsigned char *)p, size - sig_b64_size - 4, hash, 0); | 147 p += 2; |
142 | 148 /* Size of the data to hash is the size - signature line |
143 pk_init(&pub_key_ctx); | 149 * signature line is sig_b64_size - "S:" and - "\r\n" so -4*/ |
144 | 150 sha256((unsigned char *)p, size - sig_b64_size - 4, hash, 0); |
145 ret = pk_parse_public_key(&pub_key_ctx, public_key_pem, | 151 |
146 public_key_pem_size); | 152 pk_init(&pub_key_ctx); |
147 if (ret != 0) { | 153 |
148 ERRORPRINTF ("pk_parse_public_key failed with -0x%04x\n\n", -ret); | 154 ret = pk_parse_public_key(&pub_key_ctx, public_key_pem, |
149 pk_free(&pub_key_ctx); | 155 public_key_pem_size); |
150 return ret; | 156 if (ret != 0) |
151 } | 157 { |
152 | 158 ERRORPRINTF ("pk_parse_public_key failed with -0x%04x\n\n", -ret); |
153 ret = pk_verify(&pub_key_ctx, POLARSSL_MD_SHA256, hash, 0, | 159 pk_free(&pub_key_ctx); |
154 signature, sig_size); | 160 return ret; |
155 | 161 } |
156 if (ret != 0) { | 162 |
157 ERRORPRINTF ("pk_verify failed with -0x%04x\n\n", -ret); | 163 ret = pk_verify(&pub_key_ctx, POLARSSL_MD_SHA256, hash, 0, |
158 } | 164 signature, sig_size); |
159 pk_free(&pub_key_ctx); | 165 |
160 | 166 if (ret != 0) |
161 return ret; | 167 { |
168 ERRORPRINTF ("pk_verify failed with -0x%04x\n\n", -ret); | |
169 } | |
170 pk_free(&pub_key_ctx); | |
171 | |
172 return ret; | |
162 } | 173 } |
163 | 174 |
164 list_status_t read_and_verify_list(const char *file_name, char **data, | 175 list_status_t read_and_verify_list(const char *file_name, char **data, |
165 size_t *size) | 176 size_t *size) |
166 { | 177 { |
167 list_status_t retval = UnknownError; | 178 list_status_t retval = UnknownError; |
168 *data = NULL; | 179 *data = NULL; |
169 *size = 0; | 180 *size = 0; |
170 int ret = 0; | 181 int ret = 0; |
171 | 182 |
172 ret = read_file(file_name, data, size, MAX_FILESIZE); | 183 ret = read_file(file_name, data, size, MAX_FILESIZE); |
173 | 184 |
174 /* printf ("Ret: %i \n", ret); */ | 185 /* printf ("Ret: %i \n", ret); */ |
175 if (ret != 0) { | 186 if (ret != 0) |
176 if (ret == READ_FILE_TOO_LARGE) { | 187 { |
177 return TooLarge; | 188 if (ret == READ_FILE_TOO_LARGE) |
178 } | 189 { |
179 if (ret == READ_FILE_UNREADABLE) { | 190 return TooLarge; |
180 /* TODO: work with errno ? */ | 191 } |
181 /* errsv = errno; */ | 192 if (ret == READ_FILE_UNREADABLE) |
182 /* perror("read_and_verify_list(), READ_FILE_UNREADABLE:"); */ | 193 { |
183 return SeekFailed; | 194 /* TODO: work with errno ? */ |
184 } | 195 /* errsv = errno; */ |
185 if (ret == READ_FILE_READ_FAILED) { | 196 /* perror("read_and_verify_list(), READ_FILE_UNREADABLE:"); */ |
186 /* TODO: work with ferror() or feof() ? */ | 197 return SeekFailed; |
187 return ReadFailed; | 198 } |
188 } | 199 if (ret == READ_FILE_READ_FAILED) |
189 return UnknownError; | 200 { |
190 } | 201 /* TODO: work with ferror() or feof() ? */ |
191 | 202 return ReadFailed; |
192 if (!*data || !*size) { | 203 } |
193 /* File is probably empty */ | 204 return UnknownError; |
194 return UnknownError; | 205 } |
195 } | 206 |
196 | 207 if (!*data || !*size) |
197 if (**data != 'S') { | 208 { |
198 retval = InvalidFormat; | 209 /* File is probably empty */ |
199 } else { | 210 return UnknownError; |
200 ret = verify_list (*data, *size); | 211 } |
201 if (ret == 0) { | 212 |
202 /* Hooray */ | 213 if (**data != 'S') |
203 return Valid; | 214 { |
204 } | 215 retval = InvalidFormat; |
205 if (ret == -1) { | 216 } |
206 /* our error */ | 217 else |
207 retval = InvalidFormat; | 218 { |
208 } else { | 219 ret = verify_list (*data, *size); |
209 retval = InvalidSignature; | 220 if (ret == 0) |
210 } | 221 { |
211 } | 222 /* Hooray */ |
212 | 223 return Valid; |
213 if (retval != Valid && *data) { | 224 } |
214 free(*data); | 225 if (ret == -1) |
215 *data = NULL; | 226 { |
216 *size = 0; | 227 /* our error */ |
217 } | 228 retval = InvalidFormat; |
218 return retval; | 229 } |
230 else | |
231 { | |
232 retval = InvalidSignature; | |
233 } | |
234 } | |
235 | |
236 if (retval != Valid && *data) | |
237 { | |
238 free(*data); | |
239 *data = NULL; | |
240 *size = 0; | |
241 } | |
242 return retval; | |
219 } | 243 } |
220 | 244 |
221 char ** | 245 char ** |
222 get_certs_from_list (char *data, const size_t size) | 246 get_certs_from_list (char *data, const size_t size) |
223 { | 247 { |