1
0
mirror of https://github.com/Mbed-TLS/mbedtls.git synced 2025-07-29 11:41:15 +03:00

Move PEM conversion of DER data to x509write module

This commit is contained in:
Paul Bakker
2013-08-26 16:54:13 +02:00
parent 57be6e22cf
commit 135f1e9c70
3 changed files with 79 additions and 26 deletions

View File

@ -62,41 +62,23 @@ struct options
int write_certificate_request( x509_csr *req, char *output_file )
{
int ret;
FILE *f;
unsigned char output_buf[4096];
unsigned char base_buf[4096];
unsigned char *c;
int ret;
size_t len = 0, olen = 4096;
size_t len = 0;
memset(output_buf, 0, 4096);
ret = x509write_csr_der( req, output_buf, 4096 );
if( ret < 0 )
memset( output_buf, 0, 4096 );
if( ( ret = x509write_csr_pem( req, output_buf, 4096 ) ) < 0 )
return( ret );
len = ret;
c = output_buf + 4095 - len;
if( ( ret = base64_encode( base_buf, &olen, c, len ) ) != 0 )
return( ret );
c = base_buf;
len = strlen( (char *) output_buf );
if( ( f = fopen( output_file, "w" ) ) == NULL )
return( -1 );
fprintf(f, "-----BEGIN CERTIFICATE REQUEST-----\n");
while (olen)
{
int use_len = olen;
if (use_len > 64) use_len = 64;
fwrite( c, 1, use_len, f );
olen -= use_len;
c += use_len;
fprintf(f, "\n");
}
fprintf(f, "-----END CERTIFICATE REQUEST-----\n");
if( fwrite( output_buf, 1, len, f ) != len )
return( -1 );
fclose(f);
return( 0 );