mcscreatekey.cc

00001 // ----------------------------------------------------------------------^
00002 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
00003 // (mailto: <gcalderone@ifc.inaf.it>)
00004 // 
00005 // This file is part of MCS.
00006 // 
00007 // MCS is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 2 of the License, or
00010 // (at your option) any later version.
00011 // 
00012 // MCS is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU General Public License
00018 // along with MCS; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 // 
00021 // ----------------------------------------------------------------------$
00022 #include <mcs.hh>
00023 
00024 int main(int argc, char* argv[])
00025 {
00026 
00027 #ifdef HAVE_OPENSSL_SSL_H
00028   unsigned int ui;
00029   string sslcert = string(MCS_SSLCERT);
00030   string sslpriv = string(MCS_SSLPRIV), s;
00031 
00032   if ((! mcs::File_Dir_Exist(sslcert, ui))   ||
00033       (! mcs::File_Dir_Exist(sslpriv, ui))      ) {
00034 
00035     //Create certificate and private key
00036     int i;
00037     string s;
00038 
00039     cout << "Creating SSL private key and certificate for MCS server..." << endl << endl;
00040 
00041     cout << "The private key can be protected by a password, this is needed if the " << endl;
00042     cout << sslpriv << " file can be read by other users." << endl;
00043     cout << "If you choose to provide a password then you must enter that password " << endl;
00044     cout << "each time you execute the server." << endl << endl;
00045 
00046     cout << "Protect the private key with a password (y/n) ? [y]" << endl;
00047     cin >> s;
00048 
00049     s = ( ((s == "") || (s[0] == 'y') || (s[0] == 'Y'))   ?  " -des3 "  :  "");
00050 
00051     s = "openssl genrsa " + s + " -out " + string(MCS_SSLPRIV) + " 2048";
00052     cout << endl << endl << s << endl;
00053     i = system(s.c_str());
00054     if (i != 0) {
00055       cerr << "Error executing openssl" << endl;
00056       exit(i);
00057     }
00058 
00059     s = "openssl req -new -x509 -batch -key " + string(MCS_SSLPRIV) +
00060       " -out " + string(MCS_SSLCERT);
00061     cout << endl << endl << s << endl;
00062 
00063     i = system(s.c_str());
00064     if (i != 0) {
00065       cerr << "Error executing openssl" << endl;
00066       exit(i);
00067     }
00068   }
00069   else {
00070     cout << "Files for private key and certificate already exists:" << endl;
00071     cout << "    " << sslpriv << endl;
00072     cout << "    " << sslcert << endl;
00073   }
00074 #else //HAVE_OPENSSL_SSL_H
00075   cerr << "The SSL support has been disabled, try: ./configure --enable-openssl" << endl;
00076   exit(1);
00077 #endif //HAVE_OPENSSL_SSL_H
00078 
00079   //openssl genrsa -des3 -out privkey.pem 2048
00080   //openssl req -new -x509 -key privkey.pem -out cacert.pem -days 1095
00081 }

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha3
Documentation generated on Thu Mar 22 13:22:23 UTC 2012