Stun Server  Compliant with the latest RFCs including 5389, 5769, and 5780
discover the local host's own external IP address
testintegrity.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2011 John Selbie
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #include "commonincludes.hpp"
18 #include "stuncore.h"
19 #include "testintegrity.h"
20 
21 
22 // This test validates that the construction and parsing of the message integrity attribute in a stun message works as expected
23 // The test also validates both short term and long term credential modes with or without the presence of a fingerprint attribute
24 HRESULT CTestIntegrity::TestMessageIntegrity(bool fWithFingerprint, bool fLongCredentials)
25 {
26  HRESULT hr = S_OK;
27 
28  const char* pszUserName = "username";
29  const char* pszRealm = "stunrealm";
30  const char* pszPassword = "ThePassword";
31 
32  CStunMessageBuilder builder;
33  CStunMessageReader reader;
34  uint8_t *pMsg = NULL;
35  size_t sizeMsg = 0;
37  CRefCountedBuffer spBuffer;
38 
39  builder.AddBindingRequestHeader();
40  builder.AddRandomTransactionId(NULL);
41  builder.AddUserName(pszUserName);
42  builder.AddRealm(pszRealm);
43 
44 
45  if (fLongCredentials == false)
46  {
47  Chk(builder.AddMessageIntegrityShortTerm(pszPassword));
48  }
49  else
50  {
51  Chk(builder.AddMessageIntegrityLongTerm(pszUserName, pszRealm, pszPassword));
52  }
53 
54  if (fWithFingerprint)
55  {
56  builder.AddFingerprintAttribute();
57  }
58 
59  Chk(builder.GetResult(&spBuffer));
60 
61  pMsg = spBuffer->GetData();
62  sizeMsg = spBuffer->GetSize();
63 
64  state = reader.AddBytes(pMsg, sizeMsg);
65 
67 
68  ChkIfA(reader.HasMessageIntegrityAttribute()==false, E_FAIL);
69 
70  if (fLongCredentials == false)
71  {
72  ChkA(reader.ValidateMessageIntegrityShort(pszPassword));
73  }
74  else
75  {
76  ChkA(reader.ValidateMessageIntegrityLong(pszUserName, pszRealm, pszPassword));
77  }
78 
79 Cleanup:
80  return hr;
81 }
82 
83 
85 {
86  HRESULT hr = S_OK;
87 
88  // CTestReader contains a test that will the fingerprint and integrity
89  // of the message in RFC 5769 section 2.1 (short-term auth)
90 
91  // This test is a validation of section 2.4 (long term auth with integrity and fingerprint)
92 
93  const unsigned char c_requestbytes[] =
94  "\x00\x01\x00\x60" // Request type and message length
95  "\x21\x12\xa4\x42" // Magic cookie
96  "\x78\xad\x34\x33" // }
97  "\xc6\xad\x72\xc0" // } TransactionID
98  "\x29\xda\x41\x2e" // }
99  "\x00\x06\x00\x12" // USERNAME ATTRIBUTE HEADER
100  "\xe3\x83\x9e\xe3" // }
101  "\x83\x88\xe3\x83" // }
102  "\xaa\xe3\x83\x83" // } Username value (18 bytes) and padding (2 bytes)
103  "\xe3\x82\xaf\xe3" // }
104  "\x82\xb9\x00\x00" // }
105  "\x00\x15\x00\x1c" // NONCE ATTRIBUTE HEADER
106  "\x66\x2f\x2f\x34" // }
107  "\x39\x39\x6b\x39" // }
108  "\x35\x34\x64\x36" // }
109  "\x4f\x4c\x33\x34" // } Nonce value
110  "\x6f\x4c\x39\x46" // }
111  "\x53\x54\x76\x79" // }
112  "\x36\x34\x73\x41" // }
113  "\x00\x14\x00\x0b" // REALM attribute header
114  "\x65\x78\x61\x6d" // }
115  "\x70\x6c\x65\x2e" // } Realm value (11 bytes) and padding (1 byte)
116  "\x6f\x72\x67\x00" // }
117  "\x00\x08\x00\x14" // MESSAGE INTEGRITY attribute HEADER
118  "\xf6\x70\x24\x65" // }
119  "\x6d\xd6\x4a\x3e" // }
120  "\x02\xb8\xe0\x71" // } HMAC-SHA1 fingerprint
121  "\x2e\x85\xc9\xa2" // }
122  "\x8c\xa8\x96\x66"; // }
123 
124  const char c_username[] = "\xe3\x83\x9e\xe3\x83\x88\xe3\x83\xaa\xe3\x83\x83\xe3\x82\xaf\xe3\x82\xb9";
125  const char c_password[] = "TheMatrIX";
126  // const char c_nonce[] = "f//499k954d6OL34oL9FSTvy64sA";
127  const char c_realm[] = "example.org";
128 
129 
130  CStunMessageReader reader;
131 
132  reader.AddBytes(c_requestbytes, sizeof(c_requestbytes)-1); // -1 to get rid of the trailing null
134 
135  ChkIfA(reader.HasMessageIntegrityAttribute() == false, E_FAIL);
136 
137  ChkA(reader.ValidateMessageIntegrityLong(c_username, c_realm, c_password));
138 
139 
140 Cleanup:
141  return hr;
142 }
143 
144 
145 
146 
148 {
149  HRESULT hr = S_OK;
150 
151  Chk(TestMessageIntegrity(false, false));
152  ChkA(TestMessageIntegrity(true, false));
153 
154  Chk(TestMessageIntegrity(false, true));
155  ChkA(TestMessageIntegrity(true, true));
156 
157  ChkA(Test2());
158 
159 
160 Cleanup:
161  return hr;
162 }
163 
#define S_OK
Definition: hresult.h:46
const char c_password[]
Definition: testreader.cpp:45
HRESULT AddRandomTransactionId(StunTransactionId *pTransId)
Definition: stunbuilder.cpp:93
HRESULT TestMessageIntegrity(bool fWithFingerprint, bool fLongCredentials)
HRESULT AddRealm(const char *pszRealm)
HRESULT ValidateMessageIntegrityLong(const char *pszUser, const char *pszRealm, const char *pszPassword)
Definition: stunreader.cpp:282
#define Chk(expr)
Definition: chkmacros.h:53
HRESULT AddBindingRequestHeader()
Definition: stunbuilder.cpp:76
virtual HRESULT Run()
const char c_username[]
Definition: testreader.cpp:46
ReaderParseState AddBytes(const uint8_t *pData, uint32_t size)
Definition: stunreader.cpp:750
ReaderParseState GetState()
Definition: stunreader.cpp:820
HRESULT AddMessageIntegrityLongTerm(const char *pszUserName, const char *pszRealm, const char *pszPassword)
HRESULT AddFingerprintAttribute()
HRESULT ValidateMessageIntegrityShort(const char *pszPassword)
Definition: stunreader.cpp:277
int32_t HRESULT
Definition: hresult.h:22
const unsigned char c_requestbytes[]
Definition: testreader.cpp:27
HRESULT GetResult(CRefCountedBuffer *pspBuffer)
HRESULT AddMessageIntegrityShortTerm(const char *pszPassword)
#define E_FAIL
Definition: hresult.h:56
#define ChkA(expr)
Definition: chkmacros.h:73
bool HasMessageIntegrityAttribute()
Definition: stunreader.cpp:140
boost::shared_ptr< CBuffer > CRefCountedBuffer
Definition: buffer.h:65
#define ChkIfA(expr, hrerror)
Definition: chkmacros.h:84
HRESULT AddUserName(const char *pszUserName)