Fix incorrect free and leak.
[libdcp.git] / src / util.cc
index 0c63c3055e25b143e60593503cafdce3aef17fd9..18fa1b173d556b2c856b528a4e8afd576353629a 100644 (file)
@@ -307,20 +307,6 @@ libdcp::init ()
        if (xmlSecInit() < 0) {
                throw MiscError ("could not initialise xmlsec");
        }
-
-#ifdef XMLSEC_CRYPTO_DYNAMIC_LOADING
-       if (xmlSecCryptoDLLoadLibrary (BAD_CAST XMLSEC_CRYPTO) < 0) {
-               throw MiscError ("unable to load default xmlsec-crypto library");
-       }
-#endif
-       
-       if (xmlSecCryptoAppInit (0) < 0) {
-               throw MiscError ("could not initialise crypto library");
-       }
-       
-       if (xmlSecCryptoInit() < 0) {
-               throw MiscError ("could not initialise xmlsec-crypto");
-       }
 }
 
 void
@@ -335,10 +321,8 @@ libdcp::add_signature_value (xmlpp::Element* parent, CertificateChain const & ce
                
                {
                        xmlpp::Element* serial = data->add_child("X509IssuerSerial", ns);
-                       serial->add_child("X509IssuerName", ns)->add_child_text(
-                               Certificate::name_for_xml ((*i)->issuer())
-                               );
-                       serial->add_child("X509SerialNumber", ns)->add_child_text((*i)->serial());
+                       serial->add_child("X509IssuerName", ns)->add_child_text((*i)->issuer ());
+                       serial->add_child("X509SerialNumber", ns)->add_child_text((*i)->serial ());
                }
                
                data->add_child("X509Certificate", ns)->add_child_text((*i)->certificate());
@@ -348,19 +332,6 @@ libdcp::add_signature_value (xmlpp::Element* parent, CertificateChain const & ce
        if (!keys_manager) {
                throw MiscError ("could not create keys manager");
        }
-       if (xmlSecCryptoAppDefaultKeysMngrInit (keys_manager) < 0) {
-               throw MiscError ("could not initialise keys manager");
-       }
-       
-       xmlSecKeyPtr const key = xmlSecCryptoAppKeyLoad (signer_key.c_str(), xmlSecKeyDataFormatPem, 0, 0, 0);
-       if (key == 0) {
-               throw MiscError ("could not load signer key");
-               }
-       
-       if (xmlSecCryptoAppDefaultKeysMngrAdoptKey (keys_manager, key) < 0) {
-               xmlSecKeyDestroy (key);
-               throw MiscError ("could not use signer key");
-       }
        
        xmlSecDSigCtx signature_context;
        
@@ -387,15 +358,11 @@ libdcp::add_signer (xmlpp::Element* parent, CertificateChain const & certificate
                
                {
                        xmlpp::Element* serial_element = data->add_child("X509IssuerSerial", ns);
-                       serial_element->add_child("X509IssuerName", ns)->add_child_text (
-                               Certificate::name_for_xml (certificates.leaf()->issuer())
-                               );
-                       serial_element->add_child("X509SerialNumber", ns)->add_child_text (
-                               certificates.leaf()->serial()
-                               );
+                       serial_element->add_child("X509IssuerName", ns)->add_child_text (certificates.leaf()->issuer());
+                       serial_element->add_child("X509SerialNumber", ns)->add_child_text (certificates.leaf()->serial());
                }
                
-               data->add_child("X509SubjectName", ns)->add_child_text (Certificate::name_for_xml (certificates.leaf()->subject()));
+               data->add_child("X509SubjectName", ns)->add_child_text (certificates.leaf()->subject());
        }
 }
 
@@ -438,3 +405,30 @@ bool libdcp::operator!= (libdcp::Size const & a, libdcp::Size const & b)
        return !(a == b);
 }
 
+/** The base64 decode routine in KM_util.cpp gives different values to both
+ *  this and the command-line base64 for some inputs.  Not sure why.
+ */
+int
+libdcp::base64_decode (string const & in, unsigned char* out, int out_length)
+{
+       BIO* b64 = BIO_new (BIO_f_base64 ());
+
+       /* This means the input should have no newlines */
+       BIO_set_flags (b64, BIO_FLAGS_BASE64_NO_NL);
+
+       /* Copy our input string, removing newlines */
+       char in_buffer[in.size() + 1];
+       char* p = in_buffer;
+       for (size_t i = 0; i < in.size(); ++i) {
+               if (in[i] != '\n' && in[i] != '\r') {
+                       *p++ = in[i];
+               }
+       }
+               
+       BIO* bmem = BIO_new_mem_buf (in_buffer, p - in_buffer);
+       bmem = BIO_push (b64, bmem);
+       int const N = BIO_read (bmem, out, out_length);
+       BIO_free_all (bmem);
+
+       return N;
+}