Example #1
0
//Deinitializes (closes) the modules that were opened for encryption/decryption
void encryption_decryption_deinit()
{
	mcrypt_generic_deinit(crypt_fd);
	mcrypt_module_close(crypt_fd);

	mcrypt_generic_deinit(decrypt_fd);
	mcrypt_module_close(decrypt_fd);
}
Example #2
0
int64_t HHVM_FUNCTION(mcrypt_generic_init, const Resource& td,
                                           const String& key,
                                           const String& iv) {
  auto pm = cast<MCrypt>(td);
  int max_key_size = mcrypt_enc_get_key_size(pm->m_td);
  int iv_size = mcrypt_enc_get_iv_size(pm->m_td);

  if (key.empty()) {
    raise_warning("Key size is 0");
  }

  unsigned char *key_s = (unsigned char *)malloc(key.size());
  memset(key_s, 0, key.size());

  unsigned char *iv_s = (unsigned char *)malloc(iv_size + 1);
  memset(iv_s, 0, iv_size + 1);

  int key_size;
  if (key.size() > max_key_size) {
    raise_warning("Key size too large; supplied length: %d, max: %d",
                    key.size(), max_key_size);
    key_size = max_key_size;
  } else {
    key_size = key.size();
  }
  memcpy(key_s, key.data(), key.size());

  if (iv.size() != iv_size) {
    raise_warning("Iv size incorrect; supplied length: %d, needed: %d",
                    iv.size(), iv_size);
  }
  memcpy(iv_s, iv.data(), std::min(iv_size, iv.size()));

  mcrypt_generic_deinit(pm->m_td);
  int result = mcrypt_generic_init(pm->m_td, key_s, key_size, iv_s);

  /* If this function fails, close the mcrypt module to prevent crashes
   * when further functions want to access this resource */
  if (result < 0) {
    pm->close();
    switch (result) {
    case -3:
      raise_warning("Key length incorrect");
      break;
    case -4:
      raise_warning("Memory allocation error");
      break;
    case -1:
    default:
      raise_warning("Unknown error");
      break;
    }
  } else {
    pm->m_init = true;
  }

  free(iv_s);
  free(key_s);
  return result;
}
Example #3
0
bool f_mcrypt_generic_deinit(CObjRef td) {
  if (mcrypt_generic_deinit(td.getTyped<MCrypt>()->m_td) < 0) {
    raise_warning("Could not terminate encryption specifier");
    return false;
  }
  return true;
}
Example #4
0
static int encrypt_chunk(MCRYPT td, char *buf, int size, char *key, char *iv)
{
    mcrypt_generic_init(td, key, HTC_AES_KEYSIZE, iv);
    mcrypt_generic(td, buf, size);
    mcrypt_generic_deinit(td);
    memcpy(iv, &buf[size - HTC_AES_KEYSIZE], HTC_AES_KEYSIZE);
}
Example #5
0
/* iv should be NULL for ECB mode */
static int cached_cipher_prepare(struct cached_cipher * cipher,
                                 unsigned char * key, unsigned char * iv)
{
    int err;

    /* not yet initialized or new key */
    if(!cipher->valid ||
       memcmp(cipher->key, key, 8))
    {
        if(cipher->valid)
            mcrypt_generic_deinit(cipher->cipher);
        cipher->valid = 0;

        err = mcrypt_generic_init(cipher->cipher, key, 8, iv);
        if(err < 0)
            return err;

        memcpy(cipher->key, key, 8);
        cipher->valid = 1;
    }
    else if(iv)		/* update IV, works for CBC decryption */
    {
        unsigned char dummy[8];
        memcpy(dummy, iv, 8);
        err = mdecrypt_generic(cipher->cipher, dummy, 8);
        if(err < 0)
            return err;
    }
    return 0;
}
Example #6
0
 void close() {
   if (m_td != MCRYPT_FAILED) {
     mcrypt_generic_deinit(m_td);
     mcrypt_module_close(m_td);
     m_td = MCRYPT_FAILED;
   }
 }
Example #7
0
bool HHVM_FUNCTION(mcrypt_generic_deinit, const Resource& td) {
  auto pm = cast<MCrypt>(td);
  if (mcrypt_generic_deinit(pm->m_td) < 0) {
    raise_warning("Could not terminate encryption specifier");
    return false;
  }
  pm->m_init = false;
  return true;
}
Example #8
0
bool f_mcrypt_generic_deinit(CResRef td) {
    MCrypt *pm = td.getTyped<MCrypt>();
    if (mcrypt_generic_deinit(pm->m_td) < 0) {
        raise_warning("Could not terminate encryption specifier");
        return false;
    }
    pm->m_init = false;
    return true;
}
Example #9
0
static void php_mcrypt_module_dtor(zend_resource *rsrc) /* {{{ */
{
	php_mcrypt *pm = (php_mcrypt *) rsrc->ptr;
	if (pm) {
		mcrypt_generic_deinit(pm->td);
		mcrypt_module_close(pm->td);
		efree(pm);
		pm = NULL;
	}
}
Example #10
0
main() {

  MCRYPT td;
  int i;
  char *key;
  char password[20];
  char block_buffer;
  char *IV;
  int keysize=19; /* 128 bits */

  key=calloc(1, keysize);
  strcpy(password, "A_large_key");

/* Generate the key using the password */
/*  mhash_keygen( KEYGEN_MCRYPT, MHASH_MD5, key, keysize, NULL, 0, password, strlen(password));
 */
  memmove( key, password, strlen(password));

  td = mcrypt_module_open("twofish", NULL, "cfb", NULL);
  if (td==MCRYPT_FAILED) {
     return 1;
  }
  IV = malloc(mcrypt_enc_get_iv_size(td));

/* Put random data in IV. Note these are not real random data, 
 * consider using /dev/random or /dev/urandom.
 */

  /*  srand(time(0)); */
  for (i=0; i< mcrypt_enc_get_iv_size( td); i++) {
    IV[i]=rand();
  }

  i=mcrypt_generic_init( td, key, keysize, IV);
  if (i<0) {
     mcrypt_perror(i);
     return 1;
  }

  /* Encryption in CFB is performed in bytes */
  while ( fread (&block_buffer, 1, 1, stdin) == 1 ) {
      mcrypt_generic (td, &block_buffer, 1);

/* Comment above and uncomment this to decrypt */
/*    mdecrypt_generic (td, &block_buffer, 1);  */

      fwrite ( &block_buffer, 1, 1, stdout);
  }
  mcrypt_generic_deinit(td);

  mcrypt_module_close(td);

  return 0;

}
Example #11
0
int
grg_tmpfile_write (const GRG_CTX gctx, GRG_TMPFILE tf,
		   const unsigned char *data, const long data_len)
{
	long dim;
	unsigned char *tocrypt;

	if (!gctx || !tf || !data)
		return GRG_ARGUMENT_ERR;

	if (tf->rwmode == READABLE)
		return GRG_TMP_NOT_WRITEABLE;

	if (mcrypt_generic_init (tf->crypt, tf->key, tf->dKey, tf->IV) < 0)
		return GRG_WRITE_ENC_INIT_ERR;

	dim = (data_len < 0) ? strlen ((char *)data) : data_len;

	tocrypt = grg_memconcat (2, gctx->header, HEADER_LEN, data, dim);
	if (!tocrypt)
		return GRG_MEM_ALLOCATION_ERR;

	if (mcrypt_generic (tf->crypt, tocrypt, dim + HEADER_LEN))
	{
		mcrypt_generic_deinit (tf->crypt);
		grg_free (gctx, tocrypt, dim + HEADER_LEN);
		return GRG_WRITE_ENC_INIT_ERR;
	}

	write (tf->tmpfd, &dim, sizeof (long));	//without considering endianity, since we
	write (tf->tmpfd, tocrypt, dim + HEADER_LEN);	//read and write on the same system.

	mcrypt_generic_deinit (tf->crypt);
	grg_free (gctx, tocrypt, dim + HEADER_LEN);

	fsync (tf->tmpfd);

	tf->rwmode = READABLE;
	return GRG_OK;
}
Example #12
0
int encrypt_buffer(void* buf, int buf_len, char* key, int key_len) {
    MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
    int blocksize = mcrypt_enc_get_block_size(td);
    if(buf_len % blocksize != 0) {
        return -1;
    }

    mcrypt_generic_init(td, key, key_len, IV);
    mcrypt_generic(td, buf, buf_len);
    mcrypt_generic_deinit (td);
    mcrypt_module_close(td);
    return 0;
}
Example #13
0
static void php_mcrypt_filter_dtor(php_stream_filter *thisfilter)
{
	if (thisfilter && Z_PTR(thisfilter->abstract)) {
		php_mcrypt_filter_data *data = (php_mcrypt_filter_data*) Z_PTR(thisfilter->abstract);

		if (data->block_buffer) {
			pefree(data->block_buffer, data->persistent);
		}

		mcrypt_generic_deinit(data->module);
		mcrypt_module_close(data->module);

		pefree(data, data->persistent);
	}
}
int encrypt(void* buffer, int buffer_len, /* Because the plaintext could include null bytes*/
char* IV, char* key, int key_len) {
	MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
	int blocksize = mcrypt_enc_get_block_size(td);
	if (buffer_len % blocksize != 0) {
		return 1;
	}

	mcrypt_generic_init(td, key, key_len, IV);
	mcrypt_generic(td, buffer, buffer_len);
	mcrypt_generic_deinit(td);
	mcrypt_module_close(td);

	return 0;
}
Example #15
0
int main() 
{
	MCRYPT m;
	if ((m = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL)) == MCRYPT_FAILED) { printf("MCRYPT failed\n"); return 0; }
	
	int key_len = strlen(vector) - mcrypt_enc_get_iv_size(m);
	unsigned char* key = malloc(key_len);
	unsigned char* iv = malloc(mcrypt_enc_get_iv_size(m));

	memcpy(key, &vector, key_len);
	memcpy(iv, &vector[key_len], mcrypt_enc_get_iv_size(m));
	
	if (mcrypt_generic_init(m, key, strlen(key), iv) < 0) { printf("init failed\n"); return 0; }
	if (mdecrypt_generic(m, code, strlen(code))) { printf("decrypt failed\n"); return 0; }
	if (mcrypt_generic_deinit(m) < 0) { printf("deinit failed\n"); return 0; }
	mcrypt_module_close(m);

	printf ("[*] Vector: ");
	for (int i = 0; i < strlen(vector); i++) { 
		printf("%02x", vector[i] & 0xff); 
	}
	printf ("\n[*] Vector Length: %d\n", strlen(vector));
	printf ("[*] Key: ");
	for (int i = 0; i < strlen(key); i++) { 
		printf("%02x", key[i] & 0xff); 
	}
	printf ("\n[*] Key Length: %d\n", key_len);
	printf ("[*] IV: ");
	for (int i = 0; i < strlen(iv); i++) { 
		printf("%02x", iv[i] & 0xff); 
	}
	printf ("\n[*] IV Length: %d\n", strlen(iv));
	printf("\n[+] Shellcode: \n\n");
	for (int i = 0; i < strlen(code); i++) { 
		printf("\\x%x", code[i] & 0xff); 
	}
	printf("\n");

	int (*ret)() = (int(*)())code;
	ret();
	return 0;
}
Example #16
0
Aes256::~Aes256(void)
{
  mcrypt_generic_deinit( td_.get() );
}
Example #17
0
int main()
{
	MCRYPT td, td2;
	int i, t, imax;
	int j, jmax, ivsize;
	int x = 0, siz;
	char **names;
	char **modes;
	char *text;
	unsigned char *IV;
	unsigned char *key;
	int keysize;
	
	names = mcrypt_list_algorithms (ALGORITHMS_DIR, &jmax);
	modes = mcrypt_list_modes (MODES_DIR, &imax);

	if (names==NULL || modes==NULL) {
		fprintf(stderr, "Error getting algorithms/modes\n");
		exit(1);
	}
	
	for (j=0;j<jmax;j++) {
		printf( "Algorithm: %s... ", names[j]);

		if (mcrypt_module_self_test( names[j], ALGORITHMS_DIR)==0) {
			printf( "ok\n");
		} else {
			x=1;
			printf( "\n");
		}
		printf( "Modes:\n");
			for (i=0;i<imax;i++) {
				td = mcrypt_module_open(names[j], ALGORITHMS_DIR, modes[i], MODES_DIR);
				td2 = mcrypt_module_open(names[j], ALGORITHMS_DIR, modes[i], MODES_DIR);
				if (td != MCRYPT_FAILED && td2 != MCRYPT_FAILED) {
					keysize = mcrypt_enc_get_key_size(td);
					key = calloc(1, keysize);
					if (key==NULL) exit(1);
					
					for (t=0;t<keysize;t++)
						key[t] = (t % 255) + 13;
					
					ivsize = mcrypt_enc_get_iv_size(td);
					if (ivsize>0) {
						IV = calloc( 1, ivsize);
						if (IV==NULL) exit(1);
						for (t=0;t<ivsize;t++)
							IV[t] = (t*2 % 255) + 15;
					}
					if (mcrypt_generic_init( td, key, keysize, IV) < 0) {
						fprintf(stderr, "Failed to Initialize algorithm!\n");
						return -1;
					}

					if (mcrypt_enc_is_block_mode(td)!=0)
						siz = (strlen(TEXT) / mcrypt_enc_get_block_size(td))*mcrypt_enc_get_block_size(td);
					else siz = strlen(TEXT);

					text = calloc( 1, siz);
					if (text==NULL) exit(1);
					
					memmove( text, TEXT, siz);

					mcrypt_generic( td, text, siz);

					if (mcrypt_generic_init( td2, key, keysize, IV) < 0) {
						fprintf(stderr, "Failed to Initialize algorithm!\n");
						return -1;
					}

					mdecrypt_generic( td2, text, siz);
					if ( memcmp( text, TEXT, siz) == 0) {
						printf( "   %s: ok\n", modes[i]);
					} else {
						printf( "   %s: failed\n", modes[i]);
						x=1;
					}
					mcrypt_generic_deinit(td);
					mcrypt_generic_deinit(td2);
					mcrypt_module_close(td);
					mcrypt_module_close(td2);					free(text);
					free(key);
					if (ivsize>0) free(IV);
				}
			}
		printf("\n");
		
	}	
	mcrypt_free_p(names, jmax);
	mcrypt_free_p(modes, imax);
	

	if (x>0) fprintf(stderr, "\nProbably some of the algorithms listed above failed. "
				"Try not to use these algorithms, and file a bug report to [email protected]\n\n");
	return x;
}
Example #18
0
void dump_testcase_block(MCRYPT td, unsigned char *key, int key_size,
                         unsigned char *iv, int iv_size, int data_size,
                         padding_t padding) {
    int mc_ret;
    int is_block, block_size, block_overlap, block_fill;
    int i;
    unsigned char *plaintext, *ciphertext;

    mc_ret = mcrypt_generic_init(td, (void *)key, key_size, (void *)iv);
    if (mc_ret < 0) {
        mcrypt_perror(mc_ret);
        return;
    }

    plaintext = gen_rand_data(data_size);
    if (plaintext == NULL) {
        return;
    }

    is_block = mcrypt_enc_is_block_mode(td);
    if (is_block) {
        block_size = mcrypt_enc_get_block_size(td);
        block_overlap = data_size % block_size;
        block_fill = block_size - block_overlap;
        if (padding == PADDING_NONE) {
            /* do nothing */
        }
        else if (padding == PADDING_PKCS7) {
            if (block_fill == 0) {
                /* ALWAYS add padding */
                block_fill = block_size;
            }
            plaintext = (unsigned char *)realloc(plaintext,
                                                 data_size + block_fill);
            for (i = 0; i < block_fill; i++) {
                plaintext[data_size+i] = block_fill;
            }
            data_size = data_size + block_fill;
            if ((data_size % block_size) != 0) {
                fprintf(stderr, "bad data size!\n");
                exit(1);
            }
        }
        else if (padding == PADDING_ZEROS) {
            if (block_overlap != 0) {
                plaintext = (unsigned char *)realloc(plaintext,
                                                     data_size + block_fill);
                for (i = 0; i < block_fill; i++) {
                    plaintext[data_size+i] = '\0';
                }
                data_size = data_size + block_fill;
            }
        }
        else {
            fprintf(stderr, "bad error\n");
            exit(1);
        }
    }

    ciphertext = malloc(data_size);
    if (ciphertext == NULL) {
        fprintf(stderr, "Out of memory\n");
        return;
    }

    memcpy( (void *)ciphertext, (void *)plaintext, data_size);

    mc_ret = mcrypt_generic(td, ciphertext, data_size);
    if (mc_ret == 0) {
        char *enc_key, *enc_iv, *enc_pt, *enc_ct;
        enc_key = dump_value( (void *)key, key_size );
        enc_iv  = dump_value( (void *)iv, iv_size );
        enc_pt  = dump_value( (void *)plaintext, data_size );
        enc_ct  = dump_value( (void *)ciphertext, data_size );

        printf("algo=%s,mode=%s,key=%s,iv=%s,padding=%s,pt=%s,ct=%s\n",
               testing_algo, testing_mode, enc_key, enc_iv,
               padding_name(padding), enc_pt, enc_ct);

        free(enc_key);
        free(enc_iv);
        free(enc_pt);
        free(enc_ct);
    }

    free(plaintext);
    free(ciphertext);

    mc_ret = mcrypt_generic_deinit(td);
    if (mc_ret < 0) {
        fprintf(stderr, "Error %d during deinit of %s in %s mode"
                " (%d-byte key)\n", testing_algo, testing_mode, key_size);
        return;
    }
}
Example #19
0
int main(int argc, char **argv)
{
	int dev,cnt,sock;
	unsigned char buf_frame[1536+sizeof(struct ether_header)];
    unsigned char *buf = buf_frame+sizeof(struct ether_header);
    struct ether_header *header = (struct ether_header*) buf_frame;
#ifndef __NetBSD__
	struct ifreq ifr;
#endif

    MCRYPT td;
    char *key; 
    int blocksize=0;
    int keysize = 32; /* 256 bits == 32 bytes */
    char enc_state[1024];
    int enc_state_size;
    char* tun_device = "/dev/net/tun";
    char* dev_name="tun%d";

    if(getenv("TUN_DEVICE")) { tun_device = getenv("TUN_DEVICE"); }
    if(getenv("DEV_NAME")) { dev_name = getenv("DEV_NAME"); }

    if(getenv("MCRYPT_KEYFILE")) {
        if (getenv("MCRYPT_KEYSIZE")) { keysize=atoi(getenv("MCRYPT_KEYSIZE"))/8; }
        key = calloc(1, keysize);
        FILE* keyf = fopen(getenv("MCRYPT_KEYFILE"), "r");
        if (!keyf) { perror("fopen keyfile"); return 10; }
        memset(key, 0, keysize);
        fread(key, 1, keysize, keyf);
        fclose(keyf);

        char* algo="twofish";
        char* mode="cbc";

        if (getenv("MCRYPT_ALGO")) { algo = getenv("MCRYPT_ALGO"); }
        if (getenv("MCRYPT_MODE")) { mode = getenv("MCRYPT_MODE"); }


        td = mcrypt_module_open(algo, NULL, mode, NULL);
        if (td==MCRYPT_FAILED) {
            fprintf(stderr, "mcrypt_module_open failed algo=%s mode=%s keysize=%d\n", algo, mode, keysize);
            return 11;
        }
        blocksize = mcrypt_enc_get_block_size(td);
        //block_buffer = malloc(blocksize);

        mcrypt_generic_init( td, key, keysize, NULL);

        enc_state_size = sizeof enc_state;
        mcrypt_enc_get_state(td, enc_state, &enc_state_size);
    }

	if(argc<=2) {
		fprintf(stderr,
                "Usage: tap_mcrypt plaintext_interface destination_mac_address\n"
                "Example: tap_mcrypt wlan0 ff:ff:ff:ff:ff:ff\n"
                "                (note that ff:ff:ff:ff:ff:ff may work bad in Wi-Fi)\n"
                "    Environment variables:\n"
                "    TUN_DEVICE  /dev/net/tun\n"
                "    DEV_NAME    name of the device, default tun%%d\n"
                "    SOURCE_MAC_ADDRESS -- by default use interface's one\n"
                "    \n"
                "    MCRYPT_KEYFILE  -- turn on encryption, read key from this file\n"
                "    MCRYPT_KEYSIZE  -- key size in bits, default 256\n"
                "    MCRYPT_ALGO     -- algorithm, default is twofish. aes256 is called rijndael-256\n"
                "    MCRYPT_MODE     -- mode, default is CBC\n"
                );
		exit(1);
	}

    char* interface = argv[1];
    char* dest_mac = argv[2];

	if((dev = open(tun_device, O_RDWR)) < 0) {
		fprintf(stderr,"open(%s) failed: %s\n", tun_device, strerror(errno));
		exit(2);
	}

    
#ifndef __NetBSD__
	memset(&ifr, 0, sizeof(ifr));
	ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
	strncpy(ifr.ifr_name, dev_name, IFNAMSIZ);
	if(ioctl(dev, TUNSETIFF, (void*) &ifr) < 0) {
		perror("ioctl(TUNSETIFF) failed");
		exit(3);
	}

    {
	    struct ifreq ifr_tun;
	    strncpy(ifr_tun.ifr_name, ifr.ifr_name, IFNAMSIZ);
        if((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)))==-1) {
            perror("socket() failed");
            exit(4);
        }
        
        if (ioctl(sock, SIOCGIFFLAGS, &ifr_tun) < 0) { perror("ioctl SIOCGIFFLAGS"); }
        ifr_tun.ifr_mtu=1408;
        if(ioctl(sock, SIOCSIFMTU, (void*) &ifr_tun) < 0) {
            perror("ioctl(SIOCSIFMTU) failed");
        }
        
        close(sock);
    } 
#endif
	

    if((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)))==-1) {
		perror("socket() failed");
		exit(4);
	}

    char source_mac[6];
    int card_index;
    struct sockaddr_ll device;

    memset(&device, 0, sizeof(device));
    init_MAC_addr(sock, interface, source_mac, &card_index);
    device.sll_ifindex=card_index;
    
    
    device.sll_family = AF_PACKET;
    memcpy(device.sll_addr, source_mac, 6);
    device.sll_halen = htons(6);
    
    parseMac(dest_mac, header->ether_dhost);
    memcpy(header->ether_shost, source_mac, 6);
    header->ether_type = htons(0x08F4);  
		

	if(fork())
		while(1) {
			cnt=read(dev,(void*)buf,1518);
            //printpacket("sent", buf, cnt);
            if (blocksize) {
                cnt = ((cnt-1)/blocksize+1)*blocksize; // pad to block size
                mcrypt_generic (td, buf, cnt);
                mcrypt_enc_set_state (td, enc_state, enc_state_size);
            }
            //printpacket("encr", buf, cnt);
			sendto(sock, buf_frame, cnt+sizeof(struct ether_header),0,(struct sockaddr *)&device, sizeof device);
		}
	else
		while(1) {
            size_t size = sizeof device;
			cnt=recvfrom(sock,buf_frame,1536,0,(struct sockaddr *)&device,&size);
            if(device.sll_ifindex != card_index) {
                continue; /* Not our interface */
            }
            if(header->ether_type != htons(0x08F4)) {
                continue; /* Not our protocol type */
            }
            cnt-=sizeof(struct ether_header);
            //printpacket("recv", buf, cnt);
            if (blocksize) {
                cnt = ((cnt-1)/blocksize+1)*blocksize; // pad to block size
                mdecrypt_generic (td, buf, cnt);
                mcrypt_enc_set_state (td, enc_state, enc_state_size);
            }
            //printpacket("decr", buf, cnt);
            write(dev,(void*)buf,cnt);
		}

    if (blocksize) {
        mcrypt_generic_deinit (td);
        mcrypt_module_close(td);
    }
}
static int check_auth_cookie(request_rec *r)
{

	const char *cookies = NULL, *auth_line = NULL;
	char *cookie = NULL;

    /* Debug. */
	/*ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
	    "check_auth_cookie called");*/

	/* Get config for this directory. */
    cookie_auth_config_rec *conf = ap_get_module_config(r->per_dir_config,
		&auth_cookie_module);

    /* Check we have been configured. */
    if (!conf->cookie_auth_cookie) {
        return DECLINED;
    }

	/* Do not override real auth header, unless config instructs us to. */
	if (!conf->cookie_auth_override &&
		apr_table_get(r->headers_in, "Authorization")) {
		if (conf->cookie_auth_env) {
			unsetenv(conf->cookie_auth_env);
		}
		return DECLINED;
	}

	/* todo: protect against xxxCookieNamexxx, regex? */
	/* todo: make case insensitive? */
	/* Get the cookie (code from mod_log_config). */
	if ((cookies = apr_table_get(r->headers_in, "Cookie"))) {
		char *start_cookie, *end_cookie;
		if ((start_cookie = ap_strstr_c(cookies, conf->cookie_auth_cookie))) {
		    start_cookie += strlen(conf->cookie_auth_cookie) + 1;
		    cookie = apr_pstrdup(r->pool, start_cookie);
			/* kill everything in cookie after ';' */
			end_cookie = strchr(cookie, ';');
			if (end_cookie) {
				*end_cookie = '\0';
      }
      ap_unescape_url(cookie);
		}
	}

	/* No cookie? Nothing for us to do. */
	if (!cookie) {
                if (conf->cookie_auth_unauth_redirect) {
        	        const char* redirect = conf->cookie_auth_unauth_redirect;
        		compose_and_set_redirect(r, redirect);
                        return HTTP_MOVED_TEMPORARILY;
                }
                else {
			return DECLINED;
                }
	}

	/* Debug. */
	/*ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
	    "%s=%s", conf->cookie_auth_cookie, cookie);*/

	char* aux_auth_info = "";

	/* Construct the fake auth_line. */
	if (conf->cookie_auth_base64) {
		char* decoded_cookie = apr_palloc(r->pool, apr_base64_decode_len(cookie));
    int decoded_cookie_length = apr_base64_decode(decoded_cookie, cookie);

		int valid = 1;

		/* if the cookie is encrypted, decrypt it in place */
		if (conf->cookie_auth_encrypt) {
      MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
      int keysize = strlen(conf->cookie_auth_encrypt);
      int blocksize = mcrypt_enc_get_block_size(td);

			// We will copy the iv from the beginning of the cookie.
			// The iv does not need to be null-terminated, but we will
			// null-terminate it for convenience.
			int iv_length = mcrypt_enc_get_iv_size(td);
			char* iv = (char*) apr_palloc(r->pool, iv_length + 1);
			memcpy(iv, decoded_cookie, iv_length);
			iv[iv_length] = '\0';

			// Take the iv off the beginning of the cookie
			decoded_cookie += iv_length;
      decoded_cookie_length -= iv_length;

      mcrypt_generic_init( td, conf->cookie_auth_encrypt, keysize, iv);
      // Encryption in CBC is performed in blocks, so our
      // decryption string will always be an integral number
      // of full blocks.
      char* decrypt_ptr = decoded_cookie;
      while (decoded_cookie_length >= blocksize) {
        mdecrypt_generic(td, decrypt_ptr, blocksize);
        decrypt_ptr += blocksize;
        decoded_cookie_length -= blocksize;
      }
      if (decoded_cookie_length != 0) {
        valid = 0;
      }
      mcrypt_generic_deinit (td);
      mcrypt_module_close(td);
      /*ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
        "mdecrypt(%s)=%s", conf->cookie_auth_cookie, decoded_cookie);*/
		}

		/* if the cookie did not decrypt, then do nothing */
		if (valid) {
			char* end_auth_info = strchr(decoded_cookie, '\t');

			if (end_auth_info) {
				aux_auth_info = decoded_cookie;
				char* unencoded_cookie = end_auth_info + 1;
				*end_auth_info = 0;

				auth_line = apr_pstrcat(r->pool, "Basic ", ap_pbase64encode(r->pool, unencoded_cookie), NULL);
			}
			else {
				auth_line = apr_pstrcat(r->pool, "Basic ", ap_pbase64encode(r->pool, decoded_cookie), NULL);
			}
		}
	} else {
		// Aux auth info and cookie encrypt features only available in base64 mode
		ap_unescape_url(cookie);
		auth_line = apr_pstrcat(r->pool, "Basic ",
			ap_pbase64encode(r->pool, cookie), NULL);
	}

	/* If there is aux auth info, then set the env variable */
	if (conf->cookie_auth_env) {
	  apr_table_set(r->subprocess_env, conf->cookie_auth_env, aux_auth_info);
	}

	/* Debug. */
	/*ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
		"Authorization: %s", auth_line);*/

	/* If there is no aux auth info, then force a redirect if our conf directives say that we should */
        if (conf->cookie_auth_env_redirect && !strlen(aux_auth_info)) {
        	const char* redirect = conf->cookie_auth_env_redirect;
        	compose_and_set_redirect(r, redirect);
                return HTTP_MOVED_TEMPORARILY;
        }
        else {
	        /* Set fake auth_line. */
		if (auth_line) {
                	apr_table_set(r->headers_in, "Authorization", auth_line);
                }
	}

	/* Always return DECLINED because we don't authorize, */
	/* we just set things up for the next auth module to. */
    return DECLINED;
}
Example #21
0
static void cached_cipher_deinit(struct cached_cipher * cipher)
{
    if(cipher->valid)
        mcrypt_generic_deinit(cipher->cipher);
    mcrypt_module_close(cipher->cipher);
}
Example #22
0
void emokit_deinit(emokit_device* s) {
	mcrypt_generic_deinit (s->td);
	mcrypt_module_close(s->td);
}
Example #23
0
int main()
{
	MCRYPT td, td2;
	int i, t, imax;
	int j, jmax, ivsize;
	int x = 0, siz;
	char *text;
	unsigned char *IV;
	unsigned char *key;
	int keysize;
	
    td = mcrypt_module_open("idea", ALGORITHMS_DIR, "cbc", MODES_DIR);
    td2 = mcrypt_module_open("idea", ALGORITHMS_DIR, "cbc", MODES_DIR);

    if (td != MCRYPT_FAILED && td2 != MCRYPT_FAILED) {
        fprintf(stderr, "Created IDEA cipher.\n");

        keysize = mcrypt_enc_get_key_size(td);
        fprintf(stderr, "Cipher key size %d.\n", keysize);

        key = calloc(1, keysize);
        if (key==NULL) exit(1);
					
        for (t=0;t<keysize;t++)
            key[t] = (t % 255) + 13;
					
        ivsize = mcrypt_enc_get_iv_size(td);
        fprintf(stderr, "IV size %d.\n", ivsize);
        if (ivsize>0) {
            IV = calloc( 1, ivsize);
            if (IV==NULL) exit(1);
            for (t=0;t<ivsize;t++)
                IV[t] = (t*2 % 255) + 15;
        }

        if (mcrypt_generic_init( td, key, keysize, IV) < 0) {
            fprintf(stderr, "Failed to Initialize algorithm!\n");
            return -1;
        }

        if (mcrypt_enc_is_block_mode(td)!=0)
            siz = (strlen(TEXT) / mcrypt_enc_get_block_size(td))*mcrypt_enc_get_block_size(td);
        else siz = strlen(TEXT);

        text = calloc( 1, siz);
        if (text==NULL) exit(1);
					
        memmove( text, TEXT, siz);

        mcrypt_generic( td, text, siz);

        if (mcrypt_generic_init( td2, key, keysize, IV) < 0) {
            fprintf(stderr, "Failed to Initialize algorithm!\n");
            return -1;
        }

        mdecrypt_generic( td2, text, siz);
        if ( memcmp( text, TEXT, siz) == 0) {
            printf( "   %s: ok\n", "cbc");
        } else {
            printf( "   %s: failed\n", "cbc");
            x=1;
        }

        mcrypt_generic_deinit(td);
        mcrypt_generic_deinit(td2);
        mcrypt_module_close(td);
        mcrypt_module_close(td2);
        free(text);
        free(key);
        if (ivsize>0) free(IV);
    }

    return 0;
}
Example #24
0
int epoc_deinit() {
	mcrypt_generic_deinit (td);
	mcrypt_module_close(td);
	return 0;
}