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 "hsm_key.h"
31 #include "db_error.h"
32 
33 
34 #include <string.h>
35 
36 const db_enum_t hsm_key_enum_set_state[] = {
37     { "UNUSED", (hsm_key_state_t)HSM_KEY_STATE_UNUSED },
38     { "PRIVATE", (hsm_key_state_t)HSM_KEY_STATE_PRIVATE },
39     { "SHARED", (hsm_key_state_t)HSM_KEY_STATE_SHARED },
40     { "DELETE", (hsm_key_state_t)HSM_KEY_STATE_DELETE },
41     { NULL, 0 }
42 };
43 
44 const db_enum_t hsm_key_enum_set_role[] = {
45     { "KSK", (hsm_key_role_t)HSM_KEY_ROLE_KSK },
46     { "ZSK", (hsm_key_role_t)HSM_KEY_ROLE_ZSK },
47     { "CSK", (hsm_key_role_t)HSM_KEY_ROLE_CSK },
48     { NULL, 0 }
49 };
50 
51 const db_enum_t hsm_key_enum_set_key_type[] = {
52     { "RSA", (hsm_key_key_type_t)HSM_KEY_KEY_TYPE_RSA },
53     { NULL, 0 }
54 };
55 
56 const db_enum_t hsm_key_enum_set_backup[] = {
57     { "Not Required", (hsm_key_backup_t)HSM_KEY_BACKUP_NO_BACKUP },
58     { "Required", (hsm_key_backup_t)HSM_KEY_BACKUP_BACKUP_REQUIRED },
59     { "Prepared", (hsm_key_backup_t)HSM_KEY_BACKUP_BACKUP_REQUESTED },
60     { "Done", (hsm_key_backup_t)HSM_KEY_BACKUP_BACKUP_DONE },
61     { NULL, 0 }
62 };
63 
64 /**
65  * Create a new hsm key object.
66  * \param[in] connection a db_connection_t pointer.
67  * \return a hsm_key_t pointer or NULL on error.
68  */
__hsm_key_new_object(const db_connection_t * connection)69 static db_object_t* __hsm_key_new_object(const db_connection_t* connection) {
70     db_object_field_list_t* object_field_list;
71     db_object_field_t* object_field;
72     db_object_t* object;
73 
74     if (!(object = db_object_new())
75         || db_object_set_connection(object, connection)
76         || db_object_set_table(object, "hsmKey")
77         || db_object_set_primary_key_name(object, "id")
78         || !(object_field_list = db_object_field_list_new()))
79     {
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, "id")
86         || db_object_field_set_type(object_field, DB_TYPE_PRIMARY_KEY)
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, "rev")
97         || db_object_field_set_type(object_field, DB_TYPE_REVISION)
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, "policyId")
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, "locator")
119         || db_object_field_set_type(object_field, DB_TYPE_TEXT)
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, "state")
130         || db_object_field_set_type(object_field, DB_TYPE_ENUM)
131         || db_object_field_set_enum_set(object_field, hsm_key_enum_set_state)
132         || db_object_field_list_add(object_field_list, object_field))
133     {
134         db_object_field_free(object_field);
135         db_object_field_list_free(object_field_list);
136         db_object_free(object);
137         return NULL;
138     }
139 
140     if (!(object_field = db_object_field_new())
141         || db_object_field_set_name(object_field, "bits")
142         || db_object_field_set_type(object_field, DB_TYPE_UINT32)
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, "algorithm")
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, "role")
164         || db_object_field_set_type(object_field, DB_TYPE_ENUM)
165         || db_object_field_set_enum_set(object_field, hsm_key_enum_set_role)
166         || db_object_field_list_add(object_field_list, object_field))
167     {
168         db_object_field_free(object_field);
169         db_object_field_list_free(object_field_list);
170         db_object_free(object);
171         return NULL;
172     }
173 
174     if (!(object_field = db_object_field_new())
175         || db_object_field_set_name(object_field, "inception")
176         || db_object_field_set_type(object_field, DB_TYPE_UINT32)
177         || db_object_field_list_add(object_field_list, object_field))
178     {
179         db_object_field_free(object_field);
180         db_object_field_list_free(object_field_list);
181         db_object_free(object);
182         return NULL;
183     }
184 
185     if (!(object_field = db_object_field_new())
186         || db_object_field_set_name(object_field, "isRevoked")
187         || db_object_field_set_type(object_field, DB_TYPE_UINT32)
188         || db_object_field_list_add(object_field_list, object_field))
189     {
190         db_object_field_free(object_field);
191         db_object_field_list_free(object_field_list);
192         db_object_free(object);
193         return NULL;
194     }
195 
196     if (!(object_field = db_object_field_new())
197         || db_object_field_set_name(object_field, "keyType")
198         || db_object_field_set_type(object_field, DB_TYPE_ENUM)
199         || db_object_field_set_enum_set(object_field, hsm_key_enum_set_key_type)
200         || db_object_field_list_add(object_field_list, object_field))
201     {
202         db_object_field_free(object_field);
203         db_object_field_list_free(object_field_list);
204         db_object_free(object);
205         return NULL;
206     }
207 
208     if (!(object_field = db_object_field_new())
209         || db_object_field_set_name(object_field, "repository")
210         || db_object_field_set_type(object_field, DB_TYPE_TEXT)
211         || db_object_field_list_add(object_field_list, object_field))
212     {
213         db_object_field_free(object_field);
214         db_object_field_list_free(object_field_list);
215         db_object_free(object);
216         return NULL;
217     }
218 
219     if (!(object_field = db_object_field_new())
220         || db_object_field_set_name(object_field, "backup")
221         || db_object_field_set_type(object_field, DB_TYPE_ENUM)
222         || db_object_field_set_enum_set(object_field, hsm_key_enum_set_backup)
223         || db_object_field_list_add(object_field_list, object_field))
224     {
225         db_object_field_free(object_field);
226         db_object_field_list_free(object_field_list);
227         db_object_free(object);
228         return NULL;
229     }
230 
231     if (db_object_set_object_field_list(object, object_field_list)) {
232         db_object_field_list_free(object_field_list);
233         db_object_free(object);
234         return NULL;
235     }
236 
237     return object;
238 }
239 
240 /* HSM KEY */
241 
242 
243 
hsm_key_new(const db_connection_t * connection)244 hsm_key_t* hsm_key_new(const db_connection_t* connection) {
245     hsm_key_t* hsm_key =
246         (hsm_key_t*)calloc(1, sizeof(hsm_key_t));
247 
248     if (hsm_key) {
249         if (!(hsm_key->dbo = __hsm_key_new_object(connection))) {
250             free(hsm_key);
251             return NULL;
252         }
253         db_value_reset(&(hsm_key->id));
254         db_value_reset(&(hsm_key->rev));
255         db_value_reset(&(hsm_key->policy_id));
256         hsm_key->state = HSM_KEY_STATE_UNUSED;
257         hsm_key->bits = 2048;
258         hsm_key->algorithm = 1;
259         hsm_key->role = HSM_KEY_ROLE_ZSK;
260         hsm_key->key_type = HSM_KEY_KEY_TYPE_RSA;
261         hsm_key->backup = HSM_KEY_BACKUP_NO_BACKUP;
262     }
263 
264     return hsm_key;
265 }
266 
hsm_key_new_copy(const hsm_key_t * hsm_key)267 hsm_key_t* hsm_key_new_copy(const hsm_key_t* hsm_key) {
268     hsm_key_t* new_hsm_key;
269 
270     if (!hsm_key) {
271         return NULL;
272     }
273     if (!hsm_key->dbo) {
274         return NULL;
275     }
276 
277     if (!(new_hsm_key = hsm_key_new(db_object_connection(hsm_key->dbo)))
278         || hsm_key_copy(new_hsm_key, hsm_key))
279     {
280         hsm_key_free(new_hsm_key);
281         return NULL;
282     }
283     return new_hsm_key;
284 }
285 
hsm_key_free(hsm_key_t * hsm_key)286 void hsm_key_free(hsm_key_t* hsm_key) {
287     if (hsm_key) {
288         if (hsm_key->dbo) {
289             db_object_free(hsm_key->dbo);
290         }
291         db_value_reset(&(hsm_key->id));
292         db_value_reset(&(hsm_key->rev));
293         db_value_reset(&(hsm_key->policy_id));
294         if (hsm_key->private_policy_id) {
295             policy_free(hsm_key->private_policy_id);
296         }
297         if (hsm_key->locator) {
298             free(hsm_key->locator);
299         }
300         if (hsm_key->repository) {
301             free(hsm_key->repository);
302         }
303         free(hsm_key);
304     }
305 }
306 
hsm_key_copy(hsm_key_t * hsm_key,const hsm_key_t * hsm_key_copy)307 int hsm_key_copy(hsm_key_t* hsm_key, const hsm_key_t* hsm_key_copy) {
308     char* locator_text = NULL;
309     char* repository_text = NULL;
310 
311     if (!hsm_key) {
312         return DB_ERROR_UNKNOWN;
313     }
314     if (!hsm_key_copy) {
315         return DB_ERROR_UNKNOWN;
316     }
317 
318     if (hsm_key_copy->locator) {
319         if (!(locator_text = strdup(hsm_key_copy->locator))) {
320             return DB_ERROR_UNKNOWN;
321         }
322     }
323     if (hsm_key_copy->repository) {
324         if (!(repository_text = strdup(hsm_key_copy->repository))) {
325             if (locator_text) {
326                 free(locator_text);
327             }
328             return DB_ERROR_UNKNOWN;
329         }
330     }
331     if (db_value_copy(&(hsm_key->id), &(hsm_key_copy->id))) {
332         if (locator_text) {
333             free(locator_text);
334         }
335         if (repository_text) {
336             free(repository_text);
337         }
338         return DB_ERROR_UNKNOWN;
339     }
340     if (db_value_copy(&(hsm_key->rev), &(hsm_key_copy->rev))) {
341         if (locator_text) {
342             free(locator_text);
343         }
344         if (repository_text) {
345             free(repository_text);
346         }
347         return DB_ERROR_UNKNOWN;
348     }
349     if (db_value_copy(&(hsm_key->policy_id), &(hsm_key_copy->policy_id))) {
350         if (locator_text) {
351             free(locator_text);
352         }
353         if (repository_text) {
354             free(repository_text);
355         }
356         return DB_ERROR_UNKNOWN;
357     }
358     if (hsm_key->private_policy_id) {
359         policy_free(hsm_key->private_policy_id);
360         hsm_key->private_policy_id = NULL;
361     }
362     if (hsm_key_copy->private_policy_id
363         && !(hsm_key->private_policy_id = policy_new_copy(hsm_key_copy->private_policy_id)))
364     {
365         if (locator_text) {
366             free(locator_text);
367         }
368         if (repository_text) {
369             free(repository_text);
370         }
371         return DB_ERROR_UNKNOWN;
372     }
373     hsm_key->associated_policy_id = NULL;
374     if (!hsm_key_copy->private_policy_id
375         && hsm_key_copy->associated_policy_id
376         && !(hsm_key->private_policy_id = policy_new_copy(hsm_key_copy->associated_policy_id)))
377     {
378         if (locator_text) {
379             free(locator_text);
380         }
381         if (repository_text) {
382             free(repository_text);
383         }
384         return DB_ERROR_UNKNOWN;
385     }
386     if (hsm_key->locator) {
387         free(hsm_key->locator);
388     }
389     hsm_key->locator = locator_text;
390     hsm_key->state = hsm_key_copy->state;
391     hsm_key->bits = hsm_key_copy->bits;
392     hsm_key->algorithm = hsm_key_copy->algorithm;
393     hsm_key->role = hsm_key_copy->role;
394     hsm_key->inception = hsm_key_copy->inception;
395     hsm_key->is_revoked = hsm_key_copy->is_revoked;
396     hsm_key->key_type = hsm_key_copy->key_type;
397     if (hsm_key->repository) {
398         free(hsm_key->repository);
399     }
400     hsm_key->repository = repository_text;
401     hsm_key->backup = hsm_key_copy->backup;
402     return DB_OK;
403 }
404 
hsm_key_from_result(hsm_key_t * hsm_key,const db_result_t * result)405 int hsm_key_from_result(hsm_key_t* hsm_key, const db_result_t* result) {
406     const db_value_set_t* value_set;
407     int state;
408     int role;
409     int key_type;
410     int backup;
411 
412     if (!hsm_key) {
413         return DB_ERROR_UNKNOWN;
414     }
415     if (!result) {
416         return DB_ERROR_UNKNOWN;
417     }
418 
419     db_value_reset(&(hsm_key->id));
420     db_value_reset(&(hsm_key->rev));
421     db_value_reset(&(hsm_key->policy_id));
422     if (hsm_key->locator) {
423         free(hsm_key->locator);
424     }
425     hsm_key->locator = NULL;
426     if (hsm_key->repository) {
427         free(hsm_key->repository);
428     }
429     hsm_key->repository = NULL;
430     if (!(value_set = db_result_value_set(result))
431         || db_value_set_size(value_set) != 13
432         || db_value_copy(&(hsm_key->id), db_value_set_at(value_set, 0))
433         || db_value_copy(&(hsm_key->rev), db_value_set_at(value_set, 1))
434         || db_value_copy(&(hsm_key->policy_id), db_value_set_at(value_set, 2))
435         || db_value_to_text(db_value_set_at(value_set, 3), &(hsm_key->locator))
436         || db_value_to_enum_value(db_value_set_at(value_set, 4), &state, hsm_key_enum_set_state)
437         || db_value_to_uint32(db_value_set_at(value_set, 5), &(hsm_key->bits))
438         || db_value_to_uint32(db_value_set_at(value_set, 6), &(hsm_key->algorithm))
439         || db_value_to_enum_value(db_value_set_at(value_set, 7), &role, hsm_key_enum_set_role)
440         || db_value_to_uint32(db_value_set_at(value_set, 8), &(hsm_key->inception))
441         || db_value_to_uint32(db_value_set_at(value_set, 9), &(hsm_key->is_revoked))
442         || db_value_to_enum_value(db_value_set_at(value_set, 10), &key_type, hsm_key_enum_set_key_type)
443         || db_value_to_text(db_value_set_at(value_set, 11), &(hsm_key->repository))
444         || db_value_to_enum_value(db_value_set_at(value_set, 12), &backup, hsm_key_enum_set_backup))
445     {
446         return DB_ERROR_UNKNOWN;
447     }
448 
449     if (state == (hsm_key_state_t)HSM_KEY_STATE_UNUSED) {
450         hsm_key->state = HSM_KEY_STATE_UNUSED;
451     }
452     else if (state == (hsm_key_state_t)HSM_KEY_STATE_PRIVATE) {
453         hsm_key->state = HSM_KEY_STATE_PRIVATE;
454     }
455     else if (state == (hsm_key_state_t)HSM_KEY_STATE_SHARED) {
456         hsm_key->state = HSM_KEY_STATE_SHARED;
457     }
458     else if (state == (hsm_key_state_t)HSM_KEY_STATE_DELETE) {
459         hsm_key->state = HSM_KEY_STATE_DELETE;
460     }
461     else {
462         return DB_ERROR_UNKNOWN;
463     }
464 
465     if (role == (hsm_key_role_t)HSM_KEY_ROLE_KSK) {
466         hsm_key->role = HSM_KEY_ROLE_KSK;
467     }
468     else if (role == (hsm_key_role_t)HSM_KEY_ROLE_ZSK) {
469         hsm_key->role = HSM_KEY_ROLE_ZSK;
470     }
471     else if (role == (hsm_key_role_t)HSM_KEY_ROLE_CSK) {
472         hsm_key->role = HSM_KEY_ROLE_CSK;
473     }
474     else {
475         return DB_ERROR_UNKNOWN;
476     }
477 
478     if (key_type == (hsm_key_key_type_t)HSM_KEY_KEY_TYPE_RSA) {
479         hsm_key->key_type = HSM_KEY_KEY_TYPE_RSA;
480     }
481     else {
482         return DB_ERROR_UNKNOWN;
483     }
484 
485     if (backup == (hsm_key_backup_t)HSM_KEY_BACKUP_NO_BACKUP) {
486         hsm_key->backup = HSM_KEY_BACKUP_NO_BACKUP;
487     }
488     else if (backup == (hsm_key_backup_t)HSM_KEY_BACKUP_BACKUP_REQUIRED) {
489         hsm_key->backup = HSM_KEY_BACKUP_BACKUP_REQUIRED;
490     }
491     else if (backup == (hsm_key_backup_t)HSM_KEY_BACKUP_BACKUP_REQUESTED) {
492         hsm_key->backup = HSM_KEY_BACKUP_BACKUP_REQUESTED;
493     }
494     else if (backup == (hsm_key_backup_t)HSM_KEY_BACKUP_BACKUP_DONE) {
495         hsm_key->backup = HSM_KEY_BACKUP_BACKUP_DONE;
496     }
497     else {
498         return DB_ERROR_UNKNOWN;
499     }
500 
501     return DB_OK;
502 }
503 
hsm_key_id(const hsm_key_t * hsm_key)504 const db_value_t* hsm_key_id(const hsm_key_t* hsm_key) {
505     if (!hsm_key) {
506         return NULL;
507     }
508 
509     return &(hsm_key->id);
510 }
511 
hsm_key_policy_id(const hsm_key_t * hsm_key)512 const db_value_t* hsm_key_policy_id(const hsm_key_t* hsm_key) {
513     if (!hsm_key) {
514         return NULL;
515     }
516 
517     return &(hsm_key->policy_id);
518 }
519 
hsm_key_locator(const hsm_key_t * hsm_key)520 const char* hsm_key_locator(const hsm_key_t* hsm_key) {
521     if (!hsm_key) {
522         return NULL;
523     }
524 
525     return hsm_key->locator;
526 }
527 
hsm_key_state(const hsm_key_t * hsm_key)528 hsm_key_state_t hsm_key_state(const hsm_key_t* hsm_key) {
529     if (!hsm_key) {
530         return HSM_KEY_STATE_INVALID;
531     }
532 
533     return hsm_key->state;
534 }
535 
hsm_key_bits(const hsm_key_t * hsm_key)536 unsigned int hsm_key_bits(const hsm_key_t* hsm_key) {
537     if (!hsm_key) {
538         return 0;
539     }
540 
541     return hsm_key->bits;
542 }
543 
hsm_key_algorithm(const hsm_key_t * hsm_key)544 unsigned int hsm_key_algorithm(const hsm_key_t* hsm_key) {
545     if (!hsm_key) {
546         return 0;
547     }
548 
549     return hsm_key->algorithm;
550 }
551 
hsm_key_role(const hsm_key_t * hsm_key)552 hsm_key_role_t hsm_key_role(const hsm_key_t* hsm_key) {
553     if (!hsm_key) {
554         return HSM_KEY_ROLE_INVALID;
555     }
556 
557     return hsm_key->role;
558 }
559 
hsm_key_inception(const hsm_key_t * hsm_key)560 unsigned int hsm_key_inception(const hsm_key_t* hsm_key) {
561     if (!hsm_key) {
562         return 0;
563     }
564 
565     return hsm_key->inception;
566 }
567 
hsm_key_repository(const hsm_key_t * hsm_key)568 const char* hsm_key_repository(const hsm_key_t* hsm_key) {
569     if (!hsm_key) {
570         return NULL;
571     }
572 
573     return hsm_key->repository;
574 }
575 
hsm_key_backup(const hsm_key_t * hsm_key)576 hsm_key_backup_t hsm_key_backup(const hsm_key_t* hsm_key) {
577     if (!hsm_key) {
578         return HSM_KEY_BACKUP_INVALID;
579     }
580 
581     return hsm_key->backup;
582 }
583 
hsm_key_set_policy_id(hsm_key_t * hsm_key,const db_value_t * policy_id)584 int hsm_key_set_policy_id(hsm_key_t* hsm_key, const db_value_t* policy_id) {
585     if (!hsm_key) {
586         return DB_ERROR_UNKNOWN;
587     }
588     if (!policy_id) {
589         return DB_ERROR_UNKNOWN;
590     }
591     if (db_value_not_empty(policy_id)) {
592         return DB_ERROR_UNKNOWN;
593     }
594 
595     db_value_reset(&(hsm_key->policy_id));
596     if (db_value_copy(&(hsm_key->policy_id), policy_id)) {
597         return DB_ERROR_UNKNOWN;
598     }
599 
600     return DB_OK;
601 }
602 
hsm_key_set_locator(hsm_key_t * hsm_key,const char * locator_text)603 int hsm_key_set_locator(hsm_key_t* hsm_key, const char* locator_text) {
604     char* new_locator;
605 
606     if (!hsm_key) {
607         return DB_ERROR_UNKNOWN;
608     }
609     if (!locator_text) {
610         return DB_ERROR_UNKNOWN;
611     }
612 
613     if (!(new_locator = strdup(locator_text))) {
614         return DB_ERROR_UNKNOWN;
615     }
616 
617     if (hsm_key->locator) {
618         free(hsm_key->locator);
619     }
620     hsm_key->locator = new_locator;
621 
622     return DB_OK;
623 }
624 
hsm_key_set_state(hsm_key_t * hsm_key,hsm_key_state_t state)625 int hsm_key_set_state(hsm_key_t* hsm_key, hsm_key_state_t state) {
626     if (!hsm_key) {
627         return DB_ERROR_UNKNOWN;
628     }
629     if (state == HSM_KEY_STATE_INVALID) {
630         return DB_ERROR_UNKNOWN;
631     }
632 
633     hsm_key->state = state;
634 
635     return DB_OK;
636 }
637 
hsm_key_set_bits(hsm_key_t * hsm_key,unsigned int bits)638 int hsm_key_set_bits(hsm_key_t* hsm_key, unsigned int bits) {
639     if (!hsm_key) {
640         return DB_ERROR_UNKNOWN;
641     }
642 
643     hsm_key->bits = bits;
644 
645     return DB_OK;
646 }
647 
hsm_key_set_algorithm(hsm_key_t * hsm_key,unsigned int algorithm)648 int hsm_key_set_algorithm(hsm_key_t* hsm_key, unsigned int algorithm) {
649     if (!hsm_key) {
650         return DB_ERROR_UNKNOWN;
651     }
652 
653     hsm_key->algorithm = algorithm;
654 
655     return DB_OK;
656 }
657 
hsm_key_set_role(hsm_key_t * hsm_key,hsm_key_role_t role)658 int hsm_key_set_role(hsm_key_t* hsm_key, hsm_key_role_t role) {
659     if (!hsm_key) {
660         return DB_ERROR_UNKNOWN;
661     }
662     if (role == HSM_KEY_ROLE_INVALID) {
663         return DB_ERROR_UNKNOWN;
664     }
665 
666     hsm_key->role = role;
667 
668     return DB_OK;
669 }
670 
hsm_key_set_inception(hsm_key_t * hsm_key,unsigned int inception)671 int hsm_key_set_inception(hsm_key_t* hsm_key, unsigned int inception) {
672     if (!hsm_key) {
673         return DB_ERROR_UNKNOWN;
674     }
675 
676     hsm_key->inception = inception;
677 
678     return DB_OK;
679 }
680 
hsm_key_set_key_type(hsm_key_t * hsm_key,hsm_key_key_type_t key_type)681 int hsm_key_set_key_type(hsm_key_t* hsm_key, hsm_key_key_type_t key_type) {
682     if (!hsm_key) {
683         return DB_ERROR_UNKNOWN;
684     }
685     if (key_type == HSM_KEY_KEY_TYPE_INVALID) {
686         return DB_ERROR_UNKNOWN;
687     }
688 
689     hsm_key->key_type = key_type;
690 
691     return DB_OK;
692 }
693 
hsm_key_set_repository(hsm_key_t * hsm_key,const char * repository_text)694 int hsm_key_set_repository(hsm_key_t* hsm_key, const char* repository_text) {
695     char* new_repository;
696 
697     if (!hsm_key) {
698         return DB_ERROR_UNKNOWN;
699     }
700     if (!repository_text) {
701         return DB_ERROR_UNKNOWN;
702     }
703 
704     if (!(new_repository = strdup(repository_text))) {
705         return DB_ERROR_UNKNOWN;
706     }
707 
708     if (hsm_key->repository) {
709         free(hsm_key->repository);
710     }
711     hsm_key->repository = new_repository;
712 
713     return DB_OK;
714 }
715 
hsm_key_set_backup(hsm_key_t * hsm_key,hsm_key_backup_t backup)716 int hsm_key_set_backup(hsm_key_t* hsm_key, hsm_key_backup_t backup) {
717     if (!hsm_key) {
718         return DB_ERROR_UNKNOWN;
719     }
720     if (backup == HSM_KEY_BACKUP_INVALID) {
721         return DB_ERROR_UNKNOWN;
722     }
723 
724     hsm_key->backup = backup;
725 
726     return DB_OK;
727 }
728 
hsm_key_policy_id_clause(db_clause_list_t * clause_list,const db_value_t * policy_id)729 db_clause_t* hsm_key_policy_id_clause(db_clause_list_t* clause_list, const db_value_t* policy_id) {
730     db_clause_t* clause;
731 
732     if (!clause_list) {
733         return NULL;
734     }
735     if (!policy_id) {
736         return NULL;
737     }
738     if (db_value_not_empty(policy_id)) {
739         return NULL;
740     }
741 
742     if (!(clause = db_clause_new())
743         || db_clause_set_field(clause, "policyId")
744         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
745         || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_AND)
746         || db_value_copy(db_clause_get_value(clause), policy_id)
747         || db_clause_list_add(clause_list, clause))
748     {
749         db_clause_free(clause);
750         return NULL;
751     }
752 
753     return clause;
754 }
755 
hsm_key_state_clause(db_clause_list_t * clause_list,hsm_key_state_t state)756 db_clause_t* hsm_key_state_clause(db_clause_list_t* clause_list, hsm_key_state_t state) {
757     db_clause_t* clause;
758 
759     if (!clause_list) {
760         return NULL;
761     }
762 
763     if (!(clause = db_clause_new())
764         || db_clause_set_field(clause, "state")
765         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
766         || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_AND)
767         || db_value_from_enum_value(db_clause_get_value(clause), state, hsm_key_enum_set_state)
768         || db_clause_list_add(clause_list, clause))
769     {
770         db_clause_free(clause);
771         return NULL;
772     }
773 
774     return clause;
775 }
776 
hsm_key_bits_clause(db_clause_list_t * clause_list,unsigned int bits)777 db_clause_t* hsm_key_bits_clause(db_clause_list_t* clause_list, unsigned int bits) {
778     db_clause_t* clause;
779 
780     if (!clause_list) {
781         return NULL;
782     }
783 
784     if (!(clause = db_clause_new())
785         || db_clause_set_field(clause, "bits")
786         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
787         || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_AND)
788         || db_value_from_uint32(db_clause_get_value(clause), bits)
789         || db_clause_list_add(clause_list, clause))
790     {
791         db_clause_free(clause);
792         return NULL;
793     }
794 
795     return clause;
796 }
797 
hsm_key_algorithm_clause(db_clause_list_t * clause_list,unsigned int algorithm)798 db_clause_t* hsm_key_algorithm_clause(db_clause_list_t* clause_list, unsigned int algorithm) {
799     db_clause_t* clause;
800 
801     if (!clause_list) {
802         return NULL;
803     }
804 
805     if (!(clause = db_clause_new())
806         || db_clause_set_field(clause, "algorithm")
807         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
808         || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_AND)
809         || db_value_from_uint32(db_clause_get_value(clause), algorithm)
810         || db_clause_list_add(clause_list, clause))
811     {
812         db_clause_free(clause);
813         return NULL;
814     }
815 
816     return clause;
817 }
818 
hsm_key_role_clause(db_clause_list_t * clause_list,hsm_key_role_t role)819 db_clause_t* hsm_key_role_clause(db_clause_list_t* clause_list, hsm_key_role_t role) {
820     db_clause_t* clause;
821 
822     if (!clause_list) {
823         return NULL;
824     }
825 
826     if (!(clause = db_clause_new())
827         || db_clause_set_field(clause, "role")
828         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
829         || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_AND)
830         || db_value_from_enum_value(db_clause_get_value(clause), role, hsm_key_enum_set_role)
831         || db_clause_list_add(clause_list, clause))
832     {
833         db_clause_free(clause);
834         return NULL;
835     }
836 
837     return clause;
838 }
839 
hsm_key_is_revoked_clause(db_clause_list_t * clause_list,unsigned int is_revoked)840 db_clause_t* hsm_key_is_revoked_clause(db_clause_list_t* clause_list, unsigned int is_revoked) {
841     db_clause_t* clause;
842 
843     if (!clause_list) {
844         return NULL;
845     }
846 
847     if (!(clause = db_clause_new())
848         || db_clause_set_field(clause, "isRevoked")
849         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
850         || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_AND)
851         || db_value_from_uint32(db_clause_get_value(clause), is_revoked)
852         || db_clause_list_add(clause_list, clause))
853     {
854         db_clause_free(clause);
855         return NULL;
856     }
857 
858     return clause;
859 }
860 
hsm_key_key_type_clause(db_clause_list_t * clause_list,hsm_key_key_type_t key_type)861 db_clause_t* hsm_key_key_type_clause(db_clause_list_t* clause_list, hsm_key_key_type_t key_type) {
862     db_clause_t* clause;
863 
864     if (!clause_list) {
865         return NULL;
866     }
867 
868     if (!(clause = db_clause_new())
869         || db_clause_set_field(clause, "keyType")
870         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
871         || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_AND)
872         || db_value_from_enum_value(db_clause_get_value(clause), key_type, hsm_key_enum_set_key_type)
873         || db_clause_list_add(clause_list, clause))
874     {
875         db_clause_free(clause);
876         return NULL;
877     }
878 
879     return clause;
880 }
881 
hsm_key_repository_clause(db_clause_list_t * clause_list,const char * repository_text)882 db_clause_t* hsm_key_repository_clause(db_clause_list_t* clause_list, const char* repository_text) {
883     db_clause_t* clause;
884 
885     if (!clause_list) {
886         return NULL;
887     }
888     if (!repository_text) {
889         return NULL;
890     }
891 
892     if (!(clause = db_clause_new())
893         || db_clause_set_field(clause, "repository")
894         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
895         || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_AND)
896         || db_value_from_text(db_clause_get_value(clause), repository_text)
897         || db_clause_list_add(clause_list, clause))
898     {
899         db_clause_free(clause);
900         return NULL;
901     }
902 
903     return clause;
904 }
905 
hsm_key_backup_clause(db_clause_list_t * clause_list,hsm_key_backup_t backup)906 db_clause_t* hsm_key_backup_clause(db_clause_list_t* clause_list, hsm_key_backup_t backup) {
907     db_clause_t* clause;
908 
909     if (!clause_list) {
910         return NULL;
911     }
912 
913     if (!(clause = db_clause_new())
914         || db_clause_set_field(clause, "backup")
915         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
916         || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_AND)
917         || db_value_from_enum_value(db_clause_get_value(clause), backup, hsm_key_enum_set_backup)
918         || db_clause_list_add(clause_list, clause))
919     {
920         db_clause_free(clause);
921         return NULL;
922     }
923 
924     return clause;
925 }
926 
hsm_key_create(hsm_key_t * hsm_key)927 int hsm_key_create(hsm_key_t* hsm_key) {
928     db_object_field_list_t* object_field_list;
929     db_object_field_t* object_field;
930     db_value_set_t* value_set;
931     int ret;
932 
933     if (!hsm_key) {
934         return DB_ERROR_UNKNOWN;
935     }
936     if (!hsm_key->dbo) {
937         return DB_ERROR_UNKNOWN;
938     }
939     if (!db_value_not_empty(&(hsm_key->id))) {
940         return DB_ERROR_UNKNOWN;
941     }
942     if (!db_value_not_empty(&(hsm_key->rev))) {
943         return DB_ERROR_UNKNOWN;
944     }
945     if (db_value_not_empty(&(hsm_key->policy_id))) {
946         return DB_ERROR_UNKNOWN;
947     }
948     if (!hsm_key->locator) {
949         return DB_ERROR_UNKNOWN;
950     }
951     if (!hsm_key->repository) {
952         return DB_ERROR_UNKNOWN;
953     }
954     /* TODO: validate content more */
955 
956     if (!(object_field_list = db_object_field_list_new())) {
957         return DB_ERROR_UNKNOWN;
958     }
959 
960     if (!(object_field = db_object_field_new())
961         || db_object_field_set_name(object_field, "policyId")
962         || db_object_field_set_type(object_field, DB_TYPE_ANY)
963         || db_object_field_list_add(object_field_list, object_field))
964     {
965         db_object_field_free(object_field);
966         db_object_field_list_free(object_field_list);
967         return DB_ERROR_UNKNOWN;
968     }
969 
970     if (!(object_field = db_object_field_new())
971         || db_object_field_set_name(object_field, "locator")
972         || db_object_field_set_type(object_field, DB_TYPE_TEXT)
973         || db_object_field_list_add(object_field_list, object_field))
974     {
975         db_object_field_free(object_field);
976         db_object_field_list_free(object_field_list);
977         return DB_ERROR_UNKNOWN;
978     }
979 
980     if (!(object_field = db_object_field_new())
981         || db_object_field_set_name(object_field, "state")
982         || db_object_field_set_type(object_field, DB_TYPE_ENUM)
983         || db_object_field_set_enum_set(object_field, hsm_key_enum_set_state)
984         || db_object_field_list_add(object_field_list, object_field))
985     {
986         db_object_field_free(object_field);
987         db_object_field_list_free(object_field_list);
988         return DB_ERROR_UNKNOWN;
989     }
990 
991     if (!(object_field = db_object_field_new())
992         || db_object_field_set_name(object_field, "bits")
993         || db_object_field_set_type(object_field, DB_TYPE_UINT32)
994         || db_object_field_list_add(object_field_list, object_field))
995     {
996         db_object_field_free(object_field);
997         db_object_field_list_free(object_field_list);
998         return DB_ERROR_UNKNOWN;
999     }
1000 
1001     if (!(object_field = db_object_field_new())
1002         || db_object_field_set_name(object_field, "algorithm")
1003         || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1004         || db_object_field_list_add(object_field_list, object_field))
1005     {
1006         db_object_field_free(object_field);
1007         db_object_field_list_free(object_field_list);
1008         return DB_ERROR_UNKNOWN;
1009     }
1010 
1011     if (!(object_field = db_object_field_new())
1012         || db_object_field_set_name(object_field, "role")
1013         || db_object_field_set_type(object_field, DB_TYPE_ENUM)
1014         || db_object_field_set_enum_set(object_field, hsm_key_enum_set_role)
1015         || db_object_field_list_add(object_field_list, object_field))
1016     {
1017         db_object_field_free(object_field);
1018         db_object_field_list_free(object_field_list);
1019         return DB_ERROR_UNKNOWN;
1020     }
1021 
1022     if (!(object_field = db_object_field_new())
1023         || db_object_field_set_name(object_field, "inception")
1024         || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1025         || db_object_field_list_add(object_field_list, object_field))
1026     {
1027         db_object_field_free(object_field);
1028         db_object_field_list_free(object_field_list);
1029         return DB_ERROR_UNKNOWN;
1030     }
1031 
1032     if (!(object_field = db_object_field_new())
1033         || db_object_field_set_name(object_field, "isRevoked")
1034         || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1035         || db_object_field_list_add(object_field_list, object_field))
1036     {
1037         db_object_field_free(object_field);
1038         db_object_field_list_free(object_field_list);
1039         return DB_ERROR_UNKNOWN;
1040     }
1041 
1042     if (!(object_field = db_object_field_new())
1043         || db_object_field_set_name(object_field, "keyType")
1044         || db_object_field_set_type(object_field, DB_TYPE_ENUM)
1045         || db_object_field_set_enum_set(object_field, hsm_key_enum_set_key_type)
1046         || db_object_field_list_add(object_field_list, object_field))
1047     {
1048         db_object_field_free(object_field);
1049         db_object_field_list_free(object_field_list);
1050         return DB_ERROR_UNKNOWN;
1051     }
1052 
1053     if (!(object_field = db_object_field_new())
1054         || db_object_field_set_name(object_field, "repository")
1055         || db_object_field_set_type(object_field, DB_TYPE_TEXT)
1056         || db_object_field_list_add(object_field_list, object_field))
1057     {
1058         db_object_field_free(object_field);
1059         db_object_field_list_free(object_field_list);
1060         return DB_ERROR_UNKNOWN;
1061     }
1062 
1063     if (!(object_field = db_object_field_new())
1064         || db_object_field_set_name(object_field, "backup")
1065         || db_object_field_set_type(object_field, DB_TYPE_ENUM)
1066         || db_object_field_set_enum_set(object_field, hsm_key_enum_set_backup)
1067         || db_object_field_list_add(object_field_list, object_field))
1068     {
1069         db_object_field_free(object_field);
1070         db_object_field_list_free(object_field_list);
1071         return DB_ERROR_UNKNOWN;
1072     }
1073 
1074     if (!(value_set = db_value_set_new(11))) {
1075         db_object_field_list_free(object_field_list);
1076         return DB_ERROR_UNKNOWN;
1077     }
1078 
1079     if (db_value_copy(db_value_set_get(value_set, 0), &(hsm_key->policy_id))
1080         || db_value_from_text(db_value_set_get(value_set, 1), hsm_key->locator)
1081         || db_value_from_enum_value(db_value_set_get(value_set, 2), hsm_key->state, hsm_key_enum_set_state)
1082         || db_value_from_uint32(db_value_set_get(value_set, 3), hsm_key->bits)
1083         || db_value_from_uint32(db_value_set_get(value_set, 4), hsm_key->algorithm)
1084         || db_value_from_enum_value(db_value_set_get(value_set, 5), hsm_key->role, hsm_key_enum_set_role)
1085         || db_value_from_uint32(db_value_set_get(value_set, 6), hsm_key->inception)
1086         || db_value_from_uint32(db_value_set_get(value_set, 7), hsm_key->is_revoked)
1087         || db_value_from_enum_value(db_value_set_get(value_set, 8), hsm_key->key_type, hsm_key_enum_set_key_type)
1088         || db_value_from_text(db_value_set_get(value_set, 9), hsm_key->repository)
1089         || db_value_from_enum_value(db_value_set_get(value_set, 10), hsm_key->backup, hsm_key_enum_set_backup))
1090     {
1091         db_value_set_free(value_set);
1092         db_object_field_list_free(object_field_list);
1093         return DB_ERROR_UNKNOWN;
1094     }
1095 
1096     ret = db_object_create(hsm_key->dbo, object_field_list, value_set);
1097     db_value_set_free(value_set);
1098     db_object_field_list_free(object_field_list);
1099     return ret;
1100 }
1101 
hsm_key_get_by_id(hsm_key_t * hsm_key,const db_value_t * id)1102 int hsm_key_get_by_id(hsm_key_t* hsm_key, const db_value_t* id) {
1103     db_clause_list_t* clause_list;
1104     db_clause_t* clause;
1105     db_result_list_t* result_list;
1106     const db_result_t* result;
1107 
1108     if (!hsm_key) {
1109         return DB_ERROR_UNKNOWN;
1110     }
1111     if (!hsm_key->dbo) {
1112         return DB_ERROR_UNKNOWN;
1113     }
1114     if (!id) {
1115         return DB_ERROR_UNKNOWN;
1116     }
1117     if (db_value_not_empty(id)) {
1118         return DB_ERROR_UNKNOWN;
1119     }
1120 
1121     if (!(clause_list = db_clause_list_new())) {
1122         return DB_ERROR_UNKNOWN;
1123     }
1124     if (!(clause = db_clause_new())
1125         || db_clause_set_field(clause, "id")
1126         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1127         || db_value_copy(db_clause_get_value(clause), id)
1128         || db_clause_list_add(clause_list, clause))
1129     {
1130         db_clause_free(clause);
1131         db_clause_list_free(clause_list);
1132         return DB_ERROR_UNKNOWN;
1133     }
1134 
1135     result_list = db_object_read(hsm_key->dbo, NULL, clause_list);
1136     db_clause_list_free(clause_list);
1137 
1138     if (result_list) {
1139         result = db_result_list_next(result_list);
1140         if (result) {
1141             if (hsm_key_from_result(hsm_key, result)) {
1142                 db_result_list_free(result_list);
1143                 return DB_ERROR_UNKNOWN;
1144             }
1145 
1146             db_result_list_free(result_list);
1147             return DB_OK;
1148         }
1149     }
1150 
1151     db_result_list_free(result_list);
1152     return DB_ERROR_UNKNOWN;
1153 }
1154 
hsm_key_get_by_locator(hsm_key_t * hsm_key,const char * locator)1155 int hsm_key_get_by_locator(hsm_key_t* hsm_key, const char* locator) {
1156     db_clause_list_t* clause_list;
1157     db_clause_t* clause;
1158     db_result_list_t* result_list;
1159     const db_result_t* result;
1160 
1161     if (!hsm_key) {
1162         return DB_ERROR_UNKNOWN;
1163     }
1164     if (!hsm_key->dbo) {
1165         return DB_ERROR_UNKNOWN;
1166     }
1167     if (!locator) {
1168         return DB_ERROR_UNKNOWN;
1169     }
1170 
1171     if (!(clause_list = db_clause_list_new())) {
1172         return DB_ERROR_UNKNOWN;
1173     }
1174     if (!(clause = db_clause_new())
1175         || db_clause_set_field(clause, "locator")
1176         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1177         || db_value_from_text(db_clause_get_value(clause), locator)
1178         || db_clause_list_add(clause_list, clause))
1179     {
1180         db_clause_free(clause);
1181         db_clause_list_free(clause_list);
1182         return DB_ERROR_UNKNOWN;
1183     }
1184 
1185     result_list = db_object_read(hsm_key->dbo, NULL, clause_list);
1186     db_clause_list_free(clause_list);
1187 
1188     if (result_list) {
1189         result = db_result_list_next(result_list);
1190         if (result) {
1191             if (hsm_key_from_result(hsm_key, result)) {
1192                 db_result_list_free(result_list);
1193                 return DB_ERROR_UNKNOWN;
1194             }
1195 
1196             db_result_list_free(result_list);
1197             return DB_OK;
1198         }
1199     }
1200 
1201     db_result_list_free(result_list);
1202     return DB_ERROR_UNKNOWN;
1203 }
1204 
hsm_key_new_get_by_locator(const db_connection_t * connection,const char * locator)1205 hsm_key_t* hsm_key_new_get_by_locator(const db_connection_t* connection, const char* locator) {
1206     hsm_key_t* hsm_key;
1207 
1208     if (!connection) {
1209         return NULL;
1210     }
1211     if (!locator) {
1212         return NULL;
1213     }
1214 
1215     if (!(hsm_key = hsm_key_new(connection))
1216         || hsm_key_get_by_locator(hsm_key, locator))
1217     {
1218         hsm_key_free(hsm_key);
1219         return NULL;
1220     }
1221 
1222     return hsm_key;
1223 }
1224 
hsm_key_update(hsm_key_t * hsm_key)1225 int hsm_key_update(hsm_key_t* hsm_key) {
1226     db_object_field_list_t* object_field_list;
1227     db_object_field_t* object_field;
1228     db_value_set_t* value_set;
1229     db_clause_list_t* clause_list;
1230     db_clause_t* clause;
1231     int ret;
1232 
1233     if (!hsm_key) {
1234         return DB_ERROR_UNKNOWN;
1235     }
1236     if (!hsm_key->dbo) {
1237         return DB_ERROR_UNKNOWN;
1238     }
1239     if (db_value_not_empty(&(hsm_key->id))) {
1240         return DB_ERROR_UNKNOWN;
1241     }
1242     if (db_value_not_empty(&(hsm_key->rev))) {
1243         return DB_ERROR_UNKNOWN;
1244     }
1245     if (db_value_not_empty(&(hsm_key->policy_id))) {
1246         return DB_ERROR_UNKNOWN;
1247     }
1248     if (!hsm_key->locator) {
1249         return DB_ERROR_UNKNOWN;
1250     }
1251     if (!hsm_key->repository) {
1252         return DB_ERROR_UNKNOWN;
1253     }
1254     /* TODO: validate content more */
1255 
1256     if (!(object_field_list = db_object_field_list_new())) {
1257         return DB_ERROR_UNKNOWN;
1258     }
1259 
1260     if (!(object_field = db_object_field_new())
1261         || db_object_field_set_name(object_field, "policyId")
1262         || db_object_field_set_type(object_field, DB_TYPE_ANY)
1263         || db_object_field_list_add(object_field_list, object_field))
1264     {
1265         db_object_field_free(object_field);
1266         db_object_field_list_free(object_field_list);
1267         return DB_ERROR_UNKNOWN;
1268     }
1269 
1270     if (!(object_field = db_object_field_new())
1271         || db_object_field_set_name(object_field, "locator")
1272         || db_object_field_set_type(object_field, DB_TYPE_TEXT)
1273         || db_object_field_list_add(object_field_list, object_field))
1274     {
1275         db_object_field_free(object_field);
1276         db_object_field_list_free(object_field_list);
1277         return DB_ERROR_UNKNOWN;
1278     }
1279 
1280     if (!(object_field = db_object_field_new())
1281         || db_object_field_set_name(object_field, "state")
1282         || db_object_field_set_type(object_field, DB_TYPE_ENUM)
1283         || db_object_field_set_enum_set(object_field, hsm_key_enum_set_state)
1284         || db_object_field_list_add(object_field_list, object_field))
1285     {
1286         db_object_field_free(object_field);
1287         db_object_field_list_free(object_field_list);
1288         return DB_ERROR_UNKNOWN;
1289     }
1290 
1291     if (!(object_field = db_object_field_new())
1292         || db_object_field_set_name(object_field, "bits")
1293         || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1294         || db_object_field_list_add(object_field_list, object_field))
1295     {
1296         db_object_field_free(object_field);
1297         db_object_field_list_free(object_field_list);
1298         return DB_ERROR_UNKNOWN;
1299     }
1300 
1301     if (!(object_field = db_object_field_new())
1302         || db_object_field_set_name(object_field, "algorithm")
1303         || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1304         || db_object_field_list_add(object_field_list, object_field))
1305     {
1306         db_object_field_free(object_field);
1307         db_object_field_list_free(object_field_list);
1308         return DB_ERROR_UNKNOWN;
1309     }
1310 
1311     if (!(object_field = db_object_field_new())
1312         || db_object_field_set_name(object_field, "role")
1313         || db_object_field_set_type(object_field, DB_TYPE_ENUM)
1314         || db_object_field_set_enum_set(object_field, hsm_key_enum_set_role)
1315         || db_object_field_list_add(object_field_list, object_field))
1316     {
1317         db_object_field_free(object_field);
1318         db_object_field_list_free(object_field_list);
1319         return DB_ERROR_UNKNOWN;
1320     }
1321 
1322     if (!(object_field = db_object_field_new())
1323         || db_object_field_set_name(object_field, "inception")
1324         || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1325         || db_object_field_list_add(object_field_list, object_field))
1326     {
1327         db_object_field_free(object_field);
1328         db_object_field_list_free(object_field_list);
1329         return DB_ERROR_UNKNOWN;
1330     }
1331 
1332     if (!(object_field = db_object_field_new())
1333         || db_object_field_set_name(object_field, "isRevoked")
1334         || db_object_field_set_type(object_field, DB_TYPE_UINT32)
1335         || db_object_field_list_add(object_field_list, object_field))
1336     {
1337         db_object_field_free(object_field);
1338         db_object_field_list_free(object_field_list);
1339         return DB_ERROR_UNKNOWN;
1340     }
1341 
1342     if (!(object_field = db_object_field_new())
1343         || db_object_field_set_name(object_field, "keyType")
1344         || db_object_field_set_type(object_field, DB_TYPE_ENUM)
1345         || db_object_field_set_enum_set(object_field, hsm_key_enum_set_key_type)
1346         || db_object_field_list_add(object_field_list, object_field))
1347     {
1348         db_object_field_free(object_field);
1349         db_object_field_list_free(object_field_list);
1350         return DB_ERROR_UNKNOWN;
1351     }
1352 
1353     if (!(object_field = db_object_field_new())
1354         || db_object_field_set_name(object_field, "repository")
1355         || db_object_field_set_type(object_field, DB_TYPE_TEXT)
1356         || db_object_field_list_add(object_field_list, object_field))
1357     {
1358         db_object_field_free(object_field);
1359         db_object_field_list_free(object_field_list);
1360         return DB_ERROR_UNKNOWN;
1361     }
1362 
1363     if (!(object_field = db_object_field_new())
1364         || db_object_field_set_name(object_field, "backup")
1365         || db_object_field_set_type(object_field, DB_TYPE_ENUM)
1366         || db_object_field_set_enum_set(object_field, hsm_key_enum_set_backup)
1367         || db_object_field_list_add(object_field_list, object_field))
1368     {
1369         db_object_field_free(object_field);
1370         db_object_field_list_free(object_field_list);
1371         return DB_ERROR_UNKNOWN;
1372     }
1373 
1374     if (!(value_set = db_value_set_new(11))) {
1375         db_object_field_list_free(object_field_list);
1376         return DB_ERROR_UNKNOWN;
1377     }
1378 
1379     if (db_value_copy(db_value_set_get(value_set, 0), &(hsm_key->policy_id))
1380         || db_value_from_text(db_value_set_get(value_set, 1), hsm_key->locator)
1381         || db_value_from_enum_value(db_value_set_get(value_set, 2), hsm_key->state, hsm_key_enum_set_state)
1382         || db_value_from_uint32(db_value_set_get(value_set, 3), hsm_key->bits)
1383         || db_value_from_uint32(db_value_set_get(value_set, 4), hsm_key->algorithm)
1384         || db_value_from_enum_value(db_value_set_get(value_set, 5), hsm_key->role, hsm_key_enum_set_role)
1385         || db_value_from_uint32(db_value_set_get(value_set, 6), hsm_key->inception)
1386         || db_value_from_uint32(db_value_set_get(value_set, 7), hsm_key->is_revoked)
1387         || db_value_from_enum_value(db_value_set_get(value_set, 8), hsm_key->key_type, hsm_key_enum_set_key_type)
1388         || db_value_from_text(db_value_set_get(value_set, 9), hsm_key->repository)
1389         || db_value_from_enum_value(db_value_set_get(value_set, 10), hsm_key->backup, hsm_key_enum_set_backup))
1390     {
1391         db_value_set_free(value_set);
1392         db_object_field_list_free(object_field_list);
1393         return DB_ERROR_UNKNOWN;
1394     }
1395 
1396     if (!(clause_list = db_clause_list_new())) {
1397         db_value_set_free(value_set);
1398         db_object_field_list_free(object_field_list);
1399         return DB_ERROR_UNKNOWN;
1400     }
1401 
1402     if (!(clause = db_clause_new())
1403         || db_clause_set_field(clause, "id")
1404         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1405         || db_value_copy(db_clause_get_value(clause), &(hsm_key->id))
1406         || db_clause_list_add(clause_list, clause))
1407     {
1408         db_clause_free(clause);
1409         db_clause_list_free(clause_list);
1410         db_value_set_free(value_set);
1411         db_object_field_list_free(object_field_list);
1412         return DB_ERROR_UNKNOWN;
1413     }
1414 
1415     if (!(clause = db_clause_new())
1416         || db_clause_set_field(clause, "rev")
1417         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1418         || db_value_copy(db_clause_get_value(clause), &(hsm_key->rev))
1419         || db_clause_list_add(clause_list, clause))
1420     {
1421         db_clause_free(clause);
1422         db_clause_list_free(clause_list);
1423         db_value_set_free(value_set);
1424         db_object_field_list_free(object_field_list);
1425         return DB_ERROR_UNKNOWN;
1426     }
1427 
1428     ret = db_object_update(hsm_key->dbo, object_field_list, value_set, clause_list);
1429     db_value_set_free(value_set);
1430     db_object_field_list_free(object_field_list);
1431     db_clause_list_free(clause_list);
1432     return ret;
1433 }
1434 
hsm_key_count(hsm_key_t * hsm_key,db_clause_list_t * clause_list,size_t * count)1435 int hsm_key_count(hsm_key_t* hsm_key, db_clause_list_t* clause_list, size_t* count) {
1436     if (!hsm_key) {
1437         return DB_ERROR_UNKNOWN;
1438     }
1439     if (!hsm_key->dbo) {
1440         return DB_ERROR_UNKNOWN;
1441     }
1442     if (!count) {
1443         return DB_ERROR_UNKNOWN;
1444     }
1445 
1446     return db_object_count(hsm_key->dbo, NULL, clause_list, count);
1447 }
1448 
1449 /* HSM KEY LIST */
1450 
1451 
1452 
hsm_key_list_new(const db_connection_t * connection)1453 hsm_key_list_t* hsm_key_list_new(const db_connection_t* connection) {
1454     hsm_key_list_t* hsm_key_list =
1455         (hsm_key_list_t*)calloc(1, sizeof(hsm_key_list_t));
1456 
1457     if (hsm_key_list) {
1458         if (!(hsm_key_list->dbo = __hsm_key_new_object(connection))) {
1459             free(hsm_key_list);
1460             return NULL;
1461         }
1462     }
1463 
1464     return hsm_key_list;
1465 }
1466 
hsm_key_list_new_copy(const hsm_key_list_t * from_hsm_key_list)1467 hsm_key_list_t* hsm_key_list_new_copy(const hsm_key_list_t* from_hsm_key_list) {
1468     hsm_key_list_t* hsm_key_list;
1469 
1470     if (!from_hsm_key_list) {
1471         return NULL;
1472     }
1473     if (!from_hsm_key_list->dbo) {
1474         return NULL;
1475     }
1476 
1477     if (!(hsm_key_list = hsm_key_list_new(db_object_connection(from_hsm_key_list->dbo)))
1478         || hsm_key_list_copy(hsm_key_list, from_hsm_key_list))
1479     {
1480         hsm_key_list_free(hsm_key_list);
1481         return NULL;
1482     }
1483     return hsm_key_list;
1484 }
1485 
hsm_key_list_object_store(hsm_key_list_t * hsm_key_list)1486 int hsm_key_list_object_store(hsm_key_list_t* hsm_key_list) {
1487     if (!hsm_key_list) {
1488         return DB_ERROR_UNKNOWN;
1489     }
1490 
1491     hsm_key_list->object_store = 1;
1492 
1493     return DB_OK;
1494 }
1495 
hsm_key_list_free(hsm_key_list_t * hsm_key_list)1496 void hsm_key_list_free(hsm_key_list_t* hsm_key_list) {
1497     size_t i;
1498 
1499     if (hsm_key_list) {
1500         if (hsm_key_list->dbo) {
1501             db_object_free(hsm_key_list->dbo);
1502         }
1503         if (hsm_key_list->result_list) {
1504             db_result_list_free(hsm_key_list->result_list);
1505         }
1506         if (hsm_key_list->hsm_key) {
1507             hsm_key_free(hsm_key_list->hsm_key);
1508         }
1509         for (i = 0; i < hsm_key_list->object_list_size; i++) {
1510             if (hsm_key_list->object_list[i]) {
1511                 hsm_key_free(hsm_key_list->object_list[i]);
1512             }
1513         }
1514         if (hsm_key_list->object_list) {
1515             free(hsm_key_list->object_list);
1516         }
1517         if (hsm_key_list->policy_id_list) {
1518             policy_list_free(hsm_key_list->policy_id_list);
1519         }
1520         free(hsm_key_list);
1521     }
1522 }
1523 
hsm_key_list_copy(hsm_key_list_t * hsm_key_list,const hsm_key_list_t * from_hsm_key_list)1524 int hsm_key_list_copy(hsm_key_list_t* hsm_key_list, const hsm_key_list_t* from_hsm_key_list) {
1525     size_t i;
1526 
1527     if (!hsm_key_list) {
1528         return DB_ERROR_UNKNOWN;
1529     }
1530     if (!from_hsm_key_list) {
1531         return DB_ERROR_UNKNOWN;
1532     }
1533     if (from_hsm_key_list->object_list && !from_hsm_key_list->object_list_size) {
1534         return DB_ERROR_UNKNOWN;
1535     }
1536 
1537     if (hsm_key_list->result_list) {
1538         db_result_list_free(hsm_key_list->result_list);
1539         hsm_key_list->result_list = NULL;
1540     }
1541     if (from_hsm_key_list->result_list
1542         && !(hsm_key_list->result_list = db_result_list_new_copy(from_hsm_key_list->result_list)))
1543     {
1544         return DB_ERROR_UNKNOWN;
1545     }
1546 
1547     hsm_key_list->object_store = from_hsm_key_list->object_store;
1548     for (i = 0; i < hsm_key_list->object_list_size; i++) {
1549         if (hsm_key_list->object_list[i]) {
1550             hsm_key_free(hsm_key_list->object_list[i]);
1551         }
1552     }
1553     hsm_key_list->object_list_size = 0;
1554     if (hsm_key_list->object_list) {
1555         free(hsm_key_list->object_list);
1556         hsm_key_list->object_list = NULL;
1557     }
1558     if (from_hsm_key_list->object_list) {
1559         if (!(hsm_key_list->object_list = (hsm_key_t**)calloc(from_hsm_key_list->object_list_size, sizeof(hsm_key_t*)))) {
1560             return DB_ERROR_UNKNOWN;
1561         }
1562         hsm_key_list->object_list_size = from_hsm_key_list->object_list_size;
1563         for (i = 0; i < from_hsm_key_list->object_list_size; i++) {
1564             if (!from_hsm_key_list->object_list[i]) {
1565                 continue;
1566             }
1567             if (!(hsm_key_list->object_list[i] = hsm_key_new_copy(from_hsm_key_list->object_list[i]))) {
1568                 return DB_ERROR_UNKNOWN;
1569             }
1570         }
1571     }
1572     hsm_key_list->object_list_position = 0;;
1573     hsm_key_list->object_list_first = 1;
1574     hsm_key_list->associated_fetch = from_hsm_key_list->associated_fetch;
1575 
1576     if (from_hsm_key_list->policy_id_list
1577         && !(hsm_key_list->policy_id_list = policy_list_new_copy(from_hsm_key_list->policy_id_list)))
1578     {
1579         return DB_ERROR_UNKNOWN;
1580     }
1581 
1582     return DB_OK;
1583 }
1584 
hsm_key_list_get_associated(hsm_key_list_t * hsm_key_list)1585 static int hsm_key_list_get_associated(hsm_key_list_t* hsm_key_list) {
1586     const db_clause_t* clause_walk;
1587     const policy_t* policy_policy_id;
1588     int cmp;
1589     size_t i;
1590     db_clause_list_t* clause_list;
1591     db_clause_t* clause;
1592     const hsm_key_t* hsm_key;
1593 
1594     if (!hsm_key_list) {
1595         return DB_ERROR_UNKNOWN;
1596     }
1597     if (!hsm_key_list->dbo) {
1598         return DB_ERROR_UNKNOWN;
1599     }
1600     if (!hsm_key_list->associated_fetch) {
1601         return DB_ERROR_UNKNOWN;
1602     }
1603     if (!hsm_key_list->result_list) {
1604         return DB_ERROR_UNKNOWN;
1605     }
1606     if (hsm_key_list->object_list) {
1607         return DB_ERROR_UNKNOWN;
1608     }
1609 
1610     if (hsm_key_list->policy_id_list) {
1611         policy_list_free(hsm_key_list->policy_id_list);
1612         hsm_key_list->policy_id_list = NULL;
1613     }
1614 
1615     if (!(clause_list = db_clause_list_new())) {
1616         return DB_ERROR_UNKNOWN;
1617     }
1618     hsm_key = hsm_key_list_begin(hsm_key_list);
1619     while (hsm_key) {
1620         cmp = 1;
1621         clause_walk = db_clause_list_begin(clause_list);
1622         while (clause_walk) {
1623             if (db_value_cmp(db_clause_value(clause_walk), hsm_key_policy_id(hsm_key), &cmp)) {
1624                 db_clause_list_free(clause_list);
1625                 return DB_ERROR_UNKNOWN;
1626             }
1627             if (!cmp) {
1628                 break;
1629             }
1630             clause_walk = db_clause_next(clause_walk);
1631         }
1632         if (cmp) {
1633             if (!(clause = db_clause_new())
1634                 || db_clause_set_field(clause, "id")
1635                 || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1636                 || db_clause_set_operator(clause, DB_CLAUSE_OPERATOR_OR)
1637                 || db_value_copy(db_clause_get_value(clause), hsm_key_policy_id(hsm_key))
1638                 || db_clause_list_add(clause_list, clause))
1639             {
1640                 db_clause_free(clause);
1641                 db_clause_list_free(clause_list);
1642                 return DB_ERROR_UNKNOWN;
1643             }
1644         }
1645 
1646         hsm_key = hsm_key_list_next(hsm_key_list);
1647     }
1648 
1649     if (!(hsm_key_list->policy_id_list = policy_list_new(db_object_connection(hsm_key_list->dbo)))
1650         || policy_list_object_store(hsm_key_list->policy_id_list)
1651         || policy_list_get_by_clauses(hsm_key_list->policy_id_list, clause_list))
1652     {
1653         if (hsm_key_list->policy_id_list) {
1654             policy_list_free(hsm_key_list->policy_id_list);
1655             hsm_key_list->policy_id_list = NULL;
1656         }
1657         db_clause_list_free(clause_list);
1658         return DB_ERROR_UNKNOWN;
1659     }
1660     db_clause_list_free(clause_list);
1661 
1662     for (i = 0; i < hsm_key_list->object_list_size; i++) {
1663         if (!(hsm_key_list->object_list[i])) {
1664             return DB_ERROR_UNKNOWN;
1665         }
1666 
1667         policy_policy_id = policy_list_begin(hsm_key_list->policy_id_list);
1668         while (policy_policy_id) {
1669             if (db_value_cmp(hsm_key_policy_id(hsm_key_list->object_list[i]), policy_id(policy_policy_id), &cmp)) {
1670                 return DB_ERROR_UNKNOWN;
1671             }
1672             if (!cmp) {
1673                 hsm_key_list->object_list[i]->associated_policy_id = policy_policy_id;
1674             }
1675 
1676             policy_policy_id = policy_list_next(hsm_key_list->policy_id_list);
1677         }
1678     }
1679 
1680     hsm_key_list->object_list_first = 1;
1681     return DB_OK;
1682 }
1683 
hsm_key_list_get_by_clauses(hsm_key_list_t * hsm_key_list,const db_clause_list_t * clause_list)1684 int hsm_key_list_get_by_clauses(hsm_key_list_t* hsm_key_list, const db_clause_list_t* clause_list) {
1685     size_t i;
1686 
1687     if (!hsm_key_list) {
1688         return DB_ERROR_UNKNOWN;
1689     }
1690     if (!clause_list) {
1691         return DB_ERROR_UNKNOWN;
1692     }
1693     if (!hsm_key_list->dbo) {
1694         return DB_ERROR_UNKNOWN;
1695     }
1696 
1697     if (hsm_key_list->result_list) {
1698         db_result_list_free(hsm_key_list->result_list);
1699     }
1700     if (hsm_key_list->object_list_size) {
1701         for (i = 0; i < hsm_key_list->object_list_size; i++) {
1702             if (hsm_key_list->object_list[i]) {
1703                 hsm_key_free(hsm_key_list->object_list[i]);
1704             }
1705         }
1706         hsm_key_list->object_list_size = 0;
1707         hsm_key_list->object_list_first = 0;
1708     }
1709     if (hsm_key_list->object_list) {
1710         free(hsm_key_list->object_list);
1711         hsm_key_list->object_list = NULL;
1712     }
1713     if (!(hsm_key_list->result_list = db_object_read(hsm_key_list->dbo, NULL, clause_list))
1714         || db_result_list_fetch_all(hsm_key_list->result_list))
1715     {
1716         return DB_ERROR_UNKNOWN;
1717     }
1718     if (hsm_key_list->associated_fetch
1719         && hsm_key_list_get_associated(hsm_key_list))
1720     {
1721         return DB_ERROR_UNKNOWN;
1722     }
1723     return DB_OK;
1724 }
1725 
hsm_key_list_new_get_by_clauses(const db_connection_t * connection,const db_clause_list_t * clause_list)1726 hsm_key_list_t* hsm_key_list_new_get_by_clauses(const db_connection_t* connection, const db_clause_list_t* clause_list) {
1727     hsm_key_list_t* hsm_key_list;
1728 
1729     if (!connection) {
1730         return NULL;
1731     }
1732     if (!clause_list) {
1733         return NULL;
1734     }
1735 
1736     if (!(hsm_key_list = hsm_key_list_new(connection))
1737         || hsm_key_list_get_by_clauses(hsm_key_list, clause_list))
1738     {
1739         hsm_key_list_free(hsm_key_list);
1740         return NULL;
1741     }
1742 
1743     return hsm_key_list;
1744 }
1745 
hsm_key_list_get_by_policy_id(hsm_key_list_t * hsm_key_list,const db_value_t * policy_id)1746 int hsm_key_list_get_by_policy_id(hsm_key_list_t* hsm_key_list, const db_value_t* policy_id) {
1747     db_clause_list_t* clause_list;
1748     db_clause_t* clause;
1749     size_t i;
1750 
1751     if (!hsm_key_list) {
1752         return DB_ERROR_UNKNOWN;
1753     }
1754     if (!hsm_key_list->dbo) {
1755         return DB_ERROR_UNKNOWN;
1756     }
1757     if (!policy_id) {
1758         return DB_ERROR_UNKNOWN;
1759     }
1760     if (db_value_not_empty(policy_id)) {
1761         return DB_ERROR_UNKNOWN;
1762     }
1763 
1764     if (!(clause_list = db_clause_list_new())) {
1765         return DB_ERROR_UNKNOWN;
1766     }
1767     if (!(clause = db_clause_new())
1768         || db_clause_set_field(clause, "policyId")
1769         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
1770         || db_value_copy(db_clause_get_value(clause), policy_id)
1771         || db_clause_list_add(clause_list, clause))
1772     {
1773         db_clause_free(clause);
1774         db_clause_list_free(clause_list);
1775         return DB_ERROR_UNKNOWN;
1776     }
1777 
1778     if (hsm_key_list->result_list) {
1779         db_result_list_free(hsm_key_list->result_list);
1780     }
1781     if (hsm_key_list->object_list_size) {
1782         for (i = 0; i < hsm_key_list->object_list_size; i++) {
1783             if (hsm_key_list->object_list[i]) {
1784                 hsm_key_free(hsm_key_list->object_list[i]);
1785             }
1786         }
1787         hsm_key_list->object_list_size = 0;
1788         hsm_key_list->object_list_first = 0;
1789     }
1790     if (hsm_key_list->object_list) {
1791         free(hsm_key_list->object_list);
1792         hsm_key_list->object_list = NULL;
1793     }
1794     if (!(hsm_key_list->result_list = db_object_read(hsm_key_list->dbo, NULL, clause_list))
1795         || db_result_list_fetch_all(hsm_key_list->result_list))
1796     {
1797         db_clause_list_free(clause_list);
1798         return DB_ERROR_UNKNOWN;
1799     }
1800     db_clause_list_free(clause_list);
1801     if (hsm_key_list->associated_fetch
1802         && hsm_key_list_get_associated(hsm_key_list))
1803     {
1804         return DB_ERROR_UNKNOWN;
1805     }
1806     return DB_OK;
1807 }
1808 
hsm_key_list_new_get_by_policy_id(const db_connection_t * connection,const db_value_t * policy_id)1809 hsm_key_list_t* hsm_key_list_new_get_by_policy_id(const db_connection_t* connection, const db_value_t* policy_id) {
1810     hsm_key_list_t* hsm_key_list;
1811 
1812     if (!connection) {
1813         return NULL;
1814     }
1815     if (!policy_id) {
1816         return NULL;
1817     }
1818     if (db_value_not_empty(policy_id)) {
1819         return NULL;
1820     }
1821 
1822     if (!(hsm_key_list = hsm_key_list_new(connection))
1823         || hsm_key_list_get_by_policy_id(hsm_key_list, policy_id))
1824     {
1825         hsm_key_list_free(hsm_key_list);
1826         return NULL;
1827     }
1828 
1829     return hsm_key_list;
1830 }
1831 
hsm_key_list_begin(hsm_key_list_t * hsm_key_list)1832 const hsm_key_t* hsm_key_list_begin(hsm_key_list_t* hsm_key_list) {
1833     const db_result_t* result;
1834 
1835     if (!hsm_key_list) {
1836         return NULL;
1837     }
1838 
1839     if (hsm_key_list->object_store) {
1840         if (!hsm_key_list->object_list) {
1841             if (!hsm_key_list->result_list) {
1842                 return NULL;
1843             }
1844             if (!db_result_list_size(hsm_key_list->result_list)) {
1845                 return NULL;
1846             }
1847             if (!(hsm_key_list->object_list = (hsm_key_t**)calloc(db_result_list_size(hsm_key_list->result_list), sizeof(hsm_key_t*)))) {
1848                 return NULL;
1849             }
1850             hsm_key_list->object_list_size = db_result_list_size(hsm_key_list->result_list);
1851         }
1852         if (!(hsm_key_list->object_list[0])) {
1853             if (!hsm_key_list->result_list) {
1854                 return NULL;
1855             }
1856             if (!(result = db_result_list_begin(hsm_key_list->result_list))) {
1857                 return NULL;
1858             }
1859             if (!(hsm_key_list->object_list[0] = hsm_key_new(db_object_connection(hsm_key_list->dbo)))) {
1860                 return NULL;
1861             }
1862             if (hsm_key_from_result(hsm_key_list->object_list[0], result)) {
1863                 return NULL;
1864             }
1865         }
1866         hsm_key_list->object_list_position = 0;
1867         return hsm_key_list->object_list[0];
1868     }
1869 
1870     if (!hsm_key_list->result_list) {
1871         return NULL;
1872     }
1873 
1874     if (!(result = db_result_list_begin(hsm_key_list->result_list))) {
1875         return NULL;
1876     }
1877     if (!hsm_key_list->hsm_key) {
1878         if (!(hsm_key_list->hsm_key = hsm_key_new(db_object_connection(hsm_key_list->dbo)))) {
1879             return NULL;
1880         }
1881     }
1882     if (hsm_key_from_result(hsm_key_list->hsm_key, result)) {
1883         return NULL;
1884     }
1885     return hsm_key_list->hsm_key;
1886 }
1887 
hsm_key_list_get_begin(hsm_key_list_t * hsm_key_list)1888 hsm_key_t* hsm_key_list_get_begin(hsm_key_list_t* hsm_key_list) {
1889     const db_result_t* result;
1890     hsm_key_t* hsm_key;
1891 
1892     if (!hsm_key_list) {
1893         return NULL;
1894     }
1895 
1896     if (hsm_key_list->object_store) {
1897         if (!(hsm_key = hsm_key_new(db_object_connection(hsm_key_list->dbo)))) {
1898             return NULL;
1899         }
1900         if (hsm_key_copy(hsm_key, hsm_key_list_begin(hsm_key_list))) {
1901             hsm_key_free(hsm_key);
1902             return NULL;
1903         }
1904         return hsm_key;
1905     }
1906 
1907     if (!hsm_key_list->result_list) {
1908         return NULL;
1909     }
1910 
1911     if (!(result = db_result_list_begin(hsm_key_list->result_list))) {
1912         return NULL;
1913     }
1914     if (!(hsm_key = hsm_key_new(db_object_connection(hsm_key_list->dbo)))) {
1915         return NULL;
1916     }
1917     if (hsm_key_from_result(hsm_key, result)) {
1918         hsm_key_free(hsm_key);
1919         return NULL;
1920     }
1921     return hsm_key;
1922 }
1923 
hsm_key_list_next(hsm_key_list_t * hsm_key_list)1924 const hsm_key_t* hsm_key_list_next(hsm_key_list_t* hsm_key_list) {
1925     const db_result_t* result;
1926 
1927     if (!hsm_key_list) {
1928         return NULL;
1929     }
1930 
1931     if (hsm_key_list->object_store) {
1932         if (!hsm_key_list->object_list) {
1933             if (!hsm_key_list->result_list) {
1934                 return NULL;
1935             }
1936             if (!db_result_list_size(hsm_key_list->result_list)) {
1937                 return NULL;
1938             }
1939             if (!(hsm_key_list->object_list = (hsm_key_t**)calloc(db_result_list_size(hsm_key_list->result_list), sizeof(hsm_key_t*)))) {
1940                 return NULL;
1941             }
1942             hsm_key_list->object_list_size = db_result_list_size(hsm_key_list->result_list);
1943             hsm_key_list->object_list_position = 0;
1944         }
1945         else if (hsm_key_list->object_list_first) {
1946             hsm_key_list->object_list_first = 0;
1947             hsm_key_list->object_list_position = 0;
1948         }
1949         else {
1950             hsm_key_list->object_list_position++;
1951         }
1952         if (hsm_key_list->object_list_position >= hsm_key_list->object_list_size) {
1953             return NULL;
1954         }
1955         if (!(hsm_key_list->object_list[hsm_key_list->object_list_position])) {
1956             if (!hsm_key_list->result_list) {
1957                 return NULL;
1958             }
1959             if (!(result = db_result_list_next(hsm_key_list->result_list))) {
1960                 return NULL;
1961             }
1962             if (!(hsm_key_list->object_list[hsm_key_list->object_list_position] = hsm_key_new(db_object_connection(hsm_key_list->dbo)))) {
1963                 return NULL;
1964             }
1965             if (hsm_key_from_result(hsm_key_list->object_list[hsm_key_list->object_list_position], result)) {
1966                 return NULL;
1967             }
1968         }
1969         return hsm_key_list->object_list[hsm_key_list->object_list_position];
1970     }
1971 
1972     if (!hsm_key_list->result_list) {
1973         return NULL;
1974     }
1975 
1976     if (!(result = db_result_list_next(hsm_key_list->result_list))) {
1977         return NULL;
1978     }
1979     if (!hsm_key_list->hsm_key) {
1980         if (!(hsm_key_list->hsm_key = hsm_key_new(db_object_connection(hsm_key_list->dbo)))) {
1981             return NULL;
1982         }
1983     }
1984     if (hsm_key_from_result(hsm_key_list->hsm_key, result)) {
1985         return NULL;
1986     }
1987     return hsm_key_list->hsm_key;
1988 }
1989 
hsm_key_list_get_next(hsm_key_list_t * hsm_key_list)1990 hsm_key_t* hsm_key_list_get_next(hsm_key_list_t* hsm_key_list) {
1991     const db_result_t* result;
1992     hsm_key_t* hsm_key;
1993 
1994     if (!hsm_key_list) {
1995         return NULL;
1996     }
1997 
1998     if (hsm_key_list->object_store) {
1999         if (!(hsm_key = hsm_key_new(db_object_connection(hsm_key_list->dbo)))) {
2000             return NULL;
2001         }
2002         if (hsm_key_copy(hsm_key, hsm_key_list_next(hsm_key_list))) {
2003             hsm_key_free(hsm_key);
2004             return NULL;
2005         }
2006         return hsm_key;
2007     }
2008 
2009     if (!hsm_key_list->result_list) {
2010         return NULL;
2011     }
2012 
2013     if (!(result = db_result_list_next(hsm_key_list->result_list))) {
2014         return NULL;
2015     }
2016     if (!(hsm_key = hsm_key_new(db_object_connection(hsm_key_list->dbo)))) {
2017         return NULL;
2018     }
2019     if (hsm_key_from_result(hsm_key, result)) {
2020         hsm_key_free(hsm_key);
2021         return NULL;
2022     }
2023     return hsm_key;
2024 }
2025