Back to index

texmacs  1.0.7.15
openssl.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : openssl.cpp
00004 * DESCRIPTION: Functions for cryptography
00005 * COPYRIGHT  : (C) 2007  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "openssl.hpp"
00013 #include "file.hpp"
00014 
00015 /******************************************************************************
00016 * RSA encryption and decryption
00017 ******************************************************************************/
00018 
00019 void
00020 rsa_initialize () {
00021   url dir = url ("$TEXMACS_HOME_PATH") * "system/crypto";
00022   url priv= dir * "texmacs.private";
00023   url pub = dir * "texmacs.public";
00024   if (!exists (dir)) mkdir (dir);
00025   if (!exists (priv))
00026     system ("openssl genrsa -out " * as_string (priv) * " 2048 2> /dev/null");
00027   if (!exists (pub))
00028     system ("openssl rsa -in " * as_string (priv) *
00029            " -pubout -out " * as_string (pub) * " 2> /dev/null");
00030 }
00031 
00032 string
00033 rsa_my_private_key () {
00034   rsa_initialize ();
00035   url dir = url ("$TEXMACS_HOME_PATH") * "system/crypto";
00036   url priv= dir * "texmacs.private";
00037   string private_key;
00038   load_string (priv, private_key, true);
00039   return private_key;
00040 }
00041 
00042 string
00043 rsa_my_public_key () {
00044   rsa_initialize ();
00045   url dir = url ("$TEXMACS_HOME_PATH") * "system/crypto";
00046   url priv= dir * "texmacs.public";
00047   string public_key;
00048   load_string (priv, public_key, true);
00049   return public_key;
00050 }
00051 
00052 string
00053 rsa_encode (string msg, string key) {
00054   url _msg= url_temp ();
00055   save_string (_msg, msg);
00056   url _key= url_temp ();
00057   save_string (_key, key);
00058   string r= eval_system ("openssl rsautl -in " * as_string (_msg) *
00059                       " -pubin -inkey " * as_string (_key) *
00060                       " -encrypt");
00061   remove (_msg);
00062   remove (_key);
00063   return r;
00064 }
00065 
00066 string
00067 rsa_decode (string msg, string key) {
00068   url _msg= url_temp ();
00069   save_string (_msg, msg);
00070   url _key= url_temp ();
00071   save_string (_key, key);
00072   string r= eval_system ("openssl rsautl -in " * as_string (_msg) *
00073                       " -inkey " * as_string (_key) *
00074                       " -decrypt");
00075   remove (_msg);
00076   remove (_key);
00077   return r;
00078 }
00079 
00080 /******************************************************************************
00081 * AES encryption and decryption
00082 ******************************************************************************/
00083 
00084 string
00085 secret_generate (int len) {
00086   //return eval_system ("openssl rand -base64 " * as_string (len));
00087   return eval_system ("openssl rand " * as_string (len));
00088 }
00089 
00090 string
00091 secret_encode (string msg, string key) {
00092   url _msg= url_temp ();
00093   save_string (_msg, msg);
00094   url _key= url_temp ();
00095   save_string (_key, key);
00096   string r= eval_system ("openssl aes-256-cbc -nosalt -in " *
00097                       as_string (_msg) * " -pass file:" * as_string (_key));
00098   remove (_msg);
00099   remove (_key);
00100   return r;
00101 }
00102 
00103 string
00104 secret_decode (string msg, string key) {
00105   url _msg= url_temp ();
00106   save_string (_msg, msg);
00107   url _key= url_temp ();
00108   save_string (_key, key);
00109   string r= eval_system ("openssl aes-256-cbc -nosalt -d -in " *
00110                       as_string (_msg) * " -pass file:" * as_string (_key));
00111   remove (_msg);
00112   remove (_key);
00113   return r;
00114 }
00115 
00116 string
00117 secret_hash (string msg) {
00118   return secret_encode ("TeXmacs worgelt BlauwBilGorgels", msg);
00119 }