You can't usually use printf("%s") on binary data because
it will stop at the first NULL.  I've enclosed a simple
working sample.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <sys/stat.h>
#include <unistd.h>


int
main(int argc, char **argv)
{

    FILE *in;
    void *buf;
    struct stat st;

    if (argc > 1) {
        in = fopen(argv[1],"r");
    } else {
        in = fopen("file something","r");
    }

    if (!in) {
        fprintf(stderr, "Could not open in!\n");
        exit(1);
    }
    fstat(fileno(in),&st);
    buf = malloc(st.st_size);
    fread(buf,1,st.st_size,in);
    fclose(in);

    printf("Content-type: application/x-x509-user-cert\n"
           "Content-length: %ld\n\n", st.st_size);

    fwrite(buf, 1, st.st_size,stdout);

    if (buf) free(buf);

    return 0;
}

> -----Original Message-----
> From: alohaz [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, February 29, 2000 3:17 AM
> To: [EMAIL PROTECTED]
> Subject: Re: how to download a certificate to browser?
> 
> 
> Yes! the certificate signed by CA is in format of DER and 
> stored in cert.result.
> 
> now that section is as following to return the certificate to browser:
>  
> CERT=fopen("/usr/local/ssl/certs/cert.result");
> printf(Content-Type: application/x-x509-user-cert\n\n");
> while ((ch=getc(CERT))!= EOF)
>      result[i++] = ch;
> fclose(CERT);
> printf ("%s",result);
> 
> 
> the size of cert.result is 832, and i is 833. So I think I 
> have had the content of cert.result 
> is fully stored in result. however, printf ("%s",result) only 
> output the first line of cert.result. How I can overcome this problem?
> 
> Is there any C  program to do the work of downloading a 
> certificate to browser?
> 
> 
> Thanks a lot for the answer and help from you.
> 
> ----- Original Message ----- 
> 发件人: Massimiliano Pala <[EMAIL PROTECTED]>
> 收件人: <[EMAIL PROTECTED]>
> 发送时间: 2000年2月27日 10:30
> 主题: Re: how to download a certificate to browser?
> 
> 
> > alohaz wrote:
> > > 
> > > Thanks a lot to Robert,Mike and Massimiliano for your help.
> > > 
> > > I got a some suggestion form Hirsch as  following:
> > > 
> > > I believe the problem is that the join can take multiple 
> lines (if the cert is over multiple lines, and make them into 
> one string), but the C++ code is including the newlines.
> > > I am not familiar with getline, I think fgets includes 
> the newline.
> > > So my suggestion is check that you are not incorporating 
> newlines in your concatenation.
> > > 
> > > So I modified my program as:
> > > 
> > > char result[2000],ch;
> > > int i=0;
> > > CERT=fopen("/usr/local/ssl/certs/cert.result");
> > > printf(Content-Type: application/x-x509-user-cert\n\n");
> > > while( ((ch=getc(CERT))!= EOF) && ((ch=getc(CERT))!= '\n') )
> > >   result[i++] = ch;
> > > fclose(CERT);
> > > printf ("%s",result);
> > > 
> > > I can only use
> > >   print "Content-Type: application/x-x509-user-cert\n\n";
> > > in perl to have the certificate installed in browser.
> > > 
> > > print "Content-Length: $len\n\n";
> > > is not necessary.
> > > 
> > > But I still can not get certificate at browser although 
> it has been signed by CA. What's more,
> > > strlen(result) is not equal to i. For example, i is 834 
> but strlen(result) is 30. I do not know the reason.
> > > 
> > > Can you offer any help? Thanks!
> > 
> > Is the certificate you are sending in DER format ??? Please 
> also do not strip
> > any \n char, you have to send the certificate in DER 
> without modifying it... :-D
> > 
> > Hope this will fix your problem.
> > 
> > C'you,
> > 
> > Massimiliano Pala ([EMAIL PROTECTED])
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [EMAIL PROTECTED]
> Automated List Manager                           [EMAIL PROTECTED]
> 
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [EMAIL PROTECTED]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to