| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics.CodeAnalysis; |
| | | 5 | | using System.Net.Sockets; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Security |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Non-blocking TLS session bound to a caller-supplied non-blocking |
| | | 11 | | /// <see cref="SafeSocketHandle"/>. The session performs its own ciphertext |
| | | 12 | | /// I/O on the socket via <see cref="Handshake"/>, <see cref="Read"/>, |
| | | 13 | | /// <see cref="Write"/>, <see cref="Shutdown"/>, and |
| | | 14 | | /// <see cref="RequestClientCertificate"/>. The socket must be configured |
| | | 15 | | /// non-blocking; behavior on a blocking socket is unspecified. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <remarks> |
| | | 18 | | /// The session takes ownership of the supplied socket and disposes it with |
| | | 19 | | /// the session. Call <see cref="TlsSession.SetContext"/> with a client or |
| | | 20 | | /// server <see cref="TlsContext"/> before invoking any operation. |
| | | 21 | | /// </remarks> |
| | | 22 | | [Experimental(Experimentals.LowLevelTlsDiagId, UrlFormat = Experimentals.SharedUrlFormat)] |
| | | 23 | | public sealed class TlsSocketSession : TlsSession |
| | | 24 | | { |
| | | 25 | | private readonly SafeSocketHandle _socket; |
| | | 26 | | |
| | 0 | 27 | | public TlsSocketSession(SafeSocketHandle socket) |
| | 0 | 28 | | { |
| | 0 | 29 | | ArgumentNullException.ThrowIfNull(socket); |
| | 0 | 30 | | _socket = socket; |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | internal override void OnContextInitialized() |
| | 0 | 34 | | { |
| | 0 | 35 | | AttachSocket(_socket); |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary>The socket the session is bound to. Owned by the session.</summary> |
| | 0 | 39 | | public SafeSocketHandle Socket => _socket; |
| | | 40 | | |
| | | 41 | | /// <summary>Drives the TLS handshake to completion, sending and receiving via the socket.</summary> |
| | 0 | 42 | | public TlsOperationStatus Handshake() => HandshakeSocketCore(); |
| | | 43 | | |
| | | 44 | | /// <summary>Reads decrypted application bytes from the socket into <paramref name="buffer"/>.</summary> |
| | | 45 | | public TlsOperationStatus Read(Span<byte> buffer, out int bytesRead) |
| | 0 | 46 | | => ReadSocketCore(buffer, out bytesRead); |
| | | 47 | | |
| | | 48 | | /// <summary>Encrypts and sends <paramref name="buffer"/> as one or more TLS records over the socket.</summary> |
| | | 49 | | public TlsOperationStatus Write(ReadOnlySpan<byte> buffer, out int bytesWritten) |
| | 0 | 50 | | => WriteSocketCore(buffer, out bytesWritten); |
| | | 51 | | |
| | | 52 | | /// <summary>Sends a TLS <c>close_notify</c> alert on the socket.</summary> |
| | 0 | 53 | | public TlsOperationStatus Shutdown() => ShutdownSocketCore(); |
| | | 54 | | |
| | | 55 | | /// <summary>Server-side only. Requests a client certificate on the socket: a <c>CertificateRequest</c> for TLS |
| | | 56 | | /// <remarks> |
| | | 57 | | /// If the TLS 1.3 client did not offer post-handshake authentication, no request is sent: |
| | | 58 | | /// the method returns <see cref="TlsOperationStatus.Complete"/>, the handshake stays complete, no client |
| | | 59 | | /// certificate is received, and the session remains usable. |
| | | 60 | | /// </remarks> |
| | 0 | 61 | | public TlsOperationStatus RequestClientCertificate() => RequestClientCertificateSocketCore(); |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | |