1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_UI_WEBUI_CERTIFICATE_VIEWER_WEBUI_H_
6 #define CHROME_BROWSER_UI_WEBUI_CERTIFICATE_VIEWER_WEBUI_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/compiler_specific.h"
12 #include "base/macros.h"
13 #include "base/values.h"
14 #include "content/public/browser/web_ui_message_handler.h"
15 #include "net/cert/scoped_nss_types.h"
16 #include "net/cert/x509_certificate.h"
17 #include "ui/gfx/native_widget_types.h"
18 #include "ui/web_dialogs/web_dialog_delegate.h"
19 
20 namespace content {
21 class WebContents;
22 }
23 
24 class ConstrainedWebDialogDelegate;
25 
26 // Dialog for displaying detailed certificate information. This is used in linux
27 // and chromeos builds to display detailed information in a floating dialog when
28 // the user clicks on "Certificate Information" from the lock icon of a web site
29 // or "View" from the Certificate Manager.
30 class CertificateViewerDialog : public ui::WebDialogDelegate {
31  public:
32   static CertificateViewerDialog* ShowConstrained(
33       net::ScopedCERTCertificateList certs,
34       content::WebContents* web_contents,
35       gfx::NativeWindow parent);
36 
37   ~CertificateViewerDialog() override;
38 
39   gfx::NativeWindow GetNativeWebContentsModalDialog();
40 
41  private:
42   friend class CertificateViewerUITest;
43 
44   // Construct a certificate viewer for the passed in certificate. A reference
45   // to the certificate pointer is added for the lifetime of the certificate
46   // viewer.
47   explicit CertificateViewerDialog(net::ScopedCERTCertificateList certs);
48 
49   // ui::WebDialogDelegate:
50   ui::ModalType GetDialogModalType() const override;
51   base::string16 GetDialogTitle() const override;
52   GURL GetDialogContentURL() const override;
53   void GetWebUIMessageHandlers(
54       std::vector<content::WebUIMessageHandler*>* handlers) const override;
55   void GetDialogSize(gfx::Size* size) const override;
56   std::string GetDialogArgs() const override;
57   void OnDialogShown(content::WebUI* webui) override;
58   void OnDialogClosed(const std::string& json_retval) override;
59   void OnCloseContents(content::WebContents* source,
60                        bool* out_close_dialog) override;
61   bool ShouldShowDialogTitle() const override;
62 
63   // The certificate chain, as NSS cert objects.
64   net::ScopedCERTCertificateList nss_certs_;
65 
66   // The title of the certificate viewer dialog, Certificate Viewer: CN.
67   base::string16 title_;
68 
69   content::WebUI* webui_ = nullptr;
70   ConstrainedWebDialogDelegate* delegate_ = nullptr;
71 
72   DISALLOW_COPY_AND_ASSIGN(CertificateViewerDialog);
73 };
74 
75 // Dialog handler which handles calls from the JS WebUI code to view certificate
76 // details and export the certificate.
77 class CertificateViewerDialogHandler : public content::WebUIMessageHandler {
78  public:
79   CertificateViewerDialogHandler(CertificateViewerDialog* dialog,
80                                  net::ScopedCERTCertificateList cert_chain);
81   ~CertificateViewerDialogHandler() override;
82 
83   // Overridden from WebUIMessageHandler
84   void RegisterMessages() override;
85 
86  private:
87   // Brings up the export certificate dialog for the chosen certificate in the
88   // chain.
89   //
90   // The input is an integer index to the certificate in the chain to export.
91   void HandleExportCertificate(const base::ListValue* args);
92 
93   // Gets the details for a specific certificate in the certificate chain.
94   // Responds with a tree structure containing the fields and values for certain
95   // nodes.
96   //
97   // The input is an integer index to the certificate in the chain to view.
98   void HandleRequestCertificateFields(const base::ListValue* args);
99 
100   // Helper function to get the certificate index. Returns -1 if the index is
101   // out of range.
102   int GetCertificateIndex(int requested_index) const;
103 
104   // The dialog.
105   CertificateViewerDialog* dialog_;
106 
107   // The certificate chain.
108   net::ScopedCERTCertificateList cert_chain_;
109 
110   DISALLOW_COPY_AND_ASSIGN(CertificateViewerDialogHandler);
111 };
112 
113 #endif  // CHROME_BROWSER_UI_WEBUI_CERTIFICATE_VIEWER_WEBUI_H_
114