final byte[] armoredPgp = BaseEncoding.base64().decode(licenseText); final ArmoredInputStream in = new ArmoredInputStream(new ByteArrayInputStream(armoredPgp)); int ch; while ((ch = in.read()) >= 0 && in.isClearText()) { bout.write((byte) ch); final PGPPublicKeyRingCollection pgpRings = new PGPPublicKeyRingCollection(new ArmoredInputStream( LicenseHelper.class.getResourceAsStream("/KEYS")), c);
OutputStream output = out; if (isArmored) { output = new ArmoredOutputStream(out);
@Override public int read() throws IOException { if (done) { return -1; } int c = aIs.read(); if (c < 0 || !aIs.isClearText()) { done = true; return -1; } return c; }
public Content getPublicKey() throws IOException, PGPException { PGPSecretKey signKey = readSecretKey(); PGPPublicKey publicKey = signKey.getPublicKey(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try (BCPGOutputStream os = new BCPGOutputStream(new ArmoredOutputStream(buffer))) { publicKey.encode(os); } return new Content(new BytesPayload(buffer.toByteArray(), AptMimeTypes.PUBLICKEY)); }
default RSAPrivateKey extractRSAPrivateKey( NamedResource resourceKey, RSAPublicKey pubKey, RSASecretBCPGKey bcKey) throws IOException, GeneralSecurityException { if (bcKey == null) { return null; } return generatePrivateKey(KeyUtils.RSA_ALGORITHM, RSAPrivateKey.class, new RSAPrivateCrtKeySpec( bcKey.getModulus(), pubKey.getPublicExponent(), bcKey.getPrivateExponent(), bcKey.getPrimeP(), bcKey.getPrimeQ(), bcKey.getPrimeExponentP(), bcKey.getPrimeExponentQ(), bcKey.getCrtCoefficient())); }
/** * Generates the signature of the given input stream as an ASCII file into the given output stream. */ void sign(InputStream in, OutputStream signatureOutput) throws PGPException, IOException, GeneralSecurityException { BCPGOutputStream bOut = new BCPGOutputStream(new ArmoredOutputStream(signatureOutput)); sign(in).encode(bOut); bOut.close(); }
PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(skey.getPublicKey().getAlgorithm(), PGPUtil.SHA256).setProvider("BC")); PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); sGen.init(PGPSignature.CANONICAL_TEXT_DOCUMENT, prKey); Iterator userIDs = skey.getPublicKey().getUserIDs(); if (it.hasNext()) { spGen.setSignerUserID(false, (String)userIDs.next()); sGen.setHashedSubpackets(spGen.generate()); } ArmoredOutputStream aos = new ArmoredOutputStream(baos); aos.beginClearText(PGPUtil.SHA256); sGen.update(dataBytes); aos.write(dataBytes); aos.endClearText(); BCPGOutputStream bOut = new BCPGOutputStream(aos); sGen.generate().encode(bOut); aos.flush(); aos.close();
public void asciiArmor(Long keyID, InputStream publicKeyRingStream, OutputStream armoredOutputStream) throws PGPException, IOException { PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream(publicKeyRingStream), new JcaKeyFingerprintCalculator()); PGPPublicKey publicKey = findMatchingPublicKey(pgpPub, keyID); ArmoredOutputStream armored = new ArmoredOutputStream(armoredOutputStream); publicKey.encode(armored); armored.close(); }
default DSAPublicKey extractDSSPublicKey(NamedResource resourceKey, DSAPublicBCPGKey bcKey) throws IOException, GeneralSecurityException { if (bcKey == null) { return null; } BigInteger p = bcKey.getP(); BigInteger q = bcKey.getQ(); BigInteger g = bcKey.getG(); BigInteger y = bcKey.getY(); return generatePublicKey(KeyUtils.DSS_ALGORITHM, DSAPublicKey.class, new DSAPublicKeySpec(y, p, q, g)); }
public boolean isResourceSignedBySignature(final InputStream resource, InputStream signature) { PGPPublicKey publicKey = null; PGPSignature pgpSignature = null; try { if (!(signature instanceof ArmoredInputStream)) { signature = new ArmoredInputStream(signature); } pgpSignature = isSignatureAcceptable(signature).getPgpSignature(); final PGPPublicKeyRing keyRing = getPublicKey(new PgpKeyId(pgpSignature)); rememberKey(keyRing); publicKey = keyRing.getPublicKey(); Validate.notNull(publicKey, "Could not obtain public key for signer key ID '%s'", pgpSignature); pgpSignature.initVerify(publicKey, "BC"); // Now verify the signed content final byte[] buff = new byte[BUFFER_SIZE]; int chunk; do { chunk = resource.read(buff); if (chunk > 0) { pgpSignature.update(buff, 0, chunk); } } while (chunk >= 0); return pgpSignature.verify(); } catch (final Exception e) { throw new IllegalStateException(e); } }
default RSAPublicKey extractRSAPublicKey(NamedResource resourceKey, RSAPublicBCPGKey bcKey) throws IOException, GeneralSecurityException { if (bcKey == null) { return null; } BigInteger e = bcKey.getPublicExponent(); BigInteger n = bcKey.getModulus(); return generatePublicKey(KeyUtils.RSA_ALGORITHM, RSAPublicKey.class, new RSAPublicKeySpec(n, e)); }
/** * Close the encrypted output writers. * @throws IOException - stream handling had a problem */ public void close() throws IOException { // close the literal output if (literalOut != null) literalOut.close(); // close the compressor if (compressedOut != null) compressedOut.close(); // close the encrypted output if (encryptedOut != null) encryptedOut.close(); // close the armored output if (armoredOutputStream != null) armoredOutputStream.close(); }
protected int getAlgorithm() { return privateKey != null ? privateKey.getPublicKeyPacket().getAlgorithm() : 0; } }
default DSAPrivateKey extractDSSPrivateKey( NamedResource resourceKey, DSAPublicKey pubKey, DSASecretBCPGKey bcKey) throws IOException, GeneralSecurityException { if (bcKey == null) { return null; } DSAParams params = pubKey.getParams(); if (params == null) { throw new InvalidKeyException("Missing parameters in public key"); } return generatePrivateKey(KeyUtils.DSS_ALGORITHM, DSAPrivateKey.class, new DSAPrivateKeySpec(bcKey.getX(), params.getP(), params.getQ(), params.getG())); }
default ECPrivateKey extractECDSAPrivateKey( NamedResource resourceKey, ECPublicKey pubKey, ECSecretBCPGKey bcKey) throws IOException, GeneralSecurityException { if (bcKey == null) { return null; } if (!SecurityUtils.isECCSupported()) { throw new NoSuchProviderException("ECC not supported"); } ECParameterSpec params = pubKey.getParams(); BigInteger x = bcKey.getX(); return generatePrivateKey(KeyUtils.EC_ALGORITHM, ECPrivateKey.class, new ECPrivateKeySpec(x, params)); }
public byte[] signExternal(String input) throws IOException, PGPException { PGPSecretKey signKey = readSecretKey(); PGPPrivateKey privKey = signKey.extractPrivateKey( new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(config.passphrase.toCharArray())); PGPSignatureGenerator sigGenerator = new PGPSignatureGenerator( new JcaPGPContentSignerBuilder(signKey.getPublicKey().getAlgorithm(), PGPUtil.SHA256).setProvider("BC")); sigGenerator.init(PGPSignature.BINARY_DOCUMENT, privKey); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try (ArmoredOutputStream aOut = new ArmoredOutputStream(buffer)) { BCPGOutputStream bOut = new BCPGOutputStream(aOut); sigGenerator.update(input.getBytes(Charsets.UTF_8)); sigGenerator.generate().encode(bOut); } return buffer.toByteArray(); }
OutputStream output = out; if (isArmored) { output = new ArmoredOutputStream(out);
SignatureConsumer(int hashAlgorithm) throws PGPException { int keyAlgorithm = privateKey.getPublicKeyPacket().getAlgorithm(); BcPGPContentSignerBuilder contentSignerBuilder = new BcPGPContentSignerBuilder(keyAlgorithm, hashAlgorithm); this.pgpSignatureGenerator = new PGPSignatureGenerator(contentSignerBuilder); pgpSignatureGenerator.init(PGPSignature.BINARY_DOCUMENT, privateKey); }
private static void createSignature( String fileName, InputStream keyIn, OutputStream out, char[] pass, boolean armor) throws GeneralSecurityException, IOException, PGPException { if (armor) { out = new ArmoredOutputStream(out); } PGPSecretKey pgpSec = PGPExampleUtil.readSecretKey(keyIn); PGPPrivateKey pgpPrivKey = pgpSec.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(pass)); PGPSignatureGenerator sGen = new PGPSignatureGenerator(new JcaPGPContentSignerBuilder(pgpSec.getPublicKey().getAlgorithm(), PGPUtil.SHA1).setProvider("BC")); sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey); BCPGOutputStream bOut = new BCPGOutputStream(out); InputStream fIn = new BufferedInputStream(new FileInputStream(fileName)); int ch; while ((ch = fIn.read()) >= 0) { sGen.update((byte) ch); } fIn.close(); sGen.generate().encode(bOut); if (armor) { out.close(); } }
/** * Wraps with stream that outputs ascii-armored text. */ protected OutputStream armor(OutputStream out) { if (asciiArmored) return new ArmoredOutputStream(out); return null; }