static void
read_autodetect_file(const char * filename)
{
	int binary = 0, c;
	FILE *file;

	file = fopen (filename, "r");
	if (file == NULL) {
		fprintf (stderr, "Failed to open %s: %s\n",
			 filename, strerror (errno));
		exit (1);
	}

	while ((c = fgetc(file)) != EOF) {
		/* totally lazy binary detector */
		if (c < 10) {
			binary = 1;
			break;
		}
	}

	fclose(file);

	if (binary == 1)
		read_bin_file(filename);
	else
		read_data_file(filename);

}
Beispiel #2
0
bool long_class::read_bin_file_class(char* filename)
{
	long_class res;
	res.me = read_bin_file(filename);
	if (res.me.digits == NULL)
	{
		return false;
	}
	else
	{
		*this = res;
		return true;
	}
}
Beispiel #3
0
static void load_pub_key()
{
	unsigned int file_size;

	if(!get_file_size(ESA_PUB_CERTFILE_PATH, &file_size))
		int_error("Getting an emergency staff authority's public key size failed");

	GLOBAL_pub_key_data = (char *)malloc(sizeof(char)*(file_size));
	if(!GLOBAL_pub_key_data)
		int_error("Allocating memory for \"GLOBAL_pub_key_data\" failed");

	if(!read_bin_file(ESA_PUB_CERTFILE_PATH, GLOBAL_pub_key_data, file_size, NULL))
		int_error("Reading an emergency staff authority's public key failed");
}
Beispiel #4
0
int main(int argc, char *argv[])
{
  char *serial_device = DEFAULT_DEVICE;
  int   serial_speed  = DEFAULT_SPEED;

  int byteno, npages, pageno;
#ifdef linux  
  int serport;
#else 
#   ifdef windows
  HANDLE serport;
#   endif  
#endif

  flash_page *flash_pages;
  
  unsigned char cin, cout, errtype, QUIT;
  printf("%s", id_string);
  printf("\nBuilt for %s Pagesize %d bytes\n", target_string, PAGESIZE);
  if(argc<2)
    {
      fprintf(stderr, "\nUsage: %s <firmware.bin> [<serial_port> [<baudrate>]] \n", *argv);
      fprintf(stderr, "Default serial port: %s\n", serial_device);
      fprintf(stderr, "Default baud rate:   %d\n\n", get_baud_int(serial_speed));
      return -1;
    }
  flash_pages = read_bin_file(&npages, argv[1]);
  
  /*
    At his stage we've got npages flash_page structs in the flash_pages array.
  */

  /* Find out serial port parameters */
  if (argc > 2) {
		serial_device = argv[2];
  }
  if (argc > 3) {
  		serial_speed = serial_getspeed(argv[3]);
  }

  if (serial_speed == -1) {
  	fprintf(stderr, "Illegal serial port speed ...\n");
  	return -1;
  }

  printf("Using %s at %d baud.\n", serial_device, get_baud_int(serial_speed));
  
  /*
    Start communicating with AVR ...
  */
  serport = open_serport(serial_device, serial_speed);

  printf("Opened %s\n", serial_device);

  if (check_serport(serport))
    {

      QUIT = TRUE;
      errtype = 0;

      /* AVR sends 'O' to tell PC that we're alive ... */
      /* Anything else --> Quit ... */

      printf("\nWaiting for AVR...\n");
      fflush(stdout);

      serial_read(serport, &cin, 1);
      if(cin == 'O')
        {
          printf("\nOk here we go ...\n");
          fflush(stdout);
          /* Send 'S' to start the operation ...*/
          cout = 'S';
          serial_write(serport, &cout, 1);
          QUIT = FALSE;
        }
      else
        {
          printf("\nNot this time ...\n");
          fflush(stdout);
          QUIT = TRUE;
        }


      /*
        "Main loop"
        Read flash_pages, send address + data for pages that are not Empty
        "Protocol" Send byte -> Receive byte, if not the same -> Quit ...
        After last page, send address 0xFF -> AVR "gets the point" and jumps to 0x00 ...
        Variables ...:
        ----------------------------------------------------------------
        |  serport: file descriptor for serial port ...
        |  npages : Number of pagestructs in flash_pages
        |  pageno : Keeps track of current page
        |  byteno : Keeps track of current byte within page (PAGESIZE ...)
        |  cin, cout : bytes to/from AVR
        ----------------------------------------------------------------
      */


      for(pageno=0; pageno<npages && !QUIT; pageno++)
        {
          /* Check if page contains data */
          if((flash_pages[pageno].empty == NO) && !QUIT)
            {

              /* Send 'P' and wait for the AVR to reply with 'p' to start page transfer
                 Gotto come up with something better than that,
                 but, that'll have to do for now ... flow control ... protocol ...
              */
              cout = 'P';
              serial_write(serport, &cout, 1);
#ifdef DEBUG
              printf("\nWating for 'clear to transfer page' from AVR...\n");
              fflush(stdout);
#endif
              cin = 0;
              while(cin != 'p')
                {
                  serial_read(serport, &cin, 1);
                }
          
              
              /* Send & receive flash address */
              if(!QUIT)
                {
                  /* high byte of address */
                  cout = (flash_pages[pageno].flash_address & 0xFF00)>>8;
                  serial_write(serport, &cout, 1);
                  serial_read(serport, &cin, 1);
#ifdef DEBUG
                  printf("\nAddress:");
                  printf("\nSent:%02x Rec.:%02x", cout, cin);
                  fflush(stdout);
#endif
                  if(cin!=cout)
                    QUIT = TRUE;
                }
              if(!QUIT)
                {
                  /* low byte of address */
                  cout = flash_pages[pageno].flash_address & 0x00FF;
                  serial_write(serport, &cout, 1);
                  serial_read(serport, &cin, 1);
#ifdef DEBUG
                  printf("\nSent:%02x Rec.:%02x\nData:", cout, cin);
                  fflush(stdout);
#endif
                  if(cin!=cout)
                    QUIT = TRUE;
                }
              for(byteno=0; byteno<PAGESIZE && !QUIT; byteno++)
                {
                  /* send & receive databytes */
                  cout = flash_pages[pageno].data[byteno];
                  serial_write(serport, &cout, 1);
                  serial_read(serport, &cin, 1);
#ifdef DEBUG
                  printf("\nSent: %d %02x Rec.:%02x", byteno, cout, cin);
                  fflush(stdout);
#endif
                  if(cin!=cout)
                    QUIT = TRUE;
                }
            }

        }
int
main (int argc, char *argv[])
{
	uint32_t devid = 0xa011;
	char *devid_str = NULL;
	int i, c;
	int option_index = 0;
	int binary = -1;

	static struct option long_options[] = {
		{"devid", 1, 0, 'd'},
		{"ascii", 0, 0, 'a'},
		{"binary", 0, 0, 'b'},
		{ 0 }
	};

	devid_str = getenv("INTEL_DEVID_OVERRIDE");

	while((c = getopt_long(argc, argv, "ad:b",
			       long_options, &option_index)) != -1) {
		switch(c) {
		case 'd':
			devid_str = optarg;
			break;
		case 'b':
			binary = 1;
			break;
		case 'a':
			binary = 0;
			break;
		default:
			printf("unkown command options\n");
			break;
		}
	}

	if (devid_str)
		devid = strtoul(devid_str, NULL, 0);

	ctx = drm_intel_decode_context_alloc(devid);

	if (optind == argc) {
		fprintf(stderr, "no input file given\n");
		exit(-1);
	}

	for (i = optind; i < argc; i++) {
		/* For stdin input, let's read as data file */
		if (!strcmp(argv[i], "-")) {
			read_data_file(argv[i]);
			continue;
		}
		if (binary == 1)
			read_bin_file(argv[i]);
		else if (binary == 0)
			read_data_file(argv[i]);
		else
			read_autodetect_file(argv[i]);
	}

	return 0;
}
// Implementation
void generate_ssl_cert(MYSQL *db_conn, unsigned int user_or_admin_id, char *username, boolean is_admin_flag, char *passwd, char *email_address, 
	const char *ssl_cert_priv_key_path, const char *ssl_cert_req_path, const char *enc_ssl_cert_path, const char *full_enc_ssl_cert_path, 
	const char *full_enc_ssl_cert_hash_path)
{
	char         cmd[CMD_STATEMENT_LENGTH + 1];
	char         err_msg[ERR_MSG_LENGTH + 1];
	char         stat[SQL_STATEMENT_LENGTH + 1];

	unsigned int len, enc_ssl_cert_size;
	char         *enc_ssl_cert_data;
	char         enc_ssl_cert_hash[SHA1_DIGEST_LENGTH + 1];
	char         *enc_ssl_cert_chunk;
	char         *query;

	// Generate an SSL certificate
	if(is_admin_flag)
	{
		sprintf(cmd, "openssl req -newkey rsa:1024 -sha1 -keyout %s -out %s -passout pass:%s -subj '/CN=%s.%s%s"
			"/ST=Songkla/C=TH/emailAddress=%s/O=PSU/OU=PSU'", ssl_cert_priv_key_path, ssl_cert_req_path, passwd, 
			GLOBAL_authority_name, username, ADMIN_IDENTITY_TOKEN, email_address);
	}
	else
	{
		sprintf(cmd, "openssl req -newkey rsa:1024 -sha1 -keyout %s -out %s -passout pass:%s -subj '/CN=%s.%s%s"
			"/ST=Songkla/C=TH/emailAddress=%s/O=PSU/OU=PSU'", ssl_cert_priv_key_path, ssl_cert_req_path, passwd, 
			GLOBAL_authority_name, username, USER_IDENTITY_TOKEN, email_address);
	}

	exec_cmd(cmd, strlen(cmd), err_msg, sizeof(err_msg));
	if(!strstr(err_msg, "writing new private key"))
	{
		fprintf(stderr, "Error: \"%s\"\n", err_msg);
		int_error("Generating an SSL certificate failed");
	}

	sprintf(cmd, "openssl x509 -req -days 365 -in %s -sha1 -extfile %s -extensions usr_cert -CA %s -CAkey %s -CAcreateserial "
		"-out %s -passin pass:%s", ssl_cert_req_path, OPENSSL_PHRAPP_CNF_PATH, USER_CA_FULL_CERTFILE_PATH, 
		USER_CA_FULL_CERTFILE_PATH, enc_ssl_cert_path, USER_CA_CERTFILE_PASSWD);

	exec_cmd(cmd, strlen(cmd), err_msg, sizeof(err_msg));
	if(!strstr(err_msg, "Getting CA Private Key"))
	{
		fprintf(stderr, "Error: \"%s\"\n", err_msg);
		int_error("Generating an SSL certificate failed");
	}

	sprintf(cmd, "cat %s %s %s %s > %s", enc_ssl_cert_path, ssl_cert_priv_key_path, USER_CA_ONLY_CERT_CERTFILE_PATH, 
		EMU_ROOT_CA_ONLY_CERT_CERTFILE_PATH, full_enc_ssl_cert_path);

	exec_cmd(cmd, strlen(cmd), err_msg, sizeof(err_msg));
	if(strcmp(err_msg, "") != 0)
	{
		fprintf(stderr, "Error: \"%s\"\n", err_msg);
		int_error("Generating an SSL certificate failed");
	}

	// Allocate heap variables
	enc_ssl_cert_data = (char *)malloc(sizeof(char)*1000*1024);
	if(!enc_ssl_cert_data)
	{
		int_error("Allocating memory for \"enc_ssl_cert_data\" failed");
	}

	enc_ssl_cert_chunk = (char *)malloc(sizeof(char)*((1000*1024)*2+1));
	if(!enc_ssl_cert_chunk)
	{
		int_error("Allocating memory for \"enc_ssl_cert_chunk\" failed");
	}

	query = (char *)malloc(sizeof(char)*(((1000*1024)*2+1)+sizeof(stat)+1));
	if(!query)
	{
		int_error("Allocating memory for \"query\" failed");
	}

	// Read the SSL certificate into the buffer
	if(!read_bin_file(full_enc_ssl_cert_path, enc_ssl_cert_data, sizeof(char)*1000*1024, &enc_ssl_cert_size))
		int_error("Reading full encrypted SSL certificate failed");

	sum_sha1_from_file(full_enc_ssl_cert_path, enc_ssl_cert_hash, full_enc_ssl_cert_hash_path);

	// Insert encrypted SSL certificate and its hash into database
	if(is_admin_flag)
	{
		sprintf(stat, "UPDATE %s SET enc_ssl_cert='%%s', enc_ssl_cert_hash='%s' WHERE admin_id=%u", ESA__ADMINS, enc_ssl_cert_hash, user_or_admin_id);
	}
	else
	{
		sprintf(stat, "UPDATE %s SET enc_ssl_cert='%%s', enc_ssl_cert_hash='%s' WHERE user_id=%u", ESA__USERS, enc_ssl_cert_hash, user_or_admin_id);
	}

	// Delete files
	unlink(ssl_cert_priv_key_path);
	unlink(ssl_cert_req_path);
	unlink(enc_ssl_cert_path);
	unlink(full_enc_ssl_cert_path);

	// Take the escaped SQL string
	mysql_real_escape_string(db_conn, enc_ssl_cert_chunk, enc_ssl_cert_data, enc_ssl_cert_size);
  	len = snprintf(query, sizeof(stat)+sizeof(char)*((1000*1024)*2+1), stat, enc_ssl_cert_chunk);

	if(mysql_real_query(db_conn, query, len))
  	{
      		sprintf(err_msg, "Error %u: %s\n", mysql_errno(db_conn), mysql_error(db_conn));
      		int_error(err_msg);
  	}

	// Free heap variables
	if(enc_ssl_cert_data)
	{
		free(enc_ssl_cert_data);
		enc_ssl_cert_data = NULL;
	}

	if(enc_ssl_cert_chunk)
	{
		free(enc_ssl_cert_chunk);
		enc_ssl_cert_chunk = NULL;
	}

	if(query)
	{
		free(query);
		query = NULL;
	}
}
Beispiel #7
0
kanjis init(char filename[]) {
    kanjis kjs = read_bin_file(filename);
    return kjs;
}