001/** 002 * 003 * Copyright 2018 Paul Schaub. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.ox.store.abstr; 018 019import java.io.IOException; 020import java.security.InvalidAlgorithmParameterException; 021import java.security.NoSuchAlgorithmException; 022import java.security.NoSuchProviderException; 023import java.util.Date; 024import java.util.HashMap; 025import java.util.Map; 026 027import org.jivesoftware.smack.util.Objects; 028 029import org.jivesoftware.smackx.ox.OpenPgpContact; 030import org.jivesoftware.smackx.ox.callback.SecretKeyPassphraseCallback; 031import org.jivesoftware.smackx.ox.exception.MissingUserIdOnKeyException; 032import org.jivesoftware.smackx.ox.store.definition.OpenPgpKeyStore; 033import org.jivesoftware.smackx.ox.store.definition.OpenPgpMetadataStore; 034import org.jivesoftware.smackx.ox.store.definition.OpenPgpStore; 035import org.jivesoftware.smackx.ox.store.definition.OpenPgpTrustStore; 036 037import org.bouncycastle.openpgp.PGPException; 038import org.bouncycastle.openpgp.PGPPublicKeyRing; 039import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; 040import org.bouncycastle.openpgp.PGPSecretKeyRing; 041import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; 042import org.jxmpp.jid.BareJid; 043import org.pgpainless.key.OpenPgpV4Fingerprint; 044import org.pgpainless.key.protection.SecretKeyRingProtector; 045import org.pgpainless.key.protection.UnprotectedKeysProtector; 046 047public abstract class AbstractOpenPgpStore implements OpenPgpStore { 048 049 protected final OpenPgpKeyStore keyStore; 050 protected final OpenPgpMetadataStore metadataStore; 051 protected final OpenPgpTrustStore trustStore; 052 053 protected SecretKeyPassphraseCallback secretKeyPassphraseCallback; 054 protected SecretKeyRingProtector unlocker = new UnprotectedKeysProtector(); 055 protected final Map<BareJid, OpenPgpContact> contacts = new HashMap<>(); 056 057 @Override 058 public void deletePublicKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException { 059 keyStore.deletePublicKeyRing(owner, fingerprint); 060 } 061 062 @Override 063 public void deleteSecretKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException { 064 keyStore.deleteSecretKeyRing(owner, fingerprint); 065 } 066 067 protected AbstractOpenPgpStore(OpenPgpKeyStore keyStore, 068 OpenPgpMetadataStore metadataStore, 069 OpenPgpTrustStore trustStore) { 070 this.keyStore = Objects.requireNonNull(keyStore); 071 this.metadataStore = Objects.requireNonNull(metadataStore); 072 this.trustStore = Objects.requireNonNull(trustStore); 073 } 074 075 @Override 076 public OpenPgpContact getOpenPgpContact(BareJid jid) { 077 OpenPgpContact contact = contacts.get(jid); 078 if (contact == null) { 079 contact = new OpenPgpContact(jid, this); 080 contacts.put(jid, contact); 081 } 082 return contact; 083 } 084 085 @Override 086 public void setKeyRingProtector(SecretKeyRingProtector protector) { 087 this.unlocker = protector; 088 } 089 090 @Override 091 public SecretKeyRingProtector getKeyRingProtector() { 092 return unlocker; 093 } 094 095 @Override 096 public void setSecretKeyPassphraseCallback(SecretKeyPassphraseCallback callback) { 097 this.secretKeyPassphraseCallback = callback; 098 } 099 100 /* 101 OpenPgpKeyStore 102 */ 103 104 @Override 105 public PGPPublicKeyRingCollection getPublicKeysOf(BareJid owner) throws IOException, PGPException { 106 return keyStore.getPublicKeysOf(owner); 107 } 108 109 @Override 110 public PGPSecretKeyRingCollection getSecretKeysOf(BareJid owner) throws IOException, PGPException { 111 return keyStore.getSecretKeysOf(owner); 112 } 113 114 @Override 115 public PGPPublicKeyRing getPublicKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException { 116 return keyStore.getPublicKeyRing(owner, fingerprint); 117 } 118 119 @Override 120 public PGPSecretKeyRing getSecretKeyRing(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException, PGPException { 121 return keyStore.getSecretKeyRing(owner, fingerprint); 122 } 123 124 @Override 125 public PGPSecretKeyRing generateKeyRing(BareJid owner) throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException { 126 return keyStore.generateKeyRing(owner); 127 } 128 129 @Override 130 public void importSecretKey(BareJid owner, PGPSecretKeyRing secretKeys) throws IOException, PGPException, MissingUserIdOnKeyException { 131 keyStore.importSecretKey(owner, secretKeys); 132 } 133 134 @Override 135 public void importPublicKey(BareJid owner, PGPPublicKeyRing publicKeys) throws IOException, PGPException, MissingUserIdOnKeyException { 136 keyStore.importPublicKey(owner, publicKeys); 137 } 138 139 @Override 140 public Map<OpenPgpV4Fingerprint, Date> getPublicKeyFetchDates(BareJid contact) throws IOException { 141 return keyStore.getPublicKeyFetchDates(contact); 142 } 143 144 @Override 145 public void setPublicKeyFetchDates(BareJid contact, Map<OpenPgpV4Fingerprint, Date> dates) throws IOException { 146 keyStore.setPublicKeyFetchDates(contact, dates); 147 } 148 149 /* 150 OpenPgpMetadataStore 151 */ 152 153 @Override 154 public Map<OpenPgpV4Fingerprint, Date> getAnnouncedFingerprintsOf(BareJid contact) throws IOException { 155 return metadataStore.getAnnouncedFingerprintsOf(contact); 156 } 157 158 @Override 159 public void setAnnouncedFingerprintsOf(BareJid contact, Map<OpenPgpV4Fingerprint, Date> data) throws IOException { 160 metadataStore.setAnnouncedFingerprintsOf(contact, data); 161 } 162 163 /* 164 OpenPgpTrustStore 165 */ 166 167 @Override 168 public Trust getTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException { 169 return trustStore.getTrust(owner, fingerprint); 170 } 171 172 @Override 173 public void setTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust) throws IOException { 174 trustStore.setTrust(owner, fingerprint, trust); 175 } 176}