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