Need Help In Connecting TLS/SSL Connection with CA_Chain Certificate, Client Certificate and Private Key in .Net c#

Anonymous
2023-12-11T09:38:03.4733333+00:00

i need to connect with HSM with a IP and Port, and it will open a TLS/SSL TCP connection, before handshaking it need to add certificate to authanticate.i received python code for sample to connect with it. but i need that part in .net/.net core c#.

Here is few line of code of python that i need in c#:

# Create socket
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.settimeout(2.000)

# Get the script directory so certificates can be loaded
            certFolder = os.path.abspath(os.path.dirname(sys.argv[0]))

            # Create the TLS context
            context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
            if args.hsm_type.upper() == 'A':
                context.set_ciphers("AES256-GCM-SHA384")

            # Do not check the hostname when verifying the server certificate
            context.check_hostname = False

            # Load certificates and private key
            context.load_verify_locations(certFolder + "/CA_MYHSM_chain.crt")
            context.load_cert_chain(certfile=certFolder + "/client_MYHSM.crt", keyfile=certFolder + "/client_MYHSM.key")

            # Wrap socket into TLS socket
            with context.wrap_socket(sock) as ssock:
                ssock.connect((args.host, int(args.port)))
Developer technologies .NET Other
Developer technologies C#
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.