2 * Copyright (c) 2011-2014, 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 private LinkedList<IdCard> id_card_list;
37 private const string keyring_store_attribute = "Moonshot";
38 private const string keyring_store_version = "1.0";
39 private const GnomeKeyring.ItemType item_type = GnomeKeyring.ItemType.GENERIC_SECRET;
41 public void add_card(IdCard card) {
42 id_card_list.add(card);
46 public IdCard? update_card(IdCard card) {
47 id_card_list.remove(card);
48 id_card_list.add(card);
50 foreach (IdCard idcard in id_card_list)
51 if (idcard.display_name == card.display_name)
56 public bool remove_card(IdCard card) {
57 bool retval = id_card_list.remove(card);
63 public IIdentityCardStore.StoreType get_store_type() {
64 return IIdentityCardStore.StoreType.KEYRING;
67 public LinkedList<IdCard> get_card_list() {
71 /* clear all keyring-stored ids (in preparation to store current list) */
72 private void clear_keyring() {
73 GnomeKeyring.AttributeList match = new GnomeKeyring.AttributeList();
74 match.append_string(keyring_store_attribute, keyring_store_version);
75 GLib.List<GnomeKeyring.Found> items;
76 GnomeKeyring.find_items_sync(item_type, match, out items);
77 foreach(unowned GnomeKeyring.Found entry in items) {
78 GnomeKeyring.Result result = GnomeKeyring.item_delete_sync(null, entry.item_id);
79 if (result != GnomeKeyring.Result.OK) {
80 stdout.printf("GnomeKeyring.item_delete_sync() failed. result: %d", result);
85 private void load_id_cards() {
88 GnomeKeyring.AttributeList match = new GnomeKeyring.AttributeList();
89 match.append_string(keyring_store_attribute, keyring_store_version);
90 GLib.List<GnomeKeyring.Found> items;
91 GnomeKeyring.find_items_sync(item_type, match, out items);
92 foreach(unowned GnomeKeyring.Found entry in items) {
93 IdCard id_card = new IdCard ();
95 int rules_patterns_index = -1;
96 int rules_always_confirm_index = -1;
97 string store_password = null;
98 for (i=0; i<entry.attributes.len; i++) {
99 var attribute = ((GnomeKeyring.Attribute *) entry.attributes.data)[i];
100 string value = attribute.string_value;
101 if (attribute.name == "Issuer") {
102 id_card.issuer = value;
103 } else if (attribute.name == "Username") {
104 id_card.username = value;
105 } else if (attribute.name == "DisplayName") {
106 id_card.display_name = value;
107 } else if (attribute.name == "Services") {
108 id_card.services = value.split(";");
109 } else if (attribute.name == "Rules-Pattern") {
110 rules_patterns_index = i;
111 } else if (attribute.name == "Rules-AlwaysConfirm") {
112 rules_always_confirm_index = i;
113 } else if (attribute.name == "CA-Cert") {
114 id_card.trust_anchor.ca_cert = value;
115 } else if (attribute.name == "Server-Cert") {
116 id_card.trust_anchor.server_cert = value;
117 } else if (attribute.name == "Subject") {
118 id_card.trust_anchor.subject = value;
119 } else if (attribute.name == "Subject-Alt") {
120 id_card.trust_anchor.subject_alt = value;
121 } else if (attribute.name == "StorePassword") {
122 store_password = value;
125 if ((rules_always_confirm_index != -1) && (rules_patterns_index != -1)) {
126 string rules_patterns_all = ((GnomeKeyring.Attribute *) entry.attributes.data)[rules_patterns_index].string_value;
127 string rules_always_confirm_all = ((GnomeKeyring.Attribute *) entry.attributes.data)[rules_always_confirm_index].string_value;
128 string [] rules_always_confirm = rules_always_confirm_all.split(";");
129 string [] rules_patterns = rules_patterns_all.split(";");
130 if (rules_patterns.length == rules_always_confirm.length) {
131 Rule[] rules = new Rule[rules_patterns.length];
132 for (int j=0; j<rules_patterns.length; j++) {
133 rules[j].pattern = rules_patterns[j];
134 rules[j].always_confirm = rules_always_confirm[j];
136 id_card.rules = rules;
140 if (store_password != null)
141 id_card.store_password = (store_password == "yes");
143 id_card.store_password = ((entry.secret != null) && (entry.secret != ""));
145 if (id_card.store_password)
146 id_card.password = entry.secret;
148 id_card.password = null;
149 id_card_list.add(id_card);
153 public void store_id_cards () {
155 foreach (IdCard id_card in this.id_card_list) {
156 string[] rules_patterns = new string[id_card.rules.length];
157 string[] rules_always_conf = new string[id_card.rules.length];
159 for (int i=0; i<id_card.rules.length; i++) {
160 rules_patterns[i] = id_card.rules[i].pattern;
161 rules_always_conf[i] = id_card.rules[i].always_confirm;
163 string patterns = string.joinv(";", rules_patterns);
164 string always_conf = string.joinv(";", rules_always_conf);
165 string services = string.joinv(";", id_card.services);
166 GnomeKeyring.AttributeList attributes = new GnomeKeyring.AttributeList();
168 attributes.append_string(keyring_store_attribute, keyring_store_version);
169 attributes.append_string("Issuer", id_card.issuer);
170 attributes.append_string("Username", id_card.username);
171 attributes.append_string("DisplayName", id_card.display_name);
172 attributes.append_string("Services", services);
173 attributes.append_string("Rules-Pattern", patterns);
174 attributes.append_string("Rules-AlwaysConfirm", always_conf);
175 attributes.append_string("CA-Cert", id_card.trust_anchor.ca_cert);
176 attributes.append_string("Server-Cert", id_card.trust_anchor.server_cert);
177 attributes.append_string("Subject", id_card.trust_anchor.subject);
178 attributes.append_string("Subject-Alt", id_card.trust_anchor.subject_alt);
179 attributes.append_string("StorePassword", id_card.store_password ? "yes" : "no");
181 GnomeKeyring.Result result = GnomeKeyring.item_create_sync(null,
182 item_type, id_card.display_name, attributes,
183 id_card.store_password ? id_card.password : "",
185 if (result != GnomeKeyring.Result.OK) {
186 stdout.printf("GnomeKeyring.item_create_sync() failed. result: %d", result);
192 public KeyringStore () {
193 id_card_list = new LinkedList<IdCard>();