Bad Key

From: Softwaremaker (msdn_at_removethis.softwaremaker.net)
Date: 05/10/04


Date: Mon, 10 May 2004 22:14:04 +0800

Dear fellows,

OK, after working on WSE for some time, I found myself totally stumped by
this problem which seems easy to solve BUT I just dont have the solution for
it.

I am working on a demo with WSE using WS-Security. I use makecert.exe to
make a cert with this command

makecert -n "CN=SomeCompany" -pe -ss root -sr localmachine WSETest.cer.

This was then installed in the local computer under the Trusted Root
Directory. Then I publish a web service which uses this same cert to encrypt
the soap message. Then I developed a client app on the same local computer
to consume the secured Web Service using WSE1.0 sp1. The web service
generated the Web Service fine as I could view the output file...However,
the client throws an exception "Bad Key".

What do I do from here ?

WebService Code - This works fine
  Public Function GetXmlDocumentX509() As XmlDocument

    'Create a simple XML Document to return
    Dim myDOC As XmlDocument = New XmlDocument
    myDOC.InnerXml = "<EncryptedResponse>X509 Asymmetric Encryption : This
is sensitive data.</EncryptedResponse>"
    Dim myContext As SoapContext = HttpSoapContext.ResponseContext

    'Open and read the Personal certificate store for
    'the local machine account.
    Dim store As X509CertificateStore = _
    X509CertificateStore.LocalMachineStore(X509CertificateStore.RootStore)
    store.OpenRead()

    'Return store.FindCertificateBySubjectName("CN=Softwaremaker.NET Pte
Ltd").Count

    Dim cert As X509Certificate = store.FindCertificateBySubjectName _
    ("CN=SomeCompany")(0)

    'Make sure that we have a certificate that can be used for encryption.
    If cert Is Nothing Or cert.SupportsDataEncryption = False Then
      Throw New ApplicationException("Service is not able to encrypt the
response")
      Return Nothing
    Else
      'Use the valid certificate to create a security token.
      Dim tok As X509SecurityToken = New X509SecurityToken(cert)

      'Encrypt the message body using this security token.
      'WSE will use this token to encrypt the message body.
      'WSE generates a KeyInfo element used to request the
      'certificate at the client used to decrypt the message.
      Dim myEncData As EncryptedData = New EncryptedData(tok)

      'Add the encrypted data element to the SoapContext of the
      'response message.
      myContext.Security.Elements.Add(myEncData)
    End If

    Return myDOC
  End Function

Web.Config File
<x509 storeLocation="LocalMachine" verifyTrust="false" allowTestRoot="False"
/>

ClientApp
    Try
      Dim a As localhost.WSE1WSSecurityWse = New localhost.WSE1WSSecurityWse
      MessageBox.Show(a.GetXmlDocumentX509().InnerText)
    Catch ex As Exception
      MessageBox.Show(ex.Message)
    End Try
  End Sub

App.Config File
<x509 storeLocation="LocalMachine" verifyTrust="false"
allowTestRoot="false"/>

Should the app.config file be LocalMachine cos I installed the cert via
makecert in the local machine root store. I have given proper permissions to
both private keystores BUT yet still got the "Bad Key" exception.

Will appreciate any help. Thanks so much.

Regards.



Relevant Pages

  • Signing with WSE 2.0, no cert on the server side, still works
    ... I am now deploying the web service to a machine other than my dev box ... even before I put the client/calling public key cert on ... receiver doesn't need the cert - can just find the public key there. ... The cert I have signed with on the client side is one created with the ...
    (microsoft.public.dotnet.framework.webservices.enhancements)
  • Re: L2TP Connection Issue
    ... where should the Root Cert show up? ... Local computer or Personal? ... > The client needs the root certificate that the server's certificate chains ... The server needs the root certificate that the client's certificate ...
    (microsoft.public.isa.vpn)
  • Re: [WSE 3.0] I need some pointers for using security
    ... After some certificate exporting/importing and fiddling with the ... If I can use only one cert, is there any way to distribute and install ... Do a search for 'Web Service Security Patterns and Practices' on MS website. ...
    (microsoft.public.dotnet.framework.webservices)
  • RE: Bad Key
    ... Is this really a root certificate authority certificate? ... I would put these certs in the Personal store, rather than the Trusted Root store... ... Then I publish a web service which uses this same cert to encrypt ...
    (microsoft.public.dotnet.framework.webservices.enhancements)
  • Re: HttpWebRequest and SSL client certs in the Local Computer store
    ... I know the cert is on the machine properly b/c when I go ... > client that does not use WSE 2.0 but uses HttpWebRequest directly. ... > store of the Local Computer. ...
    (microsoft.public.dotnet.security)