Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-5068872

no KME for the same public key in the identity's scope

    XMLWordPrintable

Details

    Description

      Name: smR10189 Date: 06/28/2004



      The method Identity.setPublicKey must throw KeyManagementException
      if there is another identity in the identity's scope with the same public key.

      According to the JDK 1.5.0 spec."
      public void setPublicKey(PublicKey key)
                        throws KeyManagementException
      .....
      Throws:
      KeyManagementException - if another identity in the identity's scope has
                               the same public key, "

      The example below reproduces this bug:
      ------------------test.java----------------
      import java.io.*;
      import java.security.*;
      import java.util.*;
      public class test extends Identity {

          public test(String name, IdentityScope scope)
              throws KeyManagementException { super(name,scope); }
          
          public static void main(String[] av)
          {
              StubIdentityScope idss = new StubIdentityScope("name");

              StubPublicKey key1 = new StubPublicKey("key1", "name");
              StubPublicKey key2 = new StubPublicKey("key2", "name");
       
              System.out.println("Are keys equal: " + key1.equals(key2));

              test id1, id2;
              try {
                  id1 = new test("name1", idss);
                  id1.setPublicKey(key1);

                  id2 = new test("name2", idss);
              } catch(Exception e) {
                  System.out.println("Failed: unexpected exception " + e);
                  return;
              }

              try {
                  id2.setPublicKey(key2);
                  System.out.println("Failed: no KeyManagementException");
              } catch(KeyManagementException e) {
                  System.out.println("Passed");
              }
          }
      }

      class StubPublicKey implements PublicKey {
          private String key;
          public String name;

          public String getAlgorithm() { return key; }
          public byte[] getEncoded() { return key.getBytes(); }
          public String getFormat() { return key; }

          public StubPublicKey(String k, String n){
              key = k;
              name = n;
          }

          public boolean equals(Object object) {
              if( !(object instanceof StubPublicKey) || object==null )
                  return false;
              return name.equals( ((StubPublicKey)object).name );
          }
      }

      class StubIdentityScope extends IdentityScope {

          public StubIdentityScope(String name) { super(name); }

          public void addIdentity(Identity id) throws KeyManagementException {
              if( getIdentity(id.getName())==null &&
                  getIdentity(id.getPublicKey())==null )
              {
                  idents.put(id.getName(), id);
              } else
                  throw new KeyManagementException("duplicate identity in scope");
          }

          public Identity getIdentity(String name) {
              return (Identity)idents.get(name);
          }

          public Identity getIdentity(PublicKey key) {
              Enumeration e = identities();
              while( e.hasMoreElements() ) {
                  Identity id = (Identity)e.nextElement();
                  if( id.getPublicKey()!=null && id.getPublicKey().equals(key) ||
                      id.getPublicKey()==null && key==null )
                      return id;
              }
              return null;
          }

          public void removeIdentity(Identity id) throws KeyManagementException {
              if( idents.remove(id.getName())==null )
                  throw new KeyManagementException("No such identity: " + id.getName());
          }

          public int size() { return idents.size(); }
          public Enumeration identities() { return idents.elements(); }

          private Hashtable idents = new Hashtable();
      }
      ------------------OUTPUT-------------------
      Are keys equal: true
      Failed: no KeyManagementException

      % java -version
      java version "1.5.0-beta3"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta3-b57)
      Java HotSpot(TM) Server VM (build 1.5.0-beta3-b57, mixed mode)

      ======================================================================

      Attachments

        Activity

          People

            Unassigned Unassigned
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: