| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.connector.http.transport; |
| 34 | | | |
| 35 | | | import java.net.Socket; |
| 36 | | | import java.security.GeneralSecurityException; |
| 37 | | | import java.security.KeyStore; |
| 38 | | | import java.security.KeyStoreException; |
| 39 | | | import java.security.Principal; |
| 40 | | | import java.security.PrivateKey; |
| 41 | | | import java.security.cert.Certificate; |
| 42 | | | import java.security.cert.X509Certificate; |
| 43 | | | import javax.net.ssl.X509KeyManager; |
| 44 | | | |
| 45 | | | import org.jcoderz.commons.connector.InitializingSslFailedException; |
| 46 | | | import org.jcoderz.commons.util.Assert; |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | | |
| 53 | | | |
| 54 | | (1) | public class HttpsKeyManager |
| 55 | | | implements X509KeyManager |
| 56 | | | { |
| 57 | | | |
| 58 | | | private final X509KeyManager mManager; |
| 59 | | | |
| 60 | | | private final KeyStore mKeyStore; |
| 61 | | | private final String mKeyAlias; |
| 62 | | | private final String mKeyPassword; |
| 63 | | | |
| 64 | | | |
| 65 | | | private PrivateKey mPrivateKey; |
| 66 | | | |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | | @param |
| 71 | | | @param |
| 72 | | | @param |
| 73 | | | @param |
| 74 | | | |
| 75 | | | public HttpsKeyManager ( |
| 76 | | | X509KeyManager parent, KeyStore keystore, |
| 77 | | | String keyAlias, String keyPassword) |
| 78 | 0 | | { |
| 79 | 0 | | mManager = parent; |
| 80 | 0 | | mKeyStore = keystore; |
| 81 | 0 | | mKeyAlias = keyAlias; |
| 82 | 0 | | mKeyPassword = keyPassword; |
| 83 | 0 | | } |
| 84 | | | |
| 85 | | | |
| 86 | | | |
| 87 | | | |
| 88 | | | |
| 89 | | | @param |
| 90 | | | |
| 91 | | | @param |
| 92 | | | |
| 93 | | | @return |
| 94 | | | |
| 95 | | | public String[] getClientAliases (String keyType, Principal[] issuers) |
| 96 | | | { |
| 97 | 0 | | return new String[] {mKeyAlias}; |
| 98 | | | } |
| 99 | | | |
| 100 | | | |
| 101 | | | |
| 102 | | | |
| 103 | | | @param |
| 104 | | | |
| 105 | | | @param |
| 106 | | | |
| 107 | | | @return |
| 108 | | | |
| 109 | | | public String[] getServerAliases (String keyType, Principal[] issuers) |
| 110 | | | { |
| 111 | 0 | | return mManager.getServerAliases(keyType, issuers); |
| 112 | | | } |
| 113 | | | |
| 114 | | | |
| 115 | | | |
| 116 | | | |
| 117 | | | @param |
| 118 | | | @return |
| 119 | | | |
| 120 | | | public X509Certificate[] getCertificateChain (String alias) |
| 121 | | | { |
| 122 | 0 | | assertAlias(alias); |
| 123 | | | final X509Certificate[] chain; |
| 124 | | | try |
| 125 | | | { |
| 126 | 0 | | final Certificate[] certs = mKeyStore.getCertificateChain(alias); |
| 127 | 0 | | Assert.notNull(certs, "certs"); |
| 128 | 0 | | chain = new X509Certificate[certs.length]; |
| 129 | 0 | | for (int i = 0; i < chain.length; i++) |
| 130 | | | { |
| 131 | 0 | | chain[i] = (X509Certificate) certs[i]; |
| 132 | | | } |
| 133 | | | |
| 134 | | | |
| 135 | | | } |
| 136 | 0 | | catch (KeyStoreException kse) |
| 137 | | | { |
| 138 | 0 | | final String reason |
| 139 | | | = "Unable to obtain certificate chain for alias " |
| 140 | | | + "<" + alias + ">"; |
| 141 | 0 | | final InitializingSslFailedException sse |
| 142 | | | = new InitializingSslFailedException(reason, kse); |
| 143 | 0 | | throw sse; |
| 144 | 0 | | } |
| 145 | 0 | | return chain; |
| 146 | | | } |
| 147 | | | |
| 148 | | | |
| 149 | | | |
| 150 | | | |
| 151 | | | @param |
| 152 | | | @return |
| 153 | | | |
| 154 | | | public PrivateKey getPrivateKey (String alias) |
| 155 | | | { |
| 156 | 0 | | assertAlias(alias); |
| 157 | 0 | | if (mPrivateKey == null) |
| 158 | | | { |
| 159 | | | try |
| 160 | | | { |
| 161 | 0 | | mPrivateKey = (PrivateKey) mKeyStore.getKey( |
| 162 | | | alias, mKeyPassword.toCharArray()); |
| 163 | | | } |
| 164 | 0 | | catch (GeneralSecurityException gse) |
| 165 | | | { |
| 166 | 0 | | final String reason |
| 167 | | | = "Unable to obtain private key for alias " |
| 168 | | | + "<" + alias + ">"; |
| 169 | 0 | | final InitializingSslFailedException sse |
| 170 | | | = new InitializingSslFailedException(reason, gse); |
| 171 | 0 | | throw sse; |
| 172 | 0 | | } |
| 173 | | | } |
| 174 | 0 | | return mPrivateKey; |
| 175 | | | } |
| 176 | | | |
| 177 | | | {@inheritDoc} |
| 178 | | | public String chooseClientAlias ( |
| 179 | | | String[] keyType, Principal[] issuers, Socket socket) |
| 180 | | | { |
| 181 | 0 | | return mKeyAlias; |
| 182 | | | } |
| 183 | | | |
| 184 | | | {@inheritDoc} |
| 185 | | | public String chooseServerAlias ( |
| 186 | | | String keyType, Principal[] issuers, Socket socket) |
| 187 | | | { |
| 188 | 0 | | return mManager.chooseServerAlias(keyType, issuers, socket); |
| 189 | | | } |
| 190 | | | |
| 191 | | | |
| 192 | | | |
| 193 | | | @param |
| 194 | | | |
| 195 | | | private void assertAlias (String alias) |
| 196 | | | { |
| 197 | 0 | | if (!alias.equals(mKeyAlias)) |
| 198 | | | { |
| 199 | 0 | | final String reason |
| 200 | | | = "Unexpected alias <" + alias + ">"; |
| 201 | 0 | | final InitializingSslFailedException sse |
| 202 | | | = new InitializingSslFailedException(reason); |
| 203 | 0 | | throw sse; |
| 204 | | | } |
| 205 | 0 | | } |
| 206 | | | } |