Re: How do I get RSA public key from X509Certificate ?

From: Kishore Raghavan (kishorerc@hotmail.com)
Date: 07/26/02


From: "Kishore Raghavan" <kishorerc@hotmail.com>
Date: Thu, 25 Jul 2002 22:51:32 -0700


Hi Allen,

I can read the certificates from the server.. Here is my code that parses
the input and creates a chain of X509Certificate objects... I hope this is
useful to you..

//Code
private X509Certificate[] chain;
internal CertificateChain(BinaryReader din)
{
    int totalBytesInCerts = SSLUtility.ReadInt24(din);
    ArrayList tempChain = new ArrayList(3); //Three is enough for us.
    while(totalBytesInCerts > 0)
    {
        int bytesInCert = SSLUtility.ReadInt24(din);
        byte[] certBuf = new byte[bytesInCert];
        int offset = 0;
        totalBytesInCerts -= 3 + bytesInCert;
        int bytesRead;
        for(; offset != bytesInCert; offset += bytesRead)
        {
            bytesRead = din.Read(certBuf, offset, bytesInCert - offset);
            if(bytesRead < 0)
            {
                throw new IOException("Could not read the certificates");
            }
        }
        tempChain.Add( new X509Certificate( certBuf ) );
    }
    chain = new X509Certificate[tempChain.Count];
    for(int i = 0; i < chain.Length; i++)
    {
        chain[i] = (X509Certificate) tempChain[i];
    }
}
//End Code
Kishore

"Allen Owen" <alleno@intenda.co.za> wrote in message
news:e$3faT6MCHA.2488@tkmsftngp11...
> Kishore,
>
> Yup, that's pretty much what I'm trying to do. Sorry if I didn't explain
it
> particularly well.
>
> Regards
> Allen
>
>
> "Kishore" <kishorerc@hotmail.com> wrote in message
> news:OJwaeZ2MCHA.2428@tkmsftngp12...
> > Allen,
> >
> > I found out that the X509Certificate.GetPublicKey() actually returns the
> > Modulus and Exponent in a single byte[]. The modulus starts at position
5.
> > We will have to get the Modulus and Exponent from this byte[] and set it
> to
> > the RSAParameters object. Now the problem is how do we know how long the
> > Modulus will be ? Is there a spec which defines this ?
> >
> > Now, for your question:
> > I did not understand your question properly. But let me tell you what I
am
> > doing.
> >
> > I am playing with the SSL 3.0 specification and trying to write an SSL
> > client library. The server sends all the certificates according to the
> > specification. I parse the input and create X509Certificate with the
> > content.
> >
> > Thanks,
> > Kishore
> >
> >
> > "Allen Owen" <alleno@intenda.co.za> wrote in message
> > news:OZHWpZuMCHA.2244@tkmsftngp11...
> > > Hi Kishore,
> > >
> > > I use {1,0,1} for the exponent, but I can't be 100% sure that it's the
> > > correct way to go about it.
> > >
> > > A quick question for you though. Have you tried to retrieve the
matching
> > > server certificate for the X509 cert?
> > > I'd imagine that you're getting your a client cert via
> > > Request.ClientCertificate and I need to retreive the matching server
> cert
> > > and thus far can't see a way to do it :( .
> > >
> > > Cheers
> > > Allen
> > >
> > >
> > > "Kishore" <kishorerc@hotmail.com> wrote in message
> > > news:ePCLqfsMCHA.488@tkmsftngp10...
> > > > Hi Everyone,
> > > > I have a X509Certificate, I want to get a RSA object using the
public
> > key
> > > > from the cert. Is this code correct ?
> > > >
> > > > public RSA GetKey(X509Certificate cert)
> > > > {
> > > > RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
> > > > RSAParameters keyInfo = new RSAParameters();
> > > > keyInfo.Exponent = key;
> > > > keyInfo.Modulus = key;
> > > > rSA.ImportParameters(keyInfo);
> > > > return rsa;
> > > > }
> > > >
> > > > Or should I use {1,0,1} for the Exponent ?
> > > > Thanks,
> > > > Kishore
> > > >
> > > >
> > >
> > >
> >
> >
>
>