47
47
import org .elasticsearch .env .Environment ;
48
48
49
49
import java .io .BufferedReader ;
50
+ import java .io .ByteArrayInputStream ;
50
51
import java .io .IOException ;
51
52
import java .io .InputStream ;
52
53
import java .io .InputStreamReader ;
71
72
import java .nio .file .attribute .PosixFilePermissions ;
72
73
import java .security .MessageDigest ;
73
74
import java .security .NoSuchAlgorithmException ;
74
- import java .security .Security ;
75
75
import java .util .ArrayList ;
76
76
import java .util .Arrays ;
77
77
import java .util .Base64 ;
@@ -543,8 +543,8 @@ void verifySignature(final Path zip, final String urlString) throws IOException,
543
543
InputStream fin = pluginZipInputStream (zip );
544
544
// sin is a URL stream to the signature corresponding to the downloaded plugin zip
545
545
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 ()) {
548
548
final JcaPGPObjectFactory factory = new JcaPGPObjectFactory (PGPUtil .getDecoderStream (sin ));
549
549
final PGPSignature signature = ((PGPSignatureList ) factory .nextObject ()).get (0 );
550
550
@@ -555,7 +555,19 @@ void verifySignature(final Path zip, final String urlString) throws IOException,
555
555
}
556
556
557
557
// 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 ());
559
571
final PGPPublicKey key = collection .getPublicKey (signature .getKeyID ());
560
572
signature .init (new JcaPGPContentVerifierBuilderProvider ().setProvider (new BouncyCastleProvider ()), key );
561
573
final byte [] buffer = new byte [1024 ];
@@ -597,7 +609,7 @@ String getPublicKeyId() {
597
609
* @return an input stream to the public key
598
610
*/
599
611
InputStream getPublicKey () {
600
- return InstallPluginCommand .class .getResourceAsStream ("/public_key" );
612
+ return InstallPluginCommand .class .getResourceAsStream ("/public_key.asc " );
601
613
}
602
614
603
615
/**
0 commit comments