Source for gnu.javax.crypto.keyring.BaseKeyring

   1: /* BaseKeyring.java --
   2:    Copyright (C) 2003, 2006 Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2 of the License, or (at
   9: your option) any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; if not, write to the Free Software
  18: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  19: USA
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version.  */
  37: 
  38: 
  39: package gnu.javax.crypto.keyring;
  40: 
  41: import gnu.java.security.Registry;
  42: 
  43: import java.io.IOException;
  44: import java.io.InputStream;
  45: import java.io.OutputStream;
  46: import java.util.Enumeration;
  47: import java.util.List;
  48: import java.util.Map;
  49: import java.util.StringTokenizer;
  50: 
  51: public abstract class BaseKeyring
  52:     implements IKeyring
  53: {
  54:   /** The top-level keyring data. */
  55:   protected PasswordAuthenticatedEntry keyring;
  56:   protected CompressedEntry keyring2;
  57: 
  58:   public BaseKeyring()
  59:   {
  60:   }
  61: 
  62:   public void load(Map attributes) throws IOException
  63:   {
  64:     InputStream in = (InputStream) attributes.get(KEYRING_DATA_IN);
  65:     if (in == null)
  66:       throw new IllegalArgumentException("no input stream");
  67:     char[] password = (char[]) attributes.get(KEYRING_PASSWORD);
  68:     if (password == null)
  69:       password = new char[0];
  70: 
  71:     if (in.read() != Registry.GKR_MAGIC[0]
  72:         || in.read() != Registry.GKR_MAGIC[1]
  73:         || in.read() != Registry.GKR_MAGIC[2]
  74:         || in.read() != Registry.GKR_MAGIC[3])
  75:       throw new MalformedKeyringException("magic");
  76: 
  77:     load(in, password);
  78:     List l = keyring.getEntries();
  79:     if (l.size() == 1 && (l.get(0) instanceof CompressedEntry))
  80:       keyring2 = (CompressedEntry) l.get(0);
  81:   }
  82: 
  83:   public void store(Map attributes) throws IOException
  84:   {
  85:     OutputStream out = (OutputStream) attributes.get(KEYRING_DATA_OUT);
  86:     if (out == null)
  87:       throw new IllegalArgumentException("no output stream");
  88:     char[] password = (char[]) attributes.get(KEYRING_PASSWORD);
  89:     if (password == null)
  90:       password = new char[0];
  91:     if (keyring == null)
  92:       throw new IllegalStateException("empty keyring");
  93: 
  94:     out.write(Registry.GKR_MAGIC);
  95:     store(out, password);
  96:   }
  97: 
  98:   public void reset()
  99:   {
 100:     keyring = null;
 101:   }
 102: 
 103:   public int size()
 104:   {
 105:     if (keyring == null)
 106:       throw new IllegalStateException("keyring not loaded");
 107:     return ((StringTokenizer) aliases()).countTokens();
 108:   }
 109: 
 110:   public Enumeration aliases()
 111:   {
 112:     if (keyring == null)
 113:       throw new IllegalStateException("keyring not loaded");
 114:     return new StringTokenizer(keyring.getAliasList(), ";");
 115:   }
 116: 
 117:   public boolean containsAlias(String alias)
 118:   {
 119:     if (keyring == null)
 120:       throw new IllegalStateException("keyring not loaded");
 121:     return keyring.containsAlias(alias);
 122:   }
 123: 
 124:   public List get(String alias)
 125:   {
 126:     if (keyring == null)
 127:       throw new IllegalStateException("keyring not loaded");
 128:     return keyring.get(alias);
 129:   }
 130: 
 131:   public void add(Entry entry)
 132:   {
 133:     if (keyring == null)
 134:       throw new IllegalStateException("keyring not loaded");
 135:     if (keyring2 != null)
 136:       keyring2.add(entry);
 137:     else
 138:       keyring.add(entry);
 139:   }
 140: 
 141:   public void remove(String alias)
 142:   {
 143:     if (keyring == null)
 144:       throw new IllegalStateException("keyring not loaded");
 145:     keyring.remove(alias);
 146:   }
 147: 
 148:   protected String fixAlias(String alias)
 149:   {
 150:     return alias.replace(';', '_');
 151:   }
 152: 
 153:   protected abstract void load(InputStream in, char[] password)
 154:       throws IOException;
 155: 
 156:   protected abstract void store(OutputStream out, char[] password)
 157:       throws IOException;
 158: }