s2a_utils.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. *
  3. * Copyright 2021 Google LLC
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * https://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package s2a
  19. import (
  20. "context"
  21. "errors"
  22. commonpb "github.com/google/s2a-go/internal/proto/common_go_proto"
  23. "google.golang.org/grpc/credentials"
  24. "google.golang.org/grpc/peer"
  25. )
  26. // AuthInfo exposes security information from the S2A to the application.
  27. type AuthInfo interface {
  28. // AuthType returns the authentication type.
  29. AuthType() string
  30. // ApplicationProtocol returns the application protocol, e.g. "grpc".
  31. ApplicationProtocol() string
  32. // TLSVersion returns the TLS version negotiated during the handshake.
  33. TLSVersion() commonpb.TLSVersion
  34. // Ciphersuite returns the ciphersuite negotiated during the handshake.
  35. Ciphersuite() commonpb.Ciphersuite
  36. // PeerIdentity returns the authenticated identity of the peer.
  37. PeerIdentity() *commonpb.Identity
  38. // LocalIdentity returns the local identity of the application used during
  39. // session setup.
  40. LocalIdentity() *commonpb.Identity
  41. // PeerCertFingerprint returns the SHA256 hash of the peer certificate used in
  42. // the S2A handshake.
  43. PeerCertFingerprint() []byte
  44. // LocalCertFingerprint returns the SHA256 hash of the local certificate used
  45. // in the S2A handshake.
  46. LocalCertFingerprint() []byte
  47. // IsHandshakeResumed returns true if a cached session was used to resume
  48. // the handshake.
  49. IsHandshakeResumed() bool
  50. // SecurityLevel returns the security level of the connection.
  51. SecurityLevel() credentials.SecurityLevel
  52. }
  53. // AuthInfoFromPeer extracts the authinfo.S2AAuthInfo object from the given
  54. // peer, if it exists. This API should be used by gRPC clients after
  55. // obtaining a peer object using the grpc.Peer() CallOption.
  56. func AuthInfoFromPeer(p *peer.Peer) (AuthInfo, error) {
  57. s2aAuthInfo, ok := p.AuthInfo.(AuthInfo)
  58. if !ok {
  59. return nil, errors.New("no S2AAuthInfo found in Peer")
  60. }
  61. return s2aAuthInfo, nil
  62. }
  63. // AuthInfoFromContext extracts the authinfo.S2AAuthInfo object from the given
  64. // context, if it exists. This API should be used by gRPC server RPC handlers
  65. // to get information about the peer. On the client-side, use the grpc.Peer()
  66. // CallOption and the AuthInfoFromPeer function.
  67. func AuthInfoFromContext(ctx context.Context) (AuthInfo, error) {
  68. p, ok := peer.FromContext(ctx)
  69. if !ok {
  70. return nil, errors.New("no Peer found in Context")
  71. }
  72. return AuthInfoFromPeer(p)
  73. }