NetSurf
sslcert.c
Go to the documentation of this file.
1 /*
2  * Copyright 2006 John M Bell <jmb202@ecs.soton.ac.uk>
3  * Copyright 2016 Vincent Sanders <vince@netsurf-browser.org>
4  *
5  * This file is part of NetSurf, http://www.netsurf-browser.org/
6  *
7  * NetSurf is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * NetSurf is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /**
21  * \file
22  * Implementation of RISC OS certificate verification UI.
23  */
24 
25 #include <oslib/wimp.h>
26 
27 #include "utils/log.h"
28 #include "utils/nsurl.h"
29 #include "netsurf/plotters.h"
30 #include "desktop/sslcert_viewer.h"
31 
32 #include "riscos/dialog.h"
33 #include "riscos/wimp.h"
34 #include "riscos/wimp_event.h"
35 #include "riscos/wimputils.h"
36 #include "riscos/gui.h"
37 #include "riscos/toolbar.h"
38 #include "riscos/corewindow.h"
39 #include "riscos/sslcert.h"
40 
41 /* widget ID */
42 #define ICON_SSL_PANE 1
43 #define ICON_SSL_REJECT 3
44 #define ICON_SSL_ACCEPT 4
45 
46 /**
47  * RISC OS certificate viewer context.
48  */
51 
52  /** certificate view window handle */
53  wimp_w wh;
54 
55  /** SSL certificate viewer context data */
57 
58 };
59 
60 /** riscos dialog template for certificate viewer window. */
61 static wimp_window *dialog_cert_template;
62 
63 /** riscos template for certificate tree pane. */
64 static wimp_window *cert_tree_template;
65 
66 
67 /**
68  * Handle closing of the RISC OS certificate verification dialog
69  *
70  * Deleting wimp windows, freeing up the core window and ssl data block.
71  *
72  * \param certw The context associated with the dialogue.
73  */
74 static void ro_gui_cert_release_window(struct ro_cert_window *certw)
75 {
76  os_error *error;
77 
79 
81 
82  ro_corewindow_fini(&certw->core);
83 
84  error = xwimp_delete_window(certw->wh);
85  if (error) {
86  NSLOG(netsurf, INFO, "xwimp_delete_window: 0x%x:%s",
87  error->errnum, error->errmess);
88  }
89 
90  error = xwimp_delete_window(certw->core.wh);
91  if (error) {
92  NSLOG(netsurf, INFO, "xwimp_delete_window: 0x%x:%s",
93  error->errnum, error->errmess);
94  }
95 
96  free(certw);
97 }
98 
99 /**
100  * Handle acceptance of certificate via event callback.
101  *
102  * \param pointer The wimp pointer event.
103  */
104 static void ro_gui_cert_accept(wimp_pointer *pointer)
105 {
106  struct ro_cert_window *certw;
107  certw = (struct ro_cert_window *)ro_gui_wimp_event_get_user_data(pointer->w);
108 
110  ro_gui_dialog_close(certw->wh);
112 }
113 
114 
115 /**
116  * Handle rejection of certificate via event callback.
117  *
118  * \param pointer The wimp pointer block.
119  */
120 static void ro_gui_cert_reject(wimp_pointer *pointer)
121 {
122  struct ro_cert_window *certw;
123  certw = (struct ro_cert_window *)ro_gui_wimp_event_get_user_data(pointer->w);
124 
126  ro_gui_dialog_close(certw->wh);
128 }
129 
130 
131 /**
132  * Callback to handle the closure of the SSL dialogue by other means.
133  *
134  * \param w The window handle being closed.
135  */
136 static void ro_gui_cert_close_window(wimp_w w)
137 {
138  struct ro_cert_window *certw;
139  certw = (struct ro_cert_window *)ro_gui_wimp_event_get_user_data(w);
140 
142 }
143 
144 
145 /**
146  * Attach tree window as a pane to ssl window.
147  *
148  * Nest the tree window inside the pane window. To do this, we:
149  * - Get the current pane extent,
150  * - Get the parent window position and the location of the pane-
151  * locating icon inside it,
152  * - Set the visible area of the pane to suit,
153  * - Check that the pane extents are OK for this visible area, and
154  * increase them if necessary,
155  * - Before finally opening the pane as a nested part of the parent.
156  *
157  */
158 static nserror cert_attach_pane(wimp_w parent, wimp_w pane)
159 {
160  os_error *error;
161  wimp_window_state wstate;
162  wimp_window_info winfo;
163  wimp_icon_state istate;
164  bool set_extent;
165 
166  winfo.w = pane;
167  error = xwimp_get_window_info_header_only(&winfo);
168  if (error) {
169  NSLOG(netsurf, INFO, "xwimp_get_window_info: 0x%x: %s",
170  error->errnum, error->errmess);
171  return NSERROR_INIT_FAILED;
172  }
173 
174  wstate.w = parent;
175  error = xwimp_get_window_state(&wstate);
176  if (error) {
177  NSLOG(netsurf, INFO, "xwimp_get_window_state: 0x%x: %s",
178  error->errnum, error->errmess);
179  return NSERROR_INIT_FAILED;
180  }
181 
182  istate.w = parent;
183  istate.i = ICON_SSL_PANE;
184  error = xwimp_get_icon_state(&istate);
185  if (error) {
186  NSLOG(netsurf, INFO, "xwimp_get_icon_state: 0x%x: %s",
187  error->errnum, error->errmess);
188  return NSERROR_INIT_FAILED;
189  }
190 
191  wstate.w = pane;
192  wstate.visible.x1 = wstate.visible.x0 + istate.icon.extent.x1 - 20 - ro_get_vscroll_width(pane);
193  wstate.visible.x0 += istate.icon.extent.x0 + 20;
194  wstate.visible.y0 = wstate.visible.y1 + istate.icon.extent.y0 + 20 + ro_get_hscroll_height(pane);
195  wstate.visible.y1 += istate.icon.extent.y1 - 32;
196 
197  set_extent = false;
198 
199  if ((winfo.extent.x1 - winfo.extent.x0) <
200  (wstate.visible.x1 - wstate.visible.x0)) {
201  winfo.extent.x0 = 0;
202  winfo.extent.x1 = wstate.visible.x1 - wstate.visible.x0;
203  set_extent = true;
204  }
205  if ((winfo.extent.y1 - winfo.extent.y0) <
206  (wstate.visible.y1 - wstate.visible.y0)) {
207  winfo.extent.y1 = 0;
208  winfo.extent.x1 = wstate.visible.y0 - wstate.visible.y1;
209  set_extent = true;
210  }
211 
212  if (set_extent) {
213  error = xwimp_set_extent(pane, &(winfo.extent));
214  if (error) {
215  NSLOG(netsurf, INFO, "xwimp_set_extent: 0x%x: %s",
216  error->errnum, error->errmess);
217  return NSERROR_INIT_FAILED;
218  }
219  }
220 
221  error = xwimp_open_window_nested(
222  PTR_WIMP_OPEN(&wstate),
223  parent,
224  wimp_CHILD_LINKS_PARENT_VISIBLE_BOTTOM_OR_LEFT << wimp_CHILD_XORIGIN_SHIFT |
225  wimp_CHILD_LINKS_PARENT_VISIBLE_TOP_OR_RIGHT << wimp_CHILD_YORIGIN_SHIFT |
226  wimp_CHILD_LINKS_PARENT_VISIBLE_BOTTOM_OR_LEFT << wimp_CHILD_LS_EDGE_SHIFT |
227  wimp_CHILD_LINKS_PARENT_VISIBLE_BOTTOM_OR_LEFT << wimp_CHILD_RS_EDGE_SHIFT);
228  if (error) {
229  NSLOG(netsurf, INFO, "xwimp_open_window_nested: 0x%x: %s",
230  error->errnum, error->errmess);
231  return NSERROR_INIT_FAILED;
232  }
233 
234  return NSERROR_OK;
235 }
236 
237 
238 /**
239  * Callback to draw on drawable area of ro certificate viewer window.
240  *
241  * \param ro_cw The riscos core window structure.
242  * \param originx The risc os plotter x origin.
243  * \param originy The risc os plotter y origin.
244  * \param r The rectangle of the window that needs updating.
245  * \return NSERROR_OK on success otherwise apropriate error code
246  */
247 static nserror
248 cert_draw(struct ro_corewindow *ro_cw, int originx, int originy, struct rect *r)
249 {
250  struct ro_cert_window *certw;
251  struct redraw_context ctx = {
252  .interactive = true,
253  .background_images = true,
254  .plot = &ro_plotters
255  };
256 
257  certw = (struct ro_cert_window *)ro_cw;
258 
259  ro_plot_origin_x = originx;
260  ro_plot_origin_y = originy;
261  no_font_blending = true;
262  sslcert_viewer_redraw(certw->ssl_data, 0, 0, r, &ctx);
263  no_font_blending = false;
264 
265  return NSERROR_OK;
266 }
267 
268 
269 /**
270  * callback for keypress on ro certificate viewer window
271  *
272  * \param ro_cw The ro core window structure.
273  * \param nskey The netsurf key code.
274  * \return NSERROR_OK if key processed,
275  * NSERROR_NOT_IMPLEMENTED if key not processed
276  * otherwise apropriate error code
277  */
278 static nserror cert_key(struct ro_corewindow *ro_cw, uint32_t nskey)
279 {
280  struct ro_cert_window *certw;
281  certw = (struct ro_cert_window *)ro_cw;
282 
283  if (sslcert_viewer_keypress(certw->ssl_data, nskey)) {
284  return NSERROR_OK;
285  }
287 }
288 
289 
290 /**
291  * callback for mouse event on ro certificate viewer window
292  *
293  * \param ro_cw The ro core window structure.
294  * \param mouse_state mouse state
295  * \param x location of event
296  * \param y location of event
297  * \return NSERROR_OK on sucess otherwise apropriate error code.
298  */
299 static nserror
300 cert_mouse(struct ro_corewindow *ro_cw,
301  browser_mouse_state mouse_state,
302  int x, int y)
303 {
304  struct ro_cert_window *certw;
305  certw = (struct ro_cert_window *)ro_cw;
306 
307  sslcert_viewer_mouse_action(certw->ssl_data, mouse_state, x, y);
308 
309  return NSERROR_OK;
310 }
311 
312 /* exported interface documented in riscos/sslcert.h */
313 nserror
315  const struct cert_chain *chain,
316  nserror (*cb)(bool proceed, void *pw),
317  void *cbpw)
318 {
319  os_error *error;
320  struct ro_cert_window *ncwin; /* new certificate window */
321  nserror res;
322 
323  ncwin = malloc(sizeof(struct ro_cert_window));
324  if (ncwin == NULL) {
325  return NSERROR_NOMEM;
326  }
327 
328  /* initialise certificate viewing interface */
329  res = sslcert_viewer_create_session_data(url, cb, cbpw, chain,
330  &ncwin->ssl_data);
331  if (res != NSERROR_OK) {
332  free(ncwin);
333  return res;
334  }
335 
336  /* Create the SSL window */
337  error = xwimp_create_window(dialog_cert_template, &ncwin->wh);
338  if (error) {
339  NSLOG(netsurf, INFO, "xwimp_create_window: 0x%x: %s",
340  error->errnum, error->errmess);
341  free(ncwin);
342  return NSERROR_INIT_FAILED;
343  }
344 
345  /* create ssl viewer pane window */
346  error = xwimp_create_window(cert_tree_template, &ncwin->core.wh);
347  if (error) {
348  NSLOG(netsurf, INFO, "xwimp_create_window: 0x%x: %s",
349  error->errnum, error->errmess);
350  free(ncwin);
351  return NSERROR_INIT_FAILED;
352  }
353 
354  /* setup callbacks */
355  ncwin->core.draw = cert_draw;
356  ncwin->core.key = cert_key;
357  ncwin->core.mouse = cert_mouse;
358 
359  /* initialise core window */
360  res = ro_corewindow_init(&ncwin->core, NULL, NULL, 0, NULL);
361  if (res != NSERROR_OK) {
362  free(ncwin);
363  return res;
364  }
365 
366  res = sslcert_viewer_init(ncwin->core.cb_table,
367  (struct core_window *)ncwin,
368  ncwin->ssl_data);
369  if (res != NSERROR_OK) {
370  free(ncwin);
371  return res;
372  }
373 
374  /* Set up the certificate window event handling.
375  *
376  * (The action buttons are registered as button events, not OK and
377  * Cancel, as both need to carry out actions.)
378  */
379  ro_gui_wimp_event_set_user_data(ncwin->wh, ncwin);
388 
389  ro_gui_dialog_open_persistent(NULL, ncwin->wh, false);
390 
391  res = cert_attach_pane(ncwin->wh, ncwin->core.wh);
392  if (res != NSERROR_OK) {
394  }
395 
396  return res;
397 }
398 
399 
400 /* exported interface documented in riscos/sslcert.h */
402 {
403  /* Load template for the SSL certificate window */
405 
406  /* load template for ssl treeview pane and adjust the window flags. */
408 
409  cert_tree_template->flags &= ~(wimp_WINDOW_MOVEABLE |
410  wimp_WINDOW_BACK_ICON |
411  wimp_WINDOW_CLOSE_ICON |
412  wimp_WINDOW_TITLE_ICON |
413  wimp_WINDOW_SIZE_ICON |
414  wimp_WINDOW_TOGGLE_ICON);
415 }
void ro_gui_cert_initialise(void)
Load and initialise the certificate window template.
Definition: sslcert.c:401
Target independent plotting interface.
nserror sslcert_viewer_create_session_data(struct nsurl *url, nserror(*cb)(bool proceed, void *pw), void *cbpw, const struct cert_chain *chain, struct sslcert_session_data **ssl_d)
Create ssl certificate viewer session data.
static nserror cert_attach_pane(wimp_w parent, wimp_w pane)
Attach tree window as a pane to ssl window.
Definition: sslcert.c:158
nserror gui_cert_verify(nsurl *url, const struct cert_chain *chain, nserror(*cb)(bool proceed, void *pw), void *cbpw)
Prompt the user to verify a certificate with issuse.
Definition: sslcert.c:314
const struct plotter_table ro_plotters
RISC OS plotter operation table.
Definition: plotters.c:713
browser_mouse_state
Mouse state.
Definition: mouse.h:43
Functionality is not implemented.
Definition: errors.h:61
static wimp_window * cert_tree_template
riscos template for certificate tree pane.
Definition: sslcert.c:64
#define PTR_WIMP_OPEN(pstate)
Definition: wimputils.h:36
nserror sslcert_viewer_accept(struct sslcert_session_data *ssl_d)
Accept a certificate chain.
Memory exhaustion.
Definition: errors.h:32
ro core window state
Definition: corewindow.h:37
static void ro_gui_cert_reject(wimp_pointer *pointer)
Handle rejection of certificate via event callback.
Definition: sslcert.c:120
static nserror cert_mouse(struct ro_corewindow *ro_cw, browser_mouse_state mouse_state, int x, int y)
callback for mouse event on ro certificate viewer window
Definition: sslcert.c:300
#define ICON_SSL_REJECT
Definition: sslcert.c:43
static void ro_gui_cert_close_window(wimp_w w)
Callback to handle the closure of the SSL dialogue by other means.
Definition: sslcert.c:136
nserror ro_corewindow_fini(struct ro_corewindow *ro_cw)
finalise elements of ro core window.
Definition: corewindow.c:1046
RISC OS core window interface.
void ro_gui_dialog_close(wimp_w close)
Close a dialog box.
Definition: dialog.c:334
Automated RISC OS WIMP event handling (interface).
bool ro_gui_wimp_event_register_close_window(wimp_w w, void(*callback)(wimp_w w))
Register a function to be called after the window has been closed.
Definition: wimp_event.c:1492
struct sslcert_session_data * ssl_data
SSL certificate viewer context data.
Definition: sslcert.c:56
int ro_plot_origin_x
Definition: plotters.c:40
RISC OS SSL certificate viewer interface.
void sslcert_viewer_redraw(struct sslcert_session_data *ssl_d, int x, int y, struct rect *clip, const struct redraw_context *ctx)
Redraw the ssl certificate viewer.
wimp_w wh
window handle
Definition: corewindow.h:39
bool sslcert_viewer_keypress(struct sslcert_session_data *ssl_d, uint32_t key)
Key press handling.
bool interactive
Redraw to show interactive features.
Definition: plotters.h:59
nserror sslcert_viewer_init(struct core_window_callback_table *cw_t, void *core_window_handle, struct sslcert_session_data *ssl_d)
Initialise a ssl certificate viewer from session data.
nserror
Enumeration of error codes.
Definition: errors.h:29
Initialisation failed.
Definition: errors.h:38
No error.
Definition: errors.h:30
wimp_window * ro_gui_dialog_load_template(const char *template_name)
Load a template without creating a window.
Definition: dialog.c:241
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:115
wimp_w parent
Definition: dialog.c:87
bool no_font_blending
Definition: font.c:48
void * ro_gui_wimp_event_get_user_data(wimp_w w)
Gets the user data associated with a window.
Definition: wimp_event.c:486
nserror(* draw)(struct ro_corewindow *ro_cw, int originx, int originy, struct rect *r)
callback to draw on drawable area of ro core window
Definition: corewindow.h:68
nserror ro_corewindow_init(struct ro_corewindow *ro_cw, const struct button_bar_buttons *tb_buttons, char *tb_order, theme_style tb_style, const char *tb_help)
initialise elements of riscos core window.
Definition: corewindow.c:970
SSL Certificate verification UI interface.
bool ro_gui_wimp_event_set_user_data(wimp_w w, void *user)
Sets the user data associated with a window.
Definition: wimp_event.c:467
#define ICON_SSL_PANE
Definition: sslcert.c:42
static void ro_gui_cert_accept(wimp_pointer *pointer)
Handle acceptance of certificate via event callback.
Definition: sslcert.c:104
int ro_get_hscroll_height(wimp_w w)
Gets the horizontal scrollbar height.
Definition: wimp.c:58
nserror(* key)(struct ro_corewindow *ro_cw, uint32_t nskey)
callback for keypress on ro core window
Definition: corewindow.h:79
nserror sslcert_viewer_fini(struct sslcert_session_data *ssl_d)
Finalise a ssl certificate viewer.
int ro_plot_origin_y
Definition: plotters.c:41
X509 certificate chain.
Definition: ssl_certs.h:59
Redraw context.
Definition: plotters.h:51
nserror(* mouse)(struct ro_corewindow *ro_cw, browser_mouse_state mouse_state, int x, int y)
callback for mouse event on ro core window
Definition: corewindow.h:90
static wimp_window * dialog_cert_template
riscos dialog template for certificate viewer window.
Definition: sslcert.c:61
void ro_gui_dialog_open_persistent(wimp_w parent, wimp_w w, bool pointer)
Open a persistent dialog box relative to the pointer.
Definition: dialog.c:590
static nserror cert_key(struct ro_corewindow *ro_cw, uint32_t nskey)
callback for keypress on ro certificate viewer window
Definition: sslcert.c:278
General RISC OS WIMP/OS library functions (interface).
static void ro_gui_cert_release_window(struct ro_cert_window *certw)
Handle closing of the RISC OS certificate verification dialog.
Definition: sslcert.c:74
struct ro_corewindow core
Definition: sslcert.c:50
Rectangle coordinates.
Definition: types.h:40
static nserror cert_draw(struct ro_corewindow *ro_cw, int originx, int originy, struct rect *r)
Callback to draw on drawable area of ro certificate viewer window.
Definition: sslcert.c:248
struct core_window_callback_table * cb_table
table of callbacks for core window operations
Definition: corewindow.h:57
int ro_get_vscroll_width(wimp_w w)
Gets the vertical scrollbar width.
Definition: wimp.c:70
RISC OS certificate viewer context.
Definition: sslcert.c:49
nserror sslcert_viewer_reject(struct sslcert_session_data *ssl_d)
Reject a certificate chain.
void ro_gui_wimp_event_finalise(wimp_w w)
Free any resources associated with a window.
Definition: wimp_event.c:296
ssl certificate verification context.
A collection of grubby utilities for working with OSLib&#39;s wimp API.
#define ICON_SSL_ACCEPT
Definition: sslcert.c:44
NetSurf URL handling (interface).
void sslcert_viewer_mouse_action(struct sslcert_session_data *ssl_d, browser_mouse_state mouse, int x, int y)
Handles all kinds of mouse action.
struct nsurl nsurl
NetSurf URL object.
Definition: nsurl.h:31
bool ro_gui_wimp_event_register_button(wimp_w w, wimp_i i, void(*callback)(wimp_pointer *pointer))
Register a function to be called when a particular button is pressed.
Definition: wimp_event.c:1387
wimp_w wh
certificate view window handle
Definition: sslcert.c:53
Window toolbars (interface).