OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 #include "content/browser/webauth/authenticator_impl.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/json/json_writer.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "content/public/browser/render_frame_host.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "crypto/sha2.h" |
| 14 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 namespace { |
| 19 |
| 20 const char kGetAssertionType[] = "navigator.id.getAssertion"; |
| 21 |
| 22 // JSON key values |
| 23 const char kTypeKey[] = "type"; |
| 24 const char kChallengeKey[] = "challenge"; |
| 25 const char kOriginKey[] = "origin"; |
| 26 const char kCidPubkeyKey[] = "cid_pubkey"; |
| 27 |
| 28 } // namespace |
| 29 |
| 30 // Serializes the |value| to a JSON string and returns the result. |
| 31 std::string SerializeValueToJson(const base::Value& value) { |
| 32 std::string json; |
| 33 base::JSONWriter::Write(value, &json); |
| 34 return json; |
| 35 } |
| 36 |
| 37 // static |
| 38 void AuthenticatorImpl::Create( |
| 39 RenderFrameHost* render_frame_host, |
| 40 const service_manager::BindSourceInfo& source_info, |
| 41 webauth::mojom::AuthenticatorRequest request) { |
| 42 auto authenticator_impl = |
| 43 base::WrapUnique(new AuthenticatorImpl(render_frame_host)); |
| 44 mojo::MakeStrongBinding(std::move(authenticator_impl), std::move(request)); |
| 45 } |
| 46 |
| 47 AuthenticatorImpl::~AuthenticatorImpl() {} |
| 48 |
| 49 AuthenticatorImpl::AuthenticatorImpl(RenderFrameHost* render_frame_host) { |
| 50 DCHECK(render_frame_host); |
| 51 caller_origin_ = render_frame_host->GetLastCommittedOrigin(); |
| 52 } |
| 53 |
| 54 // mojom:Authenticator |
| 55 void AuthenticatorImpl::MakeCredential( |
| 56 webauth::mojom::RelyingPartyAccountPtr account, |
| 57 std::vector<webauth::mojom::ScopedCredentialParametersPtr> parameters, |
| 58 const std::vector<uint8_t>& challenge, |
| 59 webauth::mojom::ScopedCredentialOptionsPtr options, |
| 60 MakeCredentialCallback callback) { |
| 61 std::string effective_domain; |
| 62 std::string relying_party_id; |
| 63 std::string client_data_json; |
| 64 base::DictionaryValue client_data; |
| 65 |
| 66 // Steps 6 & 7 of https://w3c.github.io/webauthn/#createCredential |
| 67 // opaque origin |
| 68 if (caller_origin_.unique()) { |
| 69 std::move(callback).Run( |
| 70 webauth::mojom::AuthenticatorStatus::NOT_ALLOWED_ERROR, NULL); |
| 71 return; |
| 72 } |
| 73 |
| 74 if (!options->relying_party_id) { |
| 75 relying_party_id = caller_origin_.Serialize(); |
| 76 } else { |
| 77 effective_domain = caller_origin_.host(); |
| 78 |
| 79 DCHECK(!effective_domain.empty()); |
| 80 // TODO(kpaulhamus): Check if relyingPartyId is a registrable domain |
| 81 // suffix of and equal to effectiveDomain and set relyingPartyId |
| 82 // appropriately. |
| 83 relying_party_id = options->relying_party_id.value_or(std::string()); |
| 84 } |
| 85 |
| 86 // TODO(kpaulhamus): Check ScopedCredentialParameter's type and |
| 87 // algorithmIdentifier after algorithmIdentifier is added to mojom to |
| 88 // make sure it is U2F_V2. |
| 89 |
| 90 client_data.SetString(kTypeKey, kGetAssertionType); |
| 91 client_data.SetString( |
| 92 kChallengeKey, |
| 93 base::StringPiece(reinterpret_cast<const char*>(challenge.data()), |
| 94 challenge.size())); |
| 95 client_data.SetString(kOriginKey, relying_party_id); |
| 96 // Channel ID is optional, and missing if the browser doesn't support it. |
| 97 // It is present and set to the constant "unused" if the browser |
| 98 // supports Channel ID but is not using it to talk to the origin. |
| 99 // TODO(kpaulhamus): Fetch and add the Channel ID public key used to |
| 100 // communicate with the origin. |
| 101 client_data.SetString(kCidPubkeyKey, "unused"); |
| 102 |
| 103 // SHA-256 hash the JSON data structure |
| 104 client_data_json = SerializeValueToJson(client_data); |
| 105 std::string client_data_hash = crypto::SHA256HashString(client_data_json); |
| 106 |
| 107 std::move(callback).Run(webauth::mojom::AuthenticatorStatus::NOT_IMPLEMENTED, |
| 108 nullptr); |
| 109 } |
| 110 |
| 111 } // namespace content |
OLD | NEW |