re-arranging files for pcu control
[monitor.git] / pcucontrol / models / intelamt / digcalc.cpp
1 //-------------------------------------------------------------------
2 //   Copyright (C) The Internet Society (1999).  All Rights Reserved.
3 //
4 //   This document and translations of it may be copied and furnished to
5 //   others, and derivative works that comment on or otherwise explain it
6 //   or assist in its implementation may be prepared, copied, published
7 //   and distributed, in whole or in part, without restriction of any
8 //   kind, provided that the above copyright notice and this paragraph are
9 //   included on all such copies and derivative works.  However, this
10 //   document itself may not be modified in any way, such as by removing
11 //   the copyright notice or references to the Internet Society or other
12 //   Internet organizations, except as needed for the purpose of
13 //   developing Internet standards in which case the procedures for
14 //   copyrights defined in the Internet Standards process must be
15 //   followed, or as required to translate it into languages other than
16 //   English.
17 //
18 //   The limited permissions granted above are perpetual and will not be
19 //   revoked by the Internet Society or its successors or assigns.
20 //
21 //   This document and the information contained herein is provided on an
22 //   "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
23 //   TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
24 //   BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
25 //   HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
26 //   MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
27 //   
28 //   
29 //   Modifiyed by Intel Corporation, 2005
30 //-------------------------------------------------------------------------
31
32 #include <stddef.h>
33 #include <openssl/md5.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include "digcalc.h"
37
38 int stricmp(const char *b, const char *a)
39 {
40     while (*a != '\0' && *b != '\0' && tolower(*a) == tolower(*b))
41     {   a++;
42         b++;
43     }
44     return *a == *b ? 0 :
45            tolower(*a) < tolower(*b) ? -1 : 1;
46 }
47
48 void CvtHex(
49     HASH Bin,
50     HASHHEX Hex
51     )
52 {
53     unsigned short i;
54     unsigned char j;
55
56     for (i = 0; i < HASHLEN; i++) {
57         j = (Bin[i] >> 4) & 0xf;
58         if (j <= 9)
59             Hex[i*2] = (j + '0');
60          else
61             Hex[i*2] = (j + 'a' - 10);
62         j = Bin[i] & 0xf;
63         if (j <= 9)
64             Hex[i*2+1] = (j + '0');
65          else
66             Hex[i*2+1] = (j + 'a' - 10);
67     };
68     Hex[HASHHEXLEN] = '\0';
69 };
70
71 /* calculate H(A1) as per spec */
72 void DigestCalcHA1(
73     char * pszAlg,
74     char * pszUserName,
75     char * pszRealm,
76     char * pszPassword,
77     char * pszNonce,
78     char * pszCNonce,
79     HASHHEX SessionKey
80     )
81 {
82       MD5_CTX Md5Ctx;
83       HASH HA1;
84       HASHHEX HA1HEX;
85
86       MD5_Init(&Md5Ctx);
87       MD5_Update(&Md5Ctx, pszUserName, strlen(pszUserName));
88       MD5_Update(&Md5Ctx, ":", 1);
89       MD5_Update(&Md5Ctx, pszRealm, strlen(pszRealm));
90       MD5_Update(&Md5Ctx, ":", 1);
91       MD5_Update(&Md5Ctx, pszPassword, strlen(pszPassword));
92       MD5_Final(HA1, &Md5Ctx);
93       if (stricmp(pszAlg, "md5-sess") == 0) {
94             CvtHex(HA1, HA1HEX); 
95             MD5_Init(&Md5Ctx);
96             MD5_Update(&Md5Ctx, HA1HEX, HASHHEXLEN);
97             MD5_Update(&Md5Ctx, ":", 1);
98             MD5_Update(&Md5Ctx, pszNonce, strlen(pszNonce));
99             MD5_Update(&Md5Ctx, ":", 1);
100             MD5_Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
101             MD5_Final(HA1, &Md5Ctx);
102       };
103       CvtHex(HA1, SessionKey);
104 };
105
106 /* calculate request-digest/response-digest as per HTTP Digest spec */
107 void DigestCalcResponse(
108     HASHHEX HA1,           /* H(A1) */
109     char * pszNonce,       /* nonce from server */
110     char * pszNonceCount,  /* 8 hex digits */
111     char * pszCNonce,      /* client nonce */
112     char * pszQop,         /* qop-value: "", "auth", "auth-int" */
113     char * pszMethod,      /* method from the request */
114     char * pszDigestUri,   /* requested URL */
115     HASHHEX HEntity,       /* H(entity body) if qop="auth-int" */
116     HASHHEX Response      /* request-digest or response-digest */
117     )
118 {
119       MD5_CTX Md5Ctx;
120       HASH HA2;
121       HASH RespHash;
122       HASHHEX HA2Hex;
123
124       // calculate H(A2)
125       MD5_Init(&Md5Ctx);
126       MD5_Update(&Md5Ctx, pszMethod, strlen(pszMethod));
127       MD5_Update(&Md5Ctx, ":", 1);
128       MD5_Update(&Md5Ctx, pszDigestUri, strlen(pszDigestUri));
129       if (stricmp(pszQop, "auth-int") == 0) {
130             MD5_Update(&Md5Ctx, ":", 1);
131             MD5_Update(&Md5Ctx, HEntity, HASHHEXLEN);
132       };
133       MD5_Final(HA2, &Md5Ctx);
134       CvtHex(HA2, HA2Hex);
135
136       // calculate response
137       MD5_Init(&Md5Ctx);
138       MD5_Update(&Md5Ctx, HA1, HASHHEXLEN);
139       MD5_Update(&Md5Ctx, ":", 1);
140       MD5_Update(&Md5Ctx, pszNonce, strlen(pszNonce));
141       MD5_Update(&Md5Ctx, ":", 1);
142       if (*pszQop) {
143           MD5_Update(&Md5Ctx, pszNonceCount, strlen(pszNonceCount));
144           MD5_Update(&Md5Ctx, ":", 1);
145           MD5_Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
146           MD5_Update(&Md5Ctx, ":", 1);
147           MD5_Update(&Md5Ctx, pszQop, strlen(pszQop));
148           MD5_Update(&Md5Ctx, ":", 1);
149       };
150       MD5_Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
151       MD5_Final(RespHash, &Md5Ctx);
152       CvtHex(RespHash, Response);
153 };