| 39 | // 参考: https://docs.oracle.com/en/java/javase/11/docs/specs/security/standard-names.html |
| 40 | // 编译时需要: --add-exports java.base/sun.security.x509=ALL-UNNAMED |
| 41 | public final class Cert { |
| 42 | static { |
| 43 | try { |
| 44 | var m = Module.class.getDeclaredMethod("implAddOpensToAllUnnamed", String.class); |
| 45 | Json.setAccessible(m); // force accessible |
| 46 | m.invoke(Certificate.class.getModule(), "sun.security.rsa"); // --add-opens java.base/sun.security.rsa=ALL-UNNAMED |
| 47 | m.invoke(Certificate.class.getModule(), "sun.security.util"); // --add-opens java.base/sun.security.util=ALL-UNNAMED |
| 48 | m.invoke(Certificate.class.getModule(), "sun.security.x509"); // --add-opens java.base/sun.security.x509=ALL-UNNAMED |
| 49 | } catch (ReflectiveOperationException e) { |
| 50 | throw new ExceptionInInitializerError(e); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // 从输入流加载KeyStore(PKCS12格式的二进制密钥存储格式,有密码加密,包含私钥和公钥证书) |
| 55 | public static KeyStore loadKeyStore(InputStream inputStream, String passwd) |
| 56 | throws GeneralSecurityException, IOException { |
| 57 | var keyStore = KeyStore.getInstance("pkcs12"); |
| 58 | keyStore.load(inputStream, passwd != null ? passwd.toCharArray() : null); |
| 59 | return keyStore; |
| 60 | } |
| 61 | |
| 62 | // 从KeyStore里获取公钥证书 |
| 63 | public static Certificate getCertificate(KeyStore keyStore, String alias) throws KeyStoreException { |
| 64 | return keyStore.getCertificate(alias); |
| 65 | } |
| 66 | |
| 67 | // 从KeyStore里获取公钥 |
| 68 | public static PublicKey getPublicKey(KeyStore keyStore, String alias) throws KeyStoreException { |
| 69 | return keyStore.getCertificate(alias).getPublicKey(); |
| 70 | } |
| 71 | |
| 72 | // 从KeyStore里获取私钥 |
| 73 | public static PrivateKey getPrivateKey(KeyStore keyStore, String passwd, String alias) |
| 74 | throws GeneralSecurityException { |
| 75 | return (PrivateKey)keyStore.getKey(alias, passwd != null ? passwd.toCharArray() : null); |
| 76 | } |
| 77 | |
| 78 | // 以DER二进制编码加载X509公钥证书(此二进制编码即Certificate.getEncoded()的结果) |
| 79 | public static X509Certificate loadCertificate(byte[] encodedCertificate) throws GeneralSecurityException { |
| 80 | return new X509CertImpl(encodedCertificate); |
| 81 | } |
| 82 | |
| 83 | // 从DER或PEM编码的输入流加载X509公钥证书 |
| 84 | public static X509Certificate loadCertificate(InputStream encodedCertificate) throws GeneralSecurityException { |
| 85 | return new X509CertImpl(encodedCertificate); |
| 86 | } |
| 87 | |
| 88 | // 以PKCS#8的二进制编码加载RSA公钥(此二进制编码即PublicKey.getEncoded()的结果) |
| 89 | public static PublicKey loadRsaPublicKey(byte[] encodedPublicKey) throws GeneralSecurityException { |
| 90 | return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(encodedPublicKey)); |
| 91 | } |
| 92 | |
| 93 | // 以PKCS#8的二进制编码加载椭圆曲线公钥(此二进制编码即PublicKey.getEncoded()的结果) |
| 94 | public static PublicKey loadEcPublicKey(byte[] encodedPublicKey) throws GeneralSecurityException { |
| 95 | return KeyFactory.getInstance("EC").generatePublic(new X509EncodedKeySpec(encodedPublicKey)); |
| 96 | } |
| 97 | |
| 98 | // 以PKCS#1的二进制编码加载RSA公钥 |
nothing calls this directly
no test coverage detected