Repository navigation
Refactoring session management - #172
Conversation
|
@kruton FYI I have done some work on porting over the one of the integration tests as well. We'll probably want to get that working here as well. nmittler@8d40a89 |
|
|
||
| const SSL_CIPHER* cipher = ssl_session->cipher; | ||
| const char* name = SSL_CIPHER_get_name(cipher); | ||
| JNI_TRACE("ssl_session=%p NativeCrypto_SSL_cipher => %s", ssl_session, name); |
There was a problem hiding this comment.
The whole point was to avoid SSL_get_session and direct accesses of SSL_SESSION fields. I meant in the C++ code and the Java code. If you need to add a call to SSL_get_session, there is probably something wrong. If you need to reach into a field directly, there is also probably something wrong.
Use BoringSSL's public APIs. Don't reach into library internals. This should be SSL_get_current_cipher.
There was a problem hiding this comment.
Agreed ... I wasn't sure what the right method was here. Done.
| } | ||
|
|
||
| JNI_TRACE("ssl_session=%p NativeCrypto_get_SSL_tlsext_hostname => \"%s\"", | ||
| ssl_session, ssl_session->tlsext_hostname); |
There was a problem hiding this comment.
Ditto. SSL_get_servername.
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_cipher, "(J)Ljava/lang/String;"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, get_SSL_tlsext_hostname, "(J)Ljava/lang/String;"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_get_version, "(J)Ljava/lang/String;"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_cipher, "(J)Ljava/lang/String;"), |
There was a problem hiding this comment.
The end result of this change should be that many JNI accessors go away because they were incorrect.
There was a problem hiding this comment.
Agreed. ATM this change only affects the socket. Before submitting, the engine class should also be changed so that the old methods are no longer used (and can therefore be removed).
| * (i.e. {@link #newInstance(byte[], String, int, AbstractSessionContext)}) and offered to | ||
| * another connection, it will have all of the information needed to resume the session. | ||
| */ | ||
| final class CacheableSession implements SSLSession { |
There was a problem hiding this comment.
This should not be an SSLSession at all.
There was a problem hiding this comment.
Yes, that seems like it would simplify this section of the code. It never escapes, so it should be a simple change correct?
| * Serializes the native state of the session (ID, cipher, and keys but | ||
| * not certificates). Returns a byte[] containing the DER-encoded state. | ||
| * See apache mod_ssl. | ||
| */ |
There was a problem hiding this comment.
This does not appear to be used (and is a rather disturbing sounding function). You're not serializing an SSL
|
I didn't do a complete review, just checked on a few things to see if the refactor did what #98 was about. |
| } | ||
|
|
||
| JNI_TRACE("ssl_session=%p NativeCrypto_get_SSL_tlsext_hostname => \"%s\"", | ||
| ssl_session, ssl_session->tlsext_hostname); |
|
|
||
| const SSL_CIPHER* cipher = ssl_session->cipher; | ||
| const char* name = SSL_CIPHER_get_name(cipher); | ||
| JNI_TRACE("ssl_session=%p NativeCrypto_SSL_cipher => %s", ssl_session, name); |
There was a problem hiding this comment.
Agreed ... I wasn't sure what the right method was here. Done.
| * Serializes the native state of the session (ID, cipher, and keys but | ||
| * not certificates). Returns a byte[] containing the DER-encoded state. | ||
| * See apache mod_ssl. | ||
| */ |
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_cipher, "(J)Ljava/lang/String;"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, get_SSL_tlsext_hostname, "(J)Ljava/lang/String;"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_get_version, "(J)Ljava/lang/String;"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_cipher, "(J)Ljava/lang/String;"), |
There was a problem hiding this comment.
Agreed. ATM this change only affects the socket. Before submitting, the engine class should also be changed so that the old methods are no longer used (and can therefore be removed).
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("unused") // used by NativeCrypto.SSLHandshakeCallbacks / info_callback |
There was a problem hiding this comment.
This comment probably needs to be changed from info_callback.
There was a problem hiding this comment.
Done .... renamed to new_session_callback.
| @SuppressWarnings("MissingOverride") // For compilation with Java 6. | ||
| public SSLSession getHandshakeSession() { | ||
| return handshakeSession; | ||
| return sslSession; |
There was a problem hiding this comment.
I'm kind of nervous about this relaxing a restriction we'll have to live with forever. The Javadoc for this method says:
Returns: null if this instance is not currently handshaking, ...
| OpenSSLSocketImplWrapper(Socket socket, String hostname, int port, | ||
| boolean autoClose, SSLParametersImpl sslParameters) throws IOException { | ||
| super(socket, hostname, port, autoClose, sslParameters); | ||
| if (!socket.isConnected()) { |
There was a problem hiding this comment.
Why was this class changed? This seems unrelated to the topic.
There was a problem hiding this comment.
Ah right ... I was originally hacking this to get the new integration tests to pass. Let me revert for now and will address the integration tests with a follow-on PR.
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, getDirectBufferAddress, "(Ljava/nio/Buffer;)J"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_BIO_new, "(J)J"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_get0_session, "(J)J"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_get1_session, "(J)J"), |
There was a problem hiding this comment.
I suppose this will be removed when OpenSSLEngineImpl is also fixed?
|
@kruton I've just pushed some major changes. PTAL. |
3aac1ad to
aa808b0
Compare
| Iterator<SSLSession> i = sessions.values().iterator(); | ||
| do { | ||
| SSLSession session = i.next(); | ||
| Iterator<SslSessionWrapper> i = sessionsIterator(); |
There was a problem hiding this comment.
nit: whitespace. git clang-format should have caught the whitespace, though.
| */ | ||
| private final HashMap<HostAndPort, SSLSession> sessionsByHostAndPort = new HashMap<>(); | ||
| @SuppressWarnings("serial") | ||
| private final HashMap<HostAndPort, SslSessionWrapper> sessionsByHostAndPort = |
There was a problem hiding this comment.
Either Map or LinkedHashMap would be a better choice for the field type here.
|
|
||
| /** | ||
| * Object used for locking access to the sessions. | ||
| */ |
There was a problem hiding this comment.
This seems unorthodox, but it works.
There was a problem hiding this comment.
Yeah it's admittedly ugly. Alternatively, we could synchronize on the class. WDYT?
|
|
||
| bundled_test_java_files := $(filter-out \ | ||
| %/org/conscrypt/AbstractSessionContextTest.java \ | ||
| %/org/conscrypt/ClientSessionContextTest.java \ |
There was a problem hiding this comment.
nit: not sure if github is displaying this wrong or if there is missing whitespace here.
| Method m_getResponses; | ||
| try { | ||
| m_getResponses = session.getClass().getDeclaredMethod("getStatusResponses"); | ||
| m_getResponses = session.getClass().getDeclaredMethod("getOcspStapledResponse"); |
There was a problem hiding this comment.
This probably shouldn't be renamed since it's the name of record currently.
There was a problem hiding this comment.
Ah good point. done.
29301c5 to
af80971
Compare
af80971 to
10c86d4
Compare
0bb075a to
98f3316
Compare
|
@kruton I've re-based onto the latest master (with the engine/engine-socket changes). PTAL |
| static SSLSession wrapSSLSession(AbstractOpenSSLSession sslSession) { | ||
| return new OpenSSLExtendedSessionImpl(sslSession); | ||
| static SSLSession wrapSSLSession(SSLSession sslSession) { | ||
| return new DelegatingExtendedSSLSession((ActiveSession) sslSession); |
There was a problem hiding this comment.
We should change these to take ActiveSession as the input for type safety.
| static SSLSession wrapSSLSession(AbstractOpenSSLSession sslSession) { | ||
| return new OpenSSLExtendedSessionImpl(sslSession); | ||
| static SSLSession wrapSSLSession(SSLSession sslSession) { | ||
| return new DelegatingExtendedSSLSession((ActiveSession) sslSession); |
There was a problem hiding this comment.
We should change these to take ActiveSession as the input for type safety.
| } | ||
|
|
||
| return new OpenSSLExtendedSessionImpl(sslSession); | ||
| return new DelegatingExtendedSSLSession((ActiveSession) sslSession); |
There was a problem hiding this comment.
We should change these to take ActiveSession as the input for type safety.
| // timeout as part of their validity condition. | ||
| if (!session.isValid()) { | ||
| // Let the subclass know. | ||
| removingSession(session); |
There was a problem hiding this comment.
Maybe call this onBeforeRemoveSession or something indicating it an event callback (prefix of on...).
| } else { | ||
| return session; | ||
| // Let the subclass know. | ||
| addingSession(session); |
| abstract SslSessionWrapper getSessionFromPersistentCache(byte[] sessionId); | ||
|
|
||
| /** | ||
| * Makes sure cache size is < maximumSize. |
There was a problem hiding this comment.
Should this sort by oldest to newest? That would seem to make the most sense.
There was a problem hiding this comment.
The logic here should be identical to the old code. Since they are stored in a LinkedHashMap, iteration should be based on insertion order. Or am I missing something?
| @@ -0,0 +1,334 @@ | |||
| package org.conscrypt; | |||
This change breaks session management into two distinct types: - SslSessionWrapper: These are created as BoringSSL calls back the new session handler, allowing the application to cache sessions. Clients will also offer these to BoringSSL for reuse if a compatible session was found. BoringSSL is free to use it or not, but the Conscrypt code no longer makes assumptions here. Instead, it always uses the ActiveSession. - ActiveSession: This is a session that wraps the SSL instance (rather than the SSL_SESSION wherever possible). That way no concern has to be paid to what BoringSSL is doing with sessions under the covers. Fixes google#98
241f530 to
3c9f16e
Compare
|
@kruton any other comments? |
|
No, LGTM. |
davidben
left a comment
There was a problem hiding this comment.
I only skimmed this since I don't really know the Java half of Conscrypt. I'll defer to others for that. I mostly just looked at the C side and immediate callers. Some comments, but otherwise looks good!
Thanks for doing this, and sorry about my slowness. :-(
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_session_id, "(J)[B"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_get_version, "(J)Ljava/lang/String;"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_cipher, "(J)Ljava/lang/String;"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, get_SSL_SESSION_tlsext_hostname, "(J)Ljava/lang/String;"), |
There was a problem hiding this comment.
This seems to be unused now (YAY!!!!).
There was a problem hiding this comment.
Ah cool, I missed that! Removed!
| try { | ||
| // Cache the newly established session. | ||
| AbstractSessionContext ctx = sessionContext(); | ||
| ctx.cacheSession(SslSessionWrapper.newInstance(sslSessionNativePtr, sslSession)); |
There was a problem hiding this comment.
SslSessionWrapper.newInstance takes ownership of sslSessionNativePtr, right? So suppose ctx.cacheSession throws an exception. We've already taken ownership of sslSessionNativePtr, but this function will return false. That will cause the C code to think we haven't taken ownership and double-free.
Two possible fixes:
-
The C-level callback returns true if it's called the callback at all and this method returns
void. The promise would be that the Java code, no matter what happens, takes ownership ofsslSessionNativePtr. That would mean you want to wrapsslSessionNativePtras the first line I guess. -
Alternatively,
SslSessionWrapper.newInstancedoes not take ownership. Rather it callsSSL_SESSION_up_refbefore stashing the pointer on itself. The JavaonNewSessionEstablishedthen also does not take ownership of the provided reference, so the C-level callback always returns false. As in the first option, this function also now returnsvoid.
(It's perfectly fine for the return value to not match whether the session was. It's some bizarre OpenSSL legacy thing and doesn't terminate the connection or anything. Think of the return value as that goofy out_copy parameter to the other callback. It's another one of those weird things where OpenSSL should have just picked one and left it at that. Pick whichever return value works better and don't think too hard about making it match whether operations succeeded or not.)
There was a problem hiding this comment.
@davidben Good catch! Yes I think moving newInstance to the first line is the way to go. I think I'd like to keep the signature to this method the same as the BoringSSL API, however for consistency.
| } | ||
|
|
||
| static SSL_SESSION* server_session_requested_callback(SSL* ssl, uint8_t* id, int id_len, | ||
| int* out_copy) { |
There was a problem hiding this comment.
This doesn't do anything right now, right? I probably would have left this and the Java stub out for now. Your call.
(This callback only gets called if the internal session cache can't find anything, so it's fine to leave it out.)
There was a problem hiding this comment.
I'd probably also suggest bothering to caching anything via the callback on the server if you're not implementing this hook. (You might already be doing this.)
There was a problem hiding this comment.
Yeah exactly ... the callback isn't currently being used on the Java side, but I do have a TBD to implement it. So I think I'd prefer keeping this here so that the only required change is to Java.
There was a problem hiding this comment.
Bothering or not bothering? The wording makes it sound like a typo here.
There was a problem hiding this comment.
Er, sorry I meant "not bothering". I'm really bad at typing.
| } | ||
|
|
||
| static SSL_SESSION* server_session_requested_callback(SSL* ssl, uint8_t* id, int id_len, | ||
| int* out_copy) { |
There was a problem hiding this comment.
Yeah exactly ... the callback isn't currently being used on the Java side, but I do have a TBD to implement it. So I think I'd prefer keeping this here so that the only required change is to Java.
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_session_id, "(J)[B"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_get_version, "(J)Ljava/lang/String;"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, SSL_SESSION_cipher, "(J)Ljava/lang/String;"), | ||
| CONSCRYPT_NATIVE_METHOD(NativeCrypto, get_SSL_SESSION_tlsext_hostname, "(J)Ljava/lang/String;"), |
There was a problem hiding this comment.
Ah cool, I missed that! Removed!
| try { | ||
| // Cache the newly established session. | ||
| AbstractSessionContext ctx = sessionContext(); | ||
| ctx.cacheSession(SslSessionWrapper.newInstance(sslSessionNativePtr, sslSession)); |
There was a problem hiding this comment.
@davidben Good catch! Yes I think moving newInstance to the first line is the way to go. I think I'd like to keep the signature to this method the same as the BoringSSL API, however for consistency.
| return true; | ||
| } catch (Exception ignored) { | ||
| sessionWrapper = SslSessionWrapper.newInstance(sslSessionNativePtr, sslSession); | ||
| } catch (Throwable e) { |
There was a problem hiding this comment.
Hrm. This is still kinda screwy. If newInstance throws in activeSession.getSessionContext() (sorry, I missed that there was something before the constructor call), then this works. But if it throws in Impl(), at SSL_SESSION_get_version, the finalizer will still run, right? Which means that Impl has taken ownership of the pointer and we double-free.
I'm not sure I see the point of making the return value match the C one. You don't match the out_copy thing and it's the same deal. Again, the return value doesn't mean success or failure. It's purely a weird lifetimes thing. In a language like Java, it doesn't make sense to handle lifetimes by tossing booleans around, so it doesn't make sense to keep the return value.
There was a problem hiding this comment.
I've reworked things ... take a look and see if it's more clear now.
There was a problem hiding this comment.
Much clearer. Thanks! One small comment: you should be able to avoid checking for NativeRef.SSL_SESSION throwing. I suppose it does technically throw NPE, but then all this freeing is moot anyway. (Also, you'd have crashed before that point in up_ref and the callback will never give you a NULL pointer.)
| jclass cls = env->GetObjectClass(sslHandshakeCallbacks); | ||
| jmethodID methodID = env->GetMethodID(cls, "serverSessionRequested", "([B)J"); | ||
| JNI_TRACE("ssl=%p server_session_requested_callback calling serverSessionRequested", ssl); | ||
| jlong ssl_session_address = env->CallLongMethod(sslHandshakeCallbacks, methodID, id_array); |
There was a problem hiding this comment.
The lifetimes here are very non-obvious without C and Java halves together. See the mess with the new session callback. The thing about these callbacks across JNI is that you cannot review one without the other. I believe this is correct, but you need to remember to SSL_SESSION_up_ref in Java while the SslSessionWrapper is alive.
It would be much better to take it out so that, when the Java half is reviewed, the C half is in the diff at the same time for simultaneous review.
| JNI_TRACE("ssl=%p new_session_callback calling onNewSessionEstablished", ssl); | ||
| jboolean ret = env->CallBooleanMethod(sslHandshakeCallbacks, methodID, | ||
| env->CallBooleanMethod(sslHandshakeCallbacks, methodID, | ||
| reinterpret_cast<jlong>(session)); |
There was a problem hiding this comment.
Super nit-picky nitpick: formatting? (I just have a key-binding for clang-format and try not to think too hard about such things.)
| jmethodID methodID = env->GetMethodID(cls, "onNewSessionEstablished", "(J)V"); | ||
| JNI_TRACE("ssl=%p new_session_callback calling onNewSessionEstablished", ssl); | ||
| jboolean ret = env->CallBooleanMethod(sslHandshakeCallbacks, methodID, | ||
| env->CallBooleanMethod(sslHandshakeCallbacks, methodID, |
| } catch (Exception ignored) { | ||
| // Failed constructing the wrapper. Its finalizer will not release the reference, so | ||
| // we need to here. | ||
| NativeCrypto.SSL_SESSION_free(sslSessionNativePtr); |
There was a problem hiding this comment.
I think this works, since you moved the saving to the end, but this relies on, e.g., SslSessionWrapper.newInstance not doing anything after creating the SslSessionWrapper. Another option would be to say SslSessionWrapper.newInstance and SslSessionWrapper.Impl() do not take ownership of the reference. Instead, the Impl constructor just looks like:
NativeCrypto.SSL_SESSION_up_ref(sslSessionNativePointer);
this.sslSessionNativePointer = sslSessionNativePointer;
Though I guess that might be different from the rest of Conscrypt? Whatever works.
There was a problem hiding this comment.
As discussed offline, now using NativeRef.
|
@davidben PTAL |
| return true; | ||
| } catch (Exception ignored) { | ||
| sessionWrapper = SslSessionWrapper.newInstance(sslSessionNativePtr, sslSession); | ||
| } catch (Throwable e) { |
There was a problem hiding this comment.
Much clearer. Thanks! One small comment: you should be able to avoid checking for NativeRef.SSL_SESSION throwing. I suppose it does technically throw NPE, but then all this freeing is moot anyway. (Also, you'd have crashed before that point in up_ref and the callback will never give you a NULL pointer.)
|
Hrm, GitHub is hiding my comment:
|
kruton
left a comment
There was a problem hiding this comment.
Looks good to me after latest round with NativeRef where it takes ownership immediately.
This change breaks session management into two distinct types:
SslSessionWrapper: These are created as BoringSSL calls back the new session handler, allowing the application to cache sessions. Clients will also offer these to BoringSSL for reuse if a compatible session was found. BoringSSL is free to use it or not, but the Conscrypt code no longer makes assumptions here. Instead, it always uses the ActiveSession.
ActiveSession: This is a session that wraps the SSL instance (rather than the SSL_SESSION wherever possible). That way no concern has to be paid to what BoringSSL is doing with sessions under the covers.
Fixes #98