1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 #include "net/android/dummy_spnego_authenticator.h"
5
6 #include "base/android/jni_string.h"
7 #include "base/base64.h"
8 #include "net/net_test_jni_headers/DummySpnegoAuthenticator_jni.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 using base::android::JavaParamRef;
12
13 namespace net {
14
15 // iso.org.dod.internet.security.mechanism.snego (1.3.6.1.5.5.2)
16 // From RFC 4178, which uses SNEGO not SPNEGO.
17 static const unsigned char kSpnegoOid[] = {0x2b, 0x06, 0x01, 0x05, 0x05, 0x02};
18 gss_OID_desc CHROME_GSS_SPNEGO_MECH_OID_DESC_VAL = {
19 std::size(kSpnegoOid), const_cast<unsigned char*>(kSpnegoOid)};
20
21 gss_OID CHROME_GSS_SPNEGO_MECH_OID_DESC = &CHROME_GSS_SPNEGO_MECH_OID_DESC_VAL;
22
23 namespace {
24
25 // gss_OID helpers.
26 // NOTE: gss_OID's do not own the data they point to, which should be static.
ClearOid(gss_OID dest)27 void ClearOid(gss_OID dest) {
28 if (!dest)
29 return;
30 dest->length = 0;
31 dest->elements = nullptr;
32 }
33
SetOid(gss_OID dest,const void * src,size_t length)34 void SetOid(gss_OID dest, const void* src, size_t length) {
35 if (!dest)
36 return;
37 ClearOid(dest);
38 if (!src)
39 return;
40 dest->length = length;
41 if (length)
42 dest->elements = const_cast<void*>(src);
43 }
44
CopyOid(gss_OID dest,const gss_OID_desc * src)45 void CopyOid(gss_OID dest, const gss_OID_desc* src) {
46 if (!dest)
47 return;
48 ClearOid(dest);
49 if (!src)
50 return;
51 SetOid(dest, src->elements, src->length);
52 }
53
54 } // namespace
55
56 namespace test {
57
GssContextMockImpl()58 GssContextMockImpl::GssContextMockImpl()
59 : lifetime_rec(0), ctx_flags(0), locally_initiated(0), open(0) {
60 ClearOid(&mech_type);
61 }
62
GssContextMockImpl(const GssContextMockImpl & other)63 GssContextMockImpl::GssContextMockImpl(const GssContextMockImpl& other)
64 : src_name(other.src_name),
65 targ_name(other.targ_name),
66 lifetime_rec(other.lifetime_rec),
67 ctx_flags(other.ctx_flags),
68 locally_initiated(other.locally_initiated),
69 open(other.open) {
70 CopyOid(&mech_type, &other.mech_type);
71 }
72
GssContextMockImpl(const char * src_name_in,const char * targ_name_in,uint32_t lifetime_rec_in,const gss_OID_desc & mech_type_in,uint32_t ctx_flags_in,int locally_initiated_in,int open_in)73 GssContextMockImpl::GssContextMockImpl(const char* src_name_in,
74 const char* targ_name_in,
75 uint32_t lifetime_rec_in,
76 const gss_OID_desc& mech_type_in,
77 uint32_t ctx_flags_in,
78 int locally_initiated_in,
79 int open_in)
80 : src_name(src_name_in ? src_name_in : ""),
81 targ_name(targ_name_in ? targ_name_in : ""),
82 lifetime_rec(lifetime_rec_in),
83 ctx_flags(ctx_flags_in),
84 locally_initiated(locally_initiated_in),
85 open(open_in) {
86 CopyOid(&mech_type, &mech_type_in);
87 }
88
~GssContextMockImpl()89 GssContextMockImpl::~GssContextMockImpl() {
90 ClearOid(&mech_type);
91 }
92
93 } // namespace test
94
95 namespace android {
96
SecurityContextQuery(const std::string & in_expected_package,uint32_t in_response_code,uint32_t in_minor_response_code,const test::GssContextMockImpl & in_context_info,const std::string & in_expected_input_token,const std::string & in_output_token)97 DummySpnegoAuthenticator::SecurityContextQuery::SecurityContextQuery(
98 const std::string& in_expected_package,
99 uint32_t in_response_code,
100 uint32_t in_minor_response_code,
101 const test::GssContextMockImpl& in_context_info,
102 const std::string& in_expected_input_token,
103 const std::string& in_output_token)
104 : expected_package(in_expected_package),
105 response_code(in_response_code),
106 minor_response_code(in_minor_response_code),
107 context_info(in_context_info),
108 expected_input_token(in_expected_input_token),
109 output_token(in_output_token) {
110 }
111
SecurityContextQuery(const std::string & in_expected_package,uint32_t in_response_code,uint32_t in_minor_response_code,const test::GssContextMockImpl & in_context_info,const char * in_expected_input_token,const char * in_output_token)112 DummySpnegoAuthenticator::SecurityContextQuery::SecurityContextQuery(
113 const std::string& in_expected_package,
114 uint32_t in_response_code,
115 uint32_t in_minor_response_code,
116 const test::GssContextMockImpl& in_context_info,
117 const char* in_expected_input_token,
118 const char* in_output_token)
119 : expected_package(in_expected_package),
120 response_code(in_response_code),
121 minor_response_code(in_minor_response_code),
122 context_info(in_context_info) {
123 if (in_expected_input_token)
124 expected_input_token = in_expected_input_token;
125 if (in_output_token)
126 output_token = in_output_token;
127 }
128
SecurityContextQuery()129 DummySpnegoAuthenticator::SecurityContextQuery::SecurityContextQuery()
130 : response_code(0), minor_response_code(0) {
131 }
132
133 DummySpnegoAuthenticator::SecurityContextQuery::SecurityContextQuery(
134 const SecurityContextQuery& other) = default;
135
136 DummySpnegoAuthenticator::SecurityContextQuery::~SecurityContextQuery() =
137 default;
138
139 base::android::ScopedJavaLocalRef<jstring>
GetTokenToReturn(JNIEnv * env)140 DummySpnegoAuthenticator::SecurityContextQuery::GetTokenToReturn(JNIEnv* env) {
141 return base::android::ConvertUTF8ToJavaString(env, output_token.c_str());
142 }
GetResult(JNIEnv *)143 int DummySpnegoAuthenticator::SecurityContextQuery::GetResult(JNIEnv* /*env*/) {
144 return response_code;
145 }
146
CheckGetTokenArguments(JNIEnv * env,const JavaParamRef<jstring> & j_incoming_token)147 void DummySpnegoAuthenticator::SecurityContextQuery::CheckGetTokenArguments(
148 JNIEnv* env,
149 const JavaParamRef<jstring>& j_incoming_token) {
150 std::string incoming_token =
151 base::android::ConvertJavaStringToUTF8(env, j_incoming_token);
152 EXPECT_EQ(expected_input_token, incoming_token);
153 }
154
155 // Needed to satisfy "complex class" clang requirements.
156 DummySpnegoAuthenticator::DummySpnegoAuthenticator() = default;
157
158 DummySpnegoAuthenticator::~DummySpnegoAuthenticator() = default;
159
EnsureTestAccountExists()160 void DummySpnegoAuthenticator::EnsureTestAccountExists() {
161 Java_DummySpnegoAuthenticator_ensureTestAccountExists(
162 base::android::AttachCurrentThread());
163 }
164
RemoveTestAccounts()165 void DummySpnegoAuthenticator::RemoveTestAccounts() {
166 Java_DummySpnegoAuthenticator_removeTestAccounts(
167 base::android::AttachCurrentThread());
168 }
169
ExpectSecurityContext(const std::string & expected_package,uint32_t response_code,uint32_t minor_response_code,const test::GssContextMockImpl & context_info,const std::string & expected_input_token,const std::string & output_token)170 void DummySpnegoAuthenticator::ExpectSecurityContext(
171 const std::string& expected_package,
172 uint32_t response_code,
173 uint32_t minor_response_code,
174 const test::GssContextMockImpl& context_info,
175 const std::string& expected_input_token,
176 const std::string& output_token) {
177 SecurityContextQuery query(expected_package, response_code,
178 minor_response_code, context_info,
179 expected_input_token, output_token);
180 expected_security_queries_.push_back(query);
181 Java_DummySpnegoAuthenticator_setNativeAuthenticator(
182 base::android::AttachCurrentThread(), reinterpret_cast<intptr_t>(this));
183 }
184
GetNextQuery(JNIEnv *)185 long DummySpnegoAuthenticator::GetNextQuery(JNIEnv* /*env*/) {
186 CheckQueueNotEmpty();
187 current_query_ = expected_security_queries_.front();
188 expected_security_queries_.pop_front();
189 return reinterpret_cast<intptr_t>(¤t_query_);
190 }
191
CheckQueueNotEmpty()192 void DummySpnegoAuthenticator::CheckQueueNotEmpty() {
193 ASSERT_FALSE(expected_security_queries_.empty());
194 }
195
196 } // namespace android
197 } // namespace net
198