protected void encodePayload() throws IOException {
   ByteArrayOutputStream bout = new ByteArrayOutputStream(1024);
   byte[] enc = null;
   try {
     for (int i = 0; i < path.length; i++) {
       bout.write(path[i].getEncoded());
     }
   } catch (CertificateEncodingException cee) {
     throw new IOException(cee.toString());
   }
   payload = bout.toByteArray();
 }
Example #2
0
  public void writeObject(Object var1) throws IOException {
    String var2;
    byte[] var3;
    byte[] var4;
    if (var1 instanceof X509Certificate) {
      var2 = "CERTIFICATE";

      try {
        var3 = ((X509Certificate) var1).getEncoded();
      } catch (CertificateEncodingException var32) {
        StringBuilder var6 = (new StringBuilder()).append("Cannot encode object: ");
        String var7 = var32.toString();
        String var8 = var6.append(var7).toString();
        throw new IOException(var8);
      }

      var4 = var3;
    } else if (var1 instanceof X509CRL) {
      var2 = "X509 CRL";

      try {
        var3 = ((X509CRL) var1).getEncoded();
      } catch (CRLException var31) {
        StringBuilder var10 = (new StringBuilder()).append("Cannot encode object: ");
        String var11 = var31.toString();
        String var12 = var10.append(var11).toString();
        throw new IOException(var12);
      }

      var4 = var3;
    } else {
      if (var1 instanceof KeyPair) {
        PrivateKey var13 = ((KeyPair) var1).getPrivate();
        this.writeObject(var13);
        return;
      }

      if (var1 instanceof PrivateKey) {
        ASN1Sequence var14 = (ASN1Sequence) ASN1Object.fromByteArray(((Key) var1).getEncoded());
        PrivateKeyInfo var15 = new PrivateKeyInfo(var14);
        if (var1 instanceof RSAPrivateKey) {
          var2 = "RSA PRIVATE KEY";
          var4 = var15.getPrivateKey().getEncoded();
        } else if (var1 instanceof DSAPrivateKey) {
          var2 = "DSA PRIVATE KEY";
          DSAParameter var16 = DSAParameter.getInstance(var15.getAlgorithmId().getParameters());
          ASN1EncodableVector var17 = new ASN1EncodableVector();
          DERInteger var18 = new DERInteger(0);
          var17.add(var18);
          BigInteger var19 = var16.getP();
          DERInteger var20 = new DERInteger(var19);
          var17.add(var20);
          BigInteger var21 = var16.getQ();
          DERInteger var22 = new DERInteger(var21);
          var17.add(var22);
          BigInteger var23 = var16.getG();
          DERInteger var24 = new DERInteger(var23);
          var17.add(var24);
          BigInteger var25 = ((DSAPrivateKey) var1).getX();
          BigInteger var26 = var16.getG();
          BigInteger var27 = var16.getP();
          BigInteger var28 = var26.modPow(var25, var27);
          DERInteger var29 = new DERInteger(var28);
          var17.add(var29);
          DERInteger var30 = new DERInteger(var25);
          var17.add(var30);
          var4 = (new DERSequence(var17)).getEncoded();
        } else {
          if (!((PrivateKey) var1).getAlgorithm().equals("ECDSA")) {
            throw new IOException("Cannot identify private key");
          }

          var2 = "EC PRIVATE KEY";
          var4 = var15.getPrivateKey().getEncoded();
        }
      } else if (var1 instanceof PublicKey) {
        var2 = "PUBLIC KEY";
        var4 = ((PublicKey) var1).getEncoded();
      } else if (var1 instanceof X509AttributeCertificate) {
        var2 = "ATTRIBUTE CERTIFICATE";
        var4 = ((X509V2AttributeCertificate) var1).getEncoded();
      } else if (var1 instanceof PKCS10CertificationRequest) {
        var2 = "CERTIFICATE REQUEST";
        var4 = ((PKCS10CertificationRequest) var1).getEncoded();
      } else {
        if (!(var1 instanceof ContentInfo)) {
          throw new IOException("unknown object passed - can\'t encode.");
        }

        var2 = "PKCS7";
        var4 = ((ContentInfo) var1).getEncoded();
      }
    }

    this.writeHeader(var2);
    this.writeEncoded(var4);
    this.writeFooter(var2);
  }
  private PemObject createPemObject(Object o) throws IOException {
    String type;
    byte[] encoding;

    if (o instanceof PemObject) {
      return (PemObject) o;
    }
    if (o instanceof PemObjectGenerator) {
      return ((PemObjectGenerator) o).generate();
    }
    if (o instanceof X509Certificate) {
      type = "CERTIFICATE";
      try {
        encoding = ((X509Certificate) o).getEncoded();
      } catch (CertificateEncodingException e) {
        throw new PemGenerationException("Cannot encode object: " + e.toString());
      }
    } else if (o instanceof X509CRL) {
      type = "X509 CRL";
      try {
        encoding = ((X509CRL) o).getEncoded();
      } catch (CRLException e) {
        throw new PemGenerationException("Cannot encode object: " + e.toString());
      }
    } else if (o instanceof KeyPair) {
      return createPemObject(((KeyPair) o).getPrivate());
    } else if (o instanceof PrivateKey) {
      PrivateKeyInfo info =
          new PrivateKeyInfo((ASN1Sequence) ASN1Primitive.fromByteArray(((Key) o).getEncoded()));

      if (o instanceof RSAPrivateKey) {
        type = "RSA PRIVATE KEY";

        encoding = info.parsePrivateKey().toASN1Primitive().getEncoded();
      } else if (o instanceof DSAPrivateKey) {
        type = "DSA PRIVATE KEY";

        DSAParameter p = DSAParameter.getInstance(info.getPrivateKeyAlgorithm().getParameters());
        ASN1EncodableVector v = new ASN1EncodableVector();

        v.add(new DERInteger(0));
        v.add(new DERInteger(p.getP()));
        v.add(new DERInteger(p.getQ()));
        v.add(new DERInteger(p.getG()));

        BigInteger x = ((DSAPrivateKey) o).getX();
        BigInteger y = p.getG().modPow(x, p.getP());

        v.add(new DERInteger(y));
        v.add(new DERInteger(x));

        encoding = new DERSequence(v).getEncoded();
      } else if (((PrivateKey) o).getAlgorithm().equals("ECDSA")) {
        type = "EC PRIVATE KEY";

        encoding = info.parsePrivateKey().toASN1Primitive().getEncoded();
      } else {
        throw new IOException("Cannot identify private key");
      }
    } else if (o instanceof PublicKey) {
      type = "PUBLIC KEY";

      encoding = ((PublicKey) o).getEncoded();
    } else if (o instanceof X509AttributeCertificate) {
      type = "ATTRIBUTE CERTIFICATE";
      encoding = ((X509V2AttributeCertificate) o).getEncoded();
    } else if (o instanceof PKCS10CertificationRequest) {
      type = "CERTIFICATE REQUEST";
      encoding = ((PKCS10CertificationRequest) o).getEncoded();
    } else if (o instanceof ContentInfo) {
      type = "PKCS7";
      encoding = ((ContentInfo) o).getEncoded();
    } else {
      throw new PemGenerationException("unknown object passed - can't encode.");
    }

    return new PemObject(type, encoding);
  }