xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.nuvole.hnnx.hnnxPay;
 
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.Assert;
 
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.Key;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author awlwen
 * @since 2017/11/30
 */
public class KeyProviderImpl implements KeyProvider {
    private static final Map<String, String> SUPPORTED_KEY_TYPE = new HashMap<String, String>();
 
    static {
        SUPPORTED_KEY_TYPE.put("ks", "jks");
        SUPPORTED_KEY_TYPE.put("jks", "jks");
        SUPPORTED_KEY_TYPE.put("pfx", "pkcs12");
        SUPPORTED_KEY_TYPE.put("p12", "pkcs12");
    }
 
    /**
     * 密钥容器文件路径
     */
    private String file;
    /**
     * 密钥容器密码
     */
    private String password;
    /**
     * 密钥容器
     */
    private KeyStore keystore;
    /**
     * 初始化标志
     */
    private boolean initialized = false;
 
    /**
     * 构造器
     *
     * @param file     密钥容器文件路径
     * @param password 密钥容器密码
     */
    public KeyProviderImpl(String file, String password) {
        super();
        Assert.notNull(file, "file is required; it must not be null");
        init(file, password);
    }
 
    /**
     * 根据文件扩展名获取密钥容器类型
     *
     * @param file
     * @return
     */
    private String resolveType(String file) {
        Assert.notNull(file, "file is required; it must not be null");
        String suffix = file.substring(file.lastIndexOf(".") + 1);
        Assert.notNull(suffix, "suffix could not be resolved");
        String supportedType = SUPPORTED_KEY_TYPE.get(suffix.toLowerCase());
        Assert.notNull(supportedType, "unsupported key type");
        return supportedType;
    }
 
    /**
     * 初始化,加载密钥容器
     *
     * @param file     密钥容器文件路径
     * @param password 密钥容器密码
     */
    private void init(String file, String password) {
        InputStream is = null;
        try {
            //建立文件输入流读取密钥容器
            if (file.startsWith("classpath:")) {
                is = new ClassPathResource(file.replace("classpath:", "")).getInputStream();
            } else {
                is = new BufferedInputStream(new FileInputStream(file));
            }
 
            //根据文件扩展名建立相应的密钥容器类型
            keystore = KeyStore.getInstance(resolveType(file));
            //将密钥容器密码转换为char数组
            char[] pwd = password == null ? null : password.toCharArray();
            //从指定的输入流中加载密钥容器
            keystore.load(is, pwd);
            initialized = true;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
    }
 
    /* (non-Javadoc)
     * @see com.csii.sg.security.KeyProvider#getKey(java.lang.String, java.lang.String)
     */
    public Key getKey(String id, String password) {
        Assert.isTrue(initialized, "keyProvider not been initialized");
        try {
            if (id == null) {
                Enumeration<String> aliases = keystore.aliases();
                for (int count = 0; aliases.hasMoreElements(); count++) {
                    if (count > 1) {
                        throw new IllegalArgumentException("key id not specified");
                    }
                    String alias = aliases.nextElement();
                    if (keystore.isKeyEntry(alias)) {
                        id = alias;
                    }
                }
            }
            char[] pwd = password == null ? null : password.toCharArray();
            return keystore.getKey(id, pwd);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
 
    /* (non-Javadoc)
     * @see com.csii.sg.security.KeyProvider#getCertificate(java.lang.String)
     */
    public Certificate getCertificate(String id) {
        Assert.isTrue(initialized, "keyProvider not been initialized");
        try {
            return keystore.getCertificate(id);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
 
    /* (non-Javadoc)
     * @see com.csii.sg.security.KeyProvider#getCertificateChain(java.lang.String)
     */
    public Certificate[] getCertificateChain(String id) {
        Assert.isTrue(initialized, "keyProvider not been initialized");
        try {
            return keystore.getCertificateChain(id);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
 
    /**
     * @param file the file to set
     */
    public void setFile(String file) {
        this.file = file;
    }
 
    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }
}