zhangzengfei
2022-07-20 c90c3e794bdd95127d0c34ff1d9e8759d18a0445
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/******************************************************************************
* FILE:    MD5Calc.h
* Description:
*    MD5 arithmetic calculation interface class.
*
* Modified Code History
* Mark  Date        By          Modification Reason
*******************************************************************************
* 01    2007-6-6    Song-Xiaowen Initial creation.
******************************************************************************/
 
#if !defined(__MD5_CALC_H__)
#define __MD5_CALC_H__
 
#include "TypeDef.h"
//#include "osipparser2/osip_md5.h"
#include "osip_md5.h"
#include "str.h"
 
using namespace std;
 
#define DIGEST_MD5HEX_LEN           32
#define RANDOM_TAG_LEN              16
#define DIGEST_NONCE_LEN            64
#define MD5_SESSION_KEY_LEN         64
#define MD5_RESPONSE_LEN            64
#define DIGEST_HASH_LEN             16
#define SIP_PRIVATE_KEY             "private key"
#define SIP_REGISTER_NONCE_EXPIRE   6 //6 minute
#define SIP_MAX_CHALLENGES          3 //if failed challenges > 3,means failure
#define SIP_MD5_URL_MAXLEN            128
 
 
/**
 * Definition of the Authorization header.
 * @struct osip_authorization
 */
struct rtsp_authorization_t
{
    char auth_type[32];        /**< Authentication Type (Basic or Digest) */
    char username[64];        /**< login */
    char realm[128];        /**< realm (as a quoted-string) */
    char nonce[64];        /**< nonce */
    char uri[128];          /**< uri */
    char response[64];        /**< response */
    char digest[64];        /**< digest */
    char algorithm[64];        /**< algorithm (optionnal) */
    char cnonce[64];        /**< cnonce (optionnal) */
    char opaque[64];        /**< opaque (optionnal) */
    char message_qop[64];        /**< message_qop (optionnal) */
    char nonce_count[64];        /**< nonce_count (optionnal) */
    char auth_param[64];        /**< other parameters (optionnal) */
};
 
/* Class for MD5 calculation */
class CMD5Calc
{
public:
    CMD5Calc();
    ~CMD5Calc();
    
    inline bool MD5Encode(const char *pSource, char target[DIGEST_MD5HEX_LEN+1]);
    void SIPGenerateNonce(char *pTarget, UINT32 timestamp, const char *pETag);
    inline bool SIPCheckNonceExpire(const char *pszNonce, const char *pETag);
    void DigestCalcHA1
    ( 
        char *pszAlg,
        char *pszUserName,
        char *pszRealm,
        char *pszPassword,
        char *pszNonce,
        char *pszCNonce,
        char SessionKey[DIGEST_MD5HEX_LEN+1]
    );
    void DigestCalcResponse
    ( 
        char HA1[DIGEST_MD5HEX_LEN+1],          /* H(A1) */
        char *pszNonce,                         /* nonce from server */
        char *pszNonceCount,                    /* 8 hex digits */
        char *pszCNonce,                        /* client nonce */
        char *pszQop,                           /* qop-value: "", "auth", "auth-int" */
        char *pszMethod,                        /* method from the request */
        char *pszDigestUri,                     /* requested URL */
        char HEntity[DIGEST_MD5HEX_LEN+1],      /* H(entity body) if qop="auth-int" */
        char Response[DIGEST_MD5HEX_LEN+1]      /* request-digest or response-digest */
    );
    
    //Random character string operations
    inline UINT32 GenerateRandom(UINT32 i1, UINT32 i2);
    char *GenerateRandomTag(char *pStr);
    
    char *unquote(char* s);
};
 
/* Reference md5 functions in libosip2 */
typedef osip_MD5_CTX  MD5_CTX;
#define MD5Init   osip_MD5Init
#define MD5Update osip_MD5Update
#define MD5Final  osip_MD5Final
 
#endif  //#if !defined(__MD5_CALC_H__)