Example #1
0
  public void encode(OutputStream outStream) throws IOException {
    BCPGOutputStream out;

    if (outStream instanceof BCPGOutputStream) {
      out = (BCPGOutputStream) outStream;
    } else {
      out = new BCPGOutputStream(outStream);
    }

    out.writePacket(secret);
    if (pub.trustPk != null) {
      out.writePacket(pub.trustPk);
    }

    if (pub.subSigs == null) // is not a sub key
    {
      for (int i = 0; i != pub.keySigs.size(); i++) {
        ((PGPSignature) pub.keySigs.get(i)).encode(out);
      }

      for (int i = 0; i != pub.ids.size(); i++) {
        if (pub.ids.get(i) instanceof UserIDPacket) {
          UserIDPacket id = (UserIDPacket) pub.ids.get(i);

          out.writePacket(id);
        } else {
          PGPUserAttributeSubpacketVector v = (PGPUserAttributeSubpacketVector) pub.ids.get(i);

          out.writePacket(new UserAttributePacket(v.toSubpacketArray()));
        }

        if (pub.idTrusts.get(i) != null) {
          out.writePacket((ContainedPacket) pub.idTrusts.get(i));
        }

        List sigs = (ArrayList) pub.idSigs.get(i);

        for (int j = 0; j != sigs.size(); j++) {
          ((PGPSignature) sigs.get(j)).encode(out);
        }
      }
    } else {
      for (int j = 0; j != pub.subSigs.size(); j++) {
        ((PGPSignature) pub.subSigs.get(j)).encode(out);
      }
    }
  }
Example #2
0
  private static SecretKeyPacket buildSecretKeyPacket(
      boolean isMasterKey,
      PGPPrivateKey privKey,
      PGPPublicKey pubKey,
      PBESecretKeyEncryptor keyEncryptor,
      PGPDigestCalculator checksumCalculator)
      throws PGPException {
    BCPGObject secKey = (BCPGObject) privKey.getPrivateKeyDataPacket();

    if (secKey == null) {
      if (isMasterKey) {
        return new SecretKeyPacket(
            pubKey.publicPk, SymmetricKeyAlgorithmTags.NULL, null, null, new byte[0]);
      } else {
        return new SecretSubkeyPacket(
            pubKey.publicPk, SymmetricKeyAlgorithmTags.NULL, null, null, new byte[0]);
      }
    }

    try {
      ByteArrayOutputStream bOut = new ByteArrayOutputStream();
      BCPGOutputStream pOut = new BCPGOutputStream(bOut);

      pOut.writeObject(secKey);

      byte[] keyData = bOut.toByteArray();

      pOut.write(checksum(checksumCalculator, keyData, keyData.length));

      int encAlgorithm =
          (keyEncryptor != null) ? keyEncryptor.getAlgorithm() : SymmetricKeyAlgorithmTags.NULL;

      if (encAlgorithm != SymmetricKeyAlgorithmTags.NULL) {
        keyData = bOut.toByteArray(); // include checksum

        byte[] encData = keyEncryptor.encryptKeyData(keyData, 0, keyData.length);
        byte[] iv = keyEncryptor.getCipherIV();

        S2K s2k = keyEncryptor.getS2K();

        int s2kUsage;

        if (checksumCalculator != null) {
          if (checksumCalculator.getAlgorithm() != HashAlgorithmTags.SHA1) {
            throw new PGPException("only SHA1 supported for key checksum calculations.");
          }
          s2kUsage = SecretKeyPacket.USAGE_SHA1;
        } else {
          s2kUsage = SecretKeyPacket.USAGE_CHECKSUM;
        }

        if (isMasterKey) {
          return new SecretKeyPacket(pubKey.publicPk, encAlgorithm, s2kUsage, s2k, iv, encData);
        } else {
          return new SecretSubkeyPacket(pubKey.publicPk, encAlgorithm, s2kUsage, s2k, iv, encData);
        }
      } else {
        if (isMasterKey) {
          return new SecretKeyPacket(pubKey.publicPk, encAlgorithm, null, null, bOut.toByteArray());
        } else {
          return new SecretSubkeyPacket(
              pubKey.publicPk, encAlgorithm, null, null, bOut.toByteArray());
        }
      }
    } catch (PGPException e) {
      throw e;
    } catch (Exception e) {
      throw new PGPException("Exception encrypting key", e);
    }
  }