4747import org .elasticsearch .env .Environment ;
4848
4949import java .io .BufferedReader ;
50+ import java .io .ByteArrayInputStream ;
5051import java .io .IOException ;
5152import java .io .InputStream ;
5253import java .io .InputStreamReader ;
7172import java .nio .file .attribute .PosixFilePermissions ;
7273import java .security .MessageDigest ;
7374import java .security .NoSuchAlgorithmException ;
74- import java .security .Security ;
7575import java .util .ArrayList ;
7676import java .util .Arrays ;
7777import java .util .Base64 ;
@@ -543,8 +543,8 @@ void verifySignature(final Path zip, final String urlString) throws IOException,
543543 InputStream fin = pluginZipInputStream (zip );
544544 // sin is a URL stream to the signature corresponding to the downloaded plugin zip
545545 InputStream sin = urlOpenStream (ascUrl );
546- // pin is a decoded base64 stream over the embedded public key in RFC2045 format
547- InputStream pin = Base64 . getMimeDecoder (). wrap ( getPublicKey () )) {
546+ // pin is a input stream to the public key in ASCII-Armor format (RFC4880); the Armor data is in RFC2045 format
547+ InputStream pin = getPublicKey ()) {
548548 final JcaPGPObjectFactory factory = new JcaPGPObjectFactory (PGPUtil .getDecoderStream (sin ));
549549 final PGPSignature signature = ((PGPSignatureList ) factory .nextObject ()).get (0 );
550550
@@ -555,7 +555,19 @@ void verifySignature(final Path zip, final String urlString) throws IOException,
555555 }
556556
557557 // compute the signature of the downloaded plugin zip
558- final PGPPublicKeyRingCollection collection = new PGPPublicKeyRingCollection (pin , new JcaKeyFingerprintCalculator ());
558+ final List <String > lines =
559+ new BufferedReader (new InputStreamReader (pin , StandardCharsets .UTF_8 )).lines ().collect (Collectors .toList ());
560+ // skip armor headers and possible blank line
561+ int index = 1 ;
562+ for (; index < lines .size (); index ++) {
563+ if (lines .get (index ).matches (".*: .*" ) == false && lines .get (index ).matches ("\\ s*" ) == false ) {
564+ break ;
565+ }
566+ }
567+ final byte [] armoredData =
568+ lines .subList (index , lines .size () - 1 ).stream ().collect (Collectors .joining ("\n " )).getBytes (StandardCharsets .UTF_8 );
569+ final InputStream ain = Base64 .getMimeDecoder ().wrap (new ByteArrayInputStream (armoredData ));
570+ final PGPPublicKeyRingCollection collection = new PGPPublicKeyRingCollection (ain , new JcaKeyFingerprintCalculator ());
559571 final PGPPublicKey key = collection .getPublicKey (signature .getKeyID ());
560572 signature .init (new JcaPGPContentVerifierBuilderProvider ().setProvider (new BouncyCastleProvider ()), key );
561573 final byte [] buffer = new byte [1024 ];
@@ -597,7 +609,7 @@ String getPublicKeyId() {
597609 * @return an input stream to the public key
598610 */
599611 InputStream getPublicKey () {
600- return InstallPluginCommand .class .getResourceAsStream ("/public_key" );
612+ return InstallPluginCommand .class .getResourceAsStream ("/public_key.asc " );
601613 }
602614
603615 /**
0 commit comments