|
| 1 | +package org.nhindirect.config.spring.config; |
| 2 | + |
| 3 | +import org.nhindirect.common.crypto.KeyStoreProtectionManager; |
| 4 | +import org.nhindirect.common.crypto.impl.BootstrappedKeyStoreProtectionManager; |
| 5 | +import org.nhindirect.common.crypto.impl.BootstrappedPKCS11Credential; |
| 6 | +import org.nhindirect.common.crypto.impl.StaticCachedPKCS11TokenKeyStoreProtectionManager; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | +import org.springframework.beans.factory.annotation.Value; |
| 10 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 11 | +import org.springframework.context.annotation.Bean; |
| 12 | +import org.springframework.context.annotation.Configuration; |
| 13 | + |
| 14 | +@Configuration |
| 15 | +public class KeyStoreConfig |
| 16 | +{ |
| 17 | + private static final Logger LOGGER = LoggerFactory.getLogger(KeyStoreConfig.class); |
| 18 | + |
| 19 | + @Value("${direct.config.keystore.keyStorePin:som3randomp!n}") |
| 20 | + private String keyStorePin; |
| 21 | + |
| 22 | + @Value("${direct.config.keystore.keyStoreType:Luna}") |
| 23 | + private String keyStoreType; |
| 24 | + |
| 25 | + @Value("${direct.config.keystore.keyStoreSourceAsString:slot:0}") |
| 26 | + private String keyStoreSourceAsString; |
| 27 | + |
| 28 | + @Value("${direct.config.keystore.keyStoreProviderName:com.safenetinc.luna.provider.LunaProvider}") |
| 29 | + private String keyStoreProviderName; |
| 30 | + |
| 31 | + @Value("${direct.config.keystore.keyStorePassPhraseAlias:keyStorePassPhrase}") |
| 32 | + private String keyStorePassPhraseAlias; |
| 33 | + |
| 34 | + @Value("${direct.config.keystore.privateKeyPassPhraseAlias:privateKeyPassPhrase}") |
| 35 | + private String privateKeyPassPhraseAlias; |
| 36 | + |
| 37 | + @Value("${direct.config.keystore.initOnStart:true}") |
| 38 | + private String initOnStart; |
| 39 | + |
| 40 | + @Value("${direct.config.keystore.keyStorePassPhrase:H1TBr0s!}") |
| 41 | + private String keyStorePassPhrase; |
| 42 | + |
| 43 | + @Value("${direct.config.keystore.privateKeyPassPhrase:H1TCh1ckS!}") |
| 44 | + private String privateKeyPassPhrase; |
| 45 | + |
| 46 | + @Bean |
| 47 | + @ConditionalOnProperty(name="direct.config.keystore.hsmpresent", havingValue="true") |
| 48 | + public KeyStoreProtectionManager hsmKeyStoreProtectionManager() |
| 49 | + { |
| 50 | + LOGGER.info("HSM configured. Attempting to connect to device."); |
| 51 | + |
| 52 | + try |
| 53 | + { |
| 54 | + final BootstrappedPKCS11Credential cred = new BootstrappedPKCS11Credential(keyStorePin); |
| 55 | + final StaticCachedPKCS11TokenKeyStoreProtectionManager mgr = new StaticCachedPKCS11TokenKeyStoreProtectionManager(); |
| 56 | + mgr.setCredential(cred); |
| 57 | + mgr.setKeyStoreType(keyStoreType); |
| 58 | + mgr.setKeyStoreSourceAsString(keyStoreSourceAsString); |
| 59 | + mgr.setKeyStoreProviderName(keyStoreProviderName); |
| 60 | + mgr.setKeyStorePassPhraseAlias(keyStorePassPhraseAlias); |
| 61 | + mgr.setPrivateKeyPassPhraseAlias(privateKeyPassPhraseAlias); |
| 62 | + |
| 63 | + if (Boolean.parseBoolean(initOnStart)) |
| 64 | + mgr.initTokenStore(); |
| 65 | + |
| 66 | + return mgr; |
| 67 | + } |
| 68 | + catch (Exception e) |
| 69 | + { |
| 70 | + throw new RuntimeException(e); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Bean |
| 75 | + @ConditionalOnProperty(name="direct.config.keystore.hsmpresent", havingValue="false", matchIfMissing=true) |
| 76 | + public KeyStoreProtectionManager nonHSMKeyStoreProtectionManager() |
| 77 | + { |
| 78 | + LOGGER.info("No HSM configured."); |
| 79 | + |
| 80 | + final BootstrappedKeyStoreProtectionManager mgr = new BootstrappedKeyStoreProtectionManager(keyStorePassPhrase, privateKeyPassPhrase); |
| 81 | + |
| 82 | + return mgr; |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments