1
1
/*
2
- Copyright (c) 2015, 2016 , Oracle and/or its affiliates. All rights reserved.
2
+ Copyright (c) 2015, 2017 , Oracle and/or its affiliates. All rights reserved.
3
3
4
4
This program is free software; you can redistribute it and/or modify
5
5
it under the terms of the GNU General Public License as published by
@@ -71,6 +71,12 @@ enum certs
71
71
OPENSSL_RND
72
72
};
73
73
74
+ enum extfiles
75
+ {
76
+ CAV3_EXT=0 ,
77
+ CERTV3_EXT
78
+ };
79
+
74
80
Sql_string_t cert_files[] =
75
81
{
76
82
create_string (" ca.pem" ),
@@ -87,6 +93,12 @@ Sql_string_t cert_files[] =
87
93
create_string (" .rnd" )
88
94
};
89
95
96
+ Sql_string_t ext_files[] =
97
+ {
98
+ create_string (" cav3.ext" ),
99
+ create_string (" certv3.ext" )
100
+ };
101
+
90
102
#define MAX_PATH_LEN (FN_REFLEN - strlen(FN_DIRSEP) \
91
103
- cert_files[SERVER_CERT].length() - 1 )
92
104
/*
@@ -314,6 +326,49 @@ class X509_key
314
326
stringstream m_subj_prefix;
315
327
};
316
328
329
+ class X509v3_ext_writer
330
+ {
331
+ public:
332
+ X509v3_ext_writer ()
333
+ {
334
+ m_cav3_ext_options << " basicConstraints=CA:TRUE" << std::endl;
335
+
336
+ m_certv3_ext_options << " basicConstraints=CA:FALSE" << std::endl;
337
+ }
338
+ ~X509v3_ext_writer () {};
339
+
340
+ bool operator ()(const Sql_string_t &cav3_ext_file,
341
+ const Sql_string_t &certv3_ext_file)
342
+ {
343
+ if (!cav3_ext_file.length () ||
344
+ !certv3_ext_file.length ())
345
+ return true ;
346
+
347
+ std::ofstream ext_file;
348
+
349
+ ext_file.open (cav3_ext_file.c_str (),
350
+ std::ios::out|std::ios::trunc );
351
+ if (!ext_file.is_open ())
352
+ return true ;
353
+ ext_file << m_cav3_ext_options.str ();
354
+ ext_file.close ();
355
+
356
+ ext_file.open (certv3_ext_file.c_str (),
357
+ std::ios::out|std::ios::trunc );
358
+ if (!ext_file.is_open ())
359
+ {
360
+ remove_file (cav3_ext_file.c_str (), false );
361
+ return true ;
362
+ }
363
+ ext_file << m_certv3_ext_options.str ();
364
+ ext_file.close ();
365
+
366
+ return false ;
367
+ }
368
+ private:
369
+ stringstream m_cav3_ext_options;
370
+ stringstream m_certv3_ext_options;
371
+ };
317
372
318
373
class X509_cert
319
374
{
@@ -328,15 +383,17 @@ class X509_cert
328
383
uint32_t serial,
329
384
bool self_signed,
330
385
const Sql_string_t &sign_key_file,
331
- const Sql_string_t &sign_cert_file)
386
+ const Sql_string_t &sign_cert_file,
387
+ const Sql_string_t &ext_file)
332
388
{
333
389
stringstream command;
334
390
command << " openssl x509 -sha256 -days " << m_validity;
335
- command << " -set_serial " << serial << " -req -in " << req_file << " " ;
391
+ command << " -extfile " << ext_file;
392
+ command << " -set_serial " << serial << " -req -in " << req_file;
336
393
if (self_signed)
337
- command << " -signkey " << sign_key_file;
394
+ command << " -signkey " << sign_key_file;
338
395
else
339
- command << " -CA " << sign_cert_file << " -CAkey " << sign_key_file;
396
+ command << " -CA " << sign_cert_file << " -CAkey " << sign_key_file;
340
397
command << " -out " << cert_file;
341
398
342
399
return command.str ();
@@ -551,6 +608,7 @@ int main(int argc, char *argv[])
551
608
Sql_string_t empty_string (" " );
552
609
X509_key x509_key (suffix_string);
553
610
X509_cert x509_cert;
611
+ X509v3_ext_writer x509v3_ext_writer;
554
612
555
613
/* Delete existing files if any */
556
614
remove_file (cert_files[CA_REQ], false );
@@ -560,14 +618,23 @@ int main(int argc, char *argv[])
560
618
remove_file (cert_files[CLIENT_KEY], false );
561
619
remove_file (cert_files[OPENSSL_RND], false );
562
620
621
+ /* Remove existing v3 extension files */
622
+ remove_file (ext_files[CAV3_EXT], false );
623
+ remove_file (ext_files[CERTV3_EXT], false );
624
+
625
+ /* Create v3 extension files */
626
+ if (x509v3_ext_writer (ext_files[CAV3_EXT], ext_files[CERTV3_EXT]))
627
+ goto end;
628
+
563
629
/* Generate CA Key and Certificate */
564
630
if ((ret_val= execute_command (x509_key (" _Auto_Generated_CA_Certificate" ,
565
631
cert_files[CA_KEY], cert_files[CA_REQ]),
566
632
" Error generating ca_key.pem and ca_req.pem" )))
567
633
goto end;
568
634
569
635
if ((ret_val= execute_command (x509_cert (cert_files[CA_REQ], cert_files[CA_CERT], 1 ,
570
- true , cert_files[CA_KEY], empty_string),
636
+ true , cert_files[CA_KEY], empty_string,
637
+ ext_files[CAV3_EXT]),
571
638
" Error generating ca_cert.pem" )))
572
639
goto end;
573
640
@@ -578,7 +645,8 @@ int main(int argc, char *argv[])
578
645
goto end;
579
646
580
647
if ((ret_val= execute_command (x509_cert (cert_files[SERVER_REQ], cert_files[SERVER_CERT], 2 ,
581
- false , cert_files[CA_KEY], cert_files[CA_CERT]),
648
+ false , cert_files[CA_KEY], cert_files[CA_CERT],
649
+ ext_files[CERTV3_EXT]),
582
650
" Error generating server_cert.pem" )))
583
651
goto end;
584
652
@@ -589,7 +657,8 @@ int main(int argc, char *argv[])
589
657
goto end;
590
658
591
659
if ((ret_val= execute_command (x509_cert (cert_files[CLIENT_REQ], cert_files[CLIENT_CERT], 3 ,
592
- false , cert_files[CA_KEY], cert_files[CA_CERT]),
660
+ false , cert_files[CA_KEY], cert_files[CA_CERT],
661
+ ext_files[CERTV3_EXT]),
593
662
" Error generating client_cert.pem" )))
594
663
goto end;
595
664
@@ -622,6 +691,11 @@ int main(int argc, char *argv[])
622
691
goto end;
623
692
624
693
remove_file (cert_files[OPENSSL_RND], false );
694
+
695
+ /* Remove existing v3 extension files */
696
+ remove_file (ext_files[CAV3_EXT], false );
697
+ remove_file (ext_files[CERTV3_EXT], false );
698
+
625
699
}
626
700
627
701
/*
0 commit comments