52
52
import java .nio .charset .StandardCharsets ;
53
53
import java .security .InvalidAlgorithmParameterException ;
54
54
import java .security .KeyManagementException ;
55
- import java .security .KeyStore ;
56
55
import java .security .KeyStoreException ;
57
56
import java .security .NoSuchAlgorithmException ;
58
57
import java .security .PrivateKey ;
59
58
import java .security .UnrecoverableKeyException ;
60
59
import java .security .cert .CRLException ;
61
- import java .security .cert .Certificate ;
62
60
import java .security .cert .CertificateException ;
63
61
import java .security .cert .X509Certificate ;
64
62
import java .security .spec .InvalidKeySpecException ;
65
63
import java .util .ArrayList ;
66
64
import java .util .Collections ;
67
- import java .util .Enumeration ;
68
65
import java .util .List ;
69
66
import java .util .logging .Level ;
70
67
125
122
import com .oracle .truffle .api .frame .VirtualFrame ;
126
123
import com .oracle .truffle .api .interop .UnsupportedMessageException ;
127
124
import com .oracle .truffle .api .library .CachedLibrary ;
125
+ import java .security .cert .CertificateEncodingException ;
126
+ import java .security .cert .CertificateParsingException ;
128
127
129
128
@ CoreFunctions (extendClasses = PythonBuiltinClassType .PSSLContext )
130
129
public class SSLContextBuiltins extends PythonBuiltins {
@@ -629,24 +628,15 @@ abstract static class CertStoreStatsNode extends PythonUnaryBuiltinNode {
629
628
@ Specialization
630
629
Object storeStats (PSSLContext self ) {
631
630
try {
632
- KeyStore keystore = self .getCAKeyStore ();
633
- Enumeration <String > aliases = keystore .aliases ();
634
631
int x509 = 0 , crl = 0 , ca = 0 ;
635
- while (aliases .hasMoreElements ()) {
636
- String alias = aliases .nextElement ();
637
- if (keystore .isCertificateEntry (alias )) {
638
- Certificate cert = keystore .getCertificate (alias );
639
- if (cert instanceof X509Certificate ) {
640
- X509Certificate x509Cert = (X509Certificate ) cert ;
641
- boolean [] keyUsage = ((X509Certificate ) cert ).getKeyUsage ();
642
- if (CertUtils .isCrl (keyUsage )) {
643
- crl ++;
644
- } else {
645
- x509 ++;
646
- if (CertUtils .isCA (x509Cert , keyUsage )) {
647
- ca ++;
648
- }
649
- }
632
+ for (X509Certificate cert : self .getCACerts ()) {
633
+ boolean [] keyUsage = cert .getKeyUsage ();
634
+ if (CertUtils .isCrl (keyUsage )) {
635
+ crl ++;
636
+ } else {
637
+ x509 ++;
638
+ if (CertUtils .isCA (cert , keyUsage )) {
639
+ ca ++;
650
640
}
651
641
}
652
642
}
@@ -995,16 +985,13 @@ abstract static class GetCACerts extends PythonBinaryClinicBuiltinNode {
995
985
Object getCerts (PSSLContext self , @ SuppressWarnings ("unused" ) boolean binary_form ) {
996
986
try {
997
987
List <PDict > result = new ArrayList <>();
998
- KeyStore ks = self .getCAKeyStore ();
999
- Enumeration <String > aliases = ks .aliases ();
1000
- while (aliases .hasMoreElements ()) {
1001
- X509Certificate cert = (X509Certificate ) ks .getCertificate (aliases .nextElement ());
988
+ for (X509Certificate cert : self .getCACerts ()) {
1002
989
if (CertUtils .isCA (cert , cert .getKeyUsage ())) {
1003
990
result .add (CertUtils .decodeCertificate (cert ));
1004
991
}
1005
992
}
1006
993
return factory ().createList (result .toArray (new Object [result .size ()]));
1007
- } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException ex ) {
994
+ } catch (KeyStoreException | NoSuchAlgorithmException | IOException | CertificateParsingException ex ) {
1008
995
throw PRaiseSSLErrorNode .raiseUncached (this , SSLErrorCode .ERROR_SSL , ex );
1009
996
}
1010
997
}
@@ -1014,16 +1001,13 @@ Object getCerts(PSSLContext self, @SuppressWarnings("unused") boolean binary_for
1014
1001
Object getCertsBinary (PSSLContext self , @ SuppressWarnings ("unused" ) boolean binary_form ) {
1015
1002
try {
1016
1003
List <PBytes > result = new ArrayList <>();
1017
- KeyStore ks = self .getCAKeyStore ();
1018
- Enumeration <String > aliases = ks .aliases ();
1019
- while (aliases .hasMoreElements ()) {
1020
- X509Certificate cert = (X509Certificate ) ks .getCertificate (aliases .nextElement ());
1004
+ for (X509Certificate cert : self .getCACerts ()) {
1021
1005
if (CertUtils .isCA (cert , cert .getKeyUsage ())) {
1022
1006
result .add (factory ().createBytes (cert .getEncoded ()));
1023
1007
}
1024
1008
}
1025
1009
return factory ().createList (result .toArray (new Object [result .size ()]));
1026
- } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException ex ) {
1010
+ } catch (KeyStoreException | NoSuchAlgorithmException | CertificateEncodingException ex ) {
1027
1011
throw PRaiseSSLErrorNode .raiseUncached (this , SSLErrorCode .ERROR_SSL , ex );
1028
1012
}
1029
1013
}
0 commit comments