Move *_pemify() function to PEM module
This commit is contained in:
parent
40ce79f1e6
commit
77e23fb0e0
8 changed files with 111 additions and 185 deletions
|
@ -363,6 +363,56 @@ int pem_read_buffer( pem_context *ctx, const char *header, const char *footer,
|
|||
return( 0 );
|
||||
}
|
||||
|
||||
int pem_write_buffer( const char *header, const char *footer,
|
||||
const unsigned char *der_data, size_t der_len,
|
||||
unsigned char *buf, size_t buf_len, size_t *olen )
|
||||
{
|
||||
int ret;
|
||||
unsigned char *encode_buf, *c, *p = buf;
|
||||
size_t len = 0, use_len = 0;
|
||||
size_t add_len = strlen( header ) + strlen( footer ) + ( use_len / 64 ) + 1;
|
||||
|
||||
base64_encode( NULL, &use_len, der_data, der_len );
|
||||
if( use_len + add_len > buf_len )
|
||||
{
|
||||
*olen = use_len + add_len;
|
||||
return( POLARSSL_ERR_BASE64_BUFFER_TOO_SMALL );
|
||||
}
|
||||
|
||||
if( ( encode_buf = polarssl_malloc( use_len ) ) == NULL )
|
||||
return( POLARSSL_ERR_PEM_MALLOC_FAILED );
|
||||
|
||||
if( ( ret = base64_encode( encode_buf, &use_len, der_data,
|
||||
der_len ) ) != 0 )
|
||||
{
|
||||
polarssl_free( encode_buf );
|
||||
return( ret );
|
||||
}
|
||||
|
||||
memcpy( p, header, strlen( header ) );
|
||||
p += strlen( header );
|
||||
c = encode_buf;
|
||||
|
||||
while( use_len )
|
||||
{
|
||||
len = ( use_len > 64 ) ? 64 : use_len;
|
||||
memcpy( p, c, len );
|
||||
use_len -= len;
|
||||
p += len;
|
||||
c += len;
|
||||
*p++ = '\n';
|
||||
}
|
||||
|
||||
memcpy( p, footer, strlen( footer ) );
|
||||
p += strlen( footer );
|
||||
|
||||
*p++ = '\0';
|
||||
*olen = p - buf;
|
||||
|
||||
polarssl_free( encode_buf );
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
void pem_free( pem_context *ctx )
|
||||
{
|
||||
if( ctx->buf )
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue