Esempio n. 1
0
ptree_leaf::ptree_leaf(readable& src)
{
	src.read_full(m_key, k_max_ptree_key);
	src.read_full(m_value, k_max_ptree_val);
	m_dkey = make_digest(m_key);
	m_merkle = make_digest(m_dkey, make_digest(m_value));
}
Esempio n. 2
0
uint64_t ptree::key_position(const string& key) const 
{
	if (!m_root) {
		return 0;
	}
	return m_root->key_position(make_digest(key));
}
Esempio n. 3
0
/* Set *DIGEST_PATH to the path to the lock/entries digest file associate
   with PATH, where PATH is the path to the lock file or lock entries file
   in FS. */
static svn_error_t *
digest_path_from_path(const char **digest_path,
                      const char *fs_path,
                      const char *path,
                      apr_pool_t *pool)
{
  const char *digest;
  SVN_ERR(make_digest(&digest, path, pool));
  *digest_path = svn_dirent_join_many(pool, fs_path, PATH_LOCKS_DIR,
                                      apr_pstrmemdup(pool, digest,
                                                     DIGEST_SUBDIR_LEN),
                                      digest, NULL);
  return SVN_NO_ERROR;
}
Esempio n. 4
0
ptree_branch::ptree_branch(uint8_t split_pos, const ptree_ptr& p1, const ptree_ptr& p2)
	: m_branches{ p1, p2 }
	, m_node_count(p1->node_count() + p2->node_count())
	, m_split_pos(split_pos)
	, m_complete(p1->complete() && p2->complete())
{
	if (p1->is_proxy()) {
		// p1 is proxy, p2 must be valid, and branches are in order
		m_prefix = p2->prefix();
	} else if (p2->is_proxy()) {
		// p2 is proxy, p1 must be valid, and branches are in order
		m_prefix = p1->prefix();
	} else {
		// Both are valid, but might be out of order
		m_prefix = p1->prefix();
		if (p1->prefix() > p2->prefix()) {
			swap(m_branches[0], m_branches[1]);
		}
	}
	m_prefix.zero_above(split_pos);
	m_merkle = make_digest(m_prefix, m_node_count, m_branches[0]->merkle(), m_branches[1]->merkle());
}
Esempio n. 5
0
ptree_leaf::ptree_leaf(const string& key, const digest& dkey, const string& value) 
	: m_key(key)
	, m_dkey(dkey)
	, m_value(value)
	, m_merkle(make_digest(dkey, make_digest(value)))
{}
Esempio n. 6
0
/* this actually makes the message block and sends it */
int
build_message (FILE * in_ptr, byte numdest,
	       char **destination, int *chain, byte numsub,
	       char **subject, char *outfile, int outfileflag,
	       REMAILER * remailer_list, int num_remailers,
	       int client)
{
  int hop, i, j, k, numpackets, packet, copy;
  long int chunk, tmpchunk;
  byte iv[8];
  byte ivarray[NUM_IV][8], innerkey[24], digest[16];
  BUFFER *sendbuff, *bodybuff, *headbuff[21], *message, *tempbuff;
  long offset;
  char line[1024];
  byte *key[5], packetID[16], commonpacketID[16], messageID[16];
  byte tmpbyte;
  byte packet_type;
#ifdef USE_RSAREF
  PUBLIC_KEY pubKey, *keyPtr[5];
  unsigned int numPubKeys, keylen;
  R_ENVELOPE_CTX rsa_context;
#else
  PUBLIC_KEY pubKey;
  unsigned int keylen;
  B_ALGORITHM_OBJ rsa_context;
  B_ALGORITHM_OBJ des_context;
  B_KEY_OBJ des_key;
#endif
  unsigned long timestamp;

  message = new_buffer ();
  /* prepend destinations to body */
  add_to_buffer (message, &numdest, 1);
  for (i = 0; i < numdest; i++)
    add_to_buffer (message, destination[i], DESTSIZE);
  /* add message header lines to body */
  add_to_buffer (message, &numsub, 1);
  for (i = 0; i < numsub; i++)
    add_to_buffer (message, subject[i], SUBSIZE);

  offset = message->length;

  /* add the file to body */
  if (in_ptr)
    {
      while ((i = fread (line, 1, sizeof (line), in_ptr)) > 0)
	{
	  if (i < 0)
	    return (-1);	/* file error */
	  add_to_buffer (message, line, i);
	}
      fclose (in_ptr);
    }

  /* message is complete. */
  add_to_random (message->message, message->length);

  /* Choose the final hop now. */
  if (chain[chain[0]] == 0)
    if (rnd_select (chain[0], chain, remailer_list, num_remailers) < 0)
      return (-1);

#ifdef USE_ZLIB
  /* should we compress this message? */
  if (message->length > PACKETSIZE &&
      check_abilities (remailer_list[chain[chain[0]]].abilities, "C", ""))
    {
      tempbuff = new_buffer ();
      if (compress_buf2buf (message, tempbuff, offset))
	{
	  free_buffer (message);
	  message = tempbuff;
	}
      else
	free_buffer (tempbuff);
    }

  /* in case it already is compressed, but should not be */
  if (!check_abilities (remailer_list[chain[chain[0]]].abilities, "C", ""))
    {
      tempbuff = new_buffer ();
      if (uncompress_buf2buf (message, tempbuff, offset))
	{
	  free_buffer (message);
	  message = tempbuff;
	}
      else
	free_buffer (tempbuff);
    }
#endif

  numpackets = message->length / PACKETSIZE;
  if (message->length % PACKETSIZE != 0)
    numpackets++;

  bodybuff = new_buffer ();
  sendbuff = new_buffer ();
  for (i = 1; i <= HOPMAX; i++)
    headbuff[i] = new_buffer ();

  our_randombytes (messageID, 16);

  /* Loop to make one packet at a time */
  for (packet = 1; packet <= numpackets; packet++)
    {
      /* put the packet in bodybuff, and put the rest back in message */
      chunk = message->length;
      if (chunk > PACKETSIZE)
	chunk = PACKETSIZE;
      clear_buffer (bodybuff);

      tmpchunk = chunk;
      tmpbyte = tmpchunk & 0xFF;
      add_to_buffer (bodybuff, &tmpbyte, 1);	/* prepend length of data low byte */
      tmpchunk = tmpchunk / 256;
      tmpbyte = tmpchunk & 0xFF;
      add_to_buffer (bodybuff, &tmpbyte, 1);	/* prepend length of data 2nd byte */
      tmpchunk = tmpchunk / 256;
      tmpbyte = tmpchunk & 0xFF;
      add_to_buffer (bodybuff, &tmpbyte, 1);	/* prepend length of data 3rd byte */
      tmpchunk = tmpchunk / 256;
      tmpbyte = tmpchunk & 0xFF;
      add_to_buffer (bodybuff, &tmpbyte, 1);	/* prepend length of data high byte */

      add_to_buffer (bodybuff, message->message, chunk);
      tempbuff = new_buffer ();
      add_to_buffer (tempbuff, (message->message) + chunk, message->length - chunk);
      free_buffer (message);
      message = tempbuff;

      if (NUMCOPIES < 1 || NUMCOPIES > 10)
	{
	  fprintf (errlog, "Error: Invalid number of copies.\n");
	  return (-1);
	}
      for (copy = 1; copy <= NUMCOPIES; copy++)
	{
	  clear_buffer (sendbuff);
	  add_to_buffer (sendbuff, bodybuff->message, bodybuff->length);
	  pad_buffer (sendbuff, PACKETSIZE + 4);

	  /* Create fake header cards */
	  for (i = chain[0] + 1; i <= HOPMAX; i++)
	    {
	      reset_buffer (headbuff[i]);
	      pad_buffer (headbuff[i], HEADERSIZE);
	    }
	  if (rnd_selectchain (chain, remailer_list, num_remailers) < 0)
	    return (-1);
	  for (hop = chain[0]; hop >= 1; hop--)
	    {
	      /* Get public key for remailer */
	      if (get_pub_key (remailer_list[abs (chain[hop])].key_ID,
			       &pubKey) != 0)
		{
		  fprintf (errlog, "Can't get public key!\n");
		  return (-1);
		}
	      key[0] = malloc (MAX_ENCRYPTED_KEY_LEN);
#ifdef USE_RSAREF
	      numPubKeys = 1;
	      keyPtr[0] = &pubKey;
	      if (R_SealInit (&rsa_context, key, &keylen, iv, numPubKeys,
			      keyPtr, EA_DES_EDE3_CBC, &random_obj) != 0)
		{
		  fprintf (errlog, "R_SealInit error %x!!!\n", i);
		  exit (-1);
		}
#else
	      B_CreateAlgorithmObject (&rsa_context);
	      B_CreateAlgorithmObject (&des_context);
	      B_SetAlgorithmInfo (rsa_context, AI_PKCS_RSAPublic, 0);
	      B_SetAlgorithmInfo (des_context, AI_DES_EDE3_CBC_IV8, iv);
	      our_randombytes (line, 24);
	      B_CreateKeyObject (&des_key);
	      B_SetKeyInfo (des_key, KI_DES24Strong, line);

	      B_EncryptInit (rsa_context, pubKey, CHOOSER, NULL);
	      B_EncryptUpdate (rsa_context, key[0], &keylen,
			       MAX_ENCRYPTED_KEY_LEN, line, 24,
			       random_obj, NULL);
	      B_EncryptFinal (rsa_context, key[0] + keylen, &k,
			      MAX_ENCRYPTED_KEY_LEN - keylen, random_obj,
			      NULL);
	      B_DestroyAlgorithmObject (&rsa_context);
	      B_DestroyKeyObject (&pubKey);
	      keylen += k;

	      B_EncryptInit (des_context, des_key, CHOOSER, NULL);
	      /* XXX Error handling! */
#endif
	      /* make packet ID and innerkey */
	      /* packet ID is unique except for duplicates in the last hop */
	      if (hop != chain[0])
		  our_randombytes (packetID, 16);
	      else
	      {
		  if (copy == 1)
		      our_randombytes (commonpacketID, 16);
	      memcpy (packetID, commonpacketID, 16);
	      }

	      our_randombytes (innerkey, 24);
	      /* make the iv array */
	      for (i = 0; i < NUM_IV; i++)
		our_randombytes (ivarray[i], 8);

	      /* Now build the current header */
	      reset_buffer (headbuff[hop]);
	      add_to_buffer (headbuff[hop], packetID, 16);	/* also like another IV */
	      add_to_buffer (headbuff[hop], innerkey, 24);	/* Key used to encrypt headers and body */
	      if (hop == chain[0])
		{		/* if this is the last hop */
		  if (numpackets == 1)
		    /* final hop */
		    packet_type = P_FINAL;
		  else
		    /* partial message */
		    packet_type = P_PARTIAL;
		  add_to_buffer (headbuff[hop], &packet_type, 1);
		  if (packet_type & P_PARTIAL)
		    {
		      tmpbyte = packet;	/* which packet is this */
		      add_to_buffer (headbuff[hop], &tmpbyte, 1);
		      tmpbyte = numpackets;	/* out of how many */
		      add_to_buffer (headbuff[hop], &tmpbyte, 1);
		    }
		  add_to_buffer (headbuff[hop], messageID, 16);
		  add_to_buffer (headbuff[hop], ivarray[BODY_IV], 8);
		}
	      else
		{		/* this is not the last hop */
		  packet_type = 0;	/* packet type = intermediate packet */
		  add_to_buffer (headbuff[hop], &packet_type, 1);
		  /* insert the array of IVs */
		  for (i = 0; i < NUM_IV; i++)
		    {
		      add_to_buffer (headbuff[hop], ivarray[i], 8);
		    }
		  add_to_buffer (headbuff[hop], remailer_list[abs (chain[hop + 1])].name, 80);
		}

	      /* Extension to original mixmaster format:
		 Use timestamp to prevent replay of old messages. */
	      add_to_buffer (headbuff[hop], TSMAGIC, sizeof(TSMAGIC));
	      /* Fuzzy timestamp: don't leak more information than necessary */
	      timestamp = time(NULL) / SECONDSPERDAY - random_number(4);
	      tmpbyte = timestamp & 0xFF;
	      add_to_buffer (headbuff[hop], &tmpbyte, 1);
	      tmpbyte = (timestamp / 256) & 0xFF;
	      add_to_buffer (headbuff[hop], &tmpbyte, 1);

	      /* Make and append an MD5 checksum of the packet */
	      make_digest (headbuff[hop], digest);
	      add_to_buffer (headbuff[hop], digest, 16);

	      /* Now pad pre-encrypted header to standard size */
	      pad_buffer (headbuff[hop], INNERHEAD);

	      /* Done building headbuff[hop] so now RSA it */
	      tempbuff = new_buffer ();
	      assert (headbuff[hop]->length <= INNERHEAD);

#ifdef USE_RSAREF
	      R_SealUpdate (&rsa_context, line, &k, headbuff[hop]->message,
			    headbuff[hop]->length);
#else
	      B_EncryptUpdate (des_context, line, &k, sizeof (line),
			       headbuff[hop]->message, headbuff[hop]->length,
			       random_obj, NULL);
#endif
	      add_to_buffer (tempbuff, line, k);
#ifdef USE_RSAREF
	      R_SealFinal (&rsa_context, line, &k);
#else
	      B_EncryptFinal (des_context, line, &k,
			      INNERHEAD - k,
			      random_obj, NULL);
	      B_DestroyAlgorithmObject (&des_context);
	      B_DestroyKeyObject (&des_key);
#endif
	      add_to_buffer (tempbuff, line, k);
	      clear_buffer (headbuff[hop]);

	      /* Prepend RSA key ID */
	      add_to_buffer (headbuff[hop], remailer_list[abs (chain[hop])].key_ID, 16);
	      /* prepend keys and IV to header */
	      tmpbyte = keylen;
	      add_to_buffer (headbuff[hop], &tmpbyte, 1);
	      add_to_buffer (headbuff[hop], key[0], tmpbyte);
	      add_to_buffer (headbuff[hop], iv, 8);
	      /* add encryped header */
	      add_to_buffer (headbuff[hop], tempbuff->message, tempbuff->length);
	      free_buffer (tempbuff);

	      /* pad out encrypted header to standard size */
	      pad_buffer (headbuff[hop], HEADERSIZE);

	      /* encrypt body */
	      crypt_in_buffer (innerkey, ivarray[BODY_IV], sendbuff, 1);

	      /* encrypt all later headers */
	      /* i is the index for ivarray */
	      for (i = 0, j = hop + 1; j <= HOPMAX; j++)
		crypt_in_buffer (innerkey, ivarray[i++], headbuff[j], 1);
	    }			/* hop loop for a given packet */
	  if (VERBOSE)
	    {
	      fprintf (errlog, "Packet chain: ");
	      for (i = 1; i <= chain[0]; i++)
		fprintf (errlog, "%s;", remailer_list[abs (chain[i])].shortname);
	      fprintf (errlog, "\n");
	    }
	  if (strlen (outfile) > 0 && (!streq (outfile, "-")) && (numpackets > 1 || NUMCOPIES > 1))
	    {
	      sprintf (line, "%s.%d", outfile, (packet - 1) * NUMCOPIES + copy);
	      send_new_packet (headbuff, sendbuff, remailer_list[abs (chain[1])].name, line, outfileflag, client);
	    }
	  else
	    {
	      send_new_packet (headbuff, sendbuff, remailer_list[abs (chain[1])].name, outfile, outfileflag, client);
	    }
	}			/* copies of one packet */
    }				/* end loop processing packets */
  free_buffer (bodybuff);
  free_buffer (sendbuff);
  free_buffer (message);
  for (i = 1; i <= HOPMAX; i++)
    free_buffer (headbuff[i]);

  return (0);
}
Esempio n. 7
0
/* {{{ apc_iterator_item */
static apc_iterator_item_t* apc_iterator_item_ctor(apc_iterator_t *iterator, slot_t **slot_pp) {
    zval *zvalue;
    char md5str[33];
    slot_t *slot = *slot_pp;
    apc_context_t ctxt = {0, };
    apc_iterator_item_t *item = ecalloc(1, sizeof(apc_iterator_item_t));

    if (slot->key.type == APC_CACHE_KEY_FILE) {
        /* keys should be unique and with stat=1 we could have multiple files with the same name, so use '<device> <inode>' instead */
#ifdef PHP_WIN32
        item->key_len = spprintf(&item->key, 0, "%I64d %I64d", slot->key.data.file.device, slot->key.data.file.inode);
#else
        item->key_len = spprintf(&item->key, 0, "%ld %ld", (ulong)slot->key.data.file.device, (ulong)slot->key.data.file.inode);
#endif
        item->filename_key = estrdup(slot->value->data.file.filename);
    } else if (slot->key.type == APC_CACHE_KEY_USER) {
        item->key = estrndup((char*)slot->key.data.user.identifier, slot->key.data.user.identifier_len);
        item->key_len = slot->key.data.user.identifier_len;
        item->filename_key = item->key; 
    } else if (slot->key.type == APC_CACHE_KEY_FPFILE) {
        item->key = estrndup((char*)slot->key.data.fpfile.fullpath, slot->key.data.fpfile.fullpath_len);
        item->key_len = slot->key.data.fpfile.fullpath_len;
    } else {
        apc_eprint("Internal error, invalid entry type.");
    }

    ALLOC_INIT_ZVAL(item->value);
    array_init(item->value);

    if (APC_ITER_TYPE & iterator->format) {
        if(slot->value->type == APC_CACHE_ENTRY_FILE) {
            add_assoc_string(item->value, "type", "file", 1);
        } else if(slot->value->type == APC_CACHE_ENTRY_USER) {
            add_assoc_string(item->value, "type", "user", 1);
        }
    }
    if (APC_ITER_FILENAME & iterator->format) {
        if(slot->value->type == APC_CACHE_ENTRY_FILE) {
            if (slot->key.type == APC_CACHE_KEY_FILE) {
              add_assoc_string(item->value, "filename", slot->value->data.file.filename, 1);
            } else {  /* APC_CACHE_FPFILE */
              add_assoc_string(item->value, "filename", (char*)slot->key.data.fpfile.fullpath, 1);
            }
        }
    }
    if (APC_ITER_DEVICE & iterator->format) {
        if(slot->key.type == APC_CACHE_KEY_FILE) {
#ifdef PHP_WIN32
			char buf[20];
			sprintf(buf, "%I64d", slot->key.data.file.device);
			add_assoc_string(item->value, "device", buf, 1);
#else
            add_assoc_long(item->value, "device", slot->key.data.file.device);
#endif
        }
    }
    if (APC_ITER_INODE & iterator->format) {
        if(slot->key.type == APC_CACHE_KEY_FILE) {
#ifdef PHP_WIN32
			char buf[20];
			sprintf(buf, "%I64d", slot->key.data.file.device);
			add_assoc_string(item->value, "device", buf, 1);
#else
            add_assoc_long(item->value, "inode", slot->key.data.file.inode);
#endif
        }
    }
    if (APC_ITER_KEY & iterator->format) {
        add_assoc_stringl(item->value, "key", item->key, item->key_len, 1);
    }
    if (APC_ITER_VALUE & iterator->format) {
        if(slot->value->type == APC_CACHE_ENTRY_USER) {

            ctxt.pool = apc_pool_create(APC_UNPOOL, apc_php_malloc, apc_php_free, NULL, NULL);
            ctxt.copy = APC_COPY_OUT_USER;

            MAKE_STD_ZVAL(zvalue);
            apc_cache_fetch_zval(zvalue, slot->value->data.user.val, &ctxt);
            apc_pool_destroy(ctxt.pool);
            add_assoc_zval(item->value, "value", zvalue);
        }
    }
    if (APC_ITER_MD5 & iterator->format) {
        if(slot->value->type == APC_CACHE_ENTRY_FILE) {
            if(slot->key.md5) {
                make_digest(md5str, slot->key.md5);
                add_assoc_string(item->value, "md5", md5str, 1);
            }
        }
    }
    if (APC_ITER_NUM_HITS & iterator->format) {
        add_assoc_long(item->value, "num_hits", slot->num_hits);
    }
    if (APC_ITER_MTIME & iterator->format) {
        add_assoc_long(item->value, "mtime", slot->key.mtime);
    }
    if (APC_ITER_CTIME & iterator->format) {
        add_assoc_long(item->value, "creation_time", slot->creation_time);
    }
    if (APC_ITER_DTIME & iterator->format) {
        add_assoc_long(item->value, "deletion_time", slot->deletion_time);
    }
    if (APC_ITER_ATIME & iterator->format) {
        add_assoc_long(item->value, "access_time", slot->access_time);
    }
    if (APC_ITER_REFCOUNT & iterator->format) {
        add_assoc_long(item->value, "ref_count", slot->value->ref_count);
    }
    if (APC_ITER_MEM_SIZE & iterator->format) {
        add_assoc_long(item->value, "mem_size", slot->value->mem_size);
    }
    if (APC_ITER_TTL & iterator->format) {
        if(slot->value->type == APC_CACHE_ENTRY_USER) {
            add_assoc_long(item->value, "ttl", slot->value->data.user.ttl);
        }
    }

    return item;
}