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