Beispiel #1
0
/* Zlib test */
int main(int argc, char **argv)
{
    unsigned long int ret, srcsize, dstsize = 2010;
    char dst[2048];
    char dst2[2048];

    memset(dst, 0, 2048);
    memset(dst2, 0, 2048);

    if (argc < 2) {
        printf("%s: string\n", argv[0]);
        exit(1);
    }

    srcsize = strlen(argv[1]);
    if (srcsize > 2000) {
        printf("%s: string too large\n", argv[0]);
        exit(1);

    }

    if ((ret = os_zlib_compress(argv[1], dst, srcsize, dstsize))) {
        printf("Compressed, from %lu->%lu\n", srcsize, ret);
    } else {
        printf("FAILED compressing.\n");
        exit(1);
    }

    /* Set new srcsize for decompression */
    srcsize = ret;

    if ((ret = os_zlib_uncompress(dst, dst2, srcsize, dstsize))) {
        printf("Uncompressed ok. String: '%s', size %lu->%lu\n",
               dst2, srcsize, ret);
    } else {
        printf("FAILED uncompressing.\n");
        exit(1);
    }

    return (0);
}
Beispiel #2
0
/* Creat a encrypted message.
 * Returns the size of it
 */
size_t CreateSecMSG(const keystore *keys, const char *msg, char *msg_encrypted, int id)
{
    unsigned int bfsize;
    size_t msg_size;
    unsigned long int cmp_size;

    u_int16_t rand1;

    char _tmpmsg[OS_MAXSTR + 2];
    char _finmsg[OS_MAXSTR + 2];

    os_md5 md5sum;

    msg_size = strlen(msg);


    /* Checking for invalid msg sizes */
    if((msg_size > (OS_MAXSTR - OS_HEADER_SIZE))||(msg_size < 1))
    {
        merror(ENCSIZE_ERROR, __local_name, msg);
        return(0);
    }

    /* Random number */
    rand1 = (u_int16_t)random();


    _tmpmsg[OS_MAXSTR +1] = '\0';
    _finmsg[OS_MAXSTR +1] = '\0';
    msg_encrypted[OS_MAXSTR] = '\0';


    /* Increasing local and global counters */
    if(local_count >= 9997)
    {
        local_count = 0;
        global_count++;
    }
    local_count++;


    snprintf(_tmpmsg, OS_MAXSTR,"%05hu%010u:%04u:%s",
                              rand1, global_count, local_count,
                              msg);


    /* Generating md5sum of the unencrypted string */
    OS_MD5_Str(_tmpmsg, md5sum);



    /* Generating final msg to be compressed */
    snprintf(_finmsg, OS_MAXSTR,"%s%s",md5sum,_tmpmsg);
    msg_size = strlen(_finmsg);


    /* Compressing message.
     * We assing the first 8 bytes for padding.
     */
    cmp_size = os_zlib_compress(_finmsg, _tmpmsg + 8, msg_size, OS_MAXSTR - 12);
    if(!cmp_size)
    {
        merror(COMPRESS_ERR, __local_name, _finmsg);
        return(0);
    }
    cmp_size++;

    /* Padding the message (needs to be div by 8) */
    bfsize = 8 - (cmp_size % 8);
    if(bfsize == 8)
        bfsize = 0;

    _tmpmsg[0] = '!';
    _tmpmsg[1] = '!';
    _tmpmsg[2] = '!';
    _tmpmsg[3] = '!';
    _tmpmsg[4] = '!';
    _tmpmsg[5] = '!';
    _tmpmsg[6] = '!';
    _tmpmsg[7] = '!';

    cmp_size+=bfsize;


    /* Getting average sizes */
    c_orig_size+= msg_size;
    c_comp_size+= cmp_size;
    if(evt_count > _s_comp_print)
    {
        verbose("%s: INFO: Event count after '%u': %u->%u (%d%%)", __local_name,
                    evt_count,
                    c_orig_size,
                    c_comp_size,
                    (c_comp_size * 100)/c_orig_size);
        evt_count = 0;
        c_orig_size = 0;
        c_comp_size = 0;
    }
    evt_count++;

    /* If the ip is dynamic (not single host, append agent id
     * to the message.
     */
    if(!isSingleHost(keys->keyentries[id]->ip) && isAgent)
    {
        snprintf(msg_encrypted, 16, "!%s!:", keys->keyentries[id]->id);
        msg_size = strlen(msg_encrypted);
    }
    else
    {
        /* Setting beginning of the message */
        msg_encrypted[0] = ':';
        msg_size = 1;
    }


    /* msg_size is the ammount of non-encrypted message
     * appended to the buffer. On dynamic ips, it will
     * include the agent id.
     */

    /* Encrypting everything */
    OS_BF_Str(_tmpmsg + (7 - bfsize), msg_encrypted + msg_size,
                                      keys->keyentries[id]->key,
                                      (long) cmp_size,
                                      OS_ENCRYPT);


    /* Storing before leaving */
    StoreSenderCounter(keys, global_count, local_count);

    return(cmp_size + msg_size);
}