blob: 8ce3e712fe26ac4707173f0b7b919a9923fad0fa [file] [log] [blame]
Paul Bakker6d620502012-02-16 14:09:13 +00001BEGIN_HEADER
2#include <polarssl/x509write.h>
3#include <polarssl/x509.h>
4#include <polarssl/pem.h>
Paul Bakkerc70b9822013-04-07 22:00:46 +02005#include <polarssl/oid.h>
Paul Bakker6d620502012-02-16 14:09:13 +00006END_HEADER
7
8BEGIN_DEPENDENCIES
9depends_on:POLARSSL_X509_WRITE_C:POLARSSL_BIGNUM_C
10END_DEPENDENCIES
11
12BEGIN_CASE
13x509_cert_req_check:key_file:md_type:cert_req_check_file
14{
15 rsa_context rsa;
16 pem_context pem;
17 x509_req_name req_name, *cur;
18 unsigned char *c;
19 unsigned char buf[4000];
20 unsigned char check_buf[4000];
21 int ret;
22 size_t olen = 2000;
23 FILE *f;
24
25 cur = &req_name;
26
27 memset( cur, 0, sizeof(x509_req_name) );
Paul Bakkerc70b9822013-04-07 22:00:46 +020028 strcpy( cur->oid, OID_AT_CN );
Paul Bakker6d620502012-02-16 14:09:13 +000029 strcpy( cur->name, "PolarSSL Server 1" );
30 cur->next = malloc( sizeof(x509_req_name) );
31 cur = cur->next;
32
33 memset( cur, 0, sizeof(x509_req_name) );
Paul Bakkerc70b9822013-04-07 22:00:46 +020034 strcpy( cur->oid, OID_AT_ORGANIZATION );
Paul Bakker6d620502012-02-16 14:09:13 +000035 strcpy( cur->name, "PolarSSL" );
36 cur->next = malloc( sizeof(x509_req_name) );
37 cur = cur->next;
38
39 memset( cur, 0, sizeof(x509_req_name) );
Paul Bakkerc70b9822013-04-07 22:00:46 +020040 strcpy( cur->oid, OID_AT_COUNTRY );
Paul Bakker6d620502012-02-16 14:09:13 +000041 strcpy( cur->name, "NL" );
42
43 memset( &rsa, 0, sizeof(rsa_context) );
44 ret = x509parse_keyfile( &rsa, {key_file}, NULL );
45 TEST_ASSERT( ret == 0 );
46 if( ret != 0 )
47 return 0;
48
49 ret = x509_write_cert_req( buf, 4000, &rsa, &req_name, {md_type} );
50 TEST_ASSERT( ret >= 0 );
51
52 c = buf + 3999 - ret;
53
54 f = fopen( {cert_req_check_file}, "r" );
55 TEST_ASSERT( f != NULL );
56 fread( check_buf, 1, 4000, f );
57 fclose( f );
58
59 pem_init( &pem );
60 pem_read_buffer( &pem, "-----BEGIN CERTIFICATE REQUEST-----", "-----END CERTIFICATE REQUEST-----", check_buf, NULL, 0, &olen );
61
62 TEST_ASSERT( memcmp( c, pem.buf, pem.buflen ) == 0 );
63 TEST_ASSERT( pem.buflen == (size_t) ret );
Paul Bakker58ef6ec2013-01-03 11:33:48 +010064
65 while( ( cur = req_name.next ) != NULL )
66 {
67 req_name.next = cur->next;
68 free( cur );
69 }
70
71 rsa_free( &rsa );
72 pem_free( &pem );
Paul Bakker6d620502012-02-16 14:09:13 +000073}
74END_CASE