<commit_before>/*
 * Copyright (c) 2012 <NAME>
 */


#include "ngx_rtmp.h"

#include <openssl/hmac.h>
#include <openssl/sha.h>


static void ngx_rtmp_handshake_send(ngx_event_t *wev);
static void ngx_rtmp_handshake_recv(ngx_event_t *rev);
static void ngx_rtmp_handshake_done(ngx_rtmp_session_t *s);


/* Handshake keys */
static u_char 
ngx_rtmp_server_key[] = {
    'G', 'e', 'n', 'u', 'i', 'n', 'e', ' ', 'A', 'd', 'o', 'b', 'e', ' ',
    'F', 'l', 'a', 's', 'h', ' ', 'M', 'e', 'd', 'i', 'a', ' ',
    'S', 'e', 'r', 'v', 'e', 'r', ' ', 
    '0', '0', '1',

    0xF0, 0xEE, 0xC2, 0x4A, 0x80, 0x68, 0xBE, 0xE8, 0x2E, 0x00, 0xD0, 0xD1, 
    0x02, 0x9E, 0x7E, 0x57, 0x6E, 0xEC, 0x5D, 0x2D, 0x29, 0x80, 0x6F, 0xAB, 
    0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE
};


static u_char 
ngx_rtmp_client_key[] = {
    'G', 'e', 'n', 'u', 'i', 'n', 'e', ' ', 'A', 'd', 'o', 'b', 'e', ' ',
    'F', 'l', 'a', 's', 'h', ' ', 'P', 'l', 'a', 'y', 'e', 'r', ' ', 
    '0', '0', '1',

    0xF0, 0xEE, 0xC2, 0x4A, 0x80, 0x68, 0xBE, 0xE8, 0x2E, 0x00, 0xD0, 0xD1, 
    0x02, 0x9E, 0x7E, 0x57, 0x6E, 0xEC, 0x5D, 0x2D, 0x29, 0x80, 0x6F, 0xAB, 
    0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE
};


static const u_char
ngx_rtmp_server_version[4] = {
    0x0D, 0x0E, 0x0A, 0x0D
};


#define NGX_RTMP_KEYLEN                 SHA256_DIGEST_LENGTH 
#define NGX_RTMP_HANDSHAKE_BUFSIZE      1537


static ngx_str_t            ngx_rtmp_server_full_key 
    = { sizeof(ngx_rtmp_server_key), ngx_rtmp_server_key };
static ngx_str_t            ngx_rtmp_server_partial_key
    = { 36, ngx_rtmp_server_key };

static ngx_str_t            ngx_rtmp_client_partial_key
    = { 30, ngx_rtmp_client_key };


static ngx_int_t
ngx_rtmp_make_digest(ngx_str_t *key, ngx_buf_t *src, 
        u_char *skip, u_char *dst, ngx_log_t *log)
{
    HMAC_CTX                hmac;
    unsigned int            len;
    ngx_int_t               rc;

    rc = NGX_ERROR;
    HMAC_CTX_init(&hmac);

    if (HMAC_Init_ex(&hmac, key->data, key->len, 
                EVP_sha256(), NULL) == 0) 
    {
        ngx_log_error(NGX_LOG_INFO, log, 0, "HMAC_Init_ex error");
        goto out;
    }

    if (skip && src->pos <= skip && skip <= src->last) {
        if (skip != src->pos
                && HMAC_Update(&hmac, src->pos, skip - src->pos) == 0) 
        {
            ngx_log_error(NGX_LOG_INFO, log, 0, "HMAC_Update error");
            goto out;
        }
        if (src->last != skip + NGX_RTMP_KEYLEN
                && HMAC_Update(&hmac, skip + NGX_RTMP_KEYLEN, 
                    src->last - skip - NGX_RTMP_KEYLEN) == 0) 
        {
            ngx_log_error(NGX_LOG_INFO, log, 0, "HMAC_Update error");
            goto out;
        }
    } else if (HMAC_Update(&hmac, src->pos, src->last - src->pos) == 0) {
        ngx_log_error(NGX_LOG_INFO, log, 0, "HMAC_Update error");
        goto out;
    }

    if (HMAC_Final(&hmac, dst, &len) == 0) {
        ngx_log_error(NGX_LOG_INFO, log, 0, "HMAC_Final error");
        goto out;
    }

    rc = NGX_OK;

out:
    HMAC_CTX_cleanup(&hmac);

    return rc;
}


static ngx_int_t
ngx_rtmp_find_digest(ngx_buf_t *b, size_t base, ngx_log_t *log)
{
    size_t                  n, offs;
    u_char                  digest[NGX_RTMP_KEYLEN];
    u_char                 *p;

    offs = 0;
    for (n = 0; n < 4; ++n) {
        offs += b->pos[base + n];
    }
    offs = (offs % 728) + base + 4;
    p = b->pos + offs;

    if (ngx_rtmp_make_digest(&ngx_rtmp_client_partial_key, b,
                p, digest, log) != NGX_OK)
    {
        return NGX_ERROR;
    }

    if (ngx_memcmp(digest, p, NGX_RTMP_KEYLEN) == 0) {
        return offs;
    }
<commit_msg>removed error checks from HMAC code because some library versions do not provide return code<commit_after>/*
 * Copyright (c) 2012 <NAME>
 */


#include "ngx_rtmp.h"

#include <openssl/hmac.h>
#include <openssl/sha.h>


static void ngx_rtmp_handshake_send(ngx_event_t *wev);
static void ngx_rtmp_handshake_recv(ngx_event_t *rev);
static void ngx_rtmp_handshake_done(ngx_rtmp_session_t *s);


/* Handshake keys */
static u_char 
ngx_rtmp_server_key[] = {
    'G', 'e', 'n', 'u', 'i', 'n', 'e', ' ', 'A', 'd', 'o', 'b', 'e', ' ',
    'F', 'l', 'a', 's', 'h', ' ', 'M', 'e', 'd', 'i', 'a', ' ',
    'S', 'e', 'r', 'v', 'e', 'r', ' ', 
    '0', '0', '1',

    0xF0, 0xEE, 0xC2, 0x4A, 0x80, 0x68, 0xBE, 0xE8, 0x2E, 0x00, 0xD0, 0xD1, 
    0x02, 0x9E, 0x7E, 0x57, 0x6E, 0xEC, 0x5D, 0x2D, 0x29, 0x80, 0x6F, 0xAB, 
    0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE
};


static u_char 
ngx_rtmp_client_key[] = {
    'G', 'e', 'n', 'u', 'i', 'n', 'e', ' ', 'A', 'd', 'o', 'b', 'e', ' ',
    'F', 'l', 'a', 's', 'h', ' ', 'P', 'l', 'a', 'y', 'e', 'r', ' ', 
    '0', '0', '1',

    0xF0, 0xEE, 0xC2, 0x4A, 0x80, 0x68, 0xBE, 0xE8, 0x2E, 0x00, 0xD0, 0xD1, 
    0x02, 0x9E, 0x7E, 0x57, 0x6E, 0xEC, 0x5D, 0x2D, 0x29, 0x80, 0x6F, 0xAB, 
    0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE
};


static const u_char
ngx_rtmp_server_version[4] = {
    0x0D, 0x0E, 0x0A, 0x0D
};


#define NGX_RTMP_KEYLEN                 SHA256_DIGEST_LENGTH 
#define NGX_RTMP_HANDSHAKE_BUFSIZE      1537


static ngx_str_t            ngx_rtmp_server_full_key 
    = { sizeof(ngx_rtmp_server_key), ngx_rtmp_server_key };
static ngx_str_t            ngx_rtmp_server_partial_key
    = { 36, ngx_rtmp_server_key };

static ngx_str_t            ngx_rtmp_client_partial_key
    = { 30, ngx_rtmp_client_key };


static ngx_int_t
ngx_rtmp_make_digest(ngx_str_t *key, ngx_buf_t *src, 
        u_char *skip, u_char *dst, ngx_log_t *log)
{
    HMAC_CTX                hmac;
    unsigned int            len;

    HMAC_CTX_init(&hmac);
    HMAC_Init_ex(&hmac, key->data, key->len, EVP_sha256(), NULL);

    if (skip && src->pos <= skip && skip <= src->last) {
        if (skip != src->pos) {
            HMAC_Update(&hmac, src->pos, skip - src->pos);
        }
        if (src->last != skip + NGX_RTMP_KEYLEN) {
            HMAC_Update(&hmac, skip + NGX_RTMP_KEYLEN, 
                    src->last - skip - NGX_RTMP_KEYLEN);
        }
    } else {
        HMAC_Update(&hmac, src->pos, src->last - src->pos);
    }

    HMAC_Final(&hmac, dst, &len);
    HMAC_CTX_cleanup(&hmac);

    return NGX_OK;
}


static ngx_int_t
ngx_rtmp_find_digest(ngx_buf_t *b, size_t base, ngx_log_t *log)
{
    size_t                  n, offs;
    u_char                  digest[NGX_RTMP_KEYLEN];
    u_char                 *p;

    offs = 0;
    for (n = 0; n < 4; ++n) {
        offs += b->pos[base + n];
    }
    offs = (offs % 728) + base + 4;
    p = b->pos + offs;

    if (ngx_rtmp_make_digest(&ngx_rtmp_client_partial_key, b,
                p, digest, log) != NGX_OK)
    {
        return NGX_ERROR;
    }

    if (ngx_memcmp(digest, p, NGX_RTMP_KEYLEN) == 0) {
        return offs;
    }

{'fraction_non_alphanumeric': 0.1660522046895738, 'fraction_numerical': 0.053089514820822886, 'mean_word_length': 2.4339240506329114, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5790913', 'n_tokens_mistral': 3385, 'n_tokens_neox': 3059, 'n_words': 640}