2 * Copyright (c) 2011-2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * 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.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 public class KeyringStore : Object, IIdentityCardStore {
36 static MoonshotLogger logger = get_logger("KeyringStore");
38 private LinkedList<IdCard> id_card_list;
39 private const string keyring_store_attribute = "Moonshot";
40 private const string keyring_store_version = "1.0";
41 private const GnomeKeyring.ItemType item_type = GnomeKeyring.ItemType.GENERIC_SECRET;
43 public void add_card(IdCard card) {
44 logger.trace("add_card: Adding card '%s' with services: '%s'"
45 .printf(card.display_name, card.get_services_string("; ")));
47 id_card_list.add(card);
51 public IdCard? update_card(IdCard card) {
52 logger.trace("update_card");
54 id_card_list.remove(card);
55 id_card_list.add(card);
58 foreach (IdCard idcard in id_card_list) {
59 if (idcard.display_name == card.display_name) {
64 logger.error(@"update_card: card '$(card.display_name)' was not found after re-loading!");
68 public bool remove_card(IdCard card) {
69 bool retval = id_card_list.remove(card);
75 public IIdentityCardStore.StoreType get_store_type() {
76 return IIdentityCardStore.StoreType.KEYRING;
79 public LinkedList<IdCard> get_card_list() {
83 /* clear all keyring-stored ids (in preparation to store current list) */
84 private void clear_keyring() {
85 GnomeKeyring.AttributeList match = new GnomeKeyring.AttributeList();
86 match.append_string(keyring_store_attribute, keyring_store_version);
87 GLib.List<GnomeKeyring.Found> items;
88 GnomeKeyring.find_items_sync(item_type, match, out items);
89 foreach(unowned GnomeKeyring.Found entry in items) {
90 GnomeKeyring.Result result = GnomeKeyring.item_delete_sync(null, entry.item_id);
91 if (result != GnomeKeyring.Result.OK) {
92 stdout.printf("GnomeKeyring.item_delete_sync() failed. result: %d", result);
97 private void load_id_cards() {
100 GnomeKeyring.AttributeList match = new GnomeKeyring.AttributeList();
101 match.append_string(keyring_store_attribute, keyring_store_version);
102 GLib.List<GnomeKeyring.Found> items;
103 GnomeKeyring.find_items_sync(item_type, match, out items);
104 foreach(unowned GnomeKeyring.Found entry in items) {
105 IdCard id_card = new IdCard();
107 int rules_patterns_index = -1;
108 int rules_always_confirm_index = -1;
109 string store_password = null;
111 string server_cert = "";
113 string subject_alt = "";
114 string ta_datetime_added = "";
115 for (i = 0; i < entry.attributes.len; i++) {
116 var attribute = ((GnomeKeyring.Attribute *) entry.attributes.data)[i];
118 if (attribute.type == GnomeKeyring.AttributeType.STRING) {
119 value = attribute.string_value;
122 if (attribute.name == "Issuer") {
123 id_card.issuer = value;
124 } else if (attribute.name == "Username") {
125 id_card.username = value;
126 } else if (attribute.name == "DisplayName") {
127 id_card.display_name = value;
128 } else if (attribute.name == "Services") {
129 id_card.update_services(value.split(";"));
130 } else if (attribute.name == "Rules-Pattern") {
131 rules_patterns_index = i;
132 } else if (attribute.name == "Rules-AlwaysConfirm") {
133 rules_always_confirm_index = i;
134 } else if (attribute.name == "CA-Cert") {
135 ca_cert = value.strip();
136 } else if (attribute.name == "Server-Cert") {
138 } else if (attribute.name == "Subject") {
140 } else if (attribute.name == "Subject-Alt") {
142 } else if (attribute.name == "StorePassword") {
143 store_password = value;
144 } else if (attribute.name == "TA_DateTime_Added") {
145 ta_datetime_added = value;
149 var ta = new TrustAnchor(ca_cert, server_cert, subject, subject_alt);
150 if (ta_datetime_added != "") {
151 ta.set_datetime_added(ta_datetime_added);
153 id_card.set_trust_anchor_from_store(ta);
155 if ((rules_always_confirm_index != -1) && (rules_patterns_index != -1)) {
156 string rules_patterns_all = ((GnomeKeyring.Attribute *) entry.attributes.data)[rules_patterns_index].string_value;
157 string rules_always_confirm_all = ((GnomeKeyring.Attribute *) entry.attributes.data)[rules_always_confirm_index].string_value;
158 string [] rules_always_confirm = rules_always_confirm_all.split(";");
159 string [] rules_patterns = rules_patterns_all.split(";");
160 if (rules_patterns.length == rules_always_confirm.length) {
161 Rule[] rules = new Rule[rules_patterns.length];
162 for (int j = 0; j < rules_patterns.length; j++) {
163 rules[j].pattern = rules_patterns[j];
164 rules[j].always_confirm = rules_always_confirm[j];
166 id_card.rules = rules;
170 if (store_password != null)
171 id_card.store_password = (store_password == "yes");
173 id_card.store_password = ((entry.secret != null) && (entry.secret != ""));
175 if (id_card.store_password)
176 id_card.password = entry.secret;
178 id_card.password = null;
180 id_card_list.add(id_card);
184 internal void store_id_cards() {
185 logger.trace("store_id_cards");
187 foreach (IdCard id_card in this.id_card_list) {
188 /* workaround for Centos vala array property bug: use temp array */
189 var rules = id_card.rules;
190 string[] rules_patterns = new string[rules.length];
191 string[] rules_always_conf = new string[rules.length];
193 for (int i = 0; i < rules.length; i++) {
194 rules_patterns[i] = rules[i].pattern;
195 rules_always_conf[i] = rules[i].always_confirm;
197 string patterns = string.joinv(";", rules_patterns);
198 string always_conf = string.joinv(";", rules_always_conf);
199 string services = id_card.get_services_string(";");
200 GnomeKeyring.AttributeList attributes = new GnomeKeyring.AttributeList();
202 attributes.append_string(keyring_store_attribute, keyring_store_version);
203 attributes.append_string("Issuer", id_card.issuer);
204 attributes.append_string("Username", id_card.username);
205 attributes.append_string("DisplayName", id_card.display_name);
206 attributes.append_string("Services", services);
207 attributes.append_string("Rules-Pattern", patterns);
208 attributes.append_string("Rules-AlwaysConfirm", always_conf);
209 attributes.append_string("CA-Cert", id_card.trust_anchor.ca_cert);
210 attributes.append_string("Server-Cert", id_card.trust_anchor.server_cert);
211 attributes.append_string("Subject", id_card.trust_anchor.subject);
212 attributes.append_string("Subject-Alt", id_card.trust_anchor.subject_alt);
213 attributes.append_string("TA_DateTime_Added", id_card.trust_anchor.datetime_added);
214 attributes.append_string("StorePassword", id_card.store_password ? "yes" : "no");
216 GnomeKeyring.Result result = GnomeKeyring.item_create_sync(null,
217 item_type, id_card.display_name, attributes,
218 id_card.store_password ? id_card.password : "",
220 if (result != GnomeKeyring.Result.OK) {
221 stdout.printf("GnomeKeyring.item_create_sync() failed. result: %d", result);
227 public KeyringStore() {
228 id_card_list = new LinkedList<IdCard>();