OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 |
| 5 // This file contains utilities related to working with "facets". |
| 6 // |
| 7 // A "facet" is defined as the manifestation of a logical application on a given |
| 8 // platform. For example, "My Bank" may have released an Android application |
| 9 // and a Web application accessible from a browser. These are all facets of the |
| 10 // "My Bank" logical application. |
| 11 // |
| 12 // Facets that belong to the same logical application are said to be affiliated |
| 13 // with each other. Conceptually, "affiliations" can be seen as an equivalence |
| 14 // relation defined over the set of all facets. Each equivalence class contains |
| 15 // facets that belong to the same logical application, and therefore should be |
| 16 // treated as synonymous for certain purposes, e.g., sharing credentials. |
| 17 // |
| 18 // A valid facet identifier will be a URI of the form: |
| 19 // |
| 20 // * https://<host>[:<port>] |
| 21 // |
| 22 // For web sites. Only HTTPS sites are supported. The syntax corresponds to |
| 23 // that of 'serialized-origin' in RFC 6454. That is, in canonical form, the |
| 24 // URI must not contain components other than the scheme (required, must be |
| 25 // "https"), host (required), and port (optional); with canonicalization |
| 26 // performed the same way as it normally would be for standard URLs. |
| 27 // |
| 28 // * android://<certificate_hash>@<package_name> |
| 29 // |
| 30 // For Android applications. In canonical form, the URI must not contain |
| 31 // components other than the scheme (must be "android"), username, and host |
| 32 // (all required). The host part must be a valid Android package name, with |
| 33 // no escaping, so it must be composed of characters [a-zA-Z0-9_.]. |
| 34 // |
| 35 // The username part must be the hash of the certificate used to sign the |
| 36 // APK, base64-encoded using padding and the "URL and filename safe" base64 |
| 37 // alphabet, with no further escaping. This is normally calculated as: |
| 38 // |
| 39 // echo -n -e "$PEM_KEY" | \ |
| 40 // openssl x509 -outform DER | \ |
| 41 // openssl sha -sha512 -binary | base64 | tr '+/' '-_' |
| 42 // |
| 43 |
| 44 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_UTILS_H_ |
| 45 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_UTILS_H_ |
| 46 |
| 47 #include <string> |
| 48 #include <vector> |
| 49 |
| 50 namespace password_manager { |
| 51 |
| 52 // Encapsulates a facet URI in canonical form. |
| 53 // |
| 54 // This is a very light-weight wrapper around an std::string containing the text |
| 55 // of the URI, and can be passed around as a value. The main rationale for the |
| 56 // existance of this class is to make it clearer in the code when a certain URI |
| 57 // is known to be a valid facet URI in canonical form, and to allow verifying |
| 58 // and converting URIs to such canonical form. |
| 59 // |
| 60 // Note that it would be impractical to use GURL to represent facet URIs, as |
| 61 // GURL has built-in logic to parse the rest of the URI according to its scheme, |
| 62 // and obviously, it does not recognize the "android" scheme. Therefore, after |
| 63 // parsing, everything ends up in the path component, which is not too helpful. |
| 64 class FacetURI { |
| 65 public: |
| 66 FacetURI(); |
| 67 |
| 68 // As a light-weight std::string wrapper, allow copy and assign. |
| 69 FacetURI(const FacetURI&) = default; |
| 70 FacetURI& operator=(const FacetURI&) = default; |
| 71 |
| 72 // Constructs an instance to encapsulate the canonical form of |spec|. |
| 73 // If |spec| is not a valid facet URI, then an invalid instance is returned, |
| 74 // which then should be discarded. |
| 75 static FacetURI FromPotentiallyInvalidSpec(const std::string& spec); |
| 76 |
| 77 // Constructs a valid FacetURI instance from a valid |canonical_spec|. |
| 78 // Note: The passed-in URI is not verified at all. Use only when you are sure |
| 79 // the URI is valid and in canonical form. |
| 80 static FacetURI FromCanonicalSpec(const std::string& canonical_spec); |
| 81 |
| 82 // Comparison operators so that FacetURI can be used in std::equal. |
| 83 bool operator==(const FacetURI& other) const; |
| 84 bool operator!=(const FacetURI& other) const; |
| 85 |
| 86 // Relational operators so that FacetURI can be used in sorted containers. |
| 87 bool operator<(const FacetURI& other) const; |
| 88 bool operator>(const FacetURI& other) const; |
| 89 |
| 90 // Returns whether or not this instance represents a valid facet identifier |
| 91 // referring to a Web application. |
| 92 bool IsValidWebFacetURI() const; |
| 93 |
| 94 // Returns whether or not this instance represents a valid facet identifier |
| 95 // referring to an Android application. |
| 96 bool IsValidAndroidFacetURI() const; |
| 97 |
| 98 // Returns whether or not this instance represents a valid facet identifier |
| 99 // referring to either a Web or an Android application. |
| 100 bool is_valid() const { |
| 101 return is_valid_; |
| 102 } |
| 103 |
| 104 // Returns the text of the encapsulated canonical URI. |
| 105 const std::string& canonical_spec() const { |
| 106 return canonical_spec_; |
| 107 } |
| 108 |
| 109 private: |
| 110 // Internal constructor to be used by the static factory methods. |
| 111 FacetURI(const std::string& canonical_spec, bool is_valid); |
| 112 |
| 113 // Whether |canonical_spec_| contains a valid facet URI in canonical form. |
| 114 bool is_valid_; |
| 115 |
| 116 // The text of the encapsulated canonical URI, valid if and only if |
| 117 // |is_valid_| is true. |
| 118 std::string canonical_spec_; |
| 119 }; |
| 120 |
| 121 // A collection of facets affiliated with each other, i.e. an equivalence class. |
| 122 typedef std::vector<FacetURI> AffiliatedFacets; |
| 123 |
| 124 // Returns whether or not equivalence classes |a| and |b| are equal, that is, |
| 125 // whether or not they consist of the same set of facets. |
| 126 // |
| 127 // Note that this will do some sorting, so it can be expensive for large inputs. |
| 128 bool AreEquivalenceClassesEqual(const AffiliatedFacets& a, |
| 129 const AffiliatedFacets& b); |
| 130 |
| 131 } // namespace password_manager |
| 132 |
| 133 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_AFFILIATION_UTILS_H_ |
OLD | NEW |