From 61075fb6d48d858793b1e887f7190c419eca5c80 Mon Sep 17 00:00:00 2001 From: xiaohui249 Date: Mon, 22 Aug 2022 21:22:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96SM2Util.load*FromFile?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E4=B8=AD=E7=9A=84=E5=AD=97=E7=AC=A6=E6=B5=81?= =?UTF-8?q?=E6=88=96=E5=AD=97=E8=8A=82=E6=B5=81=E7=9A=84=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E3=80=82=20=E5=88=A0=E9=99=A4=E5=86=97=E4=BD=99=E7=9A=84?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=EF=BC=8C=E8=B0=83=E6=95=B4?= =?UTF-8?q?sm2=E5=92=8Csm3=E7=9A=84=E5=AF=B9=E8=B1=A1=E6=B1=A0=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E6=95=B0=EF=BC=8C=E9=98=B2=E6=AD=A2=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E5=B9=B6=E5=8F=91=E6=97=B6=E5=AF=BC=E8=87=B4=E7=9A=84=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiaohui249 --- .gitignore | 3 +- src/main/java/twgc/gm/sm2/SM2Util.java | 45 ++++++++++++-------------- src/test/resources/SM2Pool.properties | 4 --- src/test/resources/SM3Pool.properties | 4 --- src/test/resources/SM4Pool.properties | 4 --- src/test/resources/pool-config.yaml | 4 +-- 6 files changed, 24 insertions(+), 40 deletions(-) delete mode 100644 src/test/resources/SM2Pool.properties delete mode 100644 src/test/resources/SM3Pool.properties delete mode 100644 src/test/resources/SM4Pool.properties diff --git a/.gitignore b/.gitignore index 448fed1..124bfae 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ build *.pem gradle/ gradlew -gradlew.bat \ No newline at end of file +gradlew.bat +out/ \ No newline at end of file diff --git a/src/main/java/twgc/gm/sm2/SM2Util.java b/src/main/java/twgc/gm/sm2/SM2Util.java index 7a2bd37..452fe80 100644 --- a/src/main/java/twgc/gm/sm2/SM2Util.java +++ b/src/main/java/twgc/gm/sm2/SM2Util.java @@ -94,7 +94,6 @@ public void setSignature(Signature signature) { * 生成 PKCS#10 证书请求 * * @return RSA P10 证书请求 Base64 字符串 - * @throws InvalidAlgorithmParameterException 当采用的 ECC 算法不适用于该密钥对生成器时 */ public KeyPair generatekeyPair() { return generator.generateKeyPair(); @@ -153,7 +152,6 @@ public static String pemFrom(PrivateKey privateKey, String password) throws Oper return sw.toString(); } - public static String pemFrom(PublicKey publicKey) throws IOException { StringWriter sw = new StringWriter(); try (PemWriter pemWriter = new PemWriter(sw)) { @@ -187,42 +185,39 @@ public static String pemFrom(X509Certificate x509Certificate) throws IOException } public static PrivateKey loadPrivFromFile(String filename, String password) throws IOException, OperatorCreationException, PKCSException { - FileReader fr = new FileReader(filename); - PEMParser pemReader = new PEMParser(fr); - Object obj = pemReader.readObject(); PrivateKey priv = null; - fr.close(); - pemReader.close(); - if (password != null && password.length() > 0) { - if (obj instanceof PKCS8EncryptedPrivateKeyInfo) { - PKCS8EncryptedPrivateKeyInfo epkInfo = (PKCS8EncryptedPrivateKeyInfo) obj; - InputDecryptorProvider decryptor = new JceOpenSSLPKCS8DecryptorProviderBuilder() - .setProvider(BouncyCastleProvider.PROVIDER_NAME) - .build(password.toCharArray()); - PrivateKeyInfo pkInfo = epkInfo.decryptPrivateKeyInfo(decryptor); - priv = CONVERTER.getPrivateKey(pkInfo); + try (PEMParser pemParser = new PEMParser(new FileReader(filename))) { + Object obj = pemParser.readObject(); + if (password != null && password.length() > 0) { + if (obj instanceof PKCS8EncryptedPrivateKeyInfo) { + PKCS8EncryptedPrivateKeyInfo epkInfo = (PKCS8EncryptedPrivateKeyInfo) obj; + InputDecryptorProvider decryptor = new JceOpenSSLPKCS8DecryptorProviderBuilder() + .setProvider(BouncyCastleProvider.PROVIDER_NAME) + .build(password.toCharArray()); + PrivateKeyInfo pkInfo = epkInfo.decryptPrivateKeyInfo(decryptor); + priv = CONVERTER.getPrivateKey(pkInfo); + } + } else { + priv = CONVERTER.getPrivateKey((PrivateKeyInfo) obj); } - } else { - priv = CONVERTER.getPrivateKey((PrivateKeyInfo) obj); } return priv; } - public static PublicKey loadPublicFromFile(String filename) throws IOException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException { - FileReader fr = new FileReader(filename); - PemObject spki = new PemReader(fr).readPemObject(); - fr.close(); - Provider p = Security.getProvider(BouncyCastleProvider.PROVIDER_NAME); - return KeyFactory.getInstance(Const.EC_VALUE, BouncyCastleProvider.PROVIDER_NAME).generatePublic(new X509EncodedKeySpec(spki.getContent())); + try (PemReader pemReader = new PemReader(new FileReader(filename))) { + PemObject spki = pemReader.readPemObject(); + Security.getProvider(BouncyCastleProvider.PROVIDER_NAME); + return KeyFactory.getInstance(Const.EC_VALUE, BouncyCastleProvider.PROVIDER_NAME).generatePublic(new X509EncodedKeySpec(spki.getContent())); + } } public static X509Certificate loadX509CertificateFromFile(String filename) throws IOException, CertificateException, NoSuchProviderException { - FileInputStream in = null; - in = new FileInputStream(filename); + try (FileInputStream in = new FileInputStream(filename)) { CertificateFactory cf = CertificateFactory.getInstance("X.509", BouncyCastleProvider.PROVIDER_NAME); return (X509Certificate) cf.generateCertificate(in); + } } public static PublicKey derivePublicFromPrivate(PrivateKey privateKey) { diff --git a/src/test/resources/SM2Pool.properties b/src/test/resources/SM2Pool.properties deleted file mode 100644 index 090b381..0000000 --- a/src/test/resources/SM2Pool.properties +++ /dev/null @@ -1,4 +0,0 @@ -maxTotal=10 -maxIdle=8 -minIdle=2 -maxWaitMillis=1000 \ No newline at end of file diff --git a/src/test/resources/SM3Pool.properties b/src/test/resources/SM3Pool.properties deleted file mode 100644 index 090b381..0000000 --- a/src/test/resources/SM3Pool.properties +++ /dev/null @@ -1,4 +0,0 @@ -maxTotal=10 -maxIdle=8 -minIdle=2 -maxWaitMillis=1000 \ No newline at end of file diff --git a/src/test/resources/SM4Pool.properties b/src/test/resources/SM4Pool.properties deleted file mode 100644 index 090b381..0000000 --- a/src/test/resources/SM4Pool.properties +++ /dev/null @@ -1,4 +0,0 @@ -maxTotal=10 -maxIdle=8 -minIdle=2 -maxWaitMillis=1000 \ No newline at end of file diff --git a/src/test/resources/pool-config.yaml b/src/test/resources/pool-config.yaml index 9cecc7b..b116741 100644 --- a/src/test/resources/pool-config.yaml +++ b/src/test/resources/pool-config.yaml @@ -6,11 +6,11 @@ default: &default sm2: <<: *default - maxTotal: 12 + maxTotal: 30 sm3: <<: *default - maxIdle: 10 + maxTotal: 30 sm4: <<: *default