You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

binder_security_policy.h 3.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2021 gRPC authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef GRPCPP_SECURITY_BINDER_SECURITY_POLICY_H
  15. #define GRPCPP_SECURITY_BINDER_SECURITY_POLICY_H
  16. #include <memory>
  17. #ifdef GPR_ANDROID
  18. #include <jni.h>
  19. #endif
  20. namespace grpc
  21. {
  22. namespace experimental
  23. {
  24. namespace binder
  25. {
  26. // EXPERIMENTAL Determinines if a connection is allowed to be
  27. // established on Android. See https://source.android.com/security/app-sandbox
  28. // for more info about UID.
  29. class SecurityPolicy
  30. {
  31. public:
  32. virtual ~SecurityPolicy() = default;
  33. // Returns true if the UID is authorized to connect.
  34. // Must return the same value for the same inputs so callers can safely cache
  35. // the result.
  36. virtual bool IsAuthorized(int uid) = 0;
  37. };
  38. // EXPERIMENTAL Allows all connection. Anything on the Android device will be
  39. // able to connect, use with caution!
  40. class UntrustedSecurityPolicy : public SecurityPolicy
  41. {
  42. public:
  43. UntrustedSecurityPolicy();
  44. ~UntrustedSecurityPolicy() override;
  45. bool IsAuthorized(int uid) override;
  46. };
  47. // EXPERIMENTAL Only allows the connections from processes with the same UID. In
  48. // most cases this means "from the same APK".
  49. class InternalOnlySecurityPolicy : public SecurityPolicy
  50. {
  51. public:
  52. InternalOnlySecurityPolicy();
  53. ~InternalOnlySecurityPolicy() override;
  54. bool IsAuthorized(int uid) override;
  55. };
  56. #ifdef GPR_ANDROID
  57. // EXPERIMENTAL Only allows the connections from the APK that have the same
  58. // signature.
  59. class SameSignatureSecurityPolicy : public SecurityPolicy
  60. {
  61. public:
  62. // `context` is required for getting PackageManager Java class
  63. SameSignatureSecurityPolicy(JavaVM* jvm, jobject context);
  64. ~SameSignatureSecurityPolicy() override;
  65. bool IsAuthorized(int uid) override;
  66. private:
  67. JavaVM* jvm_;
  68. jobject context_;
  69. };
  70. #endif
  71. } // namespace binder
  72. } // namespace experimental
  73. } // namespace grpc
  74. #endif // GRPCPP_SECURITY_BINDER_SECURITY_POLICY_H