|
7 | 7 | import javax.crypto.KeyAgreement; |
8 | 8 | import java.security.NoSuchAlgorithmException; |
9 | 9 | import java.security.NoSuchProviderException; |
10 | | -import java.security.Security; |
11 | 10 |
|
12 | 11 | public class CryptoProviderService { |
13 | 12 | private static final Logger LOGGER = LoggerFactory.getLogger(CryptoProviderService.class); |
14 | 13 |
|
15 | | - // ECDH provider selection: tries ACCP first, falls back to default (SunEC) |
16 | | - private static final String ECDH_PROVIDER_NAME = initEcdhProvider(); |
| 14 | + // ACCP provider name when available (after install()); null otherwise |
| 15 | + private static final String ACCP_PROVIDER_NAME = initAccpAsDefault(); |
17 | 16 |
|
18 | | - private static String initEcdhProvider() { |
19 | | - // Try ACCP (Amazon Corretto Crypto Provider) first |
| 17 | + private static String initAccpAsDefault() { |
20 | 18 | try { |
21 | | - // Add ACCP at lowest priority so it doesn't become default for other algorithms |
22 | | - Security.addProvider(AmazonCorrettoCryptoProvider.INSTANCE); |
23 | | - |
24 | | - // Verify it works for ECDH |
25 | | - KeyAgreement ka = KeyAgreement.getInstance("ECDH", AmazonCorrettoCryptoProvider.PROVIDER_NAME); |
26 | | - LOGGER.info("ECDH using AmazonCorrettoCryptoProvider (added at lowest priority)"); |
27 | | - return AmazonCorrettoCryptoProvider.PROVIDER_NAME; |
| 19 | + AmazonCorrettoCryptoProvider.install(); |
| 20 | + if (AmazonCorrettoCryptoProvider.INSTANCE.getLoadingError() == null) { |
| 21 | + LOGGER.info("AmazonCorrettoCryptoProvider installed as default for all crypto"); |
| 22 | + return AmazonCorrettoCryptoProvider.PROVIDER_NAME; |
| 23 | + } |
28 | 24 | } catch (Throwable e) { |
29 | | - // ACCP not available |
30 | 25 | LOGGER.info("AmazonCorrettoCryptoProvider is not available: {}", e.getMessage()); |
31 | 26 | } |
32 | | - |
33 | | - // Fall back to default provider |
34 | | - LOGGER.info("ECDH using default provider (SunEC)"); |
| 27 | + LOGGER.info("Using platform default crypto provider"); |
35 | 28 | return null; |
36 | 29 | } |
37 | 30 |
|
38 | 31 | /** |
39 | | - * Create ECDH Key Agreement using ACCP if available, fall back to SunEC if not |
| 32 | + * Create ECDH Key Agreement. Uses ACCP when installed as default; otherwise platform default (e.g. SunEC). |
40 | 33 | * @return ECDH KeyAgreement |
41 | 34 | * @throws NoSuchAlgorithmException |
42 | 35 | */ |
43 | | - public static KeyAgreement createKeyAgreement() throws NoSuchAlgorithmException { |
44 | | - if (ECDH_PROVIDER_NAME != null) { |
| 36 | + public static KeyAgreement createKeyAgreement() throws NoSuchAlgorithmException { |
| 37 | + if (ACCP_PROVIDER_NAME != null) { |
45 | 38 | try { |
46 | | - return KeyAgreement.getInstance("ECDH", ECDH_PROVIDER_NAME); |
| 39 | + return KeyAgreement.getInstance("ECDH", ACCP_PROVIDER_NAME); |
47 | 40 | } catch (NoSuchProviderException e) { |
48 | | - LOGGER.info("{} is not available: {}", ECDH_PROVIDER_NAME, e.getMessage()); |
| 41 | + LOGGER.info("{} is not available: {}", ACCP_PROVIDER_NAME, e.getMessage()); |
49 | 42 | } |
50 | 43 | } |
51 | 44 | return KeyAgreement.getInstance("ECDH"); |
|
0 commit comments