home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Notes / 50b2wic.exe / DATA1.CAB / NotesProgramFilesJavaSupport / rt.jar / sun / security / x509 / X509Cert.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-04-23  |  9.2 KB  |  360 lines

  1. package sun.security.x509;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.ObjectInputStream;
  7. import java.io.ObjectOutputStream;
  8. import java.io.OutputStream;
  9. import java.io.Serializable;
  10. import java.security.Certificate;
  11. import java.security.InvalidKeyException;
  12. import java.security.Key;
  13. import java.security.NoSuchAlgorithmException;
  14. import java.security.Principal;
  15. import java.security.PrivateKey;
  16. import java.security.PublicKey;
  17. import java.security.Signature;
  18. import java.security.SignatureException;
  19. import java.util.Date;
  20. import sun.security.util.BigInt;
  21. import sun.security.util.DerInputStream;
  22. import sun.security.util.DerOutputStream;
  23. import sun.security.util.DerValue;
  24.  
  25. public class X509Cert implements Certificate, Serializable {
  26.    protected AlgorithmId algid;
  27.    private byte[] rawCert;
  28.    private byte[] signature;
  29.    private byte[] signedCert;
  30.    private X500Name subject;
  31.    private X509Key pubkey;
  32.    private Date notafter;
  33.    private Date notbefore;
  34.    private int version;
  35.    private BigInt serialnum;
  36.    private X500Name issuer;
  37.    private AlgorithmId issuerSigAlg;
  38.  
  39.    public X509Cert() {
  40.    }
  41.  
  42.    public X509Cert(byte[] var1) throws IOException {
  43.       DerValue var2 = new DerValue(var1);
  44.       this.parse(var2);
  45.       if (var2.data.available() != 0) {
  46.          throw new CertParseError("garbage at end");
  47.       } else {
  48.          this.signedCert = var1;
  49.       }
  50.    }
  51.  
  52.    public X509Cert(byte[] var1, int var2, int var3) throws IOException {
  53.       DerValue var4 = new DerValue(var1, var2, var3);
  54.       this.parse(var4);
  55.       if (var4.data.available() != 0) {
  56.          throw new CertParseError("garbage at end");
  57.       } else {
  58.          this.signedCert = new byte[var3];
  59.          System.arraycopy(var1, var2, this.signedCert, 0, var3);
  60.       }
  61.    }
  62.  
  63.    public X509Cert(DerValue var1) throws IOException {
  64.       this.parse(var1);
  65.       if (var1.data.available() != 0) {
  66.          throw new CertParseError("garbage at end");
  67.       } else {
  68.          this.signedCert = var1.toByteArray();
  69.       }
  70.    }
  71.  
  72.    public X509Cert(X500Name var1, X509Key var2, Date var3, Date var4) throws CertException {
  73.       this.subject = var1;
  74.       if (!(var2 instanceof PublicKey)) {
  75.          throw new CertException(9, "Doesn't implement PublicKey interface");
  76.       } else {
  77.          this.pubkey = var2;
  78.          this.notbefore = var3;
  79.          this.notafter = var4;
  80.          this.version = 0;
  81.       }
  82.    }
  83.  
  84.    public void decode(InputStream var1) throws IOException {
  85.       DerValue var2 = new DerValue(var1);
  86.       this.parse(var2);
  87.       if (var2.data.available() != 0) {
  88.          throw new CertParseError("garbage at end");
  89.       } else {
  90.          this.signedCert = var2.toByteArray();
  91.       }
  92.    }
  93.  
  94.    public void encode(OutputStream var1) throws IOException {
  95.       var1.write(this.getSignedCert());
  96.    }
  97.  
  98.    public boolean equals(Object var1) {
  99.       return var1 instanceof X509Cert ? this.equals((X509Cert)var1) : false;
  100.    }
  101.  
  102.    public boolean equals(X509Cert var1) {
  103.       if (this == var1) {
  104.          return true;
  105.       } else if (this.signedCert != null && var1.signedCert != null) {
  106.          if (this.signedCert.length != var1.signedCert.length) {
  107.             return false;
  108.          } else {
  109.             for(int var2 = 0; var2 < this.signedCert.length; ++var2) {
  110.                if (this.signedCert[var2] != var1.signedCert[var2]) {
  111.                   return false;
  112.                }
  113.             }
  114.  
  115.             return true;
  116.          }
  117.       } else {
  118.          return false;
  119.       }
  120.    }
  121.  
  122.    public String getFormat() {
  123.       return "X.509";
  124.    }
  125.  
  126.    public Principal getGuarantor() {
  127.       return this.getIssuerName();
  128.    }
  129.  
  130.    public Principal getPrincipal() {
  131.       return this.getSubjectName();
  132.    }
  133.  
  134.    public void verify(PublicKey var1) throws CertException {
  135.       Date var2 = new Date();
  136.       if (var2.before(this.notbefore)) {
  137.          throw new CertException(3);
  138.       } else if (var2.after(this.notafter)) {
  139.          throw new CertException(4);
  140.       } else if (this.signedCert == null) {
  141.          throw new CertException(1, "?? certificate is not signed yet ??");
  142.       } else {
  143.          Object var3 = null;
  144.  
  145.          try {
  146.             Object var4 = null;
  147.             String var8 = this.issuerSigAlg.getName();
  148.             Signature var9 = Signature.getInstance(var8);
  149.             var9.initVerify(var1);
  150.             var9.update(this.rawCert, 0, this.rawCert.length);
  151.             if (!var9.verify(this.signature)) {
  152.                throw new CertException(1, "Signature ... by <" + this.issuer + "> for <" + this.subject + ">");
  153.             }
  154.          } catch (NoSuchAlgorithmException var5) {
  155.             throw new CertException(1, "Unsupported signature algorithm (" + var3 + ")");
  156.          } catch (InvalidKeyException var6) {
  157.             throw new CertException(9, "Algorithm (" + var3 + ") rejected public key");
  158.          } catch (SignatureException var7) {
  159.             throw new CertException(1, "Signature by <" + this.issuer + "> for <" + this.subject + ">");
  160.          }
  161.       }
  162.    }
  163.  
  164.    public byte[] encodeAndSign(BigInt var1, X500Signer var2) throws IOException, SignatureException {
  165.       this.rawCert = null;
  166.       this.version = 0;
  167.       this.serialnum = var1;
  168.       this.issuer = var2.getSigner();
  169.       this.issuerSigAlg = var2.getAlgorithmId();
  170.       if (this.subject != null && this.pubkey != null && this.notbefore != null && this.notafter != null) {
  171.          this.rawCert = this.DERencode();
  172.          this.signedCert = this.sign(var2, this.rawCert);
  173.          return this.signedCert;
  174.       } else {
  175.          throw new IOException("not enough cert parameters");
  176.       }
  177.    }
  178.  
  179.    public X500Signer getSigner(AlgorithmId var1, PrivateKey var2) throws NoSuchAlgorithmException, InvalidKeyException {
  180.       if (var2 instanceof Key) {
  181.          String var3 = var2.getAlgorithm();
  182.          Signature var4 = Signature.getInstance(var1.getName());
  183.          if (!this.pubkey.getAlgorithm().equals(var3)) {
  184.             throw new InvalidKeyException("Private key algorithm " + var3 + " incompatible with certificate " + this.pubkey.getAlgorithm());
  185.          } else {
  186.             var4.initSign(var2);
  187.             return new X500Signer(var4, this.subject);
  188.          }
  189.       } else {
  190.          throw new InvalidKeyException("private key not a key!");
  191.       }
  192.    }
  193.  
  194.    public Signature getVerifier(String var1) throws NoSuchAlgorithmException, InvalidKeyException {
  195.       Signature var2 = Signature.getInstance(var1);
  196.       var2.initVerify(this.pubkey);
  197.       return var2;
  198.    }
  199.  
  200.    public byte[] getSignedCert() {
  201.       return this.signedCert;
  202.    }
  203.  
  204.    public BigInt getSerialNumber() {
  205.       return this.serialnum;
  206.    }
  207.  
  208.    public X500Name getSubjectName() {
  209.       return this.subject;
  210.    }
  211.  
  212.    public X500Name getIssuerName() {
  213.       return this.issuer;
  214.    }
  215.  
  216.    public AlgorithmId getIssuerAlgorithmId() {
  217.       return this.issuerSigAlg;
  218.    }
  219.  
  220.    public Date getNotBefore() {
  221.       return this.notbefore;
  222.    }
  223.  
  224.    public Date getNotAfter() {
  225.       return this.notafter;
  226.    }
  227.  
  228.    public PublicKey getPublicKey() {
  229.       return this.pubkey;
  230.    }
  231.  
  232.    public int getVersion() {
  233.       return this.version;
  234.    }
  235.  
  236.    public int hashCode() {
  237.       int var1 = 0;
  238.  
  239.       for(int var2 = 0; var2 < this.signedCert.length; ++var2) {
  240.          var1 += this.signedCert[var2] * var2;
  241.       }
  242.  
  243.       return var1;
  244.    }
  245.  
  246.    public String toString() {
  247.       if (this.subject != null && this.pubkey != null && this.notbefore != null && this.notafter != null && this.issuer != null && this.issuerSigAlg != null && this.serialnum != null) {
  248.          String var1 = "  X.509v" + (this.version + 1) + " certificate,\n";
  249.          var1 = var1 + "  Subject is " + this.subject + "\n";
  250.          var1 = var1 + "  Key:  " + this.pubkey;
  251.          var1 = var1 + "  Validity <" + this.notbefore + "> until <" + this.notafter + ">\n";
  252.          var1 = var1 + "  Issuer is " + this.issuer + "\n";
  253.          var1 = var1 + "  Issuer signature used " + this.issuerSigAlg.toString() + "\n";
  254.          var1 = var1 + "  Serial number = " + this.serialnum + "\n";
  255.          return "[\n" + var1 + "]";
  256.       } else {
  257.          throw new NullPointerException("X.509 cert is incomplete");
  258.       }
  259.    }
  260.  
  261.    public String toString(boolean var1) {
  262.       return this.toString();
  263.    }
  264.  
  265.    private void parse(DerValue var1) throws IOException {
  266.       DerValue[] var2 = new DerValue[]{var1.data.getDerValue(), var1.data.getDerValue(), var1.data.getDerValue()};
  267.       if (var1.data.available() != 0) {
  268.          throw new CertParseError("signed overrun, bytes = " + var1.data.available());
  269.       } else if (var2[0].tag != 48) {
  270.          throw new CertParseError("signed fields invalid");
  271.       } else {
  272.          this.rawCert = var2[0].toByteArray();
  273.          this.issuerSigAlg = AlgorithmId.parse(var2[1]);
  274.          this.signature = var2[2].getBitString();
  275.          if (var2[1].data.available() != 0) {
  276.             throw new CertParseError("algid field overrun");
  277.          } else if (var2[2].data.available() != 0) {
  278.             throw new CertParseError("signed fields overrun");
  279.          } else {
  280.             DerInputStream var3 = var2[0].data;
  281.             this.version = 0;
  282.             DerValue var4 = var3.getDerValue();
  283.             if (var4.isConstructed() && var4.isContextSpecific()) {
  284.                this.version = var4.data.getInteger().toInt();
  285.                if (var4.data.available() != 0) {
  286.                   throw new IOException("X.509 version, bad format");
  287.                }
  288.  
  289.                var4 = var3.getDerValue();
  290.             }
  291.  
  292.             this.serialnum = var4.getInteger();
  293.             var4 = var3.getDerValue();
  294.             AlgorithmId var5 = AlgorithmId.parse(var4);
  295.             if (!var5.equals(this.issuerSigAlg)) {
  296.                throw new CertParseError("CA Algorithm mismatch!");
  297.             } else {
  298.                this.algid = var5;
  299.                this.issuer = new X500Name(var3);
  300.                var4 = var3.getDerValue();
  301.                if (var4.tag != 48) {
  302.                   throw new CertParseError("corrupt validity field");
  303.                } else {
  304.                   this.notbefore = var4.data.getUTCTime();
  305.                   this.notafter = var4.data.getUTCTime();
  306.                   if (var4.data.available() != 0) {
  307.                      throw new CertParseError("excess validity data");
  308.                   } else {
  309.                      this.subject = new X500Name(var3);
  310.                      var4 = var3.getDerValue();
  311.                      this.pubkey = X509Key.parse(var4);
  312.                      var3.available();
  313.                   }
  314.                }
  315.             }
  316.          }
  317.       }
  318.    }
  319.  
  320.    private byte[] DERencode() throws IOException {
  321.       DerOutputStream var1 = new DerOutputStream();
  322.       this.encode(var1);
  323.       return ((ByteArrayOutputStream)var1).toByteArray();
  324.    }
  325.  
  326.    private void encode(DerOutputStream var1) throws IOException {
  327.       DerOutputStream var2 = new DerOutputStream();
  328.       var2.putInteger(this.serialnum);
  329.       this.issuerSigAlg.emit(var2);
  330.       this.issuer.emit(var2);
  331.       DerOutputStream var3 = new DerOutputStream();
  332.       var3.putUTCTime(this.notbefore);
  333.       var3.putUTCTime(this.notafter);
  334.       var2.write((byte)48, var3);
  335.       this.subject.emit(var2);
  336.       this.pubkey.emit(var2);
  337.       var1.write((byte)48, var2);
  338.    }
  339.  
  340.    private byte[] sign(X500Signer var1, byte[] var2) throws IOException, SignatureException {
  341.       DerOutputStream var3 = new DerOutputStream();
  342.       DerOutputStream var4 = new DerOutputStream();
  343.       ((OutputStream)var4).write(var2);
  344.       var1.getAlgorithmId().emit(var4);
  345.       var1.update(var2, 0, var2.length);
  346.       this.signature = var1.sign();
  347.       var4.putBitString(this.signature);
  348.       var3.write((byte)48, var4);
  349.       return ((ByteArrayOutputStream)var3).toByteArray();
  350.    }
  351.  
  352.    private synchronized void writeObject(ObjectOutputStream var1) throws IOException {
  353.       this.encode((OutputStream)var1);
  354.    }
  355.  
  356.    private synchronized void readObject(ObjectInputStream var1) throws IOException {
  357.       this.decode(var1);
  358.    }
  359. }
  360.