Exemplo n.º 1
0
  /**
   * Certificate(TBSCertificate tbsCertificate, AlgorithmIdentifier signatureAlgorithm, byte[]
   * signatureValue) method testing. Makes the certificate, gets its encoded form, makes new
   * certificate from this encoded form by CertificateFactory, and decodes encoded form.
   */
  public void testCertificate() throws Exception {
    // make the TBSCertificate for Certificate
    int version = 2; // v3
    BigInteger serialNumber = BigInteger.valueOf(555L);
    AlgorithmIdentifier signature = new AlgorithmIdentifier("1.2.3.44.555"); // random value
    Name issuer = new Name("O=Certificate Issuer");
    Validity validity = new Validity(new Date(100000000), new Date(200000000));
    Name subject = new Name("O=Subject Organization");
    SubjectPublicKeyInfo subjectPublicKeyInfo =
        new SubjectPublicKeyInfo(new AlgorithmIdentifier("1.2.840.113549.1.1.2"), new byte[10]);
    boolean[] issuerUniqueID =
        new boolean[] {true, false, true, false, true, false, true, false}; // random value
    boolean[] subjectUniqueID =
        new boolean[] {false, true, false, true, false, true, false, true}; // random value
    // make the Extensions for TBSCertificate
    // Subject Alternative Names
    GeneralName[] san =
        new GeneralName[] {
          new GeneralName(
              new OtherName(
                  "1.2.3.4.5",
                  ASN1Integer.getInstance().encode(BigInteger.valueOf(55L).toByteArray()))),
          new GeneralName(1, "*****@*****.**"),
          new GeneralName(2, "dNSName"),
          new GeneralName(new ORAddress()),
          new GeneralName(4, "O=Organization"),
          new GeneralName(new EDIPartyName("assigner", "party")),
          new GeneralName(6, "http://Resource.Id"),
          new GeneralName(new byte[] {1, 1, 1, 1}),
          new GeneralName(8, "1.2.3.4444.55555")
        };
    GeneralNames sans = new GeneralNames(Arrays.asList(san));
    Extension extension = new Extension("2.5.29.17", true, sans.getEncoded());
    Extensions extensions = new Extensions();
    extensions.addExtension(extension);

    byte[] encoding = extensions.getEncoded();
    Extensions.ASN1.decode(encoding);

    TBSCertificate tbsCertificate =
        new TBSCertificate(
            version,
            serialNumber,
            signature,
            issuer,
            validity,
            subject,
            subjectPublicKeyInfo,
            issuerUniqueID,
            subjectUniqueID,
            extensions);

    encoding = tbsCertificate.getEncoded();
    TBSCertificate.ASN1.decode(encoding);

    Certificate certificate = new Certificate(tbsCertificate, signature, new byte[10]);

    encoding = certificate.getEncoded();

    Certificate.ASN1.decode(encoding);

    encoding = Certificate.ASN1.encode(certificate);

    ByteArrayInputStream bais = new ByteArrayInputStream(encoding);

    // try {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    cf.generateCertificate(bais);
    // } catch (CertificateException e) {
    // there is no X.509 certificate factory implementation installed
    // }
  }