1 /*
2 * Copyright (c) 2014 Jerry Lundström <lundstrom.jerry@gmail.com>
3 * Copyright (c) 2014 .SE (The Internet Infrastructure Foundation).
4 * Copyright (c) 2014 OpenDNSSEC AB (svb)
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 #include "key_data.h"
31 #include "db_error.h"
32
33
34 #include <string.h>
35
36 const db_enum_t key_data_enum_set_role[] = {
37 { "KSK", (key_data_role_t)KEY_DATA_ROLE_KSK },
38 { "ZSK", (key_data_role_t)KEY_DATA_ROLE_ZSK },
39 { "CSK", (key_data_role_t)KEY_DATA_ROLE_CSK },
40 { NULL, 0 }
41 };
42
43 const db_enum_t key_data_enum_set_ds_at_parent[] = {
44 { "unsubmitted", (key_data_ds_at_parent_t)KEY_DATA_DS_AT_PARENT_UNSUBMITTED },
45 { "submit", (key_data_ds_at_parent_t)KEY_DATA_DS_AT_PARENT_SUBMIT },
46 { "submitted", (key_data_ds_at_parent_t)KEY_DATA_DS_AT_PARENT_SUBMITTED },
47 { "seen", (key_data_ds_at_parent_t)KEY_DATA_DS_AT_PARENT_SEEN },
48 { "retract", (key_data_ds_at_parent_t)KEY_DATA_DS_AT_PARENT_RETRACT },
49 { "retracted", (key_data_ds_at_parent_t)KEY_DATA_DS_AT_PARENT_RETRACTED },
50 { NULL, 0 }
51 };
52
53 /**
54 * Create a new key data object.
55 * \param[in] connection a db_connection_t pointer.
56 * \return a key_data_t pointer or NULL on error.
57 */
__key_data_new_object(const db_connection_t * connection)58 static db_object_t* __key_data_new_object(const db_connection_t* connection) {
59 db_object_field_list_t* object_field_list;
60 db_object_field_t* object_field;
61 db_object_t* object;
62
63 if (!(object = db_object_new())
64 || db_object_set_connection(object, connection)
65 || db_object_set_table(object, "keyData")
66 || db_object_set_primary_key_name(object, "id")
67 || !(object_field_list = db_object_field_list_new()))
68 {
69 db_object_free(object);
70 return NULL;
71 }
72
73 if (!(object_field = db_object_field_new())
74 || db_object_field_set_name(object_field, "id")
75 || db_object_field_set_type(object_field, DB_TYPE_PRIMARY_KEY)
76 || db_object_field_list_add(object_field_list, object_field))
77 {
78 db_object_field_free(object_field);
79 db_object_field_list_free(object_field_list);
80 db_object_free(object);
81 return NULL;
82 }
83
84 if (!(object_field = db_object_field_new())
85 || db_object_field_set_name(object_field, "rev")
86 || db_object_field_set_type(object_field, DB_TYPE_REVISION)
87 || db_object_field_list_add(object_field_list, object_field))
88 {
89 db_object_field_free(object_field);
90 db_object_field_list_free(object_field_list);
91 db_object_free(object);
92 return NULL;
93 }
94
95 if (!(object_field = db_object_field_new())
96 || db_object_field_set_name(object_field, "zoneId")
97 || db_object_field_set_type(object_field, DB_TYPE_ANY)
98 || db_object_field_list_add(object_field_list, object_field))
99 {
100 db_object_field_free(object_field);
101 db_object_field_list_free(object_field_list);
102 db_object_free(object);
103 return NULL;
104 }
105
106 if (!(object_field = db_object_field_new())
107 || db_object_field_set_name(object_field, "hsmKeyId")
108 || db_object_field_set_type(object_field, DB_TYPE_ANY)
109 || db_object_field_list_add(object_field_list, object_field))
110 {
111 db_object_field_free(object_field);
112 db_object_field_list_free(object_field_list);
113 db_object_free(object);
114 return NULL;
115 }
116
117 if (!(object_field = db_object_field_new())
118 || db_object_field_set_name(object_field, "algorithm")
119 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
120 || db_object_field_list_add(object_field_list, object_field))
121 {
122 db_object_field_free(object_field);
123 db_object_field_list_free(object_field_list);
124 db_object_free(object);
125 return NULL;
126 }
127
128 if (!(object_field = db_object_field_new())
129 || db_object_field_set_name(object_field, "inception")
130 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
131 || db_object_field_list_add(object_field_list, object_field))
132 {
133 db_object_field_free(object_field);
134 db_object_field_list_free(object_field_list);
135 db_object_free(object);
136 return NULL;
137 }
138
139 if (!(object_field = db_object_field_new())
140 || db_object_field_set_name(object_field, "role")
141 || db_object_field_set_type(object_field, DB_TYPE_ENUM)
142 || db_object_field_set_enum_set(object_field, key_data_enum_set_role)
143 || db_object_field_list_add(object_field_list, object_field))
144 {
145 db_object_field_free(object_field);
146 db_object_field_list_free(object_field_list);
147 db_object_free(object);
148 return NULL;
149 }
150
151 if (!(object_field = db_object_field_new())
152 || db_object_field_set_name(object_field, "introducing")
153 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
154 || db_object_field_list_add(object_field_list, object_field))
155 {
156 db_object_field_free(object_field);
157 db_object_field_list_free(object_field_list);
158 db_object_free(object);
159 return NULL;
160 }
161
162 if (!(object_field = db_object_field_new())
163 || db_object_field_set_name(object_field, "shouldRevoke")
164 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
165 || db_object_field_list_add(object_field_list, object_field))
166 {
167 db_object_field_free(object_field);
168 db_object_field_list_free(object_field_list);
169 db_object_free(object);
170 return NULL;
171 }
172
173 if (!(object_field = db_object_field_new())
174 || db_object_field_set_name(object_field, "standby")
175 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
176 || db_object_field_list_add(object_field_list, object_field))
177 {
178 db_object_field_free(object_field);
179 db_object_field_list_free(object_field_list);
180 db_object_free(object);
181 return NULL;
182 }
183
184 if (!(object_field = db_object_field_new())
185 || db_object_field_set_name(object_field, "activeZsk")
186 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
187 || db_object_field_list_add(object_field_list, object_field))
188 {
189 db_object_field_free(object_field);
190 db_object_field_list_free(object_field_list);
191 db_object_free(object);
192 return NULL;
193 }
194
195 if (!(object_field = db_object_field_new())
196 || db_object_field_set_name(object_field, "publish")
197 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
198 || db_object_field_list_add(object_field_list, object_field))
199 {
200 db_object_field_free(object_field);
201 db_object_field_list_free(object_field_list);
202 db_object_free(object);
203 return NULL;
204 }
205
206 if (!(object_field = db_object_field_new())
207 || db_object_field_set_name(object_field, "activeKsk")
208 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
209 || db_object_field_list_add(object_field_list, object_field))
210 {
211 db_object_field_free(object_field);
212 db_object_field_list_free(object_field_list);
213 db_object_free(object);
214 return NULL;
215 }
216
217 if (!(object_field = db_object_field_new())
218 || db_object_field_set_name(object_field, "dsAtParent")
219 || db_object_field_set_type(object_field, DB_TYPE_ENUM)
220 || db_object_field_set_enum_set(object_field, key_data_enum_set_ds_at_parent)
221 || db_object_field_list_add(object_field_list, object_field))
222 {
223 db_object_field_free(object_field);
224 db_object_field_list_free(object_field_list);
225 db_object_free(object);
226 return NULL;
227 }
228
229 if (!(object_field = db_object_field_new())
230 || db_object_field_set_name(object_field, "keytag")
231 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
232 || db_object_field_list_add(object_field_list, object_field))
233 {
234 db_object_field_free(object_field);
235 db_object_field_list_free(object_field_list);
236 db_object_free(object);
237 return NULL;
238 }
239
240 if (!(object_field = db_object_field_new())
241 || db_object_field_set_name(object_field, "minimize")
242 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
243 || db_object_field_list_add(object_field_list, object_field))
244 {
245 db_object_field_free(object_field);
246 db_object_field_list_free(object_field_list);
247 db_object_free(object);
248 return NULL;
249 }
250
251 if (db_object_set_object_field_list(object, object_field_list)) {
252 db_object_field_list_free(object_field_list);
253 db_object_free(object);
254 return NULL;
255 }
256
257 return object;
258 }
259
260 /* KEY DATA */
261
262
263
key_data_new(const db_connection_t * connection)264 key_data_t* key_data_new(const db_connection_t* connection) {
265 key_data_t* key_data =
266 (key_data_t*)calloc(1, sizeof(key_data_t));
267
268 if (key_data) {
269 if (!(key_data->dbo = __key_data_new_object(connection))) {
270 free(key_data);
271 return NULL;
272 }
273 db_value_reset(&(key_data->id));
274 db_value_reset(&(key_data->rev));
275 db_value_reset(&(key_data->zone_id));
276 db_value_reset(&(key_data->hsm_key_id));
277 key_data->role = KEY_DATA_ROLE_INVALID;
278 key_data->introducing = 1;
279 key_data->ds_at_parent = KEY_DATA_DS_AT_PARENT_UNSUBMITTED;
280 }
281
282 return key_data;
283 }
284
key_data_new_copy(const key_data_t * key_data)285 key_data_t* key_data_new_copy(const key_data_t* key_data) {
286 key_data_t* new_key_data;
287
288 if (!key_data) {
289 return NULL;
290 }
291 if (!key_data->dbo) {
292 return NULL;
293 }
294
295 if (!(new_key_data = key_data_new(db_object_connection(key_data->dbo)))
296 || key_data_copy(new_key_data, key_data))
297 {
298 key_data_free(new_key_data);
299 return NULL;
300 }
301 return new_key_data;
302 }
303
key_data_free(key_data_t * key_data)304 void key_data_free(key_data_t* key_data) {
305 if (key_data) {
306 if (key_data->dbo) {
307 db_object_free(key_data->dbo);
308 }
309 db_value_reset(&(key_data->id));
310 db_value_reset(&(key_data->rev));
311 db_value_reset(&(key_data->zone_id));
312 if (key_data->private_zone_id) {
313 zone_db_free(key_data->private_zone_id);
314 }
315 db_value_reset(&(key_data->hsm_key_id));
316 if (key_data->private_hsm_key_id) {
317 hsm_key_free(key_data->private_hsm_key_id);
318 }
319 if (key_data->key_state_list) {
320 key_state_list_free(key_data->key_state_list);
321 }
322 free(key_data);
323 }
324 }
325
key_data_copy(key_data_t * key_data,const key_data_t * key_data_copy)326 int key_data_copy(key_data_t* key_data, const key_data_t* key_data_copy) {
327 if (!key_data) {
328 return DB_ERROR_UNKNOWN;
329 }
330 if (!key_data_copy) {
331 return DB_ERROR_UNKNOWN;
332 }
333
334 if (db_value_copy(&(key_data->id), &(key_data_copy->id))) {
335 return DB_ERROR_UNKNOWN;
336 }
337 if (db_value_copy(&(key_data->rev), &(key_data_copy->rev))) {
338 return DB_ERROR_UNKNOWN;
339 }
340 if (db_value_copy(&(key_data->zone_id), &(key_data_copy->zone_id))) {
341 return DB_ERROR_UNKNOWN;
342 }
343 if (key_data->private_zone_id) {
344 zone_db_free(key_data->private_zone_id);
345 key_data->private_zone_id = NULL;
346 }
347 if (key_data_copy->private_zone_id
348 && !(key_data->private_zone_id = zone_db_new_copy(key_data_copy->private_zone_id)))
349 {
350 return DB_ERROR_UNKNOWN;
351 }
352 key_data->associated_zone_id = NULL;
353 if (!key_data_copy->private_zone_id
354 && key_data_copy->associated_zone_id
355 && !(key_data->private_zone_id = zone_db_new_copy(key_data_copy->associated_zone_id)))
356 {
357 return DB_ERROR_UNKNOWN;
358 }
359 if (db_value_copy(&(key_data->hsm_key_id), &(key_data_copy->hsm_key_id))) {
360 return DB_ERROR_UNKNOWN;
361 }
362 if (key_data->private_hsm_key_id) {
363 hsm_key_free(key_data->private_hsm_key_id);
364 key_data->private_hsm_key_id = NULL;
365 }
366 if (key_data_copy->private_hsm_key_id
367 && !(key_data->private_hsm_key_id = hsm_key_new_copy(key_data_copy->private_hsm_key_id)))
368 {
369 return DB_ERROR_UNKNOWN;
370 }
371 key_data->associated_hsm_key_id = NULL;
372 if (!key_data_copy->private_hsm_key_id
373 && key_data_copy->associated_hsm_key_id
374 && !(key_data->private_hsm_key_id = hsm_key_new_copy(key_data_copy->associated_hsm_key_id)))
375 {
376 return DB_ERROR_UNKNOWN;
377 }
378 if (key_data->key_state_list) {
379 key_state_list_free(key_data->key_state_list);
380 key_data->key_state_list = NULL;
381 }
382 if (key_data_copy->key_state_list
383 && !(key_data->key_state_list = key_state_list_new_copy(key_data_copy->key_state_list)))
384 {
385 return DB_ERROR_UNKNOWN;
386 }
387 key_data->algorithm = key_data_copy->algorithm;
388 key_data->inception = key_data_copy->inception;
389 key_data->role = key_data_copy->role;
390 key_data->introducing = key_data_copy->introducing;
391 key_data->should_revoke = key_data_copy->should_revoke;
392 key_data->standby = key_data_copy->standby;
393 key_data->active_zsk = key_data_copy->active_zsk;
394 key_data->publish = key_data_copy->publish;
395 key_data->active_ksk = key_data_copy->active_ksk;
396 key_data->ds_at_parent = key_data_copy->ds_at_parent;
397 key_data->keytag = key_data_copy->keytag;
398 key_data->minimize = key_data_copy->minimize;
399 return DB_OK;
400 }
401
key_data_cmp(const key_data_t * key_data_a,const key_data_t * key_data_b)402 int key_data_cmp(const key_data_t* key_data_a, const key_data_t* key_data_b) {
403 int ret;
404
405 if (!key_data_a && !key_data_b) {
406 return 0;
407 }
408 if (!key_data_a && key_data_b) {
409 return -1;
410 }
411 if (key_data_a && !key_data_b) {
412 return 1;
413 }
414
415 ret = 0;
416 db_value_cmp(&(key_data_a->zone_id), &(key_data_b->zone_id), &ret);
417 if (ret) {
418 return ret;
419 }
420
421 ret = 0;
422 db_value_cmp(&(key_data_a->hsm_key_id), &(key_data_b->hsm_key_id), &ret);
423 if (ret) {
424 return ret;
425 }
426
427 if (key_data_a->algorithm != key_data_b->algorithm) {
428 return key_data_a->algorithm < key_data_b->algorithm ? -1 : 1;
429 }
430
431 if (key_data_a->inception != key_data_b->inception) {
432 return key_data_a->inception < key_data_b->inception ? -1 : 1;
433 }
434
435 if (key_data_a->role != key_data_b->role) {
436 return key_data_a->role < key_data_b->role ? -1 : 1;
437 }
438
439 if (key_data_a->introducing != key_data_b->introducing) {
440 return key_data_a->introducing < key_data_b->introducing ? -1 : 1;
441 }
442
443 if (key_data_a->should_revoke != key_data_b->should_revoke) {
444 return key_data_a->should_revoke < key_data_b->should_revoke ? -1 : 1;
445 }
446
447 if (key_data_a->standby != key_data_b->standby) {
448 return key_data_a->standby < key_data_b->standby ? -1 : 1;
449 }
450
451 if (key_data_a->active_zsk != key_data_b->active_zsk) {
452 return key_data_a->active_zsk < key_data_b->active_zsk ? -1 : 1;
453 }
454
455 if (key_data_a->publish != key_data_b->publish) {
456 return key_data_a->publish < key_data_b->publish ? -1 : 1;
457 }
458
459 if (key_data_a->active_ksk != key_data_b->active_ksk) {
460 return key_data_a->active_ksk < key_data_b->active_ksk ? -1 : 1;
461 }
462
463 if (key_data_a->ds_at_parent != key_data_b->ds_at_parent) {
464 return key_data_a->ds_at_parent < key_data_b->ds_at_parent ? -1 : 1;
465 }
466
467 if (key_data_a->keytag != key_data_b->keytag) {
468 return key_data_a->keytag < key_data_b->keytag ? -1 : 1;
469 }
470
471 if (key_data_a->minimize != key_data_b->minimize) {
472 return key_data_a->minimize < key_data_b->minimize ? -1 : 1;
473 }
474 return 0;
475 }
476
key_data_from_result(key_data_t * key_data,const db_result_t * result)477 int key_data_from_result(key_data_t* key_data, const db_result_t* result) {
478 const db_value_set_t* value_set;
479 int role;
480 int ds_at_parent;
481
482 if (!key_data) {
483 return DB_ERROR_UNKNOWN;
484 }
485 if (!result) {
486 return DB_ERROR_UNKNOWN;
487 }
488
489 db_value_reset(&(key_data->id));
490 db_value_reset(&(key_data->rev));
491 db_value_reset(&(key_data->zone_id));
492 db_value_reset(&(key_data->hsm_key_id));
493 if (!(value_set = db_result_value_set(result))
494 || db_value_set_size(value_set) != 16
495 || db_value_copy(&(key_data->id), db_value_set_at(value_set, 0))
496 || db_value_copy(&(key_data->rev), db_value_set_at(value_set, 1))
497 || db_value_copy(&(key_data->zone_id), db_value_set_at(value_set, 2))
498 || db_value_copy(&(key_data->hsm_key_id), db_value_set_at(value_set, 3))
499 || db_value_to_uint32(db_value_set_at(value_set, 4), &(key_data->algorithm))
500 || db_value_to_uint32(db_value_set_at(value_set, 5), &(key_data->inception))
501 || db_value_to_enum_value(db_value_set_at(value_set, 6), &role, key_data_enum_set_role)
502 || db_value_to_uint32(db_value_set_at(value_set, 7), &(key_data->introducing))
503 || db_value_to_uint32(db_value_set_at(value_set, 8), &(key_data->should_revoke))
504 || db_value_to_uint32(db_value_set_at(value_set, 9), &(key_data->standby))
505 || db_value_to_uint32(db_value_set_at(value_set, 10), &(key_data->active_zsk))
506 || db_value_to_uint32(db_value_set_at(value_set, 11), &(key_data->publish))
507 || db_value_to_uint32(db_value_set_at(value_set, 12), &(key_data->active_ksk))
508 || db_value_to_enum_value(db_value_set_at(value_set, 13), &ds_at_parent, key_data_enum_set_ds_at_parent)
509 || db_value_to_uint32(db_value_set_at(value_set, 14), &(key_data->keytag))
510 || db_value_to_uint32(db_value_set_at(value_set, 15), &(key_data->minimize)))
511 {
512 return DB_ERROR_UNKNOWN;
513 }
514
515 if (role == (key_data_role_t)KEY_DATA_ROLE_KSK) {
516 key_data->role = KEY_DATA_ROLE_KSK;
517 }
518 else if (role == (key_data_role_t)KEY_DATA_ROLE_ZSK) {
519 key_data->role = KEY_DATA_ROLE_ZSK;
520 }
521 else if (role == (key_data_role_t)KEY_DATA_ROLE_CSK) {
522 key_data->role = KEY_DATA_ROLE_CSK;
523 }
524 else {
525 return DB_ERROR_UNKNOWN;
526 }
527
528 if (ds_at_parent == (key_data_ds_at_parent_t)KEY_DATA_DS_AT_PARENT_UNSUBMITTED) {
529 key_data->ds_at_parent = KEY_DATA_DS_AT_PARENT_UNSUBMITTED;
530 }
531 else if (ds_at_parent == (key_data_ds_at_parent_t)KEY_DATA_DS_AT_PARENT_SUBMIT) {
532 key_data->ds_at_parent = KEY_DATA_DS_AT_PARENT_SUBMIT;
533 }
534 else if (ds_at_parent == (key_data_ds_at_parent_t)KEY_DATA_DS_AT_PARENT_SUBMITTED) {
535 key_data->ds_at_parent = KEY_DATA_DS_AT_PARENT_SUBMITTED;
536 }
537 else if (ds_at_parent == (key_data_ds_at_parent_t)KEY_DATA_DS_AT_PARENT_SEEN) {
538 key_data->ds_at_parent = KEY_DATA_DS_AT_PARENT_SEEN;
539 }
540 else if (ds_at_parent == (key_data_ds_at_parent_t)KEY_DATA_DS_AT_PARENT_RETRACT) {
541 key_data->ds_at_parent = KEY_DATA_DS_AT_PARENT_RETRACT;
542 }
543 else if (ds_at_parent == (key_data_ds_at_parent_t)KEY_DATA_DS_AT_PARENT_RETRACTED) {
544 key_data->ds_at_parent = KEY_DATA_DS_AT_PARENT_RETRACTED;
545 }
546 else {
547 return DB_ERROR_UNKNOWN;
548 }
549
550 return DB_OK;
551 }
552
key_data_id(const key_data_t * key_data)553 const db_value_t* key_data_id(const key_data_t* key_data) {
554 if (!key_data) {
555 return NULL;
556 }
557
558 return &(key_data->id);
559 }
560
key_data_zone_id(const key_data_t * key_data)561 const db_value_t* key_data_zone_id(const key_data_t* key_data) {
562 if (!key_data) {
563 return NULL;
564 }
565
566 return &(key_data->zone_id);
567 }
568
key_data_get_zone(const key_data_t * key_data)569 zone_db_t* key_data_get_zone(const key_data_t* key_data) {
570 zone_db_t* zone_id = NULL;
571
572 if (!key_data) {
573 return NULL;
574 }
575 if (!key_data->dbo) {
576 return NULL;
577 }
578 if (db_value_not_empty(&(key_data->zone_id))) {
579 return NULL;
580 }
581
582 if (!(zone_id = zone_db_new(db_object_connection(key_data->dbo)))) {
583 return NULL;
584 }
585 if (key_data->private_zone_id) {
586 if (zone_db_copy(zone_id, key_data->private_zone_id)) {
587 zone_db_free(zone_id);
588 return NULL;
589 }
590 }
591 else if (key_data->associated_zone_id) {
592 if (zone_db_copy(zone_id, key_data->associated_zone_id)) {
593 zone_db_free(zone_id);
594 return NULL;
595 }
596 }
597 else {
598 if (zone_db_get_by_id(zone_id, &(key_data->zone_id))) {
599 zone_db_free(zone_id);
600 return NULL;
601 }
602 }
603
604 return zone_id;
605 }
606
key_data_hsm_key_id(const key_data_t * key_data)607 const db_value_t* key_data_hsm_key_id(const key_data_t* key_data) {
608 if (!key_data) {
609 return NULL;
610 }
611
612 return &(key_data->hsm_key_id);
613 }
614
key_data_cache_hsm_key(key_data_t * key_data)615 int key_data_cache_hsm_key(key_data_t* key_data) {
616 if (!key_data) {
617 return DB_ERROR_UNKNOWN;
618 }
619
620 if (key_data->associated_hsm_key_id
621 || key_data->private_hsm_key_id)
622 {
623 return DB_OK;
624 }
625
626 if (!(key_data->private_hsm_key_id = hsm_key_new(db_object_connection(key_data->dbo)))) {
627 return DB_ERROR_UNKNOWN;
628 }
629 if (hsm_key_get_by_id(key_data->private_hsm_key_id, &(key_data->hsm_key_id))) {
630 hsm_key_free(key_data->private_hsm_key_id);
631 key_data->private_hsm_key_id = NULL;
632 return DB_ERROR_UNKNOWN;
633 }
634
635 return DB_OK;
636 }
637
key_data_hsm_key(const key_data_t * key_data)638 const hsm_key_t* key_data_hsm_key(const key_data_t* key_data) {
639 if (!key_data) {
640 return NULL;
641 }
642
643 if (key_data->private_hsm_key_id) {
644 return key_data->private_hsm_key_id;
645 }
646 return key_data->associated_hsm_key_id;
647 }
648
key_data_get_hsm_key(const key_data_t * key_data)649 hsm_key_t* key_data_get_hsm_key(const key_data_t* key_data) {
650 hsm_key_t* hsm_key_id = NULL;
651
652 if (!key_data) {
653 return NULL;
654 }
655 if (!key_data->dbo) {
656 return NULL;
657 }
658 if (db_value_not_empty(&(key_data->hsm_key_id))) {
659 return NULL;
660 }
661
662 if (!(hsm_key_id = hsm_key_new(db_object_connection(key_data->dbo)))) {
663 return NULL;
664 }
665 if (key_data->private_hsm_key_id) {
666 if (hsm_key_copy(hsm_key_id, key_data->private_hsm_key_id)) {
667 hsm_key_free(hsm_key_id);
668 return NULL;
669 }
670 }
671 else if (key_data->associated_hsm_key_id) {
672 if (hsm_key_copy(hsm_key_id, key_data->associated_hsm_key_id)) {
673 hsm_key_free(hsm_key_id);
674 return NULL;
675 }
676 }
677 else {
678 if (hsm_key_get_by_id(hsm_key_id, &(key_data->hsm_key_id))) {
679 hsm_key_free(hsm_key_id);
680 return NULL;
681 }
682 }
683
684 return hsm_key_id;
685 }
686
key_data_algorithm(const key_data_t * key_data)687 unsigned int key_data_algorithm(const key_data_t* key_data) {
688 if (!key_data) {
689 return 0;
690 }
691
692 return key_data->algorithm;
693 }
694
key_data_inception(const key_data_t * key_data)695 unsigned int key_data_inception(const key_data_t* key_data) {
696 if (!key_data) {
697 return 0;
698 }
699
700 return key_data->inception;
701 }
702
key_data_role(const key_data_t * key_data)703 key_data_role_t key_data_role(const key_data_t* key_data) {
704 if (!key_data) {
705 return KEY_DATA_ROLE_INVALID;
706 }
707
708 return key_data->role;
709 }
710
key_data_role_text(const key_data_t * key_data)711 const char* key_data_role_text(const key_data_t* key_data) {
712 const db_enum_t* enum_set = key_data_enum_set_role;
713
714 if (!key_data) {
715 return NULL;
716 }
717
718 while (enum_set->text) {
719 if (enum_set->value == key_data->role) {
720 return enum_set->text;
721 }
722 enum_set++;
723 }
724 return NULL;
725 }
726
key_data_introducing(const key_data_t * key_data)727 unsigned int key_data_introducing(const key_data_t* key_data) {
728 if (!key_data) {
729 return 0;
730 }
731
732 return key_data->introducing;
733 }
734
key_data_active_zsk(const key_data_t * key_data)735 unsigned int key_data_active_zsk(const key_data_t* key_data) {
736 if (!key_data) {
737 return 0;
738 }
739
740 return key_data->active_zsk;
741 }
742
key_data_publish(const key_data_t * key_data)743 unsigned int key_data_publish(const key_data_t* key_data) {
744 if (!key_data) {
745 return 0;
746 }
747
748 return key_data->publish;
749 }
750
key_data_active_ksk(const key_data_t * key_data)751 unsigned int key_data_active_ksk(const key_data_t* key_data) {
752 if (!key_data) {
753 return 0;
754 }
755
756 return key_data->active_ksk;
757 }
758
key_data_ds_at_parent(const key_data_t * key_data)759 key_data_ds_at_parent_t key_data_ds_at_parent(const key_data_t* key_data) {
760 if (!key_data) {
761 return KEY_DATA_DS_AT_PARENT_INVALID;
762 }
763
764 return key_data->ds_at_parent;
765 }
766
key_data_keytag(const key_data_t * key_data)767 unsigned int key_data_keytag(const key_data_t* key_data) {
768 if (!key_data) {
769 return 0;
770 }
771
772 return key_data->keytag;
773 }
774
key_data_minimize(const key_data_t * key_data)775 unsigned int key_data_minimize(const key_data_t* key_data) {
776 if (!key_data) {
777 return 0;
778 }
779
780 return key_data->minimize;
781 }
782
key_data_key_state_list(key_data_t * key_data)783 key_state_list_t* key_data_key_state_list(key_data_t* key_data) {
784
785 if (!key_data) {
786 return NULL;
787 }
788 if (!key_data->dbo) {
789 return NULL;
790 }
791
792 if (!key_data->key_state_list
793 && key_data_retrieve_key_state_list(key_data))
794 {
795 return NULL;
796 }
797
798 return key_data->key_state_list;
799 }
800
key_data_retrieve_key_state_list(key_data_t * key_data)801 int key_data_retrieve_key_state_list(key_data_t* key_data) {
802 db_clause_list_t* clause_list;
803
804 if (!key_data) {
805 return DB_ERROR_UNKNOWN;
806 }
807 if (!key_data->dbo) {
808 return DB_ERROR_UNKNOWN;
809 }
810
811 if (key_data->key_state_list) {
812 key_state_list_free(key_data->key_state_list);
813 key_data->key_state_list = NULL;
814 }
815
816 if (!(clause_list = db_clause_list_new())
817 || !key_state_key_data_id_clause(clause_list, key_data_id(key_data))
818 || !(key_data->key_state_list = key_state_list_new(db_object_connection(key_data->dbo)))
819 || key_state_list_object_store(key_data->key_state_list)
820 || key_state_list_get_by_clauses(key_data->key_state_list, clause_list))
821 {
822 key_state_list_free(key_data->key_state_list);
823 key_data->key_state_list = NULL;
824 db_clause_list_free(clause_list);
825 return DB_ERROR_UNKNOWN;
826 }
827 db_clause_list_free(clause_list);
828
829 return DB_OK;
830 }
831
key_data_set_zone_id(key_data_t * key_data,const db_value_t * zone_id)832 int key_data_set_zone_id(key_data_t* key_data, const db_value_t* zone_id) {
833 if (!key_data) {
834 return DB_ERROR_UNKNOWN;
835 }
836 if (!zone_id) {
837 return DB_ERROR_UNKNOWN;
838 }
839 if (db_value_not_empty(zone_id)) {
840 return DB_ERROR_UNKNOWN;
841 }
842
843 db_value_reset(&(key_data->zone_id));
844 if (db_value_copy(&(key_data->zone_id), zone_id)) {
845 return DB_ERROR_UNKNOWN;
846 }
847
848 return DB_OK;
849 }
850
key_data_set_hsm_key_id(key_data_t * key_data,const db_value_t * hsm_key_id)851 int key_data_set_hsm_key_id(key_data_t* key_data, const db_value_t* hsm_key_id) {
852 if (!key_data) {
853 return DB_ERROR_UNKNOWN;
854 }
855 if (!hsm_key_id) {
856 return DB_ERROR_UNKNOWN;
857 }
858 if (db_value_not_empty(hsm_key_id)) {
859 return DB_ERROR_UNKNOWN;
860 }
861
862 db_value_reset(&(key_data->hsm_key_id));
863 if (db_value_copy(&(key_data->hsm_key_id), hsm_key_id)) {
864 return DB_ERROR_UNKNOWN;
865 }
866
867 return DB_OK;
868 }
869
key_data_set_algorithm(key_data_t * key_data,unsigned int algorithm)870 int key_data_set_algorithm(key_data_t* key_data, unsigned int algorithm) {
871 if (!key_data) {
872 return DB_ERROR_UNKNOWN;
873 }
874
875 key_data->algorithm = algorithm;
876
877 return DB_OK;
878 }
879
key_data_set_inception(key_data_t * key_data,unsigned int inception)880 int key_data_set_inception(key_data_t* key_data, unsigned int inception) {
881 if (!key_data) {
882 return DB_ERROR_UNKNOWN;
883 }
884
885 key_data->inception = inception;
886
887 return DB_OK;
888 }
889
key_data_set_role(key_data_t * key_data,key_data_role_t role)890 int key_data_set_role(key_data_t* key_data, key_data_role_t role) {
891 if (!key_data) {
892 return DB_ERROR_UNKNOWN;
893 }
894 if (role == KEY_DATA_ROLE_INVALID) {
895 return DB_ERROR_UNKNOWN;
896 }
897
898 key_data->role = role;
899
900 return DB_OK;
901 }
902
key_data_set_introducing(key_data_t * key_data,unsigned int introducing)903 int key_data_set_introducing(key_data_t* key_data, unsigned int introducing) {
904 if (!key_data) {
905 return DB_ERROR_UNKNOWN;
906 }
907
908 key_data->introducing = introducing;
909
910 return DB_OK;
911 }
912
key_data_set_active_zsk(key_data_t * key_data,unsigned int active_zsk)913 int key_data_set_active_zsk(key_data_t* key_data, unsigned int active_zsk) {
914 if (!key_data) {
915 return DB_ERROR_UNKNOWN;
916 }
917
918 key_data->active_zsk = active_zsk;
919
920 return DB_OK;
921 }
922
key_data_set_publish(key_data_t * key_data,unsigned int publish)923 int key_data_set_publish(key_data_t* key_data, unsigned int publish) {
924 if (!key_data) {
925 return DB_ERROR_UNKNOWN;
926 }
927
928 key_data->publish = publish;
929
930 return DB_OK;
931 }
932
key_data_set_active_ksk(key_data_t * key_data,unsigned int active_ksk)933 int key_data_set_active_ksk(key_data_t* key_data, unsigned int active_ksk) {
934 if (!key_data) {
935 return DB_ERROR_UNKNOWN;
936 }
937
938 key_data->active_ksk = active_ksk;
939
940 return DB_OK;
941 }
942
key_data_set_ds_at_parent(key_data_t * key_data,key_data_ds_at_parent_t ds_at_parent)943 int key_data_set_ds_at_parent(key_data_t* key_data, key_data_ds_at_parent_t ds_at_parent) {
944 if (!key_data) {
945 return DB_ERROR_UNKNOWN;
946 }
947 if (ds_at_parent == KEY_DATA_DS_AT_PARENT_INVALID) {
948 return DB_ERROR_UNKNOWN;
949 }
950
951 key_data->ds_at_parent = ds_at_parent;
952
953 return DB_OK;
954 }
955
key_data_set_keytag(key_data_t * key_data,unsigned int keytag)956 int key_data_set_keytag(key_data_t* key_data, unsigned int keytag) {
957 if (!key_data) {
958 return DB_ERROR_UNKNOWN;
959 }
960
961 key_data->keytag = keytag;
962
963 return DB_OK;
964 }
965
key_data_set_minimize(key_data_t * key_data,unsigned int minimize)966 int key_data_set_minimize(key_data_t* key_data, unsigned int minimize) {
967 if (!key_data) {
968 return DB_ERROR_UNKNOWN;
969 }
970
971 key_data->minimize = minimize;
972
973 return DB_OK;
974 }
975
key_data_zone_id_clause(db_clause_list_t * clause_list,const db_value_t * zone_id)976 db_clause_t* key_data_zone_id_clause(db_clause_list_t* clause_list, const db_value_t* zone_id) {
977 db_clause_t* clause;
978
979 if (!clause_list) {
980 return NULL;
981 }
982 if (!zone_id) {
983 return NULL;
984 }
985 if (db_value_not_empty(zone_id)) {
986 return NULL;
987 }
988
989 if (!(clause = db_clause_new())
990 || db_clause_set_field(clause, "zoneId")
991 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
992 || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_AND)
993 || db_value_copy(db_clause_get_value(clause), zone_id)
994 || db_clause_list_add(clause_list, clause))
995 {
996 db_clause_free(clause);
997 return NULL;
998 }
999
1000 return clause;
1001 }
1002
key_data_hsm_key_id_clause(db_clause_list_t * clause_list,const db_value_t * hsm_key_id)1003 db_clause_t* key_data_hsm_key_id_clause(db_clause_list_t* clause_list, const db_value_t* hsm_key_id) {
1004 db_clause_t* clause;
1005
1006 if (!clause_list) {
1007 return NULL;
1008 }
1009 if (!hsm_key_id) {
1010 return NULL;
1011 }
1012 if (db_value_not_empty(hsm_key_id)) {
1013 return NULL;
1014 }
1015
1016 if (!(clause = db_clause_new())
1017 || db_clause_set_field(clause, "hsmKeyId")
1018 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1019 || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_AND)
1020 || db_value_copy(db_clause_get_value(clause), hsm_key_id)
1021 || db_clause_list_add(clause_list, clause))
1022 {
1023 db_clause_free(clause);
1024 return NULL;
1025 }
1026
1027 return clause;
1028 }
1029
key_data_role_clause(db_clause_list_t * clause_list,key_data_role_t role)1030 db_clause_t* key_data_role_clause(db_clause_list_t* clause_list, key_data_role_t role) {
1031 db_clause_t* clause;
1032
1033 if (!clause_list) {
1034 return NULL;
1035 }
1036
1037 if (!(clause = db_clause_new())
1038 || db_clause_set_field(clause, "role")
1039 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1040 || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_AND)
1041 || db_value_from_enum_value(db_clause_get_value(clause), role, key_data_enum_set_role)
1042 || db_clause_list_add(clause_list, clause))
1043 {
1044 db_clause_free(clause);
1045 return NULL;
1046 }
1047
1048 return clause;
1049 }
1050
key_data_ds_at_parent_clause(db_clause_list_t * clause_list,key_data_ds_at_parent_t ds_at_parent)1051 db_clause_t* key_data_ds_at_parent_clause(db_clause_list_t* clause_list, key_data_ds_at_parent_t ds_at_parent) {
1052 db_clause_t* clause;
1053
1054 if (!clause_list) {
1055 return NULL;
1056 }
1057
1058 if (!(clause = db_clause_new())
1059 || db_clause_set_field(clause, "dsAtParent")
1060 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1061 || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_AND)
1062 || db_value_from_enum_value(db_clause_get_value(clause), ds_at_parent, key_data_enum_set_ds_at_parent)
1063 || db_clause_list_add(clause_list, clause))
1064 {
1065 db_clause_free(clause);
1066 return NULL;
1067 }
1068
1069 return clause;
1070 }
1071
key_data_keytag_clause(db_clause_list_t * clause_list,unsigned int keytag)1072 db_clause_t* key_data_keytag_clause(db_clause_list_t* clause_list, unsigned int keytag) {
1073 db_clause_t* clause;
1074
1075 if (!clause_list) {
1076 return NULL;
1077 }
1078
1079 if (!(clause = db_clause_new())
1080 || db_clause_set_field(clause, "keytag")
1081 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1082 || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_AND)
1083 || db_value_from_uint32(db_clause_get_value(clause), keytag)
1084 || db_clause_list_add(clause_list, clause))
1085 {
1086 db_clause_free(clause);
1087 return NULL;
1088 }
1089
1090 return clause;
1091 }
1092
key_data_create(key_data_t * key_data)1093 int key_data_create(key_data_t* key_data) {
1094 db_object_field_list_t* object_field_list;
1095 db_object_field_t* object_field;
1096 db_value_set_t* value_set;
1097 int ret;
1098
1099 if (!key_data) {
1100 return DB_ERROR_UNKNOWN;
1101 }
1102 if (!key_data->dbo) {
1103 return DB_ERROR_UNKNOWN;
1104 }
1105 if (!db_value_not_empty(&(key_data->id))) {
1106 return DB_ERROR_UNKNOWN;
1107 }
1108 if (!db_value_not_empty(&(key_data->rev))) {
1109 return DB_ERROR_UNKNOWN;
1110 }
1111 if (db_value_not_empty(&(key_data->zone_id))) {
1112 return DB_ERROR_UNKNOWN;
1113 }
1114 if (db_value_not_empty(&(key_data->hsm_key_id))) {
1115 return DB_ERROR_UNKNOWN;
1116 }
1117 /* TODO: validate content more */
1118
1119 if (!(object_field_list = db_object_field_list_new())) {
1120 return DB_ERROR_UNKNOWN;
1121 }
1122
1123 if (!(object_field = db_object_field_new())
1124 || db_object_field_set_name(object_field, "zoneId")
1125 || db_object_field_set_type(object_field, DB_TYPE_ANY)
1126 || db_object_field_list_add(object_field_list, object_field))
1127 {
1128 db_object_field_free(object_field);
1129 db_object_field_list_free(object_field_list);
1130 return DB_ERROR_UNKNOWN;
1131 }
1132
1133 if (!(object_field = db_object_field_new())
1134 || db_object_field_set_name(object_field, "hsmKeyId")
1135 || db_object_field_set_type(object_field, DB_TYPE_ANY)
1136 || db_object_field_list_add(object_field_list, object_field))
1137 {
1138 db_object_field_free(object_field);
1139 db_object_field_list_free(object_field_list);
1140 return DB_ERROR_UNKNOWN;
1141 }
1142
1143 if (!(object_field = db_object_field_new())
1144 || db_object_field_set_name(object_field, "algorithm")
1145 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1146 || db_object_field_list_add(object_field_list, object_field))
1147 {
1148 db_object_field_free(object_field);
1149 db_object_field_list_free(object_field_list);
1150 return DB_ERROR_UNKNOWN;
1151 }
1152
1153 if (!(object_field = db_object_field_new())
1154 || db_object_field_set_name(object_field, "inception")
1155 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1156 || db_object_field_list_add(object_field_list, object_field))
1157 {
1158 db_object_field_free(object_field);
1159 db_object_field_list_free(object_field_list);
1160 return DB_ERROR_UNKNOWN;
1161 }
1162
1163 if (!(object_field = db_object_field_new())
1164 || db_object_field_set_name(object_field, "role")
1165 || db_object_field_set_type(object_field, DB_TYPE_ENUM)
1166 || db_object_field_set_enum_set(object_field, key_data_enum_set_role)
1167 || db_object_field_list_add(object_field_list, object_field))
1168 {
1169 db_object_field_free(object_field);
1170 db_object_field_list_free(object_field_list);
1171 return DB_ERROR_UNKNOWN;
1172 }
1173
1174 if (!(object_field = db_object_field_new())
1175 || db_object_field_set_name(object_field, "introducing")
1176 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1177 || db_object_field_list_add(object_field_list, object_field))
1178 {
1179 db_object_field_free(object_field);
1180 db_object_field_list_free(object_field_list);
1181 return DB_ERROR_UNKNOWN;
1182 }
1183
1184 if (!(object_field = db_object_field_new())
1185 || db_object_field_set_name(object_field, "shouldRevoke")
1186 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1187 || db_object_field_list_add(object_field_list, object_field))
1188 {
1189 db_object_field_free(object_field);
1190 db_object_field_list_free(object_field_list);
1191 return DB_ERROR_UNKNOWN;
1192 }
1193
1194 if (!(object_field = db_object_field_new())
1195 || db_object_field_set_name(object_field, "standby")
1196 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1197 || db_object_field_list_add(object_field_list, object_field))
1198 {
1199 db_object_field_free(object_field);
1200 db_object_field_list_free(object_field_list);
1201 return DB_ERROR_UNKNOWN;
1202 }
1203
1204 if (!(object_field = db_object_field_new())
1205 || db_object_field_set_name(object_field, "activeZsk")
1206 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1207 || db_object_field_list_add(object_field_list, object_field))
1208 {
1209 db_object_field_free(object_field);
1210 db_object_field_list_free(object_field_list);
1211 return DB_ERROR_UNKNOWN;
1212 }
1213
1214 if (!(object_field = db_object_field_new())
1215 || db_object_field_set_name(object_field, "publish")
1216 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1217 || db_object_field_list_add(object_field_list, object_field))
1218 {
1219 db_object_field_free(object_field);
1220 db_object_field_list_free(object_field_list);
1221 return DB_ERROR_UNKNOWN;
1222 }
1223
1224 if (!(object_field = db_object_field_new())
1225 || db_object_field_set_name(object_field, "activeKsk")
1226 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1227 || db_object_field_list_add(object_field_list, object_field))
1228 {
1229 db_object_field_free(object_field);
1230 db_object_field_list_free(object_field_list);
1231 return DB_ERROR_UNKNOWN;
1232 }
1233
1234 if (!(object_field = db_object_field_new())
1235 || db_object_field_set_name(object_field, "dsAtParent")
1236 || db_object_field_set_type(object_field, DB_TYPE_ENUM)
1237 || db_object_field_set_enum_set(object_field, key_data_enum_set_ds_at_parent)
1238 || db_object_field_list_add(object_field_list, object_field))
1239 {
1240 db_object_field_free(object_field);
1241 db_object_field_list_free(object_field_list);
1242 return DB_ERROR_UNKNOWN;
1243 }
1244
1245 if (!(object_field = db_object_field_new())
1246 || db_object_field_set_name(object_field, "keytag")
1247 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1248 || db_object_field_list_add(object_field_list, object_field))
1249 {
1250 db_object_field_free(object_field);
1251 db_object_field_list_free(object_field_list);
1252 return DB_ERROR_UNKNOWN;
1253 }
1254
1255 if (!(object_field = db_object_field_new())
1256 || db_object_field_set_name(object_field, "minimize")
1257 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1258 || db_object_field_list_add(object_field_list, object_field))
1259 {
1260 db_object_field_free(object_field);
1261 db_object_field_list_free(object_field_list);
1262 return DB_ERROR_UNKNOWN;
1263 }
1264
1265 if (!(value_set = db_value_set_new(14))) {
1266 db_object_field_list_free(object_field_list);
1267 return DB_ERROR_UNKNOWN;
1268 }
1269
1270 if (db_value_copy(db_value_set_get(value_set, 0), &(key_data->zone_id))
1271 || db_value_copy(db_value_set_get(value_set, 1), &(key_data->hsm_key_id))
1272 || db_value_from_uint32(db_value_set_get(value_set, 2), key_data->algorithm)
1273 || db_value_from_uint32(db_value_set_get(value_set, 3), key_data->inception)
1274 || db_value_from_enum_value(db_value_set_get(value_set, 4), key_data->role, key_data_enum_set_role)
1275 || db_value_from_uint32(db_value_set_get(value_set, 5), key_data->introducing)
1276 || db_value_from_uint32(db_value_set_get(value_set, 6), key_data->should_revoke)
1277 || db_value_from_uint32(db_value_set_get(value_set, 7), key_data->standby)
1278 || db_value_from_uint32(db_value_set_get(value_set, 8), key_data->active_zsk)
1279 || db_value_from_uint32(db_value_set_get(value_set, 9), key_data->publish)
1280 || db_value_from_uint32(db_value_set_get(value_set, 10), key_data->active_ksk)
1281 || db_value_from_enum_value(db_value_set_get(value_set, 11), key_data->ds_at_parent, key_data_enum_set_ds_at_parent)
1282 || db_value_from_uint32(db_value_set_get(value_set, 12), key_data->keytag)
1283 || db_value_from_uint32(db_value_set_get(value_set, 13), key_data->minimize))
1284 {
1285 db_value_set_free(value_set);
1286 db_object_field_list_free(object_field_list);
1287 return DB_ERROR_UNKNOWN;
1288 }
1289
1290 ret = db_object_create(key_data->dbo, object_field_list, value_set);
1291 db_value_set_free(value_set);
1292 db_object_field_list_free(object_field_list);
1293 return ret;
1294 }
1295
key_data_get_by_id(key_data_t * key_data,const db_value_t * id)1296 int key_data_get_by_id(key_data_t* key_data, const db_value_t* id) {
1297 db_clause_list_t* clause_list;
1298 db_clause_t* clause;
1299 db_result_list_t* result_list;
1300 const db_result_t* result;
1301
1302 if (!key_data) {
1303 return DB_ERROR_UNKNOWN;
1304 }
1305 if (!key_data->dbo) {
1306 return DB_ERROR_UNKNOWN;
1307 }
1308 if (!id) {
1309 return DB_ERROR_UNKNOWN;
1310 }
1311 if (db_value_not_empty(id)) {
1312 return DB_ERROR_UNKNOWN;
1313 }
1314
1315 if (!(clause_list = db_clause_list_new())) {
1316 return DB_ERROR_UNKNOWN;
1317 }
1318 if (!(clause = db_clause_new())
1319 || db_clause_set_field(clause, "id")
1320 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1321 || db_value_copy(db_clause_get_value(clause), id)
1322 || db_clause_list_add(clause_list, clause))
1323 {
1324 db_clause_free(clause);
1325 db_clause_list_free(clause_list);
1326 return DB_ERROR_UNKNOWN;
1327 }
1328
1329 result_list = db_object_read(key_data->dbo, NULL, clause_list);
1330 db_clause_list_free(clause_list);
1331
1332 if (result_list) {
1333 result = db_result_list_next(result_list);
1334 if (result) {
1335 if (key_data_from_result(key_data, result)) {
1336 db_result_list_free(result_list);
1337 return DB_ERROR_UNKNOWN;
1338 }
1339
1340 db_result_list_free(result_list);
1341 return DB_OK;
1342 }
1343 }
1344
1345 db_result_list_free(result_list);
1346 return DB_ERROR_UNKNOWN;
1347 }
1348
key_data_update(key_data_t * key_data)1349 int key_data_update(key_data_t* key_data) {
1350 db_object_field_list_t* object_field_list;
1351 db_object_field_t* object_field;
1352 db_value_set_t* value_set;
1353 db_clause_list_t* clause_list;
1354 db_clause_t* clause;
1355 int ret;
1356
1357 if (!key_data) {
1358 return DB_ERROR_UNKNOWN;
1359 }
1360 if (!key_data->dbo) {
1361 return DB_ERROR_UNKNOWN;
1362 }
1363 if (db_value_not_empty(&(key_data->id))) {
1364 return DB_ERROR_UNKNOWN;
1365 }
1366 if (db_value_not_empty(&(key_data->rev))) {
1367 return DB_ERROR_UNKNOWN;
1368 }
1369 if (db_value_not_empty(&(key_data->zone_id))) {
1370 return DB_ERROR_UNKNOWN;
1371 }
1372 if (db_value_not_empty(&(key_data->hsm_key_id))) {
1373 return DB_ERROR_UNKNOWN;
1374 }
1375 /* TODO: validate content more */
1376
1377 if (!(object_field_list = db_object_field_list_new())) {
1378 return DB_ERROR_UNKNOWN;
1379 }
1380
1381 if (!(object_field = db_object_field_new())
1382 || db_object_field_set_name(object_field, "zoneId")
1383 || db_object_field_set_type(object_field, DB_TYPE_ANY)
1384 || db_object_field_list_add(object_field_list, object_field))
1385 {
1386 db_object_field_free(object_field);
1387 db_object_field_list_free(object_field_list);
1388 return DB_ERROR_UNKNOWN;
1389 }
1390
1391 if (!(object_field = db_object_field_new())
1392 || db_object_field_set_name(object_field, "hsmKeyId")
1393 || db_object_field_set_type(object_field, DB_TYPE_ANY)
1394 || db_object_field_list_add(object_field_list, object_field))
1395 {
1396 db_object_field_free(object_field);
1397 db_object_field_list_free(object_field_list);
1398 return DB_ERROR_UNKNOWN;
1399 }
1400
1401 if (!(object_field = db_object_field_new())
1402 || db_object_field_set_name(object_field, "algorithm")
1403 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1404 || db_object_field_list_add(object_field_list, object_field))
1405 {
1406 db_object_field_free(object_field);
1407 db_object_field_list_free(object_field_list);
1408 return DB_ERROR_UNKNOWN;
1409 }
1410
1411 if (!(object_field = db_object_field_new())
1412 || db_object_field_set_name(object_field, "inception")
1413 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1414 || db_object_field_list_add(object_field_list, object_field))
1415 {
1416 db_object_field_free(object_field);
1417 db_object_field_list_free(object_field_list);
1418 return DB_ERROR_UNKNOWN;
1419 }
1420
1421 if (!(object_field = db_object_field_new())
1422 || db_object_field_set_name(object_field, "role")
1423 || db_object_field_set_type(object_field, DB_TYPE_ENUM)
1424 || db_object_field_set_enum_set(object_field, key_data_enum_set_role)
1425 || db_object_field_list_add(object_field_list, object_field))
1426 {
1427 db_object_field_free(object_field);
1428 db_object_field_list_free(object_field_list);
1429 return DB_ERROR_UNKNOWN;
1430 }
1431
1432 if (!(object_field = db_object_field_new())
1433 || db_object_field_set_name(object_field, "introducing")
1434 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1435 || db_object_field_list_add(object_field_list, object_field))
1436 {
1437 db_object_field_free(object_field);
1438 db_object_field_list_free(object_field_list);
1439 return DB_ERROR_UNKNOWN;
1440 }
1441
1442 if (!(object_field = db_object_field_new())
1443 || db_object_field_set_name(object_field, "shouldRevoke")
1444 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1445 || db_object_field_list_add(object_field_list, object_field))
1446 {
1447 db_object_field_free(object_field);
1448 db_object_field_list_free(object_field_list);
1449 return DB_ERROR_UNKNOWN;
1450 }
1451
1452 if (!(object_field = db_object_field_new())
1453 || db_object_field_set_name(object_field, "standby")
1454 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1455 || db_object_field_list_add(object_field_list, object_field))
1456 {
1457 db_object_field_free(object_field);
1458 db_object_field_list_free(object_field_list);
1459 return DB_ERROR_UNKNOWN;
1460 }
1461
1462 if (!(object_field = db_object_field_new())
1463 || db_object_field_set_name(object_field, "activeZsk")
1464 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1465 || db_object_field_list_add(object_field_list, object_field))
1466 {
1467 db_object_field_free(object_field);
1468 db_object_field_list_free(object_field_list);
1469 return DB_ERROR_UNKNOWN;
1470 }
1471
1472 if (!(object_field = db_object_field_new())
1473 || db_object_field_set_name(object_field, "publish")
1474 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1475 || db_object_field_list_add(object_field_list, object_field))
1476 {
1477 db_object_field_free(object_field);
1478 db_object_field_list_free(object_field_list);
1479 return DB_ERROR_UNKNOWN;
1480 }
1481
1482 if (!(object_field = db_object_field_new())
1483 || db_object_field_set_name(object_field, "activeKsk")
1484 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1485 || db_object_field_list_add(object_field_list, object_field))
1486 {
1487 db_object_field_free(object_field);
1488 db_object_field_list_free(object_field_list);
1489 return DB_ERROR_UNKNOWN;
1490 }
1491
1492 if (!(object_field = db_object_field_new())
1493 || db_object_field_set_name(object_field, "dsAtParent")
1494 || db_object_field_set_type(object_field, DB_TYPE_ENUM)
1495 || db_object_field_set_enum_set(object_field, key_data_enum_set_ds_at_parent)
1496 || db_object_field_list_add(object_field_list, object_field))
1497 {
1498 db_object_field_free(object_field);
1499 db_object_field_list_free(object_field_list);
1500 return DB_ERROR_UNKNOWN;
1501 }
1502
1503 if (!(object_field = db_object_field_new())
1504 || db_object_field_set_name(object_field, "keytag")
1505 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1506 || db_object_field_list_add(object_field_list, object_field))
1507 {
1508 db_object_field_free(object_field);
1509 db_object_field_list_free(object_field_list);
1510 return DB_ERROR_UNKNOWN;
1511 }
1512
1513 if (!(object_field = db_object_field_new())
1514 || db_object_field_set_name(object_field, "minimize")
1515 || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1516 || db_object_field_list_add(object_field_list, object_field))
1517 {
1518 db_object_field_free(object_field);
1519 db_object_field_list_free(object_field_list);
1520 return DB_ERROR_UNKNOWN;
1521 }
1522
1523 if (!(value_set = db_value_set_new(14))) {
1524 db_object_field_list_free(object_field_list);
1525 return DB_ERROR_UNKNOWN;
1526 }
1527
1528 if (db_value_copy(db_value_set_get(value_set, 0), &(key_data->zone_id))
1529 || db_value_copy(db_value_set_get(value_set, 1), &(key_data->hsm_key_id))
1530 || db_value_from_uint32(db_value_set_get(value_set, 2), key_data->algorithm)
1531 || db_value_from_uint32(db_value_set_get(value_set, 3), key_data->inception)
1532 || db_value_from_enum_value(db_value_set_get(value_set, 4), key_data->role, key_data_enum_set_role)
1533 || db_value_from_uint32(db_value_set_get(value_set, 5), key_data->introducing)
1534 || db_value_from_uint32(db_value_set_get(value_set, 6), key_data->should_revoke)
1535 || db_value_from_uint32(db_value_set_get(value_set, 7), key_data->standby)
1536 || db_value_from_uint32(db_value_set_get(value_set, 8), key_data->active_zsk)
1537 || db_value_from_uint32(db_value_set_get(value_set, 9), key_data->publish)
1538 || db_value_from_uint32(db_value_set_get(value_set, 10), key_data->active_ksk)
1539 || db_value_from_enum_value(db_value_set_get(value_set, 11), key_data->ds_at_parent, key_data_enum_set_ds_at_parent)
1540 || db_value_from_uint32(db_value_set_get(value_set, 12), key_data->keytag)
1541 || db_value_from_uint32(db_value_set_get(value_set, 13), key_data->minimize))
1542 {
1543 db_value_set_free(value_set);
1544 db_object_field_list_free(object_field_list);
1545 return DB_ERROR_UNKNOWN;
1546 }
1547
1548 if (!(clause_list = db_clause_list_new())) {
1549 db_value_set_free(value_set);
1550 db_object_field_list_free(object_field_list);
1551 return DB_ERROR_UNKNOWN;
1552 }
1553
1554 if (!(clause = db_clause_new())
1555 || db_clause_set_field(clause, "id")
1556 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1557 || db_value_copy(db_clause_get_value(clause), &(key_data->id))
1558 || db_clause_list_add(clause_list, clause))
1559 {
1560 db_clause_free(clause);
1561 db_clause_list_free(clause_list);
1562 db_value_set_free(value_set);
1563 db_object_field_list_free(object_field_list);
1564 return DB_ERROR_UNKNOWN;
1565 }
1566
1567 if (!(clause = db_clause_new())
1568 || db_clause_set_field(clause, "rev")
1569 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1570 || db_value_copy(db_clause_get_value(clause), &(key_data->rev))
1571 || db_clause_list_add(clause_list, clause))
1572 {
1573 db_clause_free(clause);
1574 db_clause_list_free(clause_list);
1575 db_value_set_free(value_set);
1576 db_object_field_list_free(object_field_list);
1577 return DB_ERROR_UNKNOWN;
1578 }
1579
1580 ret = db_object_update(key_data->dbo, object_field_list, value_set, clause_list);
1581 db_value_set_free(value_set);
1582 db_object_field_list_free(object_field_list);
1583 db_clause_list_free(clause_list);
1584 return ret;
1585 }
1586
key_data_delete(key_data_t * key_data)1587 int key_data_delete(key_data_t* key_data) {
1588 db_clause_list_t* clause_list;
1589 db_clause_t* clause;
1590 int ret;
1591
1592 if (!key_data) {
1593 return DB_ERROR_UNKNOWN;
1594 }
1595 if (!key_data->dbo) {
1596 return DB_ERROR_UNKNOWN;
1597 }
1598 if (db_value_not_empty(&(key_data->id))) {
1599 return DB_ERROR_UNKNOWN;
1600 }
1601
1602 if (!(clause_list = db_clause_list_new())) {
1603 return DB_ERROR_UNKNOWN;
1604 }
1605
1606 if (!(clause = db_clause_new())
1607 || db_clause_set_field(clause, "id")
1608 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1609 || db_value_copy(db_clause_get_value(clause), &(key_data->id))
1610 || db_clause_list_add(clause_list, clause))
1611 {
1612 db_clause_free(clause);
1613 db_clause_list_free(clause_list);
1614 return DB_ERROR_UNKNOWN;
1615 }
1616
1617 if (!(clause = db_clause_new())
1618 || db_clause_set_field(clause, "rev")
1619 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1620 || db_value_copy(db_clause_get_value(clause), &(key_data->rev))
1621 || db_clause_list_add(clause_list, clause))
1622 {
1623 db_clause_free(clause);
1624 db_clause_list_free(clause_list);
1625 return DB_ERROR_UNKNOWN;
1626 }
1627
1628 ret = db_object_delete(key_data->dbo, clause_list);
1629 db_clause_list_free(clause_list);
1630 return ret;
1631 }
1632
key_data_count(key_data_t * key_data,db_clause_list_t * clause_list,size_t * count)1633 int key_data_count(key_data_t* key_data, db_clause_list_t* clause_list, size_t* count) {
1634 if (!key_data) {
1635 return DB_ERROR_UNKNOWN;
1636 }
1637 if (!key_data->dbo) {
1638 return DB_ERROR_UNKNOWN;
1639 }
1640 if (!count) {
1641 return DB_ERROR_UNKNOWN;
1642 }
1643
1644 return db_object_count(key_data->dbo, NULL, clause_list, count);
1645 }
1646
1647 /* KEY DATA LIST */
1648
1649
1650
key_data_list_new(const db_connection_t * connection)1651 key_data_list_t* key_data_list_new(const db_connection_t* connection) {
1652 key_data_list_t* key_data_list =
1653 (key_data_list_t*)calloc(1, sizeof(key_data_list_t));
1654
1655 if (key_data_list) {
1656 if (!(key_data_list->dbo = __key_data_new_object(connection))) {
1657 free(key_data_list);
1658 return NULL;
1659 }
1660 }
1661
1662 return key_data_list;
1663 }
1664
key_data_list_new_copy(const key_data_list_t * from_key_data_list)1665 key_data_list_t* key_data_list_new_copy(const key_data_list_t* from_key_data_list) {
1666 key_data_list_t* key_data_list;
1667
1668 if (!from_key_data_list) {
1669 return NULL;
1670 }
1671 if (!from_key_data_list->dbo) {
1672 return NULL;
1673 }
1674
1675 if (!(key_data_list = key_data_list_new(db_object_connection(from_key_data_list->dbo)))
1676 || key_data_list_copy(key_data_list, from_key_data_list))
1677 {
1678 key_data_list_free(key_data_list);
1679 return NULL;
1680 }
1681 return key_data_list;
1682 }
1683
key_data_list_object_store(key_data_list_t * key_data_list)1684 int key_data_list_object_store(key_data_list_t* key_data_list) {
1685 if (!key_data_list) {
1686 return DB_ERROR_UNKNOWN;
1687 }
1688
1689 key_data_list->object_store = 1;
1690
1691 return DB_OK;
1692 }
1693
key_data_list_free(key_data_list_t * key_data_list)1694 void key_data_list_free(key_data_list_t* key_data_list) {
1695 size_t i;
1696
1697 if (key_data_list) {
1698 if (key_data_list->dbo) {
1699 db_object_free(key_data_list->dbo);
1700 }
1701 if (key_data_list->result_list) {
1702 db_result_list_free(key_data_list->result_list);
1703 }
1704 if (key_data_list->key_data) {
1705 key_data_free(key_data_list->key_data);
1706 }
1707 for (i = 0; i < key_data_list->object_list_size; i++) {
1708 if (key_data_list->object_list[i]) {
1709 key_data_free(key_data_list->object_list[i]);
1710 }
1711 }
1712 if (key_data_list->object_list) {
1713 free(key_data_list->object_list);
1714 }
1715 if (key_data_list->zone_id_list) {
1716 zone_list_db_free(key_data_list->zone_id_list);
1717 }
1718 if (key_data_list->hsm_key_id_list) {
1719 hsm_key_list_free(key_data_list->hsm_key_id_list);
1720 }
1721 free(key_data_list);
1722 }
1723 }
1724
key_data_list_copy(key_data_list_t * key_data_list,const key_data_list_t * from_key_data_list)1725 int key_data_list_copy(key_data_list_t* key_data_list, const key_data_list_t* from_key_data_list) {
1726 size_t i;
1727
1728 if (!key_data_list) {
1729 return DB_ERROR_UNKNOWN;
1730 }
1731 if (!from_key_data_list) {
1732 return DB_ERROR_UNKNOWN;
1733 }
1734 if (from_key_data_list->object_list && !from_key_data_list->object_list_size) {
1735 return DB_ERROR_UNKNOWN;
1736 }
1737
1738 if (key_data_list->result_list) {
1739 db_result_list_free(key_data_list->result_list);
1740 key_data_list->result_list = NULL;
1741 }
1742 if (from_key_data_list->result_list
1743 && !(key_data_list->result_list = db_result_list_new_copy(from_key_data_list->result_list)))
1744 {
1745 return DB_ERROR_UNKNOWN;
1746 }
1747
1748 key_data_list->object_store = from_key_data_list->object_store;
1749 for (i = 0; i < key_data_list->object_list_size; i++) {
1750 if (key_data_list->object_list[i]) {
1751 key_data_free(key_data_list->object_list[i]);
1752 }
1753 }
1754 key_data_list->object_list_size = 0;
1755 if (key_data_list->object_list) {
1756 free(key_data_list->object_list);
1757 key_data_list->object_list = NULL;
1758 }
1759 if (from_key_data_list->object_list) {
1760 if (!(key_data_list->object_list = (key_data_t**)calloc(from_key_data_list->object_list_size, sizeof(key_data_t*)))) {
1761 return DB_ERROR_UNKNOWN;
1762 }
1763 key_data_list->object_list_size = from_key_data_list->object_list_size;
1764 for (i = 0; i < from_key_data_list->object_list_size; i++) {
1765 if (!from_key_data_list->object_list[i]) {
1766 continue;
1767 }
1768 if (!(key_data_list->object_list[i] = key_data_new_copy(from_key_data_list->object_list[i]))) {
1769 return DB_ERROR_UNKNOWN;
1770 }
1771 }
1772 }
1773 key_data_list->object_list_position = 0;;
1774 key_data_list->object_list_first = 1;
1775 key_data_list->associated_fetch = from_key_data_list->associated_fetch;
1776
1777 if (from_key_data_list->zone_id_list
1778 && !(key_data_list->zone_id_list = zone_list_db_new_copy(from_key_data_list->zone_id_list)))
1779 {
1780 return DB_ERROR_UNKNOWN;
1781 }
1782
1783 if (from_key_data_list->hsm_key_id_list
1784 && !(key_data_list->hsm_key_id_list = hsm_key_list_new_copy(from_key_data_list->hsm_key_id_list)))
1785 {
1786 return DB_ERROR_UNKNOWN;
1787 }
1788
1789 return DB_OK;
1790 }
1791
key_data_list_get_associated(key_data_list_t * key_data_list)1792 static int key_data_list_get_associated(key_data_list_t* key_data_list) {
1793 const db_clause_t* clause_walk;
1794 const zone_db_t* zone_zone_id;
1795 const hsm_key_t* hsm_key_hsm_key_id;
1796 size_t j, count;
1797 int cmp;
1798 size_t i;
1799 db_clause_list_t* clause_list;
1800 db_clause_t* clause;
1801 const key_data_t* key_data;
1802 key_state_list_t* key_state_list;
1803 const key_state_t* key_state;
1804
1805 if (!key_data_list) {
1806 return DB_ERROR_UNKNOWN;
1807 }
1808 if (!key_data_list->dbo) {
1809 return DB_ERROR_UNKNOWN;
1810 }
1811 if (!key_data_list->associated_fetch) {
1812 return DB_ERROR_UNKNOWN;
1813 }
1814 if (!key_data_list->result_list) {
1815 return DB_ERROR_UNKNOWN;
1816 }
1817 if (key_data_list->object_list) {
1818 return DB_ERROR_UNKNOWN;
1819 }
1820
1821 if (key_data_list->zone_id_list) {
1822 zone_list_db_free(key_data_list->zone_id_list);
1823 key_data_list->zone_id_list = NULL;
1824 }
1825 if (key_data_list->hsm_key_id_list) {
1826 hsm_key_list_free(key_data_list->hsm_key_id_list);
1827 key_data_list->hsm_key_id_list = NULL;
1828 }
1829
1830 if (!(clause_list = db_clause_list_new())) {
1831 return DB_ERROR_UNKNOWN;
1832 }
1833 key_data = key_data_list_begin(key_data_list);
1834 while (key_data) {
1835 cmp = 1;
1836 clause_walk = db_clause_list_begin(clause_list);
1837 while (clause_walk) {
1838 if (db_value_cmp(db_clause_value(clause_walk), key_data_zone_id(key_data), &cmp)) {
1839 db_clause_list_free(clause_list);
1840 return DB_ERROR_UNKNOWN;
1841 }
1842 if (!cmp) {
1843 break;
1844 }
1845 clause_walk = db_clause_next(clause_walk);
1846 }
1847 if (cmp) {
1848 if (!(clause = db_clause_new())
1849 || db_clause_set_field(clause, "id")
1850 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1851 || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_OR)
1852 || db_value_copy(db_clause_get_value(clause), key_data_zone_id(key_data))
1853 || db_clause_list_add(clause_list, clause))
1854 {
1855 db_clause_free(clause);
1856 db_clause_list_free(clause_list);
1857 return DB_ERROR_UNKNOWN;
1858 }
1859 }
1860
1861 key_data = key_data_list_next(key_data_list);
1862 }
1863
1864 if (!(key_data_list->zone_id_list = zone_list_db_new(db_object_connection(key_data_list->dbo)))
1865 || zone_list_db_object_store(key_data_list->zone_id_list)
1866 || zone_list_db_get_by_clauses(key_data_list->zone_id_list, clause_list))
1867 {
1868 if (key_data_list->zone_id_list) {
1869 zone_list_db_free(key_data_list->zone_id_list);
1870 key_data_list->zone_id_list = NULL;
1871 }
1872 db_clause_list_free(clause_list);
1873 return DB_ERROR_UNKNOWN;
1874 }
1875 db_clause_list_free(clause_list);
1876
1877 for (i = 0; i < key_data_list->object_list_size; i++) {
1878 if (!(key_data_list->object_list[i])) {
1879 return DB_ERROR_UNKNOWN;
1880 }
1881
1882 zone_zone_id = zone_list_db_begin(key_data_list->zone_id_list);
1883 while (zone_zone_id) {
1884 if (db_value_cmp(key_data_zone_id(key_data_list->object_list[i]), zone_db_id(zone_zone_id), &cmp)) {
1885 return DB_ERROR_UNKNOWN;
1886 }
1887 if (!cmp) {
1888 key_data_list->object_list[i]->associated_zone_id = zone_zone_id;
1889 }
1890
1891 zone_zone_id = zone_list_db_next(key_data_list->zone_id_list);
1892 }
1893 }
1894
1895 if (!(clause_list = db_clause_list_new())) {
1896 return DB_ERROR_UNKNOWN;
1897 }
1898 key_data = key_data_list_begin(key_data_list);
1899 while (key_data) {
1900 cmp = 1;
1901 clause_walk = db_clause_list_begin(clause_list);
1902 while (clause_walk) {
1903 if (db_value_cmp(db_clause_value(clause_walk), key_data_hsm_key_id(key_data), &cmp)) {
1904 db_clause_list_free(clause_list);
1905 return DB_ERROR_UNKNOWN;
1906 }
1907 if (!cmp) {
1908 break;
1909 }
1910 clause_walk = db_clause_next(clause_walk);
1911 }
1912 if (cmp) {
1913 if (!(clause = db_clause_new())
1914 || db_clause_set_field(clause, "id")
1915 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1916 || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_OR)
1917 || db_value_copy(db_clause_get_value(clause), key_data_hsm_key_id(key_data))
1918 || db_clause_list_add(clause_list, clause))
1919 {
1920 db_clause_free(clause);
1921 db_clause_list_free(clause_list);
1922 return DB_ERROR_UNKNOWN;
1923 }
1924 }
1925
1926 key_data = key_data_list_next(key_data_list);
1927 }
1928
1929 if (!(key_data_list->hsm_key_id_list = hsm_key_list_new(db_object_connection(key_data_list->dbo)))
1930 || hsm_key_list_object_store(key_data_list->hsm_key_id_list)
1931 || hsm_key_list_get_by_clauses(key_data_list->hsm_key_id_list, clause_list))
1932 {
1933 if (key_data_list->hsm_key_id_list) {
1934 hsm_key_list_free(key_data_list->hsm_key_id_list);
1935 key_data_list->hsm_key_id_list = NULL;
1936 }
1937 db_clause_list_free(clause_list);
1938 return DB_ERROR_UNKNOWN;
1939 }
1940 db_clause_list_free(clause_list);
1941
1942 for (i = 0; i < key_data_list->object_list_size; i++) {
1943 if (!(key_data_list->object_list[i])) {
1944 return DB_ERROR_UNKNOWN;
1945 }
1946
1947 hsm_key_hsm_key_id = hsm_key_list_begin(key_data_list->hsm_key_id_list);
1948 while (hsm_key_hsm_key_id) {
1949 if (db_value_cmp(key_data_hsm_key_id(key_data_list->object_list[i]), hsm_key_id(hsm_key_hsm_key_id), &cmp)) {
1950 return DB_ERROR_UNKNOWN;
1951 }
1952 if (!cmp) {
1953 key_data_list->object_list[i]->associated_hsm_key_id = hsm_key_hsm_key_id;
1954 }
1955
1956 hsm_key_hsm_key_id = hsm_key_list_next(key_data_list->hsm_key_id_list);
1957 }
1958 }
1959
1960 if (!(clause_list = db_clause_list_new())) {
1961 return DB_ERROR_UNKNOWN;
1962 }
1963 key_data = key_data_list_begin(key_data_list);
1964 while (key_data) {
1965 if (!(clause = db_clause_new())
1966 || db_clause_set_field(clause, "keyDataId")
1967 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1968 || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_OR)
1969 || db_value_copy(db_clause_get_value(clause), key_data_id(key_data))
1970 || db_clause_list_add(clause_list, clause))
1971 {
1972 db_clause_free(clause);
1973 db_clause_list_free(clause_list);
1974 return DB_ERROR_UNKNOWN;
1975 }
1976
1977 key_data = key_data_list_next(key_data_list);
1978 }
1979
1980 if (!(key_state_list = key_state_list_new(db_object_connection(key_data_list->dbo)))
1981 || key_state_list_object_store(key_state_list)
1982 || key_state_list_get_by_clauses(key_state_list, clause_list))
1983 {
1984 key_state_list_free(key_state_list);
1985 db_clause_list_free(clause_list);
1986 return DB_ERROR_UNKNOWN;
1987 }
1988
1989 for (i = 0; i < key_data_list->object_list_size; i++) {
1990 if (!(key_data_list->object_list[i])) {
1991 return DB_ERROR_UNKNOWN;
1992 }
1993
1994 count = 0;
1995 key_state = key_state_list_begin(key_state_list);
1996 while (key_state) {
1997 if (db_value_cmp(key_data_id(key_data_list->object_list[i]), key_state_key_data_id(key_state), &cmp)) {
1998 key_state_list_free(key_state_list);
1999 db_clause_list_free(clause_list);
2000 return DB_ERROR_UNKNOWN;
2001 }
2002 if (!cmp) {
2003 count++;
2004 }
2005 key_state = key_state_list_next(key_state_list);
2006 }
2007 if (key_data_list->object_list[i]->key_state_list) {
2008 key_state_list_free(key_data_list->object_list[i]->key_state_list);
2009 key_data_list->object_list[i]->key_state_list = NULL;
2010 }
2011 if (!(key_data_list->object_list[i]->key_state_list = key_state_list_new(db_object_connection(key_data_list->dbo)))) {
2012 key_state_list_free(key_state_list);
2013 db_clause_list_free(clause_list);
2014 return DB_ERROR_UNKNOWN;
2015 }
2016 if (count) {
2017 if (!(key_data_list->object_list[i]->key_state_list->object_list = (key_state_t**)calloc(count, sizeof(key_state_t*)))) {
2018 key_state_list_free(key_state_list);
2019 db_clause_list_free(clause_list);
2020 return DB_ERROR_UNKNOWN;
2021 }
2022
2023 j = 0;
2024 key_state = key_state_list_begin(key_state_list);
2025 while (key_state) {
2026 if (j >= count) {
2027 key_state_list_free(key_state_list);
2028 db_clause_list_free(clause_list);
2029 return DB_ERROR_UNKNOWN;
2030 }
2031 if (db_value_cmp(key_data_id(key_data_list->object_list[i]), key_state_key_data_id(key_state), &cmp)) {
2032 key_state_list_free(key_state_list);
2033 db_clause_list_free(clause_list);
2034 return DB_ERROR_UNKNOWN;
2035 }
2036 if (!cmp) {
2037 if (!(key_data_list->object_list[i]->key_state_list->object_list[j] = key_state_new_copy(key_state))) {
2038 key_state_list_free(key_state_list);
2039 db_clause_list_free(clause_list);
2040 return DB_ERROR_UNKNOWN;
2041 }
2042 j++;
2043 }
2044 key_state = key_state_list_next(key_state_list);
2045 }
2046 if (j != count) {
2047 key_state_list_free(key_state_list);
2048 db_clause_list_free(clause_list);
2049 return DB_ERROR_UNKNOWN;
2050 }
2051 }
2052 key_data_list->object_list[i]->key_state_list->object_store = 1;
2053 key_data_list->object_list[i]->key_state_list->object_list_size = count;
2054 key_data_list->object_list[i]->key_state_list->object_list_first = 1;
2055 }
2056 db_clause_list_free(clause_list);
2057 key_state_list_free(key_state_list);
2058
2059 key_data_list->object_list_first = 1;
2060 return DB_OK;
2061 }
2062
key_data_list_get(key_data_list_t * key_data_list)2063 int key_data_list_get(key_data_list_t* key_data_list) {
2064 size_t i;
2065
2066 if (!key_data_list) {
2067 return DB_ERROR_UNKNOWN;
2068 }
2069 if (!key_data_list->dbo) {
2070 return DB_ERROR_UNKNOWN;
2071 }
2072
2073 if (key_data_list->result_list) {
2074 db_result_list_free(key_data_list->result_list);
2075 }
2076 if (key_data_list->object_list_size) {
2077 for (i = 0; i < key_data_list->object_list_size; i++) {
2078 if (key_data_list->object_list[i]) {
2079 key_data_free(key_data_list->object_list[i]);
2080 }
2081 }
2082 key_data_list->object_list_size = 0;
2083 key_data_list->object_list_first = 0;
2084 }
2085 if (key_data_list->object_list) {
2086 free(key_data_list->object_list);
2087 key_data_list->object_list = NULL;
2088 }
2089 if (!(key_data_list->result_list = db_object_read(key_data_list->dbo, NULL, NULL))
2090 || db_result_list_fetch_all(key_data_list->result_list))
2091 {
2092 return DB_ERROR_UNKNOWN;
2093 }
2094 if (key_data_list->associated_fetch
2095 && key_data_list_get_associated(key_data_list))
2096 {
2097 return DB_ERROR_UNKNOWN;
2098 }
2099 return DB_OK;
2100 }
2101
key_data_list_new_get(const db_connection_t * connection)2102 key_data_list_t* key_data_list_new_get(const db_connection_t* connection) {
2103 key_data_list_t* key_data_list;
2104
2105 if (!connection) {
2106 return NULL;
2107 }
2108
2109 if (!(key_data_list = key_data_list_new(connection))
2110 || key_data_list_get(key_data_list))
2111 {
2112 key_data_list_free(key_data_list);
2113 return NULL;
2114 }
2115
2116 return key_data_list;
2117 }
2118
key_data_list_get_by_clauses(key_data_list_t * key_data_list,const db_clause_list_t * clause_list)2119 int key_data_list_get_by_clauses(key_data_list_t* key_data_list, const db_clause_list_t* clause_list) {
2120 size_t i;
2121
2122 if (!key_data_list) {
2123 return DB_ERROR_UNKNOWN;
2124 }
2125 if (!clause_list) {
2126 return DB_ERROR_UNKNOWN;
2127 }
2128 if (!key_data_list->dbo) {
2129 return DB_ERROR_UNKNOWN;
2130 }
2131
2132 if (key_data_list->result_list) {
2133 db_result_list_free(key_data_list->result_list);
2134 }
2135 if (key_data_list->object_list_size) {
2136 for (i = 0; i < key_data_list->object_list_size; i++) {
2137 if (key_data_list->object_list[i]) {
2138 key_data_free(key_data_list->object_list[i]);
2139 }
2140 }
2141 key_data_list->object_list_size = 0;
2142 key_data_list->object_list_first = 0;
2143 }
2144 if (key_data_list->object_list) {
2145 free(key_data_list->object_list);
2146 key_data_list->object_list = NULL;
2147 }
2148 if (!(key_data_list->result_list = db_object_read(key_data_list->dbo, NULL, clause_list))
2149 || db_result_list_fetch_all(key_data_list->result_list))
2150 {
2151 return DB_ERROR_UNKNOWN;
2152 }
2153 if (key_data_list->associated_fetch
2154 && key_data_list_get_associated(key_data_list))
2155 {
2156 return DB_ERROR_UNKNOWN;
2157 }
2158 return DB_OK;
2159 }
2160
key_data_list_new_get_by_clauses(const db_connection_t * connection,const db_clause_list_t * clause_list)2161 key_data_list_t* key_data_list_new_get_by_clauses(const db_connection_t* connection, const db_clause_list_t* clause_list) {
2162 key_data_list_t* key_data_list;
2163
2164 if (!connection) {
2165 return NULL;
2166 }
2167 if (!clause_list) {
2168 return NULL;
2169 }
2170
2171 if (!(key_data_list = key_data_list_new(connection))
2172 || key_data_list_get_by_clauses(key_data_list, clause_list))
2173 {
2174 key_data_list_free(key_data_list);
2175 return NULL;
2176 }
2177
2178 return key_data_list;
2179 }
2180
key_data_list_get_by_zone_id(key_data_list_t * key_data_list,const db_value_t * zone_id)2181 int key_data_list_get_by_zone_id(key_data_list_t* key_data_list, const db_value_t* zone_id) {
2182 db_clause_list_t* clause_list;
2183 db_clause_t* clause;
2184 size_t i;
2185
2186 if (!key_data_list) {
2187 return DB_ERROR_UNKNOWN;
2188 }
2189 if (!key_data_list->dbo) {
2190 return DB_ERROR_UNKNOWN;
2191 }
2192 if (!zone_id) {
2193 return DB_ERROR_UNKNOWN;
2194 }
2195 if (db_value_not_empty(zone_id)) {
2196 return DB_ERROR_UNKNOWN;
2197 }
2198
2199 if (!(clause_list = db_clause_list_new())) {
2200 return DB_ERROR_UNKNOWN;
2201 }
2202 if (!(clause = db_clause_new())
2203 || db_clause_set_field(clause, "zoneId")
2204 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
2205 || db_value_copy(db_clause_get_value(clause), zone_id)
2206 || db_clause_list_add(clause_list, clause))
2207 {
2208 db_clause_free(clause);
2209 db_clause_list_free(clause_list);
2210 return DB_ERROR_UNKNOWN;
2211 }
2212
2213 if (key_data_list->result_list) {
2214 db_result_list_free(key_data_list->result_list);
2215 }
2216 if (key_data_list->object_list_size) {
2217 for (i = 0; i < key_data_list->object_list_size; i++) {
2218 if (key_data_list->object_list[i]) {
2219 key_data_free(key_data_list->object_list[i]);
2220 }
2221 }
2222 key_data_list->object_list_size = 0;
2223 key_data_list->object_list_first = 0;
2224 }
2225 if (key_data_list->object_list) {
2226 free(key_data_list->object_list);
2227 key_data_list->object_list = NULL;
2228 }
2229 if (!(key_data_list->result_list = db_object_read(key_data_list->dbo, NULL, clause_list))
2230 || db_result_list_fetch_all(key_data_list->result_list))
2231 {
2232 db_clause_list_free(clause_list);
2233 return DB_ERROR_UNKNOWN;
2234 }
2235 db_clause_list_free(clause_list);
2236 if (key_data_list->associated_fetch
2237 && key_data_list_get_associated(key_data_list))
2238 {
2239 return DB_ERROR_UNKNOWN;
2240 }
2241 return DB_OK;
2242 }
2243
key_data_list_new_get_by_zone_id(const db_connection_t * connection,const db_value_t * zone_id)2244 key_data_list_t* key_data_list_new_get_by_zone_id(const db_connection_t* connection, const db_value_t* zone_id) {
2245 key_data_list_t* key_data_list;
2246
2247 if (!connection) {
2248 return NULL;
2249 }
2250 if (!zone_id) {
2251 return NULL;
2252 }
2253 if (db_value_not_empty(zone_id)) {
2254 return NULL;
2255 }
2256
2257 if (!(key_data_list = key_data_list_new(connection))
2258 || key_data_list_get_by_zone_id(key_data_list, zone_id))
2259 {
2260 key_data_list_free(key_data_list);
2261 return NULL;
2262 }
2263
2264 return key_data_list;
2265 }
2266
key_data_list_begin(key_data_list_t * key_data_list)2267 const key_data_t* key_data_list_begin(key_data_list_t* key_data_list) {
2268 const db_result_t* result;
2269
2270 if (!key_data_list) {
2271 return NULL;
2272 }
2273
2274 if (key_data_list->object_store) {
2275 if (!key_data_list->object_list) {
2276 if (!key_data_list->result_list) {
2277 return NULL;
2278 }
2279 if (!db_result_list_size(key_data_list->result_list)) {
2280 return NULL;
2281 }
2282 if (!(key_data_list->object_list = (key_data_t**)calloc(db_result_list_size(key_data_list->result_list), sizeof(key_data_t*)))) {
2283 return NULL;
2284 }
2285 key_data_list->object_list_size = db_result_list_size(key_data_list->result_list);
2286 }
2287 if (!(key_data_list->object_list[0])) {
2288 if (!key_data_list->result_list) {
2289 return NULL;
2290 }
2291 if (!(result = db_result_list_begin(key_data_list->result_list))) {
2292 return NULL;
2293 }
2294 if (!(key_data_list->object_list[0] = key_data_new(db_object_connection(key_data_list->dbo)))) {
2295 return NULL;
2296 }
2297 if (key_data_from_result(key_data_list->object_list[0], result)) {
2298 return NULL;
2299 }
2300 }
2301 key_data_list->object_list_position = 0;
2302 return key_data_list->object_list[0];
2303 }
2304
2305 if (!key_data_list->result_list) {
2306 return NULL;
2307 }
2308
2309 if (!(result = db_result_list_begin(key_data_list->result_list))) {
2310 return NULL;
2311 }
2312 if (!key_data_list->key_data) {
2313 if (!(key_data_list->key_data = key_data_new(db_object_connection(key_data_list->dbo)))) {
2314 return NULL;
2315 }
2316 }
2317 if (key_data_from_result(key_data_list->key_data, result)) {
2318 return NULL;
2319 }
2320 return key_data_list->key_data;
2321 }
2322
key_data_list_get_begin(key_data_list_t * key_data_list)2323 key_data_t* key_data_list_get_begin(key_data_list_t* key_data_list) {
2324 const db_result_t* result;
2325 key_data_t* key_data;
2326
2327 if (!key_data_list) {
2328 return NULL;
2329 }
2330
2331 if (key_data_list->object_store) {
2332 if (!(key_data = key_data_new(db_object_connection(key_data_list->dbo)))) {
2333 return NULL;
2334 }
2335 if (key_data_copy(key_data, key_data_list_begin(key_data_list))) {
2336 key_data_free(key_data);
2337 return NULL;
2338 }
2339 return key_data;
2340 }
2341
2342 if (!key_data_list->result_list) {
2343 return NULL;
2344 }
2345
2346 if (!(result = db_result_list_begin(key_data_list->result_list))) {
2347 return NULL;
2348 }
2349 if (!(key_data = key_data_new(db_object_connection(key_data_list->dbo)))) {
2350 return NULL;
2351 }
2352 if (key_data_from_result(key_data, result)) {
2353 key_data_free(key_data);
2354 return NULL;
2355 }
2356 return key_data;
2357 }
2358
key_data_list_next(key_data_list_t * key_data_list)2359 const key_data_t* key_data_list_next(key_data_list_t* key_data_list) {
2360 const db_result_t* result;
2361
2362 if (!key_data_list) {
2363 return NULL;
2364 }
2365
2366 if (key_data_list->object_store) {
2367 if (!key_data_list->object_list) {
2368 if (!key_data_list->result_list) {
2369 return NULL;
2370 }
2371 if (!db_result_list_size(key_data_list->result_list)) {
2372 return NULL;
2373 }
2374 if (!(key_data_list->object_list = (key_data_t**)calloc(db_result_list_size(key_data_list->result_list), sizeof(key_data_t*)))) {
2375 return NULL;
2376 }
2377 key_data_list->object_list_size = db_result_list_size(key_data_list->result_list);
2378 key_data_list->object_list_position = 0;
2379 }
2380 else if (key_data_list->object_list_first) {
2381 key_data_list->object_list_first = 0;
2382 key_data_list->object_list_position = 0;
2383 }
2384 else {
2385 key_data_list->object_list_position++;
2386 }
2387 if (key_data_list->object_list_position >= key_data_list->object_list_size) {
2388 return NULL;
2389 }
2390 if (!(key_data_list->object_list[key_data_list->object_list_position])) {
2391 if (!key_data_list->result_list) {
2392 return NULL;
2393 }
2394 if (!(result = db_result_list_next(key_data_list->result_list))) {
2395 return NULL;
2396 }
2397 if (!(key_data_list->object_list[key_data_list->object_list_position] = key_data_new(db_object_connection(key_data_list->dbo)))) {
2398 return NULL;
2399 }
2400 if (key_data_from_result(key_data_list->object_list[key_data_list->object_list_position], result)) {
2401 return NULL;
2402 }
2403 }
2404 return key_data_list->object_list[key_data_list->object_list_position];
2405 }
2406
2407 if (!key_data_list->result_list) {
2408 return NULL;
2409 }
2410
2411 if (!(result = db_result_list_next(key_data_list->result_list))) {
2412 return NULL;
2413 }
2414 if (!key_data_list->key_data) {
2415 if (!(key_data_list->key_data = key_data_new(db_object_connection(key_data_list->dbo)))) {
2416 return NULL;
2417 }
2418 }
2419 if (key_data_from_result(key_data_list->key_data, result)) {
2420 return NULL;
2421 }
2422 return key_data_list->key_data;
2423 }
2424
key_data_list_get_next(key_data_list_t * key_data_list)2425 key_data_t* key_data_list_get_next(key_data_list_t* key_data_list) {
2426 const db_result_t* result;
2427 key_data_t* key_data;
2428
2429 if (!key_data_list) {
2430 return NULL;
2431 }
2432
2433 if (key_data_list->object_store) {
2434 if (!(key_data = key_data_new(db_object_connection(key_data_list->dbo)))) {
2435 return NULL;
2436 }
2437 if (key_data_copy(key_data, key_data_list_next(key_data_list))) {
2438 key_data_free(key_data);
2439 return NULL;
2440 }
2441 return key_data;
2442 }
2443
2444 if (!key_data_list->result_list) {
2445 return NULL;
2446 }
2447
2448 if (!(result = db_result_list_next(key_data_list->result_list))) {
2449 return NULL;
2450 }
2451 if (!(key_data = key_data_new(db_object_connection(key_data_list->dbo)))) {
2452 return NULL;
2453 }
2454 if (key_data_from_result(key_data, result)) {
2455 key_data_free(key_data);
2456 return NULL;
2457 }
2458 return key_data;
2459 }
2460
key_data_list_size(key_data_list_t * key_data_list)2461 size_t key_data_list_size(key_data_list_t* key_data_list) {
2462 if (!key_data_list) {
2463 return 0;
2464 }
2465
2466 if (key_data_list->object_store
2467 && key_data_list->object_list)
2468 {
2469 return key_data_list->object_list_size;
2470 }
2471
2472 if (!key_data_list->result_list) {
2473 return 0;
2474 }
2475
2476 return db_result_list_size(key_data_list->result_list);
2477 }
2478
key_data_get_by_hsm_key_id(key_data_t * key_data,const db_value_t * hsm_key_id)2479 int key_data_get_by_hsm_key_id(key_data_t * key_data, const db_value_t* hsm_key_id) {
2480 db_clause_list_t* clause_list;
2481 db_clause_t* clause;
2482 db_result_list_t* result_list;
2483 const db_result_t* result;
2484
2485 if (!key_data) {
2486 return DB_ERROR_UNKNOWN;
2487 }
2488 if (!key_data->dbo) {
2489 return DB_ERROR_UNKNOWN;
2490 }
2491 if (!hsm_key_id) {
2492 return DB_ERROR_UNKNOWN;
2493 }
2494
2495 if (!(clause_list = db_clause_list_new())) {
2496 return DB_ERROR_UNKNOWN;
2497 }
2498 if (!(clause = db_clause_new())
2499 || db_clause_set_field(clause, "hsmKeyId")
2500 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
2501 || db_value_copy(db_clause_get_value(clause), hsm_key_id)
2502 || db_clause_list_add(clause_list, clause))
2503 {
2504 db_clause_free(clause);
2505 db_clause_list_free(clause_list);
2506 return DB_ERROR_UNKNOWN;
2507 }
2508
2509 result_list = db_object_read(key_data->dbo, NULL, clause_list);
2510 db_clause_list_free(clause_list);
2511
2512 if (result_list) {
2513 result = db_result_list_next(result_list);
2514 if (result) {
2515 if (key_data_from_result(key_data, result)) {
2516 db_result_list_free(result_list);
2517 return DB_ERROR_UNKNOWN;
2518 }
2519
2520 db_result_list_free(result_list);
2521 return DB_OK;
2522 }
2523 }
2524
2525 db_result_list_free(result_list);
2526 return DB_ERROR_UNKNOWN;
2527 }
2528
2529
key_data_new_get_by_hsm_key_id(const db_connection_t * connection,const db_value_t * hsm_key_id)2530 key_data_t* key_data_new_get_by_hsm_key_id(const db_connection_t* connection, const db_value_t * hsm_key_id) {
2531 key_data_t* key_data;
2532
2533 if (!connection) {
2534 return NULL;
2535 }
2536 if (!hsm_key_id) {
2537 return NULL;
2538 }
2539
2540 if (!(key_data = key_data_new(connection))
2541 || key_data_get_by_hsm_key_id(key_data, hsm_key_id))
2542 {
2543 key_data_free(key_data);
2544 return NULL;
2545 }
2546
2547 return key_data;
2548 }
2549