Export.cpp

This example shows how to export CAs, keys, certificates and CRLs from the repository

00001 #include <blocxx/Logger.hpp>
00002 #include <blocxx/AppenderLogger.hpp>
00003 #include <blocxx/CerrLogger.hpp>
00004 #include <blocxx/CerrAppender.hpp>
00005 #include <blocxx/String.hpp>
00006 #include <blocxx/PerlRegEx.hpp>
00007 #include <limal/Logger.hpp>
00008 #include <limal/PathInfo.hpp>
00009 #include <limal/PathUtils.hpp>
00010 #include <limal/Exception.hpp>
00011 #include <limal/ByteBuffer.hpp>
00012 #include <limal/ca-mgm/CA.hpp>
00013 #include <limal/ca-mgm/LocalManagement.hpp>
00014 
00015 #include <iostream>
00016 #include <fstream>
00017 #include <unistd.h>
00018 
00019 using namespace blocxx;
00020 using namespace limal;
00021 using namespace limal::ca_mgm;
00022 using namespace std;
00023 
00024 int main()
00025 {
00026     try
00027     {
00028         blocxx::StringArray cat;
00029         cat.push_back("FATAL");
00030         cat.push_back("ERROR");
00031         cat.push_back("INFO");
00032         //cat.push_back("DEBUG");
00033 
00034         // Logging
00035         LoggerRef l = limal::Logger::createCerrLogger(
00036                                                       "Export",
00037                                                       LogAppender::ALL_COMPONENTS,
00038                                                       cat,
00039                                                       "%-5p %c - %m"
00040                                                   );
00041         limal::Logger::setDefaultLogger(l);
00042         
00043         CA ca("MyRootCA", "system");
00044 
00045         
00046         // export CA certificate in PEM format ----------------------------
00047         
00048         ByteBuffer ba = ca.exportCACert(E_PEM);
00049 
00050         
00051         // export CA certificate in DER format ----------------------------
00052         
00053         ba = ca.exportCACert(E_DER);
00054         
00055 
00056         // export CA private key in PEM format (decrypted) ----------------
00057 
00058         ba = ca.exportCAKeyAsPEM("");
00059         
00060         LocalManagement::writeFile(ba, "./CAKey.key");
00061 
00062 
00063         // export CA private key in PEM format (encrypted) ----------------
00064 
00065         ba = ca.exportCAKeyAsPEM("tralla");
00066         
00067         LocalManagement::writeFile(ba, "./CAKey2.key");
00068 
00069 
00070         // export CA private key in DER format (decrypted) ----------------
00071 
00072         ba = ca.exportCAKeyAsDER();
00073         
00074         LocalManagement::writeFile(ba, "./CAKeyDER.key");
00075 
00076 
00077         // export CA in PKCS12 format (without CA chain) ------------------
00078 
00079         ba = ca.exportCAasPKCS12("tralla", false);
00080         
00081         LocalManagement::writeFile(ba, "./CA.p12");
00082 
00083 
00084         // export CA in PKCS12 format (with CA chain) ---------------------
00085 
00086         ba = ca.exportCAasPKCS12("tralla", true);
00087         
00088         LocalManagement::writeFile(ba, "./TestRepos3/testCAChain.p12");
00089 
00090 
00091         
00092         CA ca2("MyUserCA", "system");
00093 
00094         // export certificate in PEM format -------------------------------
00095 
00096         ba = ca2.exportCertificate("01:9528e1d8783f83b662fca6085a8c1467-1111161258",
00097                                    E_PEM);
00098         
00099 
00100         // export certificate in DER format -------------------------------
00101 
00102         ba = ca2.exportCertificate("01:9528e1d8783f83b662fca6085a8c1467-1111161258",
00103                                    E_DER);
00104         
00105 
00106         // export private key in PEM format (decrypted) -------------------
00107 
00108         ba = ca2.exportCertificateKeyAsPEM("01:9528e1d8783f83b662fca6085a8c1467-1111161258",
00109                                            "system", "");
00110         
00111         LocalManagement::writeFile(ba, "./Key.key");
00112 
00113 
00114         // export private key in PEM format (encrypted) -------------------
00115 
00116         ba = ca2.exportCertificateKeyAsPEM("01:9528e1d8783f83b662fca6085a8c1467-1111161258",
00117                                            "system", "tralla");
00118         
00119         LocalManagement::writeFile(ba, "./Key2.key");
00120 
00121 
00122         // export private key in DER format (decrypted) ----------------
00123 
00124         ba = ca2.exportCertificateKeyAsDER("01:9528e1d8783f83b662fca6085a8c1467-1111161258",
00125                                            "system");
00126         
00127         LocalManagement::writeFile(ba, "./TestRepos3/KeyDER.key");
00128 
00129 
00130         // export certificate in PKCS12 format (without CA chain) ------
00131 
00132         ba = ca2.exportCertificateAsPKCS12("01:9528e1d8783f83b662fca6085a8c1467-1111161258",
00133                                            "system", "tralla", false);
00134         
00135         LocalManagement::writeFile(ba, "./Cert.p12");
00136 
00137         
00138         // export certificate in PKCS12 format (with CA chain) ---------
00139 
00140         ba = ca2.exportCertificateAsPKCS12("01:9528e1d8783f83b662fca6085a8c1467-1111161258",
00141                                            "system", "tralla", true);
00142         
00143         LocalManagement::writeFile(ba, "./TestRepos3/CertChain.p12");
00144 
00145         
00146         // export CRL in PEM format ------------------------------------
00147         
00148         ba = ca2.exportCRL(E_PEM);
00149         
00150 
00151         // export CRL in DER format ------------------------------------
00152         
00153         ba = ca2.exportCRL(E_DER);
00154         
00155     }
00156     catch(Exception& e)
00157     {
00158         cerr << e << endl;
00159     }
00160     
00161     return 0;
00162 }
00163 
00164 /* vim: set ts=8 sts=8 sw=8 ai noet: */

Generated on Mon Nov 27 22:50:13 2006 for limal-ca-mgm by  doxygen 1.5.0