2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_HEADER */
|
2012-02-16 15:09:13 +01:00
|
|
|
#include <polarssl/x509write.h>
|
|
|
|
#include <polarssl/x509.h>
|
|
|
|
#include <polarssl/pem.h>
|
2013-04-07 22:00:46 +02:00
|
|
|
#include <polarssl/oid.h>
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_HEADER */
|
2012-02-16 15:09:13 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:POLARSSL_X509_WRITE_C:POLARSSL_BIGNUM_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
2012-02-16 15:09:13 +01:00
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
/* BEGIN_CASE */
|
2013-08-25 11:01:31 +02:00
|
|
|
void x509_csr_check( char *key_file, int md_type,
|
2013-08-20 11:48:36 +02:00
|
|
|
char *cert_req_check_file )
|
2012-02-16 15:09:13 +01:00
|
|
|
{
|
|
|
|
rsa_context rsa;
|
|
|
|
pem_context pem;
|
2013-08-25 11:01:31 +02:00
|
|
|
x509_csr req;
|
2012-02-16 15:09:13 +01:00
|
|
|
unsigned char *c;
|
|
|
|
unsigned char buf[4000];
|
|
|
|
unsigned char check_buf[4000];
|
|
|
|
int ret;
|
|
|
|
size_t olen = 2000;
|
|
|
|
FILE *f;
|
2013-08-25 10:33:27 +02:00
|
|
|
char *subject_name = "C=NL,O=PolarSSL,CN=PolarSSL Server 1";
|
2012-02-16 15:09:13 +01:00
|
|
|
|
|
|
|
memset( &rsa, 0, sizeof(rsa_context) );
|
2013-08-20 11:48:36 +02:00
|
|
|
ret = x509parse_keyfile_rsa( &rsa, key_file, NULL );
|
2012-02-16 15:09:13 +01:00
|
|
|
TEST_ASSERT( ret == 0 );
|
|
|
|
if( ret != 0 )
|
2013-08-20 11:48:36 +02:00
|
|
|
return;
|
2012-02-16 15:09:13 +01:00
|
|
|
|
2013-08-25 11:01:31 +02:00
|
|
|
x509write_csr_init( &req );
|
|
|
|
x509write_csr_set_md_alg( &req, md_type );
|
|
|
|
x509write_csr_set_rsa_key( &req, &rsa );
|
|
|
|
TEST_ASSERT( x509write_csr_set_subject_name( &req, subject_name ) == 0 );
|
2013-08-25 10:18:25 +02:00
|
|
|
|
2013-08-25 11:01:31 +02:00
|
|
|
ret = x509write_csr_der( &req, buf, 4000 );
|
2012-02-16 15:09:13 +01:00
|
|
|
TEST_ASSERT( ret >= 0 );
|
|
|
|
|
|
|
|
c = buf + 3999 - ret;
|
|
|
|
|
2013-08-20 11:48:36 +02:00
|
|
|
f = fopen( cert_req_check_file, "r" );
|
2012-02-16 15:09:13 +01:00
|
|
|
TEST_ASSERT( f != NULL );
|
|
|
|
fread( check_buf, 1, 4000, f );
|
|
|
|
fclose( f );
|
|
|
|
|
|
|
|
pem_init( &pem );
|
|
|
|
pem_read_buffer( &pem, "-----BEGIN CERTIFICATE REQUEST-----", "-----END CERTIFICATE REQUEST-----", check_buf, NULL, 0, &olen );
|
|
|
|
|
|
|
|
TEST_ASSERT( memcmp( c, pem.buf, pem.buflen ) == 0 );
|
|
|
|
TEST_ASSERT( pem.buflen == (size_t) ret );
|
2013-01-03 11:33:48 +01:00
|
|
|
|
2013-08-25 11:01:31 +02:00
|
|
|
x509write_csr_free( &req );
|
2013-01-03 11:33:48 +01:00
|
|
|
rsa_free( &rsa );
|
|
|
|
pem_free( &pem );
|
2012-02-16 15:09:13 +01:00
|
|
|
}
|
2013-08-20 11:48:36 +02:00
|
|
|
/* END_CASE */
|