chengkun
2025-06-05 4080b5997b38ca84b3b203c7101dcadb97b76925
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package com.alipay.easysdk.kms.aliyun;
 
import com.alipay.easysdk.kernel.AlipayConstants;
import com.alipay.easysdk.kms.aliyun.models.AsymmetricSignRequest;
import com.alipay.easysdk.kms.aliyun.models.AsymmetricSignResponse;
import com.alipay.easysdk.kms.aliyun.models.GetPublicKeyRequest;
import com.alipay.easysdk.kms.aliyun.models.GetPublicKeyResponse;
import com.alipay.easysdk.kms.aliyun.models.RuntimeOptions;
import com.aliyun.tea.TeaConverter;
import com.aliyun.tea.TeaModel;
import com.aliyun.tea.TeaPair;
import org.bouncycastle.asn1.gm.GMNamedCurves;
import org.bouncycastle.asn1.x9.X9ECParameters;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.crypto.params.ECDomainParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.math.ec.ECFieldElement;
import org.bouncycastle.util.encoders.Base64;
 
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
 
/**
 * 实现KMS的Client
 *
 * @author aliyunkms
 * @version $Id: AliyunKMSClient.java, v 0.1 2020年05月08日 10:53 PM aliyunkms Exp $
 */
public class AliyunKMSClient extends AliyunRpcClient {
    //支付宝signType与KMS签名算法映射表
    private static final Map<String, String> signAlgs    = new HashMap<>();
    private static final Map<String, String> digestAlgs  = new HashMap<>();
    private static final Map<String, String> namedCurves = new HashMap<>();
 
    static {
        digestAlgs.put("RSA_PKCS1_SHA_256", "SHA-256");
        digestAlgs.put("RSA_PSS_SHA_256", "SHA-256");
        digestAlgs.put("ECDSA_SHA_256", "SHA-256");
 
        namedCurves.put("SM2DSA", "sm2p256v1");
 
        signAlgs.put("RSA2", "RSA_PKCS1_SHA_256");
    }
 
    private String    keyId;
    private String    keyVersionId;
    private String    algorithm;
    private PublicKey publicKey;
    private String    protocol;
    private String    method;
    private String    version;
    private Integer   connectTimeout;
    private Integer   readTimeout;
    private Integer   maxAttempts;
    private boolean   ignoreSSL;
 
    public AliyunKMSClient(Map<String, Object> config) throws Exception {
        super(config);
        this.keyId = (String) config.get("kmsKeyId");
        this.keyVersionId = (String) config.get("kmsKeyVersionId");
        this.algorithm = signAlgs.get((String) config.get(AlipayConstants.SIGN_TYPE_CONFIG_KEY));
        this.publicKey = null;
        this.protocol = "HTTPS";
        this.method = "POST";
        this.version = "2016-01-20";
        this.connectTimeout = 15000;
        this.readTimeout = 15000;
        this.maxAttempts = 3;
        this.ignoreSSL = false;
    }
 
    private GetPublicKeyResponse _getPublicKey(GetPublicKeyRequest request) throws Exception {
        validateModel(request);
        RuntimeOptions runtime = RuntimeOptions.build(TeaConverter.buildMap(
                new TeaPair("connectTimeout", this.connectTimeout),
                new TeaPair("readTimeout", this.readTimeout),
                new TeaPair("maxAttempts", this.maxAttempts),
                new TeaPair("ignoreSSL", this.ignoreSSL)
        ));
        return TeaModel.toModel(
                this.doRequest("GetPublicKey", this.protocol, this.method, this.version, TeaModel.buildMap(request), null, runtime),
                new GetPublicKeyResponse());
    }
 
    private PublicKey getPublicKey(String keyId, String keyVersionId) throws Exception {
        GetPublicKeyRequest request = GetPublicKeyRequest.build(TeaConverter.buildMap(
                new TeaPair("KeyId", keyId),
                new TeaPair("KeyVersionId", keyVersionId)
        ));
        GetPublicKeyResponse response = _getPublicKey(request);
        String pemKey = response.publicKey;
        pemKey = pemKey.replaceFirst("-----BEGIN PUBLIC KEY-----", "");
        pemKey = pemKey.replaceFirst("-----END PUBLIC KEY-----", "");
        pemKey = pemKey.replaceAll("\\s", "");
        byte[] derKey = Base64.decode(pemKey);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(derKey);
        Security.addProvider(new BouncyCastleProvider());
        return KeyFactory.getInstance("EC", "BC").generatePublic(keySpec);
    }
 
    private byte[] getZ(ECPublicKeyParameters ecPublicKeyParameters, ECDomainParameters ecDomainParameters) {
        Digest digest = new SM3Digest();
        digest.reset();
 
        String userID = "1234567812345678";
        addUserID(digest, userID.getBytes());
 
        addFieldElement(digest, ecDomainParameters.getCurve().getA());
        addFieldElement(digest, ecDomainParameters.getCurve().getB());
        addFieldElement(digest, ecDomainParameters.getG().getAffineXCoord());
        addFieldElement(digest, ecDomainParameters.getG().getAffineYCoord());
        addFieldElement(digest, ecPublicKeyParameters.getQ().getAffineXCoord());
        addFieldElement(digest, ecPublicKeyParameters.getQ().getAffineYCoord());
 
        byte[] result = new byte[digest.getDigestSize()];
        digest.doFinal(result, 0);
        return result;
    }
 
    private void addUserID(Digest digest, byte[] userID) {
        int len = userID.length * 8;
        digest.update((byte) (len >> 8 & 0xFF));
        digest.update((byte) (len & 0xFF));
        digest.update(userID, 0, userID.length);
    }
 
    private void addFieldElement(Digest digest, ECFieldElement v) {
        byte[] p = v.getEncoded();
        digest.update(p, 0, p.length);
    }
 
    private byte[] calcSM3Digest(PublicKey pubKey, byte[] message) {
        X9ECParameters x9ECParameters = GMNamedCurves.getByName(namedCurves.get(this.algorithm));
        ECDomainParameters ecDomainParameters = new ECDomainParameters(x9ECParameters.getCurve(), x9ECParameters.getG(),
                x9ECParameters.getN());
        BCECPublicKey localECPublicKey = (BCECPublicKey) pubKey;
        ECPublicKeyParameters ecPublicKeyParameters = new ECPublicKeyParameters(localECPublicKey.getQ(), ecDomainParameters);
        byte[] z = getZ(ecPublicKeyParameters, ecDomainParameters);
        Digest digest = new SM3Digest();
        digest.update(z, 0, z.length);
        digest.update(message, 0, message.length);
        byte[] result = new byte[digest.getDigestSize()];
        digest.doFinal(result, 0);
        return result;
    }
 
    private AsymmetricSignResponse _asymmetricSign(AsymmetricSignRequest request) throws Exception {
        validateModel(request);
        RuntimeOptions runtime = RuntimeOptions.build(TeaConverter.buildMap(
                new TeaPair("connectTimeout", this.connectTimeout),
                new TeaPair("readTimeout", this.readTimeout),
                new TeaPair("maxAttempts", this.maxAttempts),
                new TeaPair("ignoreSSL", this.ignoreSSL)
        ));
        return TeaModel.toModel(
                this.doRequest("AsymmetricSign", this.protocol, this.method, this.version, TeaModel.buildMap(request), null, runtime),
                new AsymmetricSignResponse());
    }
 
    private String asymmetricSign(String keyId, String keyVersionId, String algorithm, byte[] message) throws Exception {
        byte[] digest;
        if (algorithm.equals("SM2DSA")) {
            if (this.publicKey == null) {
                this.publicKey = getPublicKey(keyId, keyVersionId);
            }
            digest = calcSM3Digest(this.publicKey, message);
        } else {
            digest = MessageDigest.getInstance(digestAlgs.get(algorithm)).digest(message);
        }
 
        AsymmetricSignRequest request = AsymmetricSignRequest.build(TeaConverter.buildMap(
                new TeaPair("keyId", keyId),
                new TeaPair("keyVersionId", keyVersionId),
                new TeaPair("algorithm", algorithm),
                new TeaPair("digest", Base64.toBase64String(digest))
        ));
        AsymmetricSignResponse response = _asymmetricSign(request);
        return response.value;
    }
 
    /**
     * 计算签名
     *
     * @param content 待签名的内容
     * @return 签名值的Base64串
     */
    public String sign(String content) throws Exception {
        return asymmetricSign(this.keyId, this.keyVersionId, this.algorithm, content.getBytes(AlipayConstants.DEFAULT_CHARSET));
    }
 
    public String getAlgorithm() {
        return this.algorithm;
    }
 
    public void setAlgorithm(String algorithm) {
        this.algorithm = algorithm;
    }
 
    public String getKeyId() {
        return this.keyId;
    }
 
    public void setKeyId(String keyId) {
        this.keyId = keyId;
    }
 
    public String getKeyVersionId() {
        return this.keyVersionId;
    }
 
    public void setKeyVersionId(String keyVersionId) {
        this.keyVersionId = keyVersionId;
    }
 
    public PublicKey getPublicKey() {
        return this.publicKey;
    }
 
    public void setPublicKey(PublicKey publicKey) {
        this.publicKey = publicKey;
    }
 
    public String getProtocol() {
        return this.protocol;
    }
 
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }
 
    public String getMethod() {
        return this.method;
    }
 
    public void setMethod(String method) {
        this.method = method;
    }
 
    public String getVersion() {
        return this.version;
    }
 
    public void setVersion(String version) {
        this.version = version;
    }
 
    public Integer getConnectTimeout() {
        return this.connectTimeout;
    }
 
    public void setConnectTimeout(Integer connectTimeout) {
        this.connectTimeout = connectTimeout;
    }
 
    public Integer getReadTimeout() {
        return this.readTimeout;
    }
 
    public void setReadTimeout(Integer readTimeout) {
        this.readTimeout = readTimeout;
    }
 
    public Integer getMaxAttempts() {
        return this.maxAttempts;
    }
 
    public void setMaxAttempts(Integer maxAttempts) {
        this.maxAttempts = maxAttempts;
    }
 
    public boolean getIgnoreSSL() {
        return this.ignoreSSL;
    }
 
    public void setIgnoreSSL(boolean ignoreSSL) {
        this.ignoreSSL = ignoreSSL;
    }
}