home *** CD-ROM | disk | FTP | other *** search
- package netscape.security;
-
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.StringTokenizer;
- import netscape.util.ClassInfo;
- import netscape.util.Codable;
- import netscape.util.CodingException;
- import netscape.util.Decoder;
- import netscape.util.Encoder;
-
- public final class Principal implements Codable {
- static final int VERSION = 1;
- public static final int CODEBASE_EXACT = 10;
- public static final int CODEBASE_REGEXP = 11;
- public static final int CERT = 12;
- public static final int CERT_FINGERPRINT = 13;
- public static final int CERT_KEY = 14;
- static final int COMPANY_NAME = 1;
- static final int CERT_AUTH = 2;
- static final int SERIAL_NO = 3;
- static final int EXP_DATE = 4;
- static final int NICKNAME = 5;
- static final int FINGERPRINT = 6;
- private int itsType;
- private int itsHashCode;
- private String itsStringRep;
- private byte[] itsBinaryRep;
- private byte[] itsFingerprint;
- private byte[] itsCertKey;
- private int itsZig;
- private boolean itsInited;
- private String itsNickname;
- private String itsAsciiFingerPrint;
- private String itsExpDate;
- private String itsSerialNo;
- private String itsCertAuth;
- private String itsCompanyName;
-
- public Principal() {
- this.itsInited = false;
- }
-
- public Principal(URL var1) {
- this.itsInited = false;
- this.itsStringRep = var1.toString();
- this.itsType = 10;
- this.computeHashCode();
- this.itsZig = 0;
- this.itsInited = true;
- }
-
- public Principal(int var1, String var2) {
- this.itsInited = false;
- switch (var1) {
- case 10:
- case 11:
- this.itsStringRep = var2;
- break;
- case 12:
- case 13:
- StringTokenizer var3 = new StringTokenizer(var2, ":;, \t");
- this.itsBinaryRep = new byte[var3.countTokens()];
-
- for(int var4 = 0; var3.hasMoreElements(); ++var4) {
- this.itsBinaryRep[var4] = (byte)Integer.parseInt(var3.nextToken(), 16);
- }
-
- if (var1 == 12) {
- this.certToFingerprint();
- } else {
- this.itsFingerprint = this.itsBinaryRep;
- }
- break;
- default:
- throw new IllegalArgumentException("unknown principal type");
- }
-
- this.itsType = var1;
- this.computeHashCode();
- this.itsZig = 0;
- this.itsInited = true;
- }
-
- public Principal(int var1, byte[] var2) {
- this.itsInited = false;
- switch (var1) {
- case 10:
- case 11:
- this.itsStringRep = new String(var2, 0);
- break;
- case 12:
- case 13:
- case 14:
- try {
- this.itsBinaryRep = (byte[])var2.clone();
- } catch (CloneNotSupportedException var3) {
- throw new IllegalArgumentException("bogus value");
- }
-
- if (var1 == 12) {
- this.certToFingerprint();
- } else if (var1 == 14) {
- this.itsCertKey = this.itsBinaryRep;
- } else {
- this.itsFingerprint = this.itsBinaryRep;
- }
- break;
- default:
- throw new IllegalArgumentException("unknown principal type");
- }
-
- this.itsType = var1;
- this.computeHashCode();
- this.itsZig = 0;
- this.itsInited = true;
- }
-
- public Principal(int var1, byte[] var2, Class var3) {
- this(var1, var2);
- this.itsZig = getZigPtr(var3);
- }
-
- private boolean compareByteArrays(byte[] var1, byte[] var2) {
- if (var1.length != var2.length) {
- return false;
- } else {
- for(int var3 = 0; var3 < var2.length; ++var3) {
- if (var1[var3] != var2[var3]) {
- return false;
- }
- }
-
- return true;
- }
- }
-
- public boolean equals(Object var1) {
- if (var1 == this) {
- return true;
- } else if (!(var1 instanceof Principal)) {
- return false;
- } else {
- Principal var2 = (Principal)var1;
- switch (this.itsType) {
- case 10:
- case 11:
- switch (var2.itsType) {
- case 10:
- case 11:
- return this.itsStringRep.equals(var2.itsStringRep);
- default:
- return false;
- }
- case 12:
- case 13:
- switch (var2.itsType) {
- case 12:
- case 13:
- return this.compareByteArrays(this.itsFingerprint, var2.itsFingerprint);
- default:
- return false;
- }
- case 14:
- switch (var2.itsType) {
- case 14:
- return this.compareByteArrays(this.itsCertKey, var2.itsCertKey);
- default:
- return false;
- }
- default:
- return false;
- }
- }
- }
-
- public int hashCode() {
- return this.itsHashCode;
- }
-
- private int computeHashCode(byte[] var1) {
- int var2 = 0;
-
- for(int var3 = 0; var3 < var1.length; ++var3) {
- var2 = var2 * 37 + var1[var3];
- }
-
- return var2;
- }
-
- private void computeHashCode() {
- switch (this.itsType) {
- case 10:
- case 11:
- this.itsHashCode = this.itsStringRep.hashCode();
- return;
- case 12:
- case 13:
- this.itsHashCode = this.computeHashCode(this.itsFingerprint);
- return;
- case 14:
- this.itsHashCode = this.computeHashCode(this.itsCertKey);
- return;
- default:
- }
- }
-
- public boolean isCodebase() {
- return this.itsType == 10 || this.itsType == 11;
- }
-
- public boolean isCodebaseExact() {
- return this.itsType == 10;
- }
-
- public boolean isCodebaseRegexp() {
- return this.itsType == 11;
- }
-
- boolean isSecurePrincipal() {
- if (this == PrivilegeManager.getUnknownPrincipal()) {
- System.out.println("Principal: Internal Error: Unknown principal");
- return false;
- } else if (!this.isCodebase()) {
- return true;
- } else {
- URL var1;
- try {
- var1 = new URL(this.itsStringRep);
- } catch (MalformedURLException var4) {
- return false;
- }
-
- String var2 = var1.getProtocol();
- if (!var2.equals("https") && !var2.equals("file")) {
- PrivilegeManager var3 = PrivilegeManager.getPrivilegeManager();
- return var2.equals("http") && !var3.getJavaSecurityEnabled();
- } else {
- return true;
- }
- }
- }
-
- public boolean isCert() {
- return this.itsType == 12;
- }
-
- public boolean isCertFingerprint() {
- return this.itsType == 13 || this.itsType == 14;
- }
-
- public String toString() {
- switch (this.itsType) {
- case 10:
- case 11:
- return this.itsStringRep;
- case 12:
- case 13:
- StringBuffer var1 = new StringBuffer();
-
- for(int var2 = 0; var2 < this.itsBinaryRep.length; ++var2) {
- if (var2 > 0) {
- var1.append(":");
- }
-
- var1.append(Integer.toHexString(255 & this.itsBinaryRep[var2]));
- }
-
- return var1.toString();
- case 14:
- return this.getNickname();
- default:
- return this.itsStringRep;
- }
- }
-
- public String toVerboseString() {
- String var1 = this.toString();
- switch (this.itsType) {
- case 10:
- return "CODEBASE_EXACT: " + var1;
- case 11:
- return "CODEBASE_REGEXP: " + var1;
- case 12:
- return "CERT: " + var1;
- case 13:
- return "CERT_FINGERPRINT: " + var1;
- case 14:
- return "CERT_FINGERPRINT: " + var1 + " " + this.getFingerPrint();
- default:
- return "Bogus type (" + this.itsType + "): " + var1;
- }
- }
-
- private void certToFingerprint() {
- int var1 = 0;
-
- for(int var2 = 0; var2 < this.itsBinaryRep.length; ++var2) {
- var1 = var1 * 37 + this.itsBinaryRep[var2];
- }
-
- String var3 = Integer.toString(var1);
- byte[] var4 = new byte[var3.length()];
- var3.getBytes(0, var3.length() - 1, var4, 0);
- this.itsFingerprint = var4;
- }
-
- public String getVendor() {
- switch (this.itsType) {
- case 10:
- case 11:
- default:
- return this.itsStringRep;
- case 12:
- case 13:
- case 14:
- return this.getNickname();
- }
- }
-
- public void describeClassInfo(ClassInfo var1) {
- var1.addClass("Principal", 1);
- var1.addField("Type", (byte)8);
- var1.addField("HashCode", (byte)8);
- var1.addField("StringRep", (byte)16);
- var1.addField("BinaryRep", (byte)5);
- var1.addField("Fingerprint", (byte)5);
- var1.addField("CertKey", (byte)5);
- }
-
- public void encode(Encoder var1) throws CodingException {
- var1.encodeInt("Type", this.itsType);
- var1.encodeInt("HashCode", this.itsHashCode);
- var1.encodeString("StringRep", this.itsStringRep);
- if (this.itsType == 12 || this.itsType == 13 || this.itsType == 14) {
- var1.encodeByteArray("BinaryRep", this.itsBinaryRep, 0, this.itsBinaryRep.length);
- var1.encodeByteArray("Fingerprint", this.itsFingerprint, 0, this.itsFingerprint == null ? 0 : this.itsFingerprint.length);
- var1.encodeByteArray("CertKey", this.itsCertKey, 0, this.itsCertKey == null ? 0 : this.itsCertKey.length);
- }
-
- }
-
- public void decode(Decoder var1) throws CodingException {
- if (!this.itsInited) {
- this.itsInited = true;
- this.itsType = var1.decodeInt("Type");
- this.itsHashCode = var1.decodeInt("HashCode");
- this.itsStringRep = var1.decodeString("StringRep");
- if (this.itsType == 12 || this.itsType == 13 || this.itsType == 14) {
- this.itsBinaryRep = var1.decodeByteArray("BinaryRep");
- this.itsFingerprint = var1.decodeByteArray("Fingerprint");
- this.itsCertKey = var1.decodeByteArray("CertKey");
- }
-
- }
- }
-
- public void finishDecoding() throws CodingException {
- }
-
- String getCompanyName() {
- if (this.itsCompanyName == null) {
- this.itsCompanyName = this.getCertAttribute(this.itsZig, 1, this.itsCertKey);
- }
-
- return this.itsCompanyName;
- }
-
- String getSecAuth() {
- if (this.itsCertAuth == null) {
- this.itsCertAuth = this.getCertAttribute(this.itsZig, 2, this.itsCertKey);
- }
-
- return this.itsCertAuth;
- }
-
- String getSerialNo() {
- if (this.itsSerialNo == null) {
- this.itsSerialNo = this.getCertAttribute(this.itsZig, 3, this.itsCertKey);
- }
-
- return this.itsSerialNo;
- }
-
- String getExpDate() {
- if (this.itsExpDate == null) {
- this.itsExpDate = this.getCertAttribute(this.itsZig, 4, this.itsCertKey);
- }
-
- return this.itsExpDate;
- }
-
- public String getFingerPrint() {
- switch (this.itsType) {
- case 10:
- case 11:
- case 12:
- case 13:
- return this.toString();
- default:
- if (this.itsAsciiFingerPrint == null) {
- this.itsAsciiFingerPrint = this.getCertAttribute(this.itsZig, 6, this.itsCertKey);
- }
-
- return this.itsAsciiFingerPrint;
- }
- }
-
- public String getNickname() {
- if (this.itsType == 12 && this == PrivilegeManager.getUnsignedPrincipal()) {
- return UserDialogHelper.getUnsignedPrincipal();
- } else {
- if (this.itsNickname == null) {
- this.itsNickname = this.getCertAttribute(this.itsZig, 5, this.itsCertKey);
- }
-
- return this.itsNickname;
- }
- }
-
- boolean savePrincipalPermanently() {
- return !this.isCodebase() && this.itsZig != 0 ? this.saveCert(this.itsZig, this.itsCertKey) : true;
- }
-
- static Principal createPrincipal(String var0) {
- byte[] var1 = getPrincipalCertKey(var0);
- if (var1 != null) {
- return new Principal(14, var1);
- } else {
- throw new IllegalArgumentException("Unknown principal " + var0);
- }
- }
-
- static Principal createSystemPrincipal() {
- Object[] var0 = getSystemPrincipalCertKey();
- if (var0 != null && var0.length > 0) {
- return new Principal(14, (byte[])var0[0]);
- } else {
- throw new IllegalArgumentException("Unknown system principal ");
- }
- }
-
- static boolean isSystemPrincipal(byte[] var0) {
- Principal var1 = PrivilegeManager.getSystemPrincipal();
- return var1.itsCertKey == null ? false : var1.compareByteArrays(var0, var1.itsCertKey);
- }
-
- public boolean isSystemPrincipal() {
- return this.equals(PrivilegeManager.getSystemPrincipal());
- }
-
- private static Principal[] getPrincipalAry(Class var0, String var1, Object[] var2) {
- Principal var3 = null;
- int var4 = 0;
- if (var2 != null) {
- var4 += var2.length;
- }
-
- if (var4 == 0) {
- return PrivilegeManager.getUnknownPrincipalArray();
- } else {
- var3 = new Principal[var4];
- int var5 = var4;
-
- while(var5-- > 0) {
- if (isSystemPrincipal((byte[])var2[var5])) {
- ((Object[])var3)[var5] = PrivilegeManager.getSystemPrincipal();
- } else {
- ((Object[])var3)[var5] = new Principal(14, (byte[])var2[var5], var0);
- }
- }
-
- return var3;
- }
- }
-
- private static native int getZigPtr(Class var0);
-
- native String getCertAttribute(int var1, int var2, byte[] var3);
-
- static native byte[] getPrincipalCertKey(String var0);
-
- static native Object[] getSystemPrincipalCertKey();
-
- private native boolean saveCert(int var1, byte[] var2);
- }
-