blob: 8155b5a8c4e37064f8eadce13d1dfd2031c0f3d7 [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>
5END_HEADER
6
7BEGIN_DEPENDENCIES
8depends_on:POLARSSL_X509_WRITE_C:POLARSSL_BIGNUM_C
9END_DEPENDENCIES
10
11BEGIN_CASE
12x509_cert_req_check:key_file:md_type:cert_req_check_file
13{
14 rsa_context rsa;
15 pem_context pem;
16 x509_req_name req_name, *cur;
17 unsigned char *c;
18 unsigned char buf[4000];
19 unsigned char check_buf[4000];
20 int ret;
Manuel Pégourié-Gonnarda1e6ba62015-08-10 17:15:43 +020021 size_t olen = 2000, r;
Paul Bakker6d620502012-02-16 14:09:13 +000022 FILE *f;
23
24 cur = &req_name;
25
26 memset( cur, 0, sizeof(x509_req_name) );
27 strcpy( cur->oid, OID_CN );
28 strcpy( cur->name, "PolarSSL Server 1" );
29 cur->next = malloc( sizeof(x509_req_name) );
30 cur = cur->next;
31
32 memset( cur, 0, sizeof(x509_req_name) );
33 strcpy( cur->oid, OID_ORGANIZATION );
34 strcpy( cur->name, "PolarSSL" );
35 cur->next = malloc( sizeof(x509_req_name) );
36 cur = cur->next;
37
38 memset( cur, 0, sizeof(x509_req_name) );
39 strcpy( cur->oid, OID_COUNTRY );
40 strcpy( cur->name, "NL" );
41
42 memset( &rsa, 0, sizeof(rsa_context) );
43 ret = x509parse_keyfile( &rsa, {key_file}, NULL );
44 TEST_ASSERT( ret == 0 );
45 if( ret != 0 )
46 return 0;
47
48 ret = x509_write_cert_req( buf, 4000, &rsa, &req_name, {md_type} );
49 TEST_ASSERT( ret >= 0 );
50
51 c = buf + 3999 - ret;
52
53 f = fopen( {cert_req_check_file}, "r" );
54 TEST_ASSERT( f != NULL );
Manuel Pégourié-Gonnarda1e6ba62015-08-10 17:15:43 +020055 r = fread( check_buf, 1, 4000, f );
Paul Bakker6d620502012-02-16 14:09:13 +000056 fclose( f );
Manuel Pégourié-Gonnarda1e6ba62015-08-10 17:15:43 +020057 TEST_ASSERT( r != 0 );
Paul Bakker6d620502012-02-16 14:09:13 +000058
59 pem_init( &pem );
Paul Bakker1d073c52014-07-08 20:15:51 +020060 pem_read_buffer( &pem, (char *) "-----BEGIN CERTIFICATE REQUEST-----", (char *) "-----END CERTIFICATE REQUEST-----", check_buf, NULL, 0, &olen );
Paul Bakker6d620502012-02-16 14:09:13 +000061
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