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 
5 #include "device/fido/win/logging.h"
6 
7 #include <string>
8 
9 #include "base/logging.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_piece_forward.h"
13 #include "base/strings/string_util.h"
14 #include "components/device_event_log/device_event_log.h"
15 
16 namespace {
17 
18 constexpr char kSep[] = ", ";
19 
20 // Quoted wraps |in| in double quotes and backslash-escapes all other double
21 // quote characters.
Quoted(base::StringPiece in)22 std::string Quoted(base::StringPiece in) {
23   std::string result;
24   base::ReplaceChars(in.as_string(), "\\", "\\\\", &result);
25   base::ReplaceChars(result, "\"", "\\\"", &result);
26   return "\"" + result + "\"";
27 }
28 
Quoted16(const base::StringPiece16 in)29 base::string16 Quoted16(const base::StringPiece16 in) {
30   base::string16 result;
31   base::ReplaceChars(in.as_string(), STRING16_LITERAL("\\"),
32                      STRING16_LITERAL("\\\\"), &result);
33   base::ReplaceChars(result, STRING16_LITERAL("\""), STRING16_LITERAL("\\\""),
34                      &result);
35   return STRING16_LITERAL("\"") + result + STRING16_LITERAL("\"");
36 }
37 
38 }  // namespace
39 
operator <<(std::ostream & out,const WEBAUTHN_RP_ENTITY_INFORMATION & in)40 std::ostream& operator<<(std::ostream& out,
41                          const WEBAUTHN_RP_ENTITY_INFORMATION& in) {
42   return out << "{" << in.dwVersion << kSep << Quoted16(in.pwszId) << kSep
43              << Quoted16(in.pwszName) << kSep << Quoted16(in.pwszIcon) << "}";
44 }
45 
operator <<(std::ostream & out,const WEBAUTHN_USER_ENTITY_INFORMATION & in)46 std::ostream& operator<<(std::ostream& out,
47                          const WEBAUTHN_USER_ENTITY_INFORMATION& in) {
48   return out << "{" << in.dwVersion << kSep << base::HexEncode(in.pbId, in.cbId)
49              << kSep << Quoted16(in.pwszName) << kSep << Quoted16(in.pwszIcon)
50              << kSep << Quoted16(in.pwszDisplayName) << "}";
51 }
52 
operator <<(std::ostream & out,const WEBAUTHN_COSE_CREDENTIAL_PARAMETER & in)53 std::ostream& operator<<(std::ostream& out,
54                          const WEBAUTHN_COSE_CREDENTIAL_PARAMETER& in) {
55   return out << "{" << in.dwVersion << kSep << Quoted16(in.pwszCredentialType)
56              << kSep << in.lAlg << "}";
57 }
58 
operator <<(std::ostream & out,const WEBAUTHN_COSE_CREDENTIAL_PARAMETERS & in)59 std::ostream& operator<<(std::ostream& out,
60                          const WEBAUTHN_COSE_CREDENTIAL_PARAMETERS& in) {
61   out << "{" << in.cCredentialParameters << ", &[";
62   for (size_t i = 0; i < in.cCredentialParameters; ++i) {
63     out << (i ? kSep : "") << in.pCredentialParameters[i];
64   }
65   return out << "]}";
66 }
67 
operator <<(std::ostream & out,const WEBAUTHN_CLIENT_DATA & in)68 std::ostream& operator<<(std::ostream& out, const WEBAUTHN_CLIENT_DATA& in) {
69   return out << "{" << in.dwVersion << kSep
70              << Quoted({reinterpret_cast<char*>(in.pbClientDataJSON),
71                         in.cbClientDataJSON})
72              << kSep << Quoted16(in.pwszHashAlgId) << "}";
73 }
74 
operator <<(std::ostream & out,const WEBAUTHN_CREDENTIAL & in)75 std::ostream& operator<<(std::ostream& out, const WEBAUTHN_CREDENTIAL& in) {
76   return out << "{" << in.dwVersion << kSep << base::HexEncode(in.pbId, in.cbId)
77              << kSep << Quoted16(in.pwszCredentialType) << "}";
78 }
79 
operator <<(std::ostream & out,const WEBAUTHN_CREDENTIALS & in)80 std::ostream& operator<<(std::ostream& out, const WEBAUTHN_CREDENTIALS& in) {
81   out << "{" << in.cCredentials << ", &[";
82   for (size_t i = 0; i < in.cCredentials; ++i) {
83     out << (i ? kSep : "") << in.pCredentials[i];
84   }
85   return out << "]}";
86 }
87 
operator <<(std::ostream & out,const WEBAUTHN_CREDENTIAL_EX & in)88 std::ostream& operator<<(std::ostream& out, const WEBAUTHN_CREDENTIAL_EX& in) {
89   return out << "{" << in.dwVersion << kSep << base::HexEncode(in.pbId, in.cbId)
90              << kSep << Quoted16(in.pwszCredentialType) << kSep
91              << in.dwTransports << "}";
92 }
93 
operator <<(std::ostream & out,const WEBAUTHN_CREDENTIAL_LIST & in)94 std::ostream& operator<<(std::ostream& out,
95                          const WEBAUTHN_CREDENTIAL_LIST& in) {
96   out << "{" << in.cCredentials << ", &[";
97   for (size_t i = 0; i < in.cCredentials; ++i) {
98     out << (i ? kSep : "") << "&" << *in.ppCredentials[i];
99   }
100   return out << "]}";
101 }
102 
operator <<(std::ostream & out,const WEBAUTHN_EXTENSION & in)103 std::ostream& operator<<(std::ostream& out, const WEBAUTHN_EXTENSION& in) {
104   return out << "{" << Quoted16(in.pwszExtensionIdentifier) << "}";
105 }
106 
operator <<(std::ostream & out,const WEBAUTHN_EXTENSIONS & in)107 std::ostream& operator<<(std::ostream& out, const WEBAUTHN_EXTENSIONS& in) {
108   out << "{" << in.cExtensions << ", &[";
109   for (size_t i = 0; i < in.cExtensions; ++i) {
110     out << (i ? kSep : "") << in.pExtensions[i];
111   }
112   return out << "]}";
113 }
114 
operator <<(std::ostream & out,const WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS & in)115 std::ostream& operator<<(
116     std::ostream& out,
117     const WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS& in) {
118   out << "{" << in.dwVersion << kSep << in.dwTimeoutMilliseconds << kSep
119       << in.CredentialList << kSep << in.Extensions << kSep
120       << in.dwAuthenticatorAttachment << kSep
121       << in.dwUserVerificationRequirement << kSep << in.dwFlags;
122   if (in.dwVersion < WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS_VERSION_2) {
123     return out << "}";
124   }
125   out << kSep << Quoted16(in.pwszU2fAppId);
126   if (in.pbU2fAppId) {
127     out << ", &" << *in.pbU2fAppId;
128   } else {
129     out << ", (null)";
130   }
131   if (in.dwVersion < WEBAUTHN_AUTHENTICATOR_GET_ASSERTION_OPTIONS_VERSION_3) {
132     return out << "}";
133   }
134   if (in.pAllowCredentialList) {
135     out << ", &" << *in.pAllowCredentialList;
136   } else {
137     out << ", (null)";
138   }
139   return out << "}";
140 }
141 
operator <<(std::ostream & out,const WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS & in)142 std::ostream& operator<<(
143     std::ostream& out,
144     const WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS& in) {
145   out << "{" << in.dwVersion << kSep << in.dwTimeoutMilliseconds << kSep
146       << in.CredentialList << kSep << in.Extensions << kSep
147       << in.dwAuthenticatorAttachment << kSep << in.bRequireResidentKey << kSep
148       << in.dwUserVerificationRequirement << kSep
149       << in.dwAttestationConveyancePreference << kSep << in.dwFlags;
150   if (in.dwVersion < WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS_VERSION_2) {
151     return out << "}";
152   }
153   out << kSep << in.pCancellationId;
154   if (in.dwVersion < WEBAUTHN_AUTHENTICATOR_MAKE_CREDENTIAL_OPTIONS_VERSION_3) {
155     return out << "}";
156   }
157   if (in.pExcludeCredentialList) {
158     out << ", &" << *in.pExcludeCredentialList;
159   } else {
160     out << ", (null)";
161   }
162   return out << "}";
163 }
164 
operator <<(std::ostream & out,const WEBAUTHN_CREDENTIAL_ATTESTATION & in)165 std::ostream& operator<<(std::ostream& out,
166                          const WEBAUTHN_CREDENTIAL_ATTESTATION& in) {
167   out << "{" << in.dwVersion << kSep << Quoted16(in.pwszFormatType) << kSep
168       << base::HexEncode(in.pbAuthenticatorData, in.cbAuthenticatorData) << kSep
169       << base::HexEncode(in.pbAttestation, in.cbAttestation) << kSep
170       << in.dwAttestationDecodeType << kSep
171       << base::HexEncode(in.pbAttestationObject, in.cbAttestationObject) << kSep
172       << base::HexEncode(in.pbCredentialId, in.cbCredentialId);
173   if (in.dwVersion < WEBAUTHN_CREDENTIAL_ATTESTATION_VERSION_2) {
174     return out << "}";
175   }
176   out << kSep << in.Extensions;
177   if (in.dwVersion < WEBAUTHN_CREDENTIAL_ATTESTATION_VERSION_3) {
178     return out << "}";
179   }
180   out << kSep << in.dwUsedTransport;
181   return out << "}";
182 }
183 
operator <<(std::ostream & out,const WEBAUTHN_ASSERTION & in)184 std::ostream& operator<<(std::ostream& out, const WEBAUTHN_ASSERTION& in) {
185   return out << "{" << in.dwVersion << kSep
186              << base::HexEncode(in.pbAuthenticatorData, in.cbAuthenticatorData)
187              << kSep << base::HexEncode(in.pbSignature, in.cbSignature) << kSep
188              << in.Credential << kSep
189              << base::HexEncode(in.pbUserId, in.cbUserId) << "}";
190 }
191