Source for gnu.javax.crypto.keyring.GnuPublicKeyring

   1: /* GnuPublicKeyring.java --
   2:    Copyright (C) 2003, 2006, 2010 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.Configuration;
  42: import gnu.java.security.Registry;
  43: 
  44: import java.io.DataInputStream;
  45: import java.io.DataOutputStream;
  46: import java.io.IOException;
  47: import java.io.InputStream;
  48: import java.io.OutputStream;
  49: import java.security.cert.Certificate;
  50: import java.util.Date;
  51: import java.util.Iterator;
  52: import java.util.logging.Logger;
  53: 
  54: public class GnuPublicKeyring
  55:     extends BaseKeyring
  56:     implements IPublicKeyring
  57: {
  58:   private static final Logger log = Configuration.DEBUG ?
  59:                 Logger.getLogger(GnuPublicKeyring.class.getName()) : null;
  60:   public static final int USAGE = Registry.GKR_CERTIFICATES;
  61: 
  62:   public GnuPublicKeyring(String mac, int macLen)
  63:   {
  64:     keyring = new PasswordAuthenticatedEntry(mac, macLen, new Properties());
  65:     keyring2 = new CompressedEntry(new Properties());
  66:     keyring.add(keyring2);
  67:   }
  68: 
  69:   public GnuPublicKeyring()
  70:   {
  71:   }
  72: 
  73:   public boolean containsCertificate(String alias)
  74:   {
  75:     if (Configuration.DEBUG)
  76:       log.entering(this.getClass().getName(), "containsCertificate", alias);
  77:     boolean result = false;
  78:     if (containsAlias(alias))
  79:       for (Iterator it = get(alias).iterator(); it.hasNext();)
  80:         if (it.next() instanceof CertificateEntry)
  81:           {
  82:             result = true;
  83:             break;
  84:           }
  85:     if (Configuration.DEBUG)
  86:       log.exiting(this.getClass().getName(), "containsCertificate",
  87:                   Boolean.valueOf(result));
  88:     return result;
  89:   }
  90: 
  91:   public Certificate getCertificate(String alias)
  92:   {
  93:     if (Configuration.DEBUG)
  94:       log.entering(this.getClass().getName(), "getCertificate", alias);
  95:     Certificate result = null;
  96:     if (containsAlias(alias))
  97:       for (Iterator it = get(alias).iterator(); it.hasNext();)
  98:         {
  99:           Entry e = (Entry) it.next();
 100:           if (e instanceof CertificateEntry)
 101:             {
 102:               result = ((CertificateEntry) e).getCertificate();
 103:               break;
 104:             }
 105:         }
 106:     if (Configuration.DEBUG)
 107:       log.exiting(this.getClass().getName(), "getCertificate", result);
 108:     return result;
 109:   }
 110: 
 111:   public void putCertificate(String alias, Certificate cert)
 112:   {
 113:     if (Configuration.DEBUG)
 114:       log.entering(this.getClass().getName(), "putCertificate",
 115:                    new Object[] { alias, cert });
 116:     if (! containsCertificate(alias))
 117:       {
 118:         Properties p = new Properties();
 119:         p.put("alias", fixAlias(alias));
 120:         add(new CertificateEntry(cert, new Date(), p));
 121:       }
 122:     else if (Configuration.DEBUG)
 123:       log.fine("Keyring already contains alias: " + alias);
 124:     if (Configuration.DEBUG)
 125:       log.exiting(this.getClass().getName(), "putCertificate");
 126:   }
 127: 
 128:   protected void load(InputStream in, char[] password) throws IOException
 129:   {
 130:     if (Configuration.DEBUG)
 131:       log.entering(this.getClass().getName(), "load");
 132:     if (in.read() != USAGE)
 133:       throw new MalformedKeyringException("incompatible keyring usage");
 134:     if (in.read() != PasswordAuthenticatedEntry.TYPE)
 135:       throw new MalformedKeyringException(
 136:           "expecting password-authenticated entry tag");
 137:     DataInputStream dis = new DataInputStream(in);
 138:     keyring = PasswordAuthenticatedEntry.decode(dis, password);
 139:     if (Configuration.DEBUG)
 140:       log.exiting(this.getClass().getName(), "load");
 141:   }
 142: 
 143:   protected void store(OutputStream out, char[] password) throws IOException
 144:   {
 145:     if (Configuration.DEBUG)
 146:       log.entering(this.getClass().getName(), "store");
 147:     out.write(USAGE);
 148:     keyring.encode(new DataOutputStream(out), password);
 149:     if (Configuration.DEBUG)
 150:       log.exiting(this.getClass().getName(), "store");
 151:   }
 152: }