Skip to content

Commit ca28b7e

Browse files
committed
support retrieving (provider's) KeyAgreement using our SecurityHelper
1 parent 23b6b5d commit ca28b7e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/main/java/org/jruby/ext/openssl/SecurityHelper.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464

6565
import javax.crypto.Cipher;
6666
import javax.crypto.CipherSpi;
67+
import javax.crypto.KeyAgreement;
68+
import javax.crypto.KeyAgreementSpi;
6769
import javax.crypto.KeyGenerator;
6870
import javax.crypto.KeyGeneratorSpi;
6971
import javax.crypto.Mac;
@@ -542,6 +544,30 @@ static KeyGenerator getKeyGenerator(final String algorithm, final Provider provi
542544
);
543545
}
544546

547+
/**
548+
* @note code calling this should not assume BC provider internals !
549+
*/
550+
public static KeyAgreement getKeyAgreement(final String algorithm) throws NoSuchAlgorithmException {
551+
try {
552+
final Provider provider = getSecurityProvider();
553+
if ( provider != null ) return getKeyAgreement(algorithm, provider);
554+
}
555+
catch (NoSuchAlgorithmException e) { }
556+
catch (SecurityException e) { debugStackTrace(e); }
557+
return KeyAgreement.getInstance(algorithm);
558+
}
559+
560+
static KeyAgreement getKeyAgreement(final String algorithm, final Provider provider)
561+
throws NoSuchAlgorithmException {
562+
final KeyAgreementSpi spi = (KeyAgreementSpi) getImplEngine("KeyAgreement", algorithm);
563+
if ( spi == null ) throw new NoSuchAlgorithmException(algorithm + " not found");
564+
565+
return newInstance(KeyAgreement.class,
566+
new Class[] { KeyAgreementSpi.class, Provider.class, String.class },
567+
new Object[] { spi, provider, algorithm }
568+
);
569+
}
570+
545571
/**
546572
* @note code calling this should not assume BC provider internals !
547573
*/

0 commit comments

Comments
 (0)