72
72
import javax .net .ssl .SSLEngine ;
73
73
import javax .net .ssl .SSLParameters ;
74
74
75
- import com .oracle .truffle .api .frame .MaterializedFrame ;
76
75
import org .bouncycastle .util .encoders .DecoderException ;
77
76
78
77
import com .oracle .graal .python .annotations .ArgumentClinic ;
122
121
import com .oracle .graal .python .nodes .util .CannotCastException ;
123
122
import com .oracle .graal .python .nodes .util .CastToJavaLongExactNode ;
124
123
import com .oracle .graal .python .nodes .util .CastToJavaStringNode ;
124
+ import com .oracle .graal .python .runtime .PythonContext ;
125
125
import com .oracle .graal .python .runtime .exception .PException ;
126
126
import com .oracle .graal .python .util .IPAddressUtil ;
127
127
import com .oracle .graal .python .util .PythonUtils ;
@@ -199,11 +199,11 @@ protected ArgumentClinicProvider getArgumentClinic() {
199
199
}
200
200
201
201
@ TruffleBoundary
202
- static SSLEngine createSSLEngine (MaterializedFrame frame , PConstructAndRaiseNode constructAndRaiseNode , PNodeWithRaise node , PSSLContext context , boolean serverMode , String serverHostname ) {
202
+ static SSLEngine createSSLEngine (PNodeWithRaise node , PSSLContext context , boolean serverMode , String serverHostname ) {
203
203
try {
204
204
context .init ();
205
205
} catch (NoSuchAlgorithmException | KeyStoreException | UnrecoverableKeyException | KeyManagementException | InvalidAlgorithmParameterException | IOException | CertificateException ex ) {
206
- throw constructAndRaiseNode . raiseSSLError ( frame , SSLErrorCode .ERROR_SSL , ex );
206
+ throw PConstructAndRaiseNode . raiseUncachedSSLError ( SSLErrorCode .ERROR_SSL , ex );
207
207
}
208
208
SSLParameters parameters = new SSLParameters ();
209
209
SSLEngine engine ;
@@ -268,14 +268,13 @@ private static boolean isIPAddress(String str) {
268
268
@ GenerateNodeFactory
269
269
abstract static class WrapSocketNode extends PythonClinicBuiltinNode {
270
270
@ Specialization
271
- Object wrap (VirtualFrame frame , PSSLContext context , PSocket sock , boolean serverSide , Object serverHostnameObj , Object owner , @ SuppressWarnings ("unused" ) PNone session ,
272
- @ Cached StringNodes .CastToJavaStringCheckedNode cast ,
273
- @ Cached PConstructAndRaiseNode constructAndRaiseNode ) {
271
+ Object wrap (PSSLContext context , PSocket sock , boolean serverSide , Object serverHostnameObj , Object owner , @ SuppressWarnings ("unused" ) PNone session ,
272
+ @ Cached StringNodes .CastToJavaStringCheckedNode cast ) {
274
273
String serverHostname = null ;
275
274
if (!(serverHostnameObj instanceof PNone )) {
276
275
serverHostname = cast .cast (serverHostnameObj , ErrorMessages .S_MUST_BE_NONE_OR_STRING , "serverHostname" , serverHostnameObj );
277
276
}
278
- SSLEngine engine = createSSLEngine (frame . materialize (), constructAndRaiseNode , this , context , serverSide , serverHostname );
277
+ SSLEngine engine = createSSLEngine (this , context , serverSide , serverHostname );
279
278
PSSLSocket sslSocket = factory ().createSSLSocket (PythonBuiltinClassType .PSSLSocket , context , engine , sock );
280
279
if (!(owner instanceof PNone )) {
281
280
sslSocket .setOwner (owner );
@@ -301,15 +300,14 @@ protected ArgumentClinicProvider getArgumentClinic() {
301
300
@ GenerateNodeFactory
302
301
abstract static class WrapBIONode extends PythonClinicBuiltinNode {
303
302
@ Specialization
304
- Object wrap (VirtualFrame frame , PSSLContext context , PMemoryBIO incoming , PMemoryBIO outgoing , boolean serverSide , Object serverHostnameObj , Object owner ,
303
+ Object wrap (PSSLContext context , PMemoryBIO incoming , PMemoryBIO outgoing , boolean serverSide , Object serverHostnameObj , Object owner ,
305
304
@ SuppressWarnings ("unused" ) PNone session ,
306
- @ Cached StringNodes .CastToJavaStringCheckedNode cast ,
307
- @ Cached PConstructAndRaiseNode constructAndRaiseNode ) {
305
+ @ Cached StringNodes .CastToJavaStringCheckedNode cast ) {
308
306
String serverHostname = null ;
309
307
if (!(serverHostnameObj instanceof PNone )) {
310
308
serverHostname = cast .cast (serverHostnameObj , ErrorMessages .S_MUST_BE_NONE_OR_STRING , "serverHostname" , serverHostnameObj );
311
309
}
312
- SSLEngine engine = createSSLEngine (frame . materialize (), constructAndRaiseNode , this , context , serverSide , serverHostname );
310
+ SSLEngine engine = createSSLEngine (this , context , serverSide , serverHostname );
313
311
PSSLSocket sslSocket = factory ().createSSLSocket (PythonBuiltinClassType .PSSLSocket , context , engine , incoming , outgoing );
314
312
if (!(owner instanceof PNone )) {
315
313
sslSocket .setOwner (owner );
@@ -693,10 +691,10 @@ Object load(VirtualFrame frame, PSSLContext self, Object cafile, Object capath,
693
691
if (!(cadata instanceof PNone )) {
694
692
Collection <?> certificates ;
695
693
try {
696
- certificates = fromString (frame , constructAndRaiseNode , castToString .execute (cadata ));
694
+ certificates = fromString (castToString .execute (cadata ));
697
695
} catch (CannotCastException cannotCastException ) {
698
696
if (cadata instanceof PBytesLike ) {
699
- certificates = fromBytesLike (frame . materialize (), constructAndRaiseNode , toBytes , cadata );
697
+ certificates = fromBytesLike (toBytes . execute ((( PBytesLike ) cadata ). getSequenceStorage ()) );
700
698
} else {
701
699
throw raise (TypeError , ErrorMessages .S_SHOULD_BE_ASCII_OR_BYTELIKE , "cadata" );
702
700
}
@@ -729,48 +727,47 @@ private TruffleFile toTruffleFile(VirtualFrame frame, PyUnicodeFSDecoderNode asP
729
727
}
730
728
}
731
729
732
- private List <Object > fromString (VirtualFrame frame , PConstructAndRaiseNode constructAndRaiseNode , String dataString )
730
+ private List <Object > fromString (String dataString )
733
731
throws IOException , CertificateException , KeyStoreException , NoSuchAlgorithmException , CRLException {
734
732
if (dataString .isEmpty ()) {
735
733
throw raise (ValueError , ErrorMessages .EMPTY_CERTIFICATE_DATA );
736
734
}
737
- return getCertificates (frame . materialize (), constructAndRaiseNode , dataString );
735
+ return getCertificates (dataString );
738
736
}
739
737
740
738
@ TruffleBoundary
741
- private List <Object > getCertificates (MaterializedFrame frame , PConstructAndRaiseNode constructAndRaiseNode , String dataString )
739
+ private List <Object > getCertificates (String dataString )
742
740
throws PException , CRLException , IOException , CertificateException {
743
741
try (BufferedReader r = new BufferedReader (new StringReader (dataString ))) {
744
742
try {
745
743
List <Object > certificates = CertUtils .getCertificates (r );
746
744
if (certificates .isEmpty ()) {
747
- throw constructAndRaiseNode . raiseSSLError ( frame , SSLErrorCode .ERROR_NO_START_LINE , ErrorMessages .SSL_PEM_NO_START_LINE );
745
+ throw PConstructAndRaiseNode . raiseUncachedSSLError ( SSLErrorCode .ERROR_NO_START_LINE , ErrorMessages .SSL_PEM_NO_START_LINE );
748
746
}
749
747
return certificates ;
750
748
} catch (DecoderException e ) {
751
- throw constructAndRaiseNode . raiseSSLError ( frame , SSLErrorCode .ERROR_BAD_BASE64_DECODE , ErrorMessages .BAD_BASE64_DECODE );
749
+ throw PConstructAndRaiseNode . raiseUncachedSSLError ( SSLErrorCode .ERROR_BAD_BASE64_DECODE , ErrorMessages .BAD_BASE64_DECODE );
752
750
} catch (IOException e ) {
753
- throw constructAndRaiseNode . raiseSSLError ( frame , SSLErrorCode .ERROR_SSL_PEM_LIB , ErrorMessages .SSL_PEM_LIB );
751
+ throw PConstructAndRaiseNode . raiseUncachedSSLError ( SSLErrorCode .ERROR_SSL_PEM_LIB , ErrorMessages .SSL_PEM_LIB );
754
752
}
755
753
}
756
754
}
757
755
758
756
@ TruffleBoundary
759
- private Collection <?> fromBytesLike (MaterializedFrame frame , PConstructAndRaiseNode constructAndRaiseNode , ToByteArrayNode toBytes , Object cadata )
757
+ private Collection <?> fromBytesLike (byte [] bytes )
760
758
throws KeyStoreException , IOException , NoSuchAlgorithmException {
761
- byte [] bytes = toBytes .execute (((PBytesLike ) cadata ).getSequenceStorage ());
762
759
try {
763
760
return CertUtils .generateCertificates (bytes );
764
761
} catch (CertificateException ex ) {
765
762
String msg = ex .getMessage ();
766
763
if (msg != null ) {
767
764
if (msg .contains ("No certificate data found" )) {
768
- throw constructAndRaiseNode . raiseSSLError ( frame , SSLErrorCode .ERROR_NOT_ENOUGH_DATA , ErrorMessages .NOT_ENOUGH_DATA );
765
+ throw PConstructAndRaiseNode . raiseUncachedSSLError ( SSLErrorCode .ERROR_NOT_ENOUGH_DATA , ErrorMessages .NOT_ENOUGH_DATA );
769
766
}
770
767
} else {
771
768
msg = "error while reading cadata" ;
772
769
}
773
- throw constructAndRaiseNode . raiseSSLError ( frame , SSLErrorCode .ERROR_SSL , msg );
770
+ throw PConstructAndRaiseNode . raiseUncachedSSLError ( null , SSLErrorCode .ERROR_SSL , msg );
774
771
}
775
772
}
776
773
}
@@ -795,12 +792,12 @@ Object load(VirtualFrame frame, PSSLContext self, Object certfile, Object keyfil
795
792
TruffleFile keyTruffleFile = toTruffleFile (frame , asPath .execute (frame , kf ));
796
793
try {
797
794
try {
798
- return load (frame . materialize (), constructAndRaiseNode , certTruffleFile , keyTruffleFile , null , self );
795
+ return load (getContext () , certTruffleFile , keyTruffleFile , null , self );
799
796
} catch (NeedsPasswordException e ) {
800
797
if (passwordObj != PNone .NONE ) {
801
798
char [] password = getPasswordNode .execute (frame , passwordObj );
802
799
try {
803
- return load (frame . materialize (), constructAndRaiseNode , certTruffleFile , keyTruffleFile , password , self );
800
+ return load (getContext () , certTruffleFile , keyTruffleFile , password , self );
804
801
} catch (NeedsPasswordException e1 ) {
805
802
throw CompilerDirectives .shouldNotReachHere ();
806
803
}
@@ -813,11 +810,11 @@ Object load(VirtualFrame frame, PSSLContext self, Object certfile, Object keyfil
813
810
}
814
811
815
812
@ TruffleBoundary
816
- private Object load (MaterializedFrame frame , PConstructAndRaiseNode constructAndRaiseNode , TruffleFile certTruffleFile , TruffleFile keyTruffleFile , char [] password , PSSLContext self )
813
+ private Object load (PythonContext context , TruffleFile certTruffleFile , TruffleFile keyTruffleFile , char [] password , PSSLContext self )
817
814
throws IOException , NeedsPasswordException {
818
815
try (BufferedReader certReader = getReader (certTruffleFile , "certfile" );
819
816
BufferedReader keyReader = getReader (keyTruffleFile , "keyfile" )) {
820
- return load (frame , constructAndRaiseNode , self , certReader , keyReader , password );
817
+ return load (context , self , certReader , keyReader , password );
821
818
}
822
819
}
823
820
@@ -830,7 +827,7 @@ private BufferedReader getReader(TruffleFile file, String arg) throws IOExceptio
830
827
}
831
828
}
832
829
833
- private Object load (MaterializedFrame frame , PConstructAndRaiseNode constructAndRaiseNode , PSSLContext self , BufferedReader certReader , BufferedReader keyReader , char [] password )
830
+ private Object load (PythonContext context , PSSLContext self , BufferedReader certReader , BufferedReader keyReader , char [] password )
834
831
throws NeedsPasswordException {
835
832
// TODO add logging
836
833
try {
@@ -839,17 +836,17 @@ private Object load(MaterializedFrame frame, PConstructAndRaiseNode constructAnd
839
836
List <Object > certificates = CertUtils .getCertificates (certReader , true );
840
837
certs = certificates .toArray (new X509Certificate [certificates .size ()]);
841
838
if (certs .length == 0 ) {
842
- throw constructAndRaiseNode . raiseSSLError ( frame , SSLErrorCode .ERROR_SSL_PEM_LIB , ErrorMessages .SSL_PEM_LIB );
839
+ throw PConstructAndRaiseNode . raiseUncachedSSLError ( SSLErrorCode .ERROR_SSL_PEM_LIB , ErrorMessages .SSL_PEM_LIB );
843
840
}
844
841
} catch (IOException | DecoderException e ) {
845
- throw constructAndRaiseNode . raiseSSLError ( frame , SSLErrorCode .ERROR_SSL_PEM_LIB , ErrorMessages .SSL_PEM_LIB );
842
+ throw PConstructAndRaiseNode . raiseUncachedSSLError ( SSLErrorCode .ERROR_SSL_PEM_LIB , ErrorMessages .SSL_PEM_LIB );
846
843
}
847
844
// if keyReader and certReader are from the same file, key is expected to come first
848
- PrivateKey pk = CertUtils .getPrivateKey (frame , constructAndRaiseNode , keyReader , password , certs [0 ]);
845
+ PrivateKey pk = CertUtils .getPrivateKey (context , keyReader , password , certs [0 ]);
849
846
self .setCertChain (pk , PythonUtils .EMPTY_CHAR_ARRAY , certs );
850
847
return PNone .NONE ;
851
848
} catch (GeneralSecurityException | IOException ex ) {
852
- throw constructAndRaiseNode . raiseSSLError ( frame , SSLErrorCode .ERROR_SSL , ex );
849
+ throw PConstructAndRaiseNode . raiseUncachedSSLError ( SSLErrorCode .ERROR_SSL , ex );
853
850
}
854
851
}
855
852
@@ -1004,8 +1001,7 @@ Object getCerts(VirtualFrame frame, PSSLContext self, @SuppressWarnings("unused"
1004
1001
}
1005
1002
1006
1003
@ Specialization (guards = "binary_form" )
1007
- Object getCertsBinary (VirtualFrame frame , PSSLContext self , @ SuppressWarnings ("unused" ) boolean binary_form ,
1008
- @ Cached PConstructAndRaiseNode constructAndRaiseNode ) {
1004
+ Object getCertsBinary (PSSLContext self , @ SuppressWarnings ("unused" ) boolean binary_form ) {
1009
1005
try {
1010
1006
List <PBytes > result = PythonUtils .newList ();
1011
1007
for (X509Certificate cert : self .getCACerts ()) {
@@ -1015,7 +1011,7 @@ Object getCertsBinary(VirtualFrame frame, PSSLContext self, @SuppressWarnings("u
1015
1011
}
1016
1012
return factory ().createList (PythonUtils .toArray (result ));
1017
1013
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateEncodingException ex ) {
1018
- throw constructAndRaiseNode . raiseSSLError ( frame , SSLErrorCode .ERROR_SSL , ex );
1014
+ throw PConstructAndRaiseNode . raiseUncachedSSLError ( SSLErrorCode .ERROR_SSL , ex );
1019
1015
}
1020
1016
}
1021
1017
0 commit comments