bool verify( context& d, buffer_view_t signature, buffer_view_t hvalue, hash_t halgo) { if (type_of(d) != pk_t::rsa && !can_do(d, pk_t::ecdsa)) throw exceptions::support_error{}; check_crypt_size_of(d, hvalue); int ret = mbedtls_pk_verify( &d.pk_, to_native(halgo), hvalue.data(), hvalue.size(), signature.data(), signature.size()); // TODO: check when to report other errors switch (ret) { case 0: return true; case MBEDTLS_ERR_PK_BAD_INPUT_DATA: case MBEDTLS_ERR_PK_TYPE_MISMATCH: throw exception{ret, "failed to verify the signature"}; break; default: break; } return false; }
// Ruby.primitive! :fixnum_sub Integer* sub(STATE, Fixnum* other) { native_int r = to_native() - other->to_native(); if(r > FIXNUM_MAX || r < FIXNUM_MIN) { return Bignum::from(state, r); } else { return Fixnum::from(r); } }
bool can_do(const context& d, pk_t ptype) { int ret = mbedtls_pk_can_do(&d.pk_, to_native(ptype)); // refinement due to build options if (type_of(d) == pk_t::eckey && ptype == pk_t::ecdsa) { if (!supports(pk_t::ecdsa)) ret = 0; } return ret == 1; }
void generate_ec_key(context& d, curve_t ctype) { #if defined(MBEDTLS_ECP_C) // resets previous states pk::reset_as(d, pk_t::eckey); mbedcrypto_c_call( mbedtls_ecp_gen_key, to_native(ctype), mbedtls_pk_ec(d.pk_), rnd_generator::maker, &d.rnd_); // set the key type d.key_is_private_ = true; #else // MBEDTLS_ECP_C throw exceptions::ecp_missed{}; #endif // MBEDTLS_ECP_C }
buffer_t sign(context& d, buffer_view_t hvalue, hash_t halgo) { if (type_of(d) != pk_t::rsa && !can_do(d, pk_t::ecdsa)) throw exceptions::support_error{}; check_crypt_size_of(d, hvalue); size_t olen = 32 + max_crypt_size(d); buffer_t output(olen, '\0'); mbedcrypto_c_call( mbedtls_pk_sign, &d.pk_, to_native(halgo), hvalue.data(), hvalue.size(), to_ptr(output), &olen, rnd_generator::maker, &d.rnd_); output.resize(olen); return output; }
static bool create_directory(const char * path) { BaseString native(path); to_native(native); BaseString tmp(path); Vector<BaseString> list; if (tmp.split(list, "/") == 0) { g_logger.error("Failed to create directory: %s", tmp.c_str()); return false; } BaseString cwd = IF_WIN("","/"); for (unsigned i = 0; i < list.size(); i++) { cwd.append(list[i].c_str()); cwd.append("/"); NdbDir::create(cwd.c_str(), NdbDir::u_rwx() | NdbDir::g_r() | NdbDir::g_x(), true); } struct stat sbuf; if (lstat(native.c_str(), &sbuf) != 0 || !S_ISDIR(sbuf.st_mode)) { g_logger.error("Failed to create directory: %s (%s)", native.c_str(), cwd.c_str()); return false; } return true; }
// Ruby.primitive! :fixnum_lt Object* lt(STATE, Fixnum* other) { return to_native() < other->to_native() ? Qtrue : Qfalse; }
bool setup_files(atrt_config& config, int setup, int sshx) { /** * 0 = validate * 1 = setup * 2 = setup+clean */ BaseString mycnf; mycnf.assfmt("%s/my.cnf", g_basedir); if (!create_directory(g_basedir)) { return false; } if (mycnf != g_my_cnf) { struct stat sbuf; int ret = lstat(to_native(mycnf).c_str(), &sbuf); if (ret == 0) { if (unlink(to_native(mycnf).c_str()) != 0) { g_logger.error("Failed to remove %s", mycnf.c_str()); return false; } } BaseString cp; cp.assfmt("cp %s %s", g_my_cnf, mycnf.c_str()); to_fwd_slashes(cp); if (sh(cp.c_str()) != 0) { g_logger.error("Failed to '%s'", cp.c_str()); return false; } } if (setup == 2 || config.m_generated) { /** * Do mysql_install_db */ for (unsigned i = 0; i < config.m_clusters.size(); i++) { atrt_cluster& cluster = *config.m_clusters[i]; for (unsigned j = 0; j<cluster.m_processes.size(); j++) { atrt_process& proc = *cluster.m_processes[j]; if (proc.m_type == atrt_process::AP_MYSQLD) #ifndef _WIN32 { const char * val; require(proc.m_options.m_loaded.get("--datadir=", &val)); BaseString tmp; tmp.assfmt("%s --defaults-file=%s/my.cnf --basedir=%s --datadir=%s > %s/mysql_install_db.log 2>&1", g_mysql_install_db_bin_path, g_basedir, g_prefix, val, proc.m_proc.m_cwd.c_str()); to_fwd_slashes(tmp); if (sh(tmp.c_str()) != 0) { g_logger.error("Failed to mysql_install_db for %s, cmd: '%s'", proc.m_proc.m_cwd.c_str(), tmp.c_str()); } else { g_logger.info("mysql_install_db for %s", proc.m_proc.m_cwd.c_str()); } } #else { g_logger.info("not running mysql_install_db for %s", proc.m_proc.m_cwd.c_str()); } #endif } } } FILE * out = NULL; bool retval = true; if (config.m_generated == false) { g_logger.info("Nothing configured..."); } else { out = fopen(mycnf.c_str(), "a+"); if (out == 0) { g_logger.error("Failed to open %s for append", mycnf.c_str()); return false; } time_t now = time(0); fprintf(out, "#\n# Generated by atrt\n"); fprintf(out, "# %s\n", ctime(&now)); } for (unsigned i = 0; i < config.m_clusters.size(); i++) { atrt_cluster& cluster = *config.m_clusters[i]; if (out) { Properties::Iterator it(&cluster.m_options.m_generated); printfile(out, cluster.m_options.m_generated, "[mysql_cluster%s]", cluster.m_name.c_str()); } for (unsigned j = 0; j<cluster.m_processes.size(); j++) { atrt_process& proc = *cluster.m_processes[j]; if (out) { switch(proc.m_type){ case atrt_process::AP_NDB_MGMD: printfile(out, proc.m_options.m_generated, "[cluster_config.ndb_mgmd.%d%s]", proc.m_index, proc.m_cluster->m_name.c_str()); break; case atrt_process::AP_NDBD: printfile(out, proc.m_options.m_generated, "[cluster_config.ndbd.%d%s]", proc.m_index, proc.m_cluster->m_name.c_str()); break; case atrt_process::AP_MYSQLD: printfile(out, proc.m_options.m_generated, "[mysqld.%d%s]", proc.m_index, proc.m_cluster->m_name.c_str()); break; case atrt_process::AP_NDB_API: break; case atrt_process::AP_CLIENT: printfile(out, proc.m_options.m_generated, "[client.%d%s]", proc.m_index, proc.m_cluster->m_name.c_str()); break; case atrt_process::AP_ALL: case atrt_process::AP_CLUSTER: abort(); } } /** * Create env.sh */ BaseString tmp; tmp.assfmt("%s/env.sh", proc.m_proc.m_cwd.c_str()); to_native(tmp); char **env = BaseString::argify(0, proc.m_proc.m_env.c_str()); if (env[0] || proc.m_proc.m_path.length()) { Vector<BaseString> keys; FILE *fenv = fopen(tmp.c_str(), "w+"); if (fenv == 0) { g_logger.error("Failed to open %s for writing", tmp.c_str()); retval = false; goto end; } for (size_t k = 0; env[k]; k++) { tmp = env[k]; ssize_t pos = tmp.indexOf('='); require(pos > 0); env[k][pos] = 0; fprintf(fenv, "%s=\"%s\"\n", env[k], env[k]+pos+1); keys.push_back(env[k]); free(env[k]); } if (proc.m_proc.m_path.length()) { fprintf(fenv, "CMD=\"%s", proc.m_proc.m_path.c_str()); if (proc.m_proc.m_args.length()) { fprintf(fenv, " %s", proc.m_proc.m_args.c_str()); } fprintf(fenv, "\"\nexport CMD\n"); } fprintf(fenv, "PATH="); for (int i = 0; g_search_path[i] != 0; i++) { fprintf(fenv, "%s/%s:", g_prefix, g_search_path[i]); } fprintf(fenv, "$PATH\n"); keys.push_back("PATH"); { /** * In 5.5...binaries aren't compiled with rpath * So we need an explicit LD_LIBRARY_PATH * * Use path from libmysqlclient.so */ char * dir = dirname(g_libmysqlclient_so_path); fprintf(fenv, "LD_LIBRARY_PATH=%s:$LD_LIBRARY_PATH\n", dir); free(dir); keys.push_back("LD_LIBRARY_PATH"); } for (unsigned k = 0; k<keys.size(); k++) fprintf(fenv, "export %s\n", keys[k].c_str()); fflush(fenv); fclose(fenv); } free(env); { tmp.assfmt("%s/ssh-login.sh", proc.m_proc.m_cwd.c_str()); FILE* fenv = fopen(tmp.c_str(), "w+"); if (fenv == 0) { g_logger.error("Failed to open %s for writing", tmp.c_str()); retval = false; goto end; } fprintf(fenv, "#!/bin/sh\n"); fprintf(fenv, "cd %s\n", proc.m_proc.m_cwd.c_str()); fprintf(fenv, "[ -f /etc/profile ] && . /etc/profile\n"); fprintf(fenv, ". ./env.sh\n"); fprintf(fenv, "ulimit -Sc unlimited\n"); fprintf(fenv, "bash -i"); fflush(fenv); fclose(fenv); } } } end: if (out) { fclose(out); } return retval; }
void Msg_reader_core::read_metadata_block(VSILFILE* fin) { _open_success = true; unsigned int i; CPL_IGNORE_RET_VAL(VSIFReadL(&_main_header, sizeof(_main_header), 1, fin)); CPL_IGNORE_RET_VAL(VSIFReadL(&_sec_header, sizeof(_sec_header), 1, fin)); #ifdef DEBUG // print out all the fields in the header PH_DATA* hd = (PH_DATA*)&_main_header; for (int i=0; i < 6; i++) { to_string(*hd); printf("[%02d] %s %s", i, hd->name, hd->value);/*ok*/ hd++; } PH_DATA_ID* hdi = (PH_DATA_ID*)&_main_header.dataSetIdentification; for (i=0; i < 5; i++) { printf("%s %s %s", hdi->name, hdi->size, hdi->address);/*ok*/ hdi++; } hd = (PH_DATA*)(&_main_header.totalFileSize); for (int i=0; i < 19; i++) { to_string(*hd); printf("[%02d] %s %s", i, hd->name, hd->value);/*ok*/ hd++; } #endif // DEBUG // extract data & header positions for (i=0; i < 5; i++) { PH_DATA_ID* hdi = (PH_DATA_ID*)&_main_header.dataSetIdentification[i]; if (STARTS_WITH(hdi->name, "15Header")) { sscanf(hdi->size, "%u", &_f_header_size); sscanf(hdi->address, "%u", &_f_header_offset); } else if (STARTS_WITH(hdi->name, "15Data")) { sscanf(hdi->size, "%u", &_f_data_size); sscanf(hdi->address, "%u", &_f_data_offset); } } #ifdef DEBUG printf("Data: %u %u\n", _f_data_offset, _f_data_size);/*ok*/ printf("Header: %u %u\n", _f_header_offset, _f_header_size);/*ok*/ #endif // DEBUG unsigned int lines; sscanf(_sec_header.northLineSelectedRectangle.value, "%u", &_lines); sscanf(_sec_header.southLineSelectedRectangle.value, "%u", &lines); _line_start = lines; _lines -= lines - 1; unsigned int cols; sscanf(_sec_header.westColumnSelectedRectangle.value, "%u", &_columns); sscanf(_sec_header.eastColumnSelectedRectangle.value, "%u", &cols); _col_start = cols; _columns -= cols - 1; #ifdef DEBUG printf("lines = %u, cols = %u\n", _lines, _columns);/*ok*/ #endif // DEBUG int records_per_line = 0; for (i=0; i < MSG_NUM_CHANNELS; i++) { if (_sec_header.selectedBandIds.value[i] == 'X') { _bands[i] = 1; records_per_line += (i == (MSG_NUM_CHANNELS-1)) ? 3 : 1; } else { _bands[i] = 0; } } #ifdef DEBUG printf("reading a total of %d records per line\n", records_per_line);/*ok*/ #endif // DEBUG // extract time fields, assume that SNIT is the correct field: sscanf(_main_header.snit.value + 0, "%04u", &_year); sscanf(_main_header.snit.value + 4, "%02u", &_month); sscanf(_main_header.snit.value + 6, "%02u", &_day); sscanf(_main_header.snit.value + 8, "%02u", &_hour); sscanf(_main_header.snit.value + 10, "%02u", &_minute); // read radiometric block RADIOMETRIC_PROCESSING_RECORD rad; off_t offset = RADIOMETRICPROCESSING_RECORD_OFFSET + _f_header_offset + sizeof(GP_PK_HEADER) + sizeof(GP_PK_SH1) + 1; CPL_IGNORE_RET_VAL(VSIFSeekL(fin, offset, SEEK_SET)); CPL_IGNORE_RET_VAL(VSIFReadL(&rad, sizeof(RADIOMETRIC_PROCESSING_RECORD), 1, fin)); to_native(rad); memcpy((void*)_calibration, (void*)&rad.level1_5ImageCalibration,sizeof(_calibration)); #ifdef DEBUG for (unsigned int i=0; i < MSG_NUM_CHANNELS; i++) { if (_calibration[i].cal_slope < 0 || _calibration[i].cal_slope > 0.4) { printf("Warning: calibration slope (%f) out of nominal range. MSG reader probably broken\n", _calibration[i].cal_slope);/*ok*/ } if (_calibration[i].cal_offset > 0 || _calibration[i].cal_offset < -20) { printf("Warning: calibration offset (%f) out of nominal range. MSG reader probably broken\n",/*ok*/ _calibration[i].cal_offset); } } #endif // read image description block IMAGE_DESCRIPTION_RECORD idr; offset = RADIOMETRICPROCESSING_RECORD_OFFSET - IMAGEDESCRIPTION_RECORD_LENGTH + _f_header_offset + sizeof(GP_PK_HEADER) + sizeof(GP_PK_SH1) + 1; CPL_IGNORE_RET_VAL(VSIFSeekL(fin, offset, SEEK_SET)); CPL_IGNORE_RET_VAL(VSIFReadL(&idr, sizeof(IMAGE_DESCRIPTION_RECORD), 1, fin)); to_native(idr); _line_dir_step = idr.referencegrid_visir.lineDirGridStep; _col_dir_step = idr.referencegrid_visir.columnDirGridStep; // Rather convoluted, but this code is required to compute the real data block sizes // It does this by reading in the first line of every band, to get to the packet size field GP_PK_HEADER gp_header; GP_PK_SH1 sub_header; SUB_VISIRLINE visir_line; CPL_IGNORE_RET_VAL(VSIFSeekL(fin, _f_data_offset, SEEK_SET)); _hrv_packet_size = 0; _interline_spacing = 0; visir_line.channelId = 0; int scanned_bands[MSG_NUM_CHANNELS]; int band_count = 0; for (i=0; i < MSG_NUM_CHANNELS; i++) { scanned_bands[i] = _bands[i]; band_count += _bands[i]; } do { if( VSIFReadL(&gp_header, sizeof(GP_PK_HEADER), 1, fin) != 1 || VSIFReadL(&sub_header, sizeof(GP_PK_SH1), 1, fin) != 1 || VSIFReadL(&visir_line, sizeof(SUB_VISIRLINE), 1, fin) != 1 ) { _open_success = false; break; } to_native(visir_line); to_native(gp_header); // skip over the actual line data CPL_IGNORE_RET_VAL(VSIFSeekL(fin, gp_header.packetLength - (sizeof(GP_PK_SH1) + sizeof(SUB_VISIRLINE) - 1), SEEK_CUR )); if (visir_line.channelId == 0 || visir_line.channelId > MSG_NUM_CHANNELS) { _open_success = false; break; } if (scanned_bands[visir_line.channelId - 1]) { scanned_bands[visir_line.channelId - 1] = 0; band_count--; if (visir_line.channelId != 12) { // not the HRV channel _visir_bytes_per_line = gp_header.packetLength - (unsigned int)(sizeof(GP_PK_SH1) + sizeof(SUB_VISIRLINE) - 1); _visir_packet_size = gp_header.packetLength + (unsigned int)sizeof(GP_PK_HEADER) + 1; _interline_spacing += _visir_packet_size; } else { _hrv_bytes_per_line = gp_header.packetLength - (unsigned int)(sizeof(GP_PK_SH1) + sizeof(SUB_VISIRLINE) - 1); _hrv_packet_size = gp_header.packetLength + (unsigned int)sizeof(GP_PK_HEADER) + 1; _interline_spacing += 3*_hrv_packet_size; CPL_IGNORE_RET_VAL(VSIFSeekL(fin, 2*gp_header.packetLength, SEEK_CUR )); } } } while (band_count > 0); }