Пример #1
0
std::string OutputFileHandler::MakeFoldersAndReturnFullPath(const std::string& rDirectory) const
{
    fs::path output_root(GetChasteTestOutputDirectory());
    fs::path rel_path(rDirectory);

    if (!rel_path.empty() && (*(--rel_path.end())) == ".")
    {
        // rDirectory has a trailing slash, which gives an unhelpful last component
        rel_path.remove_leaf();
    }

    // Make master wait (because other processes may be checking whether a directory exists)
    PetscTools::Barrier("OutputFileHandler::MakeFoldersAndReturnFullPathBeforeCreation");
    // Are we the master process? Only the master should make any new directories
    if (PetscTools::AmMaster())
    {
        try
        {
            // If necessary make the ChasteTestOutputDirectory - don't make it deleteable by Chaste
            fs::create_directories(output_root); // Note that this is a no-op if the folder exists already

            // Now make all the sub-folders requested one-by-one and add the .chaste_deletable_folder file to them
            fs::path next_folder(output_root);
            for (fs::path::iterator path_iter = rel_path.begin(); path_iter != rel_path.end(); ++path_iter)
            {
                next_folder /= *path_iter;
                bool created_dir = fs::create_directory(next_folder);
                if (created_dir)
                {
                    // Add the Chaste signature file
                    fs::ofstream sig_file(next_folder / SIG_FILE_NAME);
                    sig_file.close();
                }
            }
        }
        // LCOV_EXCL_START
        catch (const fs::filesystem_error& e)
        {
            TERMINATE("Error making test output folder: " << e.what());
        }
        // LCOV_EXCL_STOP
    }

    // Wait for master to finish before going on to use the directory.
    PetscTools::Barrier("OutputFileHandler::MakeFoldersAndReturnFullPath");

    std::string path_with_slash = (output_root / rel_path).string();
    AddTrailingSlash(path_with_slash);
    return path_with_slash;
}
Пример #2
0
bool validate_enc_cell( const wxString &sig_file_name, const wxString &cell_file_name)
{
    bool ret_val = false;
    
    //  Open the signature file, and extract the first R/S strings
    wxArrayString sig_array;
    wxArrayString sig_save_array;
    if( wxFileName::FileExists(sig_file_name) ){
        wxTextFile sig_file( sig_file_name );
        if( sig_file.Open() ){
            
            wxString line = sig_file.GetFirstLine();
            
            while( !sig_file.Eof() ){
                sig_array.Add(line);
                line = sig_file.GetNextLine();
            }
            
            // Remove and save the first two R/S signature lines 
             for(unsigned int i=0 ; i < sig_array.Count() ; i++){
                wxString line = sig_array[i];
                
                if( line.Upper().Find(_T("PART R")) != wxNOT_FOUND ){
                    sig_save_array.Add(line);
                    sig_save_array.Add(sig_array[i+1]);
                    sig_array.RemoveAt(i, 2);
                    break;
                }
            }                
            
            for(unsigned int i=0 ; i < sig_array.Count() ; i++){
                wxString line = sig_array[i];
                
                if( line.Upper().Find(_T("PART S")) != wxNOT_FOUND ){
                    sig_save_array.Add(line);
                    sig_save_array.Add(sig_array[i+1]);
                    sig_array.RemoveAt(i, 2);
                    break;
                }
            }                
            
            // Remove the next two (R/S) lines, the Data Server Signature
            for(unsigned int i=0 ; i < sig_array.Count() ; i++){
                wxString line = sig_array[i];
                
                if( line.Upper().Find(_T("PART R")) != wxNOT_FOUND ){
                    sig_array.RemoveAt(i, 2);
                    break;
                }
            }                
            
            for(unsigned int i=0 ; i < sig_array.Count() ; i++){
                wxString line = sig_array[i];
                
                if( line.Upper().Find(_T("PART S")) != wxNOT_FOUND ){
                    sig_array.RemoveAt(i, 2);
                    break;
                }
            }                
            
            //  Make a public key from the leftover part of the line array
        }   // File Open
    }
            
    pub_key public_key;
    public_key.ReadKey( sig_array );

    if( !public_key.m_OK )
        return false;

    //  Hash the ENC
    SHA1Context sha1;
    uint8_t sha1sum[SHA1HashSize];
    SHA1Reset(&sha1);
    
#define S63BUFSIZ 512 * 1024    
    wxFileInputStream fin( cell_file_name );
    if( fin.IsOk() ) {
        unsigned char buf[S63BUFSIZ];
        for(;;){
            if( fin.Eof())
                break;
            fin.Read(buf, S63BUFSIZ);
            size_t n_read = fin.LastRead();
 
            SHA1Input(&sha1, (uint8_t *)buf, n_read );
        }
    }
        
    SHA1Result(&sha1, sha1sum);
        

    mp_int hash, r, s;
    mp_init(&hash);
    mp_init( &r );
    mp_init( &s );
    mp_read_unsigned_bin(&hash, sha1sum, sizeof(sha1sum));

    //  Prepare the signature
    for(unsigned int i=0 ; i < sig_save_array.Count() ; i++){
        wxString line = sig_save_array[i];
        if( line.Upper().Find(_T("PART R")) != wxNOT_FOUND ){
            if( (i+1) < sig_save_array.Count() ){
                wxString sig_line = sig_save_array[i+1];
                sig_line.Replace(_T(" "), _T("") );
        wxCharBuffer lbuf = sig_line.ToUTF8();
        mp_read_radix(&r, lbuf.data(), 16);
            }
        }
        
        else if( line.Upper().Find(_T("PART S")) != wxNOT_FOUND ){
            if( (i+1) < sig_save_array.Count() ){
                wxString sig_line = sig_save_array[i+1];
                sig_line.Replace(_T(" "), _T("") );
        wxCharBuffer lbuf = sig_line.ToUTF8();
        mp_read_radix(&s, lbuf.data(), 16);
            }
        }
    }
    
    //  Verify the blob
    int val = _dsa_verify_hash(&r, &s, &hash, &(public_key.m_g), &(public_key.m_p), &(public_key.m_q), &(public_key.m_y) );            
    ret_val = (val == 1);
    
    return ret_val;   
}
Пример #3
0
bool validate_enc_signature( const wxString &sig_file_name, const wxString &key_file_name)
{
    bool ret_val = false;
    
    // Read the key_file_name, and create an instance of pub_key
    pub_key public_key;
    if( wxFileName::FileExists(key_file_name) ){
        wxTextFile key_file( key_file_name );
        if( key_file.Open() ){
            
            wxArrayString key_array;
            wxString line = key_file.GetFirstLine();
            
            while( !key_file.Eof() ){
                key_array.Add(line);
                line = key_file.GetNextLine();
            }
            
            public_key.ReadKey( key_array );
        }
    }

    if( !public_key.m_OK )
        return false;
    
    //  Validate the ENC signature file according to Spec 5.4.2.7 and 10.6.2

    //  Read the file into a string array
    if( wxFileName::FileExists(sig_file_name) ){
        wxTextFile sig_file( sig_file_name );
        if( sig_file.Open() ){
            
            wxArrayString sig_array;
            wxString line = sig_file.GetFirstLine();
            
            while( !sig_file.Eof() ){
                sig_array.Add(line);
                line = sig_file.GetNextLine();
            }

            
            // Remove the first two (R/S) lines, the Data Server Signature
            for(unsigned int i=0 ; i < sig_array.Count() ; i++){
                wxString line = sig_array[i];
                
                if( line.Upper().Find(_T("PART R")) != wxNOT_FOUND ){
                    sig_array.RemoveAt(i, 2);
                    break;
                }
            }                
                
            for(unsigned int i=0 ; i < sig_array.Count() ; i++){
                wxString line = sig_array[i];
                
                if( line.Upper().Find(_T("PART S")) != wxNOT_FOUND ){
                    sig_array.RemoveAt(i, 2);
                    break;
                }
            }                
                
            // Remove and save the next two R/S signature lines 
            wxArrayString sig_save_array;
            for(unsigned int i=0 ; i < sig_array.Count() ; i++){
                wxString line = sig_array[i];
                
                if( line.Upper().Find(_T("PART R")) != wxNOT_FOUND ){
                    sig_save_array.Add(line);
                    sig_save_array.Add(sig_array[i+1]);
                    sig_array.RemoveAt(i, 2);
                    break;
                }
            }                
            
            for(unsigned int i=0 ; i < sig_array.Count() ; i++){
                wxString line = sig_array[i];
                
                if( line.Upper().Find(_T("PART S")) != wxNOT_FOUND ){
                    sig_save_array.Add(line);
                    sig_save_array.Add(sig_array[i+1]);
                    sig_array.RemoveAt(i, 2);
                    break;
                }
            }                
            
            //  Make one long string of the remainder of the file, to treat as a blob
            wxString pub_key_blob;
            for(unsigned int i=0 ; i < sig_array.Count() ; i++){
                wxString line = sig_array[i];
                pub_key_blob += line;
                pub_key_blob += _T("\r\n");
            }                
            
            wxCharBuffer blob_buf = pub_key_blob.ToUTF8();

            //  Hash the blob
            SHA1Context sha1;
            uint8_t sha1sum[SHA1HashSize];
            SHA1Reset(&sha1);
            
            SHA1Input(&sha1, (uint8_t *)blob_buf.data(), strlen( blob_buf.data()) );
            SHA1Result(&sha1, sha1sum);
            
            mp_int hash, r, s;
            mp_init(&hash);
            mp_init( &r );
            mp_init( &s );
            mp_read_unsigned_bin(&hash, sha1sum, sizeof(sha1sum));
            
            
            //  Prepare the signature
            for(unsigned int i=0 ; i < sig_save_array.Count() ; i++){
                wxString line = sig_save_array[i];
                if( line.Upper().Find(_T("PART R")) != wxNOT_FOUND ){
                    if( (i+1) < sig_save_array.Count() ){
                        wxString sig_line = sig_save_array[i+1];
                        sig_line.Replace(_T(" "), _T("") );
                        wxCharBuffer lbuf = sig_line.ToUTF8();
                        mp_read_radix(&r, lbuf.data(), 16);
                    }
                }
                
                else if( line.Upper().Find(_T("PART S")) != wxNOT_FOUND ){
                    if( (i+1) < sig_save_array.Count() ){
                        wxString sig_line = sig_save_array[i+1];
                        sig_line.Replace(_T(" "), _T("") );
                        wxCharBuffer lbuf = sig_line.ToUTF8();
                        mp_read_radix(&s, lbuf.data(), 16);
                    }
                }
            }
                
            //  Verify the blob
            int val = _dsa_verify_hash(&r, &s, &hash, &(public_key.m_g), &(public_key.m_p), &(public_key.m_q), &(public_key.m_y) );            
            ret_val = (val == 1);
            
        }
    }
    
    return ret_val;
}
Пример #4
0
bool check_enc_signature_format( const wxString &sig_file_name )
{
    //  check the format of the ENC signature file according to Spec 5.4.2.7
    
    //  Read the file into a string array
    if( wxFileName::FileExists(sig_file_name) ){
        wxTextFile sig_file( sig_file_name );
        if( sig_file.Open() ){
            
            wxArrayString sig_array;
            wxString line = sig_file.GetFirstLine();
            
            while( !sig_file.Eof() ){
                sig_array.Add(line);
                line = sig_file.GetNextLine();
            }
            
            
            // Check the lines for length
            for(unsigned int i=0 ; i < sig_array.Count() ; i++){
                wxString line = sig_array[i];
                
                if( line.Upper().Find(_T("PART R")) != wxNOT_FOUND ){
                    if(i+1 < sig_array.Count()){
                        if(sig_array[i+1].Length() != 50)
                            return false;
                    }
                    else
                        return false;
                }
                
                if( line.Upper().Find(_T("PART S")) != wxNOT_FOUND ){
                    if(i+1 < sig_array.Count()){
                        if(sig_array[i+1].Length() != 50)
                            return false;
                    }
                    else
                        return false;
                }
                
                if( line.Upper().Find(_T("BIG P")) != wxNOT_FOUND ){
                    if(i+1 < sig_array.Count()){
                        if(sig_array[i+1].Length() != 160)
                            return false;
                    }
                    else
                        return false;
                }
                
                if( line.Upper().Find(_T("BIG Q")) != wxNOT_FOUND ){
                    if(i+1 < sig_array.Count()){
                        if(sig_array[i+1].Length() != 50)
                            return false;
                    }
                    else
                        return false;
                }
                
                if( line.Upper().Find(_T("BIG G")) != wxNOT_FOUND ){
                    if(i+1 < sig_array.Count()){
                        if(sig_array[i+1].Length() != 160)
                            return false;
                    }
                    else
                        return false;
                }
                
                if( line.Upper().Find(_T("BIG Y")) != wxNOT_FOUND ){
                    if(i+1 < sig_array.Count()){
                        if(sig_array[i+1].Length() != 160)
                            return false;
                    }
                    else
                        return false;
                }
                
            }
            
            return true;
        }
    }
    
    return false;
}