certificate_principal_pattern.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_CERTIFICATE_MATCHING_CERTIFICATE_PRINCIPAL_PATTERN_H_
  5. #define COMPONENTS_CERTIFICATE_MATCHING_CERTIFICATE_PRINCIPAL_PATTERN_H_
  6. #include <string>
  7. #include "base/component_export.h"
  8. #include "base/strings/string_piece_forward.h"
  9. #include "base/values.h"
  10. namespace net {
  11. struct CertPrincipal;
  12. } // namespace net
  13. namespace certificate_matching {
  14. // Class to represent fields of a principal (issuer or subject) of a X509
  15. // certificate and compare them.
  16. class COMPONENT_EXPORT(CERTIFICATE_MATCHING) CertificatePrincipalPattern {
  17. public:
  18. // Creates a pattern that matches every certificate principal.
  19. CertificatePrincipalPattern();
  20. // Creates a pattern that requires an exact equality with the specified
  21. // |common_name|, |locality|, |organization| and |organization_unit| for a
  22. // certificate to match. If one of these is empty, no constraint is put on the
  23. // corresponding principal field.
  24. CertificatePrincipalPattern(std::string common_name,
  25. std::string locality,
  26. std::string organization,
  27. std::string organization_unit);
  28. CertificatePrincipalPattern(const CertificatePrincipalPattern& rhs);
  29. CertificatePrincipalPattern(CertificatePrincipalPattern&& rhs);
  30. ~CertificatePrincipalPattern();
  31. CertificatePrincipalPattern& operator=(
  32. const CertificatePrincipalPattern& rhs);
  33. CertificatePrincipalPattern& operator=(CertificatePrincipalPattern&& rhs);
  34. // Returns true if all fields in the pattern are empty. A return value of true
  35. // means that this pattern will match every |CertPrincipal|.
  36. bool Empty() const;
  37. // Returns true if this pattern matches |principal|.
  38. bool Matches(const net::CertPrincipal& principal) const;
  39. const std::string& common_name() const { return common_name_; }
  40. const std::string& locality() const { return locality_; }
  41. const std::string& organization() const { return organization_; }
  42. const std::string& organization_unit() const { return organization_unit_; }
  43. // Parses |value| to create a |CertificatePrincipalPattern|. If |value| is
  44. // present and a dictionary, the |key_*| parameters will be used to fill
  45. // corresponding fields of the resulting |CertificatePrincipalPattern|. If a
  46. // key is not present in the dictionary, the corresponding field will be left
  47. // empty (putting no constraint on the principal field). If |value| is nullptr
  48. // or not a dictionary, returns an empty pattern.
  49. static CertificatePrincipalPattern ParseFromOptionalDict(
  50. const base::Value::Dict* dict,
  51. base::StringPiece key_common_name,
  52. base::StringPiece key_locality,
  53. base::StringPiece key_organization,
  54. base::StringPiece key_organization_unit);
  55. private:
  56. std::string common_name_;
  57. std::string locality_;
  58. std::string organization_;
  59. std::string organization_unit_;
  60. };
  61. } // namespace certificate_matching
  62. #endif // COMPONENTS_CERTIFICATE_MATCHING_CERTIFICATE_PRINCIPAL_PATTERN_H_