NetSurf
cert.c
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Daniel Silverstone <dsilvers@digital-scurf.org>
3  *
4  * This file is part of NetSurf, http://www.netsurf-browser.org/
5  *
6  * NetSurf is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * NetSurf is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 
23 #include "utils/ring.h"
24 #include "utils/nsurl.h"
25 #include "content/urldb.h"
26 
27 #include "monkey/output.h"
28 #include "monkey/cert.h"
29 
30 struct monkey_cert {
32  uint32_t num;
33  nserror (*cb)(bool,void*);
34  void *cbpw;
36 };
37 
38 static struct monkey_cert *cert_ring = NULL;
39 static uint32_t cert_ctr = 0;
40 
41 nserror
43  const struct cert_chain *chain,
44  nserror (*cb)(bool proceed, void *pw),
45  void *cbpw)
46 {
47  struct monkey_cert *mcrt_ctx;
48 
49  mcrt_ctx = calloc(sizeof(*mcrt_ctx), 1);
50  if (mcrt_ctx == NULL) {
51  return NSERROR_NOMEM;
52  }
53 
54  mcrt_ctx->cb = cb;
55  mcrt_ctx->cbpw = cbpw;
56  mcrt_ctx->num = cert_ctr++;
57  mcrt_ctx->url = nsurl_ref(url);
58 
59  RING_INSERT(cert_ring, mcrt_ctx);
60 
61  moutf(MOUT_SSLCERT, "VERIFY CWIN %u URL %s",
62  mcrt_ctx->num, nsurl_access(url));
63 
64  return NSERROR_OK;
65 }
66 
67 
68 static struct monkey_cert *
69 monkey_find_sslcert_by_num(uint32_t sslcert_num)
70 {
71  struct monkey_cert *ret = NULL;
72 
73  RING_ITERATE_START(struct monkey_cert, cert_ring, c_ring) {
74  if (c_ring->num == sslcert_num) {
75  ret = c_ring;
76  RING_ITERATE_STOP(cert_ring, c_ring);
77  }
78  } RING_ITERATE_END(cert_ring, c_ring);
79 
80  return ret;
81 }
82 
83 static void free_sslcert_context(struct monkey_cert *mcrt_ctx) {
84  moutf(MOUT_SSLCERT, "DESTROY CWIN %u", mcrt_ctx->num);
85  RING_REMOVE(cert_ring, mcrt_ctx);
86  if (mcrt_ctx->url) {
87  nsurl_unref(mcrt_ctx->url);
88  }
89  free(mcrt_ctx);
90 }
91 
92 static void
93 monkey_sslcert_handle_go(int argc, char **argv)
94 {
95  struct monkey_cert *mcrt_ctx;
96 
97  if (argc != 3) {
98  moutf(MOUT_ERROR, "SSLCERT GO ARGS BAD");
99  return;
100  }
101 
102  mcrt_ctx = monkey_find_sslcert_by_num(atoi(argv[2]));
103  if (mcrt_ctx == NULL) {
104  moutf(MOUT_ERROR, "SSLCERT NUM BAD");
105  return;
106  }
107 
108  urldb_set_cert_permissions(mcrt_ctx->url, true);
109 
110  mcrt_ctx->cb(true, mcrt_ctx->cbpw);
111 
112  free_sslcert_context(mcrt_ctx);
113 }
114 
115 static void
116 monkey_sslcert_handle_destroy(int argc, char **argv)
117 {
118  struct monkey_cert *mcrt_ctx;
119 
120  if (argc != 3) {
121  moutf(MOUT_ERROR, "SSLCERT DESTROY ARGS BAD");
122  return;
123  }
124 
125  mcrt_ctx = monkey_find_sslcert_by_num(atoi(argv[2]));
126  if (mcrt_ctx == NULL) {
127  moutf(MOUT_ERROR, "SSLCERT NUM BAD");
128  return;
129  }
130 
131  mcrt_ctx->cb(false, mcrt_ctx->cbpw);
132 
133  free_sslcert_context(mcrt_ctx);
134 }
135 
136 void
137 monkey_sslcert_handle_command(int argc, char **argv)
138 {
139  if (argc == 1)
140  return;
141 
142  if (strcmp(argv[1], "DESTROY") == 0) {
143  monkey_sslcert_handle_destroy(argc, argv);
144  } else if (strcmp(argv[1], "GO") == 0) {
145  monkey_sslcert_handle_go(argc, argv);
146  } else {
147  moutf(MOUT_ERROR, "SSLCERT COMMAND UNKNOWN %s", argv[1]);
148  }
149 }
int moutf(enum monkey_output_type mout_type, const char *fmt,...)
Definition: output.c:40
uint32_t num
Definition: cert.c:32
static void free_sslcert_context(struct monkey_cert *mcrt_ctx)
Definition: cert.c:83
static uint32_t cert_ctr
Definition: cert.c:39
nserror(* cb)(bool, void *)
Definition: cert.c:33
static void monkey_sslcert_handle_go(int argc, char **argv)
Definition: cert.c:93
Interface to utility string handling.
Memory exhaustion.
Definition: errors.h:32
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: cert.c:42
#define RING_ITERATE_END(ring, iteratorptr)
Definition: ring.h:136
nsurl * url
Definition: cert.c:35
#define RING_INSERT(ring, element)
Insert the given item into the specified ring.
Definition: ring.h:40
void monkey_sslcert_handle_command(int argc, char **argv)
Definition: cert.c:137
struct monkey_cert * r_prev
Definition: cert.c:31
Ring list structure.
nserror
Enumeration of error codes.
Definition: errors.h:29
void * cbpw
Definition: cert.c:34
static void monkey_sslcert_handle_destroy(int argc, char **argv)
Definition: cert.c:116
No error.
Definition: errors.h:30
#define RING_REMOVE(ring, element)
Remove the given element from the specified ring.
Definition: ring.h:53
X509 certificate chain.
Definition: ssl_certs.h:59
void urldb_set_cert_permissions(struct nsurl *url, bool permit)
Set certificate verification permissions.
Definition: urldb.c:3458
nsurl * nsurl_ref(nsurl *url)
Increment the reference count to a NetSurf URL object.
#define RING_ITERATE_START(ringtype, ring, iteratorptr)
Definition: ring.h:127
struct monkey_cert * r_next
Definition: cert.c:31
static struct monkey_cert * cert_ring
Definition: cert.c:38
const char * nsurl_access(const nsurl *url)
Access a NetSurf URL object as a string.
NetSurf URL handling (interface).
struct nsurl nsurl
NetSurf URL object.
Definition: nsurl.h:31
static struct monkey_cert * monkey_find_sslcert_by_num(uint32_t sslcert_num)
Definition: cert.c:69
void nsurl_unref(nsurl *url)
Drop a reference to a NetSurf URL object.
#define RING_ITERATE_STOP(ring, iteratorptr)
Definition: ring.h:133
Unified URL information database internal interface.