home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VPage / Java.bin / CLASSES.ZIP / sun / security / x509 / AlgorithmId.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-07-08  |  6.8 KB  |  258 lines

  1. package sun.security.x509;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.OutputStream;
  6. import java.io.Serializable;
  7. import java.security.NoSuchAlgorithmException;
  8. import java.security.Security;
  9. import sun.security.util.DerInputStream;
  10. import sun.security.util.DerOutputStream;
  11. import sun.security.util.DerValue;
  12. import sun.security.util.ObjectIdentifier;
  13.  
  14. public class AlgorithmId implements Serializable {
  15.    private static boolean debug;
  16.    private ObjectIdentifier algid;
  17.    protected DerValue params;
  18.    private static final int[] MD2_data = new int[]{1, 2, 840, 113549, 2, 2};
  19.    private static final int[] MD5_data = new int[]{1, 2, 840, 113549, 2, 5};
  20.    private static final int[] SHA1_data = new int[]{1, 3, 14, 3, 2, 26};
  21.    public static final ObjectIdentifier MD2_oid;
  22.    public static final ObjectIdentifier MD5_oid;
  23.    public static final ObjectIdentifier SHA_oid;
  24.    private static final int[] DH_data;
  25.    private static final int[] dsa_data;
  26.    private static final int[] RSA_data;
  27.    private static final int[] RSAEncryption_data;
  28.    public static final ObjectIdentifier DH_oid;
  29.    public static final ObjectIdentifier DSA_oid;
  30.    public static final ObjectIdentifier RSA_oid;
  31.    public static final ObjectIdentifier RSAEncryption_oid;
  32.    private static final int[] md2WithRSAEncryption_data;
  33.    private static final int[] md5WithRSAEncryption_data;
  34.    private static final int[] shaWithDSA_data;
  35.    public static final ObjectIdentifier md2WithRSAEncryption_oid;
  36.    public static final ObjectIdentifier md5WithRSAEncryption_oid;
  37.    public static final ObjectIdentifier shaWithDSA_oid;
  38.  
  39.    public static AlgorithmId getAlgorithmId(String var0) throws NoSuchAlgorithmException {
  40.       return get(var0);
  41.    }
  42.  
  43.    public static AlgorithmId get(String var0) throws NoSuchAlgorithmException {
  44.       ObjectIdentifier var1 = algOID(var0);
  45.       if (var1 == null) {
  46.          throw new NoSuchAlgorithmException("unrecognized algorithm name: " + var0);
  47.       } else {
  48.          return new AlgorithmId(var1);
  49.       }
  50.    }
  51.  
  52.    public static AlgorithmId parse(DerValue var0) throws IOException {
  53.       if (var0.tag != 48) {
  54.          throw new IOException("algid parse error, not a sequence");
  55.       } else {
  56.          DerInputStream var3 = var0.toDerInputStream();
  57.          ObjectIdentifier var1 = var3.getOID();
  58.          DerValue var2;
  59.          if (var3.available() == 0) {
  60.             var2 = null;
  61.          } else {
  62.             var2 = var3.getDerValue();
  63.             if (var2.tag == 5) {
  64.                var2 = null;
  65.             }
  66.          }
  67.  
  68.          AlgorithmId var4 = buildAlgorithmId(var1, var2);
  69.          var4.decodeParams();
  70.          return var4;
  71.       }
  72.    }
  73.  
  74.    private static AlgorithmId buildAlgorithmId(ObjectIdentifier var0, DerValue var1) throws IOException {
  75.       String var2 = Security.getAlgorithmProperty(var0.toString(), "Class");
  76.       if (debug) {
  77.          System.out.println("dynamic class name for [" + var0.toString() + "|Class]: " + var2);
  78.       }
  79.  
  80.       if (var2 != null && !var2.equals("")) {
  81.          try {
  82.             Class var3 = Class.forName(var2);
  83.             Object var4 = var3.newInstance();
  84.             if (!(var4 instanceof AlgorithmId)) {
  85.                System.err.println("Misconfiguration:  faulty algorithm config, " + var2);
  86.                return new AlgorithmId(var0, var1);
  87.             } else {
  88.                AlgorithmId var5 = (AlgorithmId)var4;
  89.                var5.algid = var0;
  90.                var5.params = var1;
  91.                var5.decodeParams();
  92.                return var5;
  93.             }
  94.          } catch (ClassNotFoundException var6) {
  95.             System.err.println("Misconfiguration:  unknown algorithm class, " + var2);
  96.             return new AlgorithmId(var0, var1);
  97.          } catch (IllegalAccessException var7) {
  98.             throw new IOException(var2 + " [internal error]");
  99.          } catch (InstantiationException var8) {
  100.             System.err.println("Misconfiguration:  faulty algorithm class, " + var2);
  101.             return new AlgorithmId(var0, var1);
  102.          }
  103.       } else {
  104.          return new AlgorithmId(var0, var1);
  105.       }
  106.    }
  107.  
  108.    public AlgorithmId(ObjectIdentifier var1) {
  109.       this.algid = var1;
  110.    }
  111.  
  112.    private AlgorithmId(ObjectIdentifier var1, DerValue var2) throws IOException {
  113.       this.algid = var1;
  114.       this.params = var2;
  115.       this.decodeParams();
  116.    }
  117.  
  118.    public AlgorithmId() {
  119.    }
  120.  
  121.    protected void decodeParams() throws IOException {
  122.    }
  123.  
  124.    public final void emit(DerOutputStream var1) throws IOException {
  125.       DerOutputStream var2 = new DerOutputStream();
  126.       var2.putOID(this.algid);
  127.       if (this.params == null) {
  128.          var2.putNull();
  129.       } else {
  130.          var2.putDerValue(this.params);
  131.       }
  132.  
  133.       var1.write((byte)48, var2);
  134.    }
  135.  
  136.    public final void encode(OutputStream var1) throws IOException {
  137.       DerOutputStream var2 = new DerOutputStream();
  138.       this.emit(var2);
  139.       var1.write(((ByteArrayOutputStream)var2).toByteArray());
  140.    }
  141.  
  142.    public final byte[] encode() throws IOException {
  143.       DerOutputStream var1 = new DerOutputStream();
  144.       this.emit(var1);
  145.       return ((ByteArrayOutputStream)var1).toByteArray();
  146.    }
  147.  
  148.    private static ObjectIdentifier algOID(String var0) {
  149.       if (var0.equals("MD5")) {
  150.          return MD5_oid;
  151.       } else if (var0.equals("MD2")) {
  152.          return MD2_oid;
  153.       } else if (!var0.equals("SHA") && !var0.equals("SHA1") && !var0.equals("SHA-1")) {
  154.          if (var0.equals("RSA")) {
  155.             return RSA_oid;
  156.          } else if (!var0.equals("Diffie-Hellman") && !var0.equals("DH")) {
  157.             if (var0.equals("DSA")) {
  158.                return DSA_oid;
  159.             } else if (!var0.equals("MD5withRSA") && !var0.equals("MD5/RSA")) {
  160.                if (!var0.equals("MD2withRSA") && !var0.equals("MD2/RSA")) {
  161.                   return !var0.equals("SHAwithDSA") && !var0.equals("SHA/DSA") ? null : shaWithDSA_oid;
  162.                } else {
  163.                   return md2WithRSAEncryption_oid;
  164.                }
  165.             } else {
  166.                return md5WithRSAEncryption_oid;
  167.             }
  168.          } else {
  169.             return DH_oid;
  170.          }
  171.       } else {
  172.          return SHA_oid;
  173.       }
  174.    }
  175.  
  176.    private String algName() {
  177.       if (this.algid.equals(MD5_oid)) {
  178.          return "MD5";
  179.       } else if (this.algid.equals(MD2_oid)) {
  180.          return "MD2";
  181.       } else if (this.algid.equals(SHA_oid)) {
  182.          return "SHA";
  183.       } else if (!this.algid.equals(RSAEncryption_oid) && !this.algid.equals(RSA_oid)) {
  184.          if (this.algid.equals(DH_oid)) {
  185.             return "Diffie-Hellman";
  186.          } else if (this.algid.equals(DSA_oid)) {
  187.             return "DSA";
  188.          } else if (this.algid.equals(md5WithRSAEncryption_oid)) {
  189.             return "MD5withRSA";
  190.          } else if (this.algid.equals(md2WithRSAEncryption_oid)) {
  191.             return "MD2withRSA";
  192.          } else {
  193.             return this.algid.equals(shaWithDSA_oid) ? "SHA1withDSA" : "OID." + this.algid.toString();
  194.          }
  195.       } else {
  196.          return "RSA";
  197.       }
  198.    }
  199.  
  200.    public final ObjectIdentifier getOID() {
  201.       return this.algid;
  202.    }
  203.  
  204.    public String getName() {
  205.       return this.algName();
  206.    }
  207.  
  208.    public String toString() {
  209.       return "[" + this.algName() + this.paramsToString() + "]";
  210.    }
  211.  
  212.    protected String paramsToString() {
  213.       return this.params == null ? "" : ", params unparsed";
  214.    }
  215.  
  216.    public boolean equals(AlgorithmId var1) {
  217.       if (!this.algid.equals(var1.algid)) {
  218.          return false;
  219.       } else if (this.params == null && var1.params == null) {
  220.          return true;
  221.       } else {
  222.          return this.params == null ? false : this.params.equals(var1.params);
  223.       }
  224.    }
  225.  
  226.    public boolean equals(Object var1) {
  227.       if (var1 instanceof AlgorithmId) {
  228.          return this.equals((AlgorithmId)var1);
  229.       } else {
  230.          return var1 instanceof ObjectIdentifier ? this.equals((ObjectIdentifier)var1) : false;
  231.       }
  232.    }
  233.  
  234.    public final boolean equals(ObjectIdentifier var1) {
  235.       return this.algid.equals(var1);
  236.    }
  237.  
  238.    static {
  239.       MD2_oid = new ObjectIdentifier(MD2_data);
  240.       MD5_oid = new ObjectIdentifier(MD5_data);
  241.       SHA_oid = new ObjectIdentifier(SHA1_data);
  242.       DH_data = new int[]{1, 2, 840, 113549, 1, 3, 1};
  243.       dsa_data = new int[]{1, 3, 14, 3, 2, 12};
  244.       RSA_data = new int[]{1, 2, 5, 8, 1, 1};
  245.       RSAEncryption_data = new int[]{1, 2, 840, 113549, 1, 1, 1};
  246.       DH_oid = new ObjectIdentifier(DH_data);
  247.       DSA_oid = new ObjectIdentifier(dsa_data);
  248.       RSA_oid = new ObjectIdentifier(RSA_data);
  249.       RSAEncryption_oid = new ObjectIdentifier(RSAEncryption_data);
  250.       md2WithRSAEncryption_data = new int[]{1, 2, 840, 113549, 1, 1, 2};
  251.       md5WithRSAEncryption_data = new int[]{1, 2, 840, 113549, 1, 1, 4};
  252.       shaWithDSA_data = new int[]{1, 3, 14, 3, 2, 13};
  253.       md2WithRSAEncryption_oid = new ObjectIdentifier(md2WithRSAEncryption_data);
  254.       md5WithRSAEncryption_oid = new ObjectIdentifier(md5WithRSAEncryption_data);
  255.       shaWithDSA_oid = new ObjectIdentifier(shaWithDSA_data);
  256.    }
  257. }
  258.