Search code examples
c#javasslcisco

C# equivalent to Java SSL-socket-connection


Hey guys I'm trying to write an AXL-client (SOAP) for the Cisco Unified Communications Manager. For that purpose I need to establish an ssl-connection to the AXL-service. Unfortunatly I dont know much about all that ssl-stuff.

However I was able to find a working Java-example, that does, what I want. The problem is, i need that in C#.NET. So I'm hoping, that someone could "translate" the following Java-code in a C#-version. But it has to do exactly the same, espacially the authentication and certificate-stuff.

Here is the code:

    String sAXLSOAPRequest = "...";
    byte[] bArray = null; // buffer for reading response from
    Socket socket = null; // socket to AXL server
    OutputStream out = null; // output stream to server
    InputStream in = null; // input stream from server

    X509TrustManager xtm = new MyTrustManager();
    TrustManager[] mytm = { xtm };
    SSLContext ctx = SSLContext.getInstance("SSL");
    ctx.init(null, mytm, null);
    SSLSocketFactory sslFact = (SSLSocketFactory) ctx.getSocketFactory();

    socket = (SSLSocket) sslFact.createSocket("192.168.1.100", Integer.parseInt("8443"));
    in = socket.getInputStream();
    // send the request to the server
    // read the response from the server
    StringBuffer sb = new StringBuffer(2048);
    bArray = new byte[2048];
    int ch = 0;
    int sum = 0;
    out = socket.getOutputStream();
    out.write(sAXLSOAPRequest.getBytes());

    while ((ch = in.read(bArray)) != -1) {
        sum += ch;
        sb.append(new String(bArray, 0, ch));
    }
    socket.close();
    // output the response to the standard output
    System.out.println(sb.toString());

and this is the MyTrustManager-Class:

public class MyTrustManager implements X509TrustManager {
MyTrustManager() {
    // create/load keystore
}

public void checkClientTrusted(X509Certificate chain[], String authType)
        throws CertificateException {
}

public void checkServerTrusted(X509Certificate chain[], String authType)
        throws CertificateException {
}

public X509Certificate[] getAcceptedIssuers() {
    return null;
}

}

Any help would be appreciated.

Thanks

edit: sorry i should have mentioned: youre right i can generate a proxy-class, but sadly its not working properly. cisco did a really bad job with that (not to mention the really bad documentation). the proxy class throws some xml-errors when parsing some responses. so i have to do it manually for that cases...

i'll worry about the certificate security later


Solution

  • Have you tried consuming the web service the "proper" way? Add a SOAP web service reference to your C# project in Visual Studio, gets the stubs etc? That's the easiest way of doing it from C#. You can just specify a https protocol in the URL when you add the reference.