> From: [email protected] On Behalf Of Mrigank
> Sent: Monday, 23 February, 2009 07:47

> ----- Original Message -----
> From: "Dave Thompson" <[email protected]>
> Sent: Sunday, February 22, 2009 5:34 AM

> > Actually, that's an idea. Is this on Windows with MS compiler
>
> Yeah. I am using VC++ 2005 Express Ed on Windows Vista. How could that
> affect the writing of keys using PEM_write_RSAPublicKey and
> _write_RSAPublicKey??
>
It could affect all file I/O done in openssl: PEM_read/write*,
BIO_s_file, etc.; and maybe some other I've forgotten/missed.
It's because Microsoft has 3 different C libraries,
6 counting debug, not entirely compatible with each other.

You need to either:

- compile your code, and link, using the same C library option
as was used to compile openssl (and link, if DLL version).
E.g. use /MT and CLIBMT, or /MD and MSVCRT . For VC++6
this is in Project / Settings / C/C++ / Code Generation .
(The link setting defaults to the compile setting; I think
there's a way to override it, but that will cause chaos.)
I assume it's at least similar in newer VC++.

- or use the newish (0.9.8-something) "applink" feature.
AIUI you either #include (the contents of) applink.c into
one of your (C) source files (so it gets compiled), or add
(a copy of) the file as a source file in your project ditto.
It should then get linked into your .exe and automatically
get called by openssl at runtime to fix the library references.
I haven't actually used this method since I had already
implemented the compile-time method above.

More below.

> I am giving the sequence of statements I used to create and write
> the keys:
> RSA * rsa_stuct = RSA_generate_key(2048, 65537, NULL, NULL);
> RSA_check_key(rsa_stuct);
>
> FILE *fp=fopen("RSAPublicKey.pem","w");
> PEM_write_RSAPublicKey(fp,rsa_stuct);
> fclose(fp);
>
> fp=fopen("RSAPrivateKey.pem","w");
> error_code = PEM_write_RSAPrivateKey(fp,rsa_stuct,NULL,NULL,0,NULL,NULL);
> fclose(fp);
>
> Is there anything else one needs to do other than that above in order to
> create and write the keys??
>
You should add error handling, but for the normal=success case
that should be enough if the library is OK. Except it won't encrypt
the privatekey, which you earlier said you wanted; for that you need to
use the cipher and passphrase or callback arguments to write-Private .

> When I try to execute the PEM_write_RSAPublicKey(fp,rsa_stuct)
> statement, I
> get this error:
> Microsoft Visual Studio C Runtime Library has detected a fatal error in
> "program_name.exe".
>
That certainly sounds like it can be a C library problem.

> Also, are there any other formats other than the .pem format for writing
> keys? I tried using all the key-write functions in the pem.h but
> all of them
> are failing with the same error.
>
There are several PEM formats; in particular there are (some, including RSA)
per-cipher versions like the ones you're using; there are also generic ones
(contains algid + per-cipher data) (for private keys this is PKCS8).

Similarly there are several DER (binary) formats, I believe corresponding to
all the available PEM ones or nearly so, but I haven't gone through the
list.

There is also, as I mentioned, the common usage of certificates,
specifically X.509, to convey public keys. In a sense you could
consider this a "format" for public keys. Again there are both
PEM format and DER format files for a cert (sometimes multiple certs).

> > or compatible e.g. mingw, and if so, did you either use the same
> > C library option for your code as for openssl library, or use
> > the (recentish) 'applink' feature to set the correct C library?
>
> Could you please give me a little more information about this??
> What exactly
> do I need to do? I had downloaded pre-compiled binaries of
> OpenSSL so I have
> no idea what configuration parameters it was built with..
>
> > If not, file I/O in openssl could malfunction in bizarre ways.
> >
Ah. If it's a DLL, it probably should have used /MD, and doing DUMPBIN
/IMPORTS on it should show references to MSVCRT. If so you use /MD too
(DLL in IDE) or if it shows MSVCRTD you use /MDd (DLL Debugging in IDE).
Otherwise (/MT[d] or /ML[d]) I don't know any easy way to tell.

If it's a static LIB, DUMPBIN /DIRECTIVES on it (or one or a few of the OBJs
from it) should show /DEFAULTLIB: with one of LIBC[D] LIBCMT[D] or MSVCRT[D]
.

Or just use the applink method, which is supposed to be easier,
although I can't personally vouch for it.



______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [email protected]
Automated List Manager                           [email protected]

Reply via email to