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 char const *
hsm_key_to_backup_state(hsm_key_t const * hsm_key)34 hsm_key_to_backup_state(hsm_key_t const *hsm_key) {
35     int i;
36     for (i = 0; hsm_key_enum_set_backup[i].text; i++) {
37         if (hsm_key_enum_set_backup[i].value == hsm_key_backup(hsm_key))
38             return hsm_key_enum_set_backup[i].text;
39     }
40     return NULL;
41 }
42 
hsm_key_list_new_get_by_policy_key(const policy_key_t * pkey)43 hsm_key_list_t* hsm_key_list_new_get_by_policy_key(const policy_key_t *pkey)
44 {
45     hsm_key_list_t* hkey_list = NULL;
46     db_clause_list_t* clause_list;
47     db_clause_t* clause;
48 
49     if (!pkey || !pkey->dbo || !(clause_list = db_clause_list_new()))
50         return NULL;
51 
52     if (!(clause = db_clause_new())
53         || db_clause_set_field(clause, "policyId")
54         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
55         || db_value_copy(db_clause_get_value(clause), policy_key_policy_id(pkey))
56         || db_clause_list_add(clause_list, clause)
57 
58         || !(clause = db_clause_new())
59         || db_clause_set_field(clause, "algorithm")
60         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
61         || db_value_from_uint32(db_clause_get_value(clause), policy_key_algorithm(pkey))
62         || db_clause_list_add(clause_list, clause)
63 
64         || !(clause = db_clause_new())
65         || db_clause_set_field(clause, "bits")
66         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
67         || db_value_from_uint32(db_clause_get_value(clause), policy_key_bits(pkey))
68         || db_clause_list_add(clause_list, clause)
69 
70         || !(clause = db_clause_new())
71         || db_clause_set_field(clause, "repository")
72         || db_clause_set_type(clause, DB_CLAUSE_EQUAL)
73         || db_value_from_text(db_clause_get_value(clause), policy_key_repository(pkey))
74         || db_clause_list_add(clause_list, clause))
75     {
76         db_clause_free(clause);
77         db_clause_list_free(clause_list);
78         return NULL;
79     }
80 
81     hkey_list = hsm_key_list_new_get_by_clauses(
82         db_object_connection(pkey->dbo), clause_list);
83     db_clause_list_free(clause_list);
84     return hkey_list;
85 }
86