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
34 #if GNOME_KEYRING || LIBSECRET_KEYRING
36 public abstract class KeyringStoreBase : Object, IIdentityCardStore {
37 protected static MoonshotLogger logger = get_logger("KeyringStore");
39 protected LinkedList<IdCard> id_card_list;
40 internal const string keyring_store_attribute = "Moonshot";
41 internal const string keyring_store_version = "1.0";
44 * This class is directly useful for the libsecret implementation.
45 * However, we convert the gnome keyring attributes into a HashTable
46 * so we can share the serialization code between the two
47 * implementations. This ends up decreasing complexity even of the
50 protected class Attributes: GLib.HashTable<string, string> {
52 base.full(GLib.str_hash, GLib.str_equal, GLib.g_free, GLib.g_free);
56 protected static Attributes match_attributes;
58 protected static IdCard deserialize(GLib.HashTable<string,string> attrs, string? secret)
60 IdCard id_card = new IdCard();
61 unowned string store_password = attrs.lookup("StorePassword");
62 unowned string ca_cert = attrs.lookup("CA-Cert") ?? "";
63 unowned string server_cert = attrs.lookup("Server-Cert") ?? "";
64 unowned string subject = attrs.lookup("Subject") ?? "";
65 unowned string subject_alt = attrs.lookup("Subject-Alt") ?? "";
66 unowned string ta_datetime_added = attrs.lookup("TA_DateTime_Added");
68 id_card.issuer = attrs.lookup("Issuer");
69 id_card.username = attrs.lookup("Username");
70 id_card.display_name = attrs.lookup("DisplayName");
71 unowned string services = attrs.lookup("Services");
72 if ((services != null) && services != "") {
73 id_card.update_services(services.split(";"));
75 var ta = new TrustAnchor(ca_cert, server_cert, subject, subject_alt);
76 if (ta_datetime_added != null) {
77 ta.set_datetime_added(ta_datetime_added);
79 id_card.set_trust_anchor_from_store(ta);
81 unowned string rules_pattern_all = attrs.lookup("Rules-Pattern");
82 unowned string rules_always_confirm_all = attrs.lookup("Rules-AlwaysConfirm");
83 if ((rules_pattern_all != null) && (rules_always_confirm_all != null)) {
84 string[] rules_patterns = rules_pattern_all.split(";");
85 string[] rules_always_confirm = rules_always_confirm_all.split(";");
86 if (rules_patterns.length == rules_always_confirm.length) {
87 Rule[] rules = new Rule[rules_patterns.length];
88 for (int i = 0; i < rules_patterns.length; i++) {
89 rules[i].pattern = (owned) rules_patterns[i];
90 rules[i].always_confirm = (owned) rules_always_confirm[i];
92 id_card.rules = rules;
96 if (store_password != null)
97 id_card.store_password = (store_password == "yes");
99 id_card.store_password = ((secret != null) && (secret != ""));
101 if (id_card.store_password)
102 id_card.password = secret;
104 id_card.password = null;
109 internal static Attributes serialize(IdCard id_card)
111 /* workaround for Centos vala array property bug: use temp array */
112 var rules = id_card.rules;
113 string[] rules_patterns = new string[rules.length];
114 string[] rules_always_conf = new string[rules.length];
116 for (int i = 0; i < rules.length; i++) {
117 rules_patterns[i] = rules[i].pattern;
118 rules_always_conf[i] = rules[i].always_confirm;
120 string patterns = string.joinv(";", rules_patterns);
121 string always_conf = string.joinv(";", rules_always_conf);
122 string services = id_card.get_services_string(";");
123 Attributes attributes = new Attributes();
124 attributes.insert(keyring_store_attribute, keyring_store_version);
125 attributes.insert("Issuer", id_card.issuer);
126 attributes.insert("Username", id_card.username);
127 attributes.insert("DisplayName", id_card.display_name);
128 attributes.insert("Services", services);
129 attributes.insert("Rules-Pattern", patterns);
130 attributes.insert("Rules-AlwaysConfirm", always_conf);
131 attributes.insert("CA-Cert", id_card.trust_anchor.ca_cert);
132 attributes.insert("Server-Cert", id_card.trust_anchor.server_cert);
133 attributes.insert("Subject", id_card.trust_anchor.subject);
134 attributes.insert("Subject-Alt", id_card.trust_anchor.subject_alt);
135 attributes.insert("TA_DateTime_Added", id_card.trust_anchor.datetime_added);
136 attributes.insert("StorePassword", id_card.store_password ? "yes" : "no");
141 match_attributes = new Attributes();
142 match_attributes.insert(keyring_store_attribute, keyring_store_version);
145 public void add_card(IdCard card) {
146 logger.trace("add_card: Adding card '%s' with services: '%s'"
147 .printf(card.display_name, card.get_services_string("; ")));
148 id_card_list.add(card);
152 public IdCard? update_card(IdCard card) {
153 logger.trace("update_card");
155 id_card_list.remove(card);
156 id_card_list.add(card);
159 foreach (IdCard idcard in id_card_list) {
160 if (idcard.display_name == card.display_name) {
165 logger.error(@"update_card: card '$(card.display_name)' was not found after re-loading!");
169 public bool remove_card(IdCard card) {
170 bool retval = id_card_list.remove(card);
176 public IIdentityCardStore.StoreType get_store_type() {
177 return IIdentityCardStore.StoreType.KEYRING;
180 public LinkedList<IdCard> get_card_list() {
184 protected abstract void clear_keyring();
185 protected abstract void load_id_cards() throws GLib.Error;
186 internal abstract void store_id_cards();
188 public KeyringStoreBase() {
189 id_card_list = new LinkedList<IdCard>();
192 } catch( GLib.Error e) {
193 stdout.printf("Unable to load ID cards: %s\n", e.message);