home *** CD-ROM | disk | FTP | other *** search
- package sun.security.x509;
-
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.Serializable;
- import java.security.NoSuchAlgorithmException;
- import java.security.Security;
- import sun.security.util.DerInputStream;
- import sun.security.util.DerOutputStream;
- import sun.security.util.DerValue;
- import sun.security.util.ObjectIdentifier;
-
- public class AlgorithmId implements Serializable {
- private static boolean debug;
- private ObjectIdentifier algid;
- protected DerValue params;
- private static final int[] MD2_data = new int[]{1, 2, 840, 113549, 2, 2};
- private static final int[] MD5_data = new int[]{1, 2, 840, 113549, 2, 5};
- private static final int[] SHA1_data = new int[]{1, 3, 14, 3, 2, 26};
- public static final ObjectIdentifier MD2_oid;
- public static final ObjectIdentifier MD5_oid;
- public static final ObjectIdentifier SHA_oid;
- private static final int[] DH_data;
- private static final int[] dsa_data;
- private static final int[] RSA_data;
- private static final int[] RSAEncryption_data;
- public static final ObjectIdentifier DH_oid;
- public static final ObjectIdentifier DSA_oid;
- public static final ObjectIdentifier RSA_oid;
- public static final ObjectIdentifier RSAEncryption_oid;
- private static final int[] md2WithRSAEncryption_data;
- private static final int[] md5WithRSAEncryption_data;
- private static final int[] shaWithDSA_data;
- public static final ObjectIdentifier md2WithRSAEncryption_oid;
- public static final ObjectIdentifier md5WithRSAEncryption_oid;
- public static final ObjectIdentifier shaWithDSA_oid;
-
- public static AlgorithmId getAlgorithmId(String var0) throws NoSuchAlgorithmException {
- return get(var0);
- }
-
- public static AlgorithmId get(String var0) throws NoSuchAlgorithmException {
- ObjectIdentifier var1 = algOID(var0);
- if (var1 == null) {
- throw new NoSuchAlgorithmException("unrecognized algorithm name: " + var0);
- } else {
- return new AlgorithmId(var1);
- }
- }
-
- public static AlgorithmId parse(DerValue var0) throws IOException {
- if (var0.tag != 48) {
- throw new IOException("algid parse error, not a sequence");
- } else {
- DerInputStream var3 = var0.toDerInputStream();
- ObjectIdentifier var1 = var3.getOID();
- DerValue var2;
- if (var3.available() == 0) {
- var2 = null;
- } else {
- var2 = var3.getDerValue();
- if (var2.tag == 5) {
- var2 = null;
- }
- }
-
- AlgorithmId var4 = buildAlgorithmId(var1, var2);
- var4.decodeParams();
- return var4;
- }
- }
-
- private static AlgorithmId buildAlgorithmId(ObjectIdentifier var0, DerValue var1) throws IOException {
- String var2 = Security.getAlgorithmProperty(var0.toString(), "Class");
- if (debug) {
- System.out.println("dynamic class name for [" + var0.toString() + "|Class]: " + var2);
- }
-
- if (var2 != null && !var2.equals("")) {
- try {
- Class var3 = Class.forName(var2);
- Object var4 = var3.newInstance();
- if (!(var4 instanceof AlgorithmId)) {
- System.err.println("Misconfiguration: faulty algorithm config, " + var2);
- return new AlgorithmId(var0, var1);
- } else {
- AlgorithmId var5 = (AlgorithmId)var4;
- var5.algid = var0;
- var5.params = var1;
- var5.decodeParams();
- return var5;
- }
- } catch (ClassNotFoundException var6) {
- System.err.println("Misconfiguration: unknown algorithm class, " + var2);
- return new AlgorithmId(var0, var1);
- } catch (IllegalAccessException var7) {
- throw new IOException(var2 + " [internal error]");
- } catch (InstantiationException var8) {
- System.err.println("Misconfiguration: faulty algorithm class, " + var2);
- return new AlgorithmId(var0, var1);
- }
- } else {
- return new AlgorithmId(var0, var1);
- }
- }
-
- public AlgorithmId(ObjectIdentifier var1) {
- this.algid = var1;
- }
-
- private AlgorithmId(ObjectIdentifier var1, DerValue var2) throws IOException {
- this.algid = var1;
- this.params = var2;
- this.decodeParams();
- }
-
- public AlgorithmId() {
- }
-
- protected void decodeParams() throws IOException {
- }
-
- public final void emit(DerOutputStream var1) throws IOException {
- DerOutputStream var2 = new DerOutputStream();
- var2.putOID(this.algid);
- if (this.params == null) {
- var2.putNull();
- } else {
- var2.putDerValue(this.params);
- }
-
- var1.write((byte)48, var2);
- }
-
- public final void encode(OutputStream var1) throws IOException {
- DerOutputStream var2 = new DerOutputStream();
- this.emit(var2);
- var1.write(((ByteArrayOutputStream)var2).toByteArray());
- }
-
- public final byte[] encode() throws IOException {
- DerOutputStream var1 = new DerOutputStream();
- this.emit(var1);
- return ((ByteArrayOutputStream)var1).toByteArray();
- }
-
- private static ObjectIdentifier algOID(String var0) {
- if (var0.equals("MD5")) {
- return MD5_oid;
- } else if (var0.equals("MD2")) {
- return MD2_oid;
- } else if (!var0.equals("SHA") && !var0.equals("SHA1") && !var0.equals("SHA-1")) {
- if (var0.equals("RSA")) {
- return RSA_oid;
- } else if (!var0.equals("Diffie-Hellman") && !var0.equals("DH")) {
- if (var0.equals("DSA")) {
- return DSA_oid;
- } else if (!var0.equals("MD5withRSA") && !var0.equals("MD5/RSA")) {
- if (!var0.equals("MD2withRSA") && !var0.equals("MD2/RSA")) {
- return !var0.equals("SHAwithDSA") && !var0.equals("SHA/DSA") ? null : shaWithDSA_oid;
- } else {
- return md2WithRSAEncryption_oid;
- }
- } else {
- return md5WithRSAEncryption_oid;
- }
- } else {
- return DH_oid;
- }
- } else {
- return SHA_oid;
- }
- }
-
- private String algName() {
- if (this.algid.equals(MD5_oid)) {
- return "MD5";
- } else if (this.algid.equals(MD2_oid)) {
- return "MD2";
- } else if (this.algid.equals(SHA_oid)) {
- return "SHA";
- } else if (!this.algid.equals(RSAEncryption_oid) && !this.algid.equals(RSA_oid)) {
- if (this.algid.equals(DH_oid)) {
- return "Diffie-Hellman";
- } else if (this.algid.equals(DSA_oid)) {
- return "DSA";
- } else if (this.algid.equals(md5WithRSAEncryption_oid)) {
- return "MD5withRSA";
- } else if (this.algid.equals(md2WithRSAEncryption_oid)) {
- return "MD2withRSA";
- } else {
- return this.algid.equals(shaWithDSA_oid) ? "SHA1withDSA" : "OID." + this.algid.toString();
- }
- } else {
- return "RSA";
- }
- }
-
- public final ObjectIdentifier getOID() {
- return this.algid;
- }
-
- public String getName() {
- return this.algName();
- }
-
- public String toString() {
- return "[" + this.algName() + this.paramsToString() + "]";
- }
-
- protected String paramsToString() {
- return this.params == null ? "" : ", params unparsed";
- }
-
- public boolean equals(AlgorithmId var1) {
- if (!this.algid.equals(var1.algid)) {
- return false;
- } else if (this.params == null && var1.params == null) {
- return true;
- } else {
- return this.params == null ? false : this.params.equals(var1.params);
- }
- }
-
- public boolean equals(Object var1) {
- if (var1 instanceof AlgorithmId) {
- return this.equals((AlgorithmId)var1);
- } else {
- return var1 instanceof ObjectIdentifier ? this.equals((ObjectIdentifier)var1) : false;
- }
- }
-
- public final boolean equals(ObjectIdentifier var1) {
- return this.algid.equals(var1);
- }
-
- static {
- MD2_oid = new ObjectIdentifier(MD2_data);
- MD5_oid = new ObjectIdentifier(MD5_data);
- SHA_oid = new ObjectIdentifier(SHA1_data);
- DH_data = new int[]{1, 2, 840, 113549, 1, 3, 1};
- dsa_data = new int[]{1, 3, 14, 3, 2, 12};
- RSA_data = new int[]{1, 2, 5, 8, 1, 1};
- RSAEncryption_data = new int[]{1, 2, 840, 113549, 1, 1, 1};
- DH_oid = new ObjectIdentifier(DH_data);
- DSA_oid = new ObjectIdentifier(dsa_data);
- RSA_oid = new ObjectIdentifier(RSA_data);
- RSAEncryption_oid = new ObjectIdentifier(RSAEncryption_data);
- md2WithRSAEncryption_data = new int[]{1, 2, 840, 113549, 1, 1, 2};
- md5WithRSAEncryption_data = new int[]{1, 2, 840, 113549, 1, 1, 4};
- shaWithDSA_data = new int[]{1, 3, 14, 3, 2, 13};
- md2WithRSAEncryption_oid = new ObjectIdentifier(md2WithRSAEncryption_data);
- md5WithRSAEncryption_oid = new ObjectIdentifier(md5WithRSAEncryption_data);
- shaWithDSA_oid = new ObjectIdentifier(shaWithDSA_data);
- }
- }
-