8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  | 
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  | 
-
-
-
-
-
-
-
+
+
+
-
-
-
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
  | 
#include <ppapi/cpp/module.h>
#include <ppapi/cpp/core.h>
#include <ppapi/cpp/var.h>
#include <ppapi/cpp/var_dictionary.h>
#include <ppapi/cpp/var_array.h>
#include <ppapi/cpp/var_array_buffer.h>
#include <stdio.h>
#include "pcsc-nacl.h"
#include "cackey-chrome.h"
class CACKeyInstance : public pp::Instance {
	private:
		void pcscNaClInitWrapper(pp::Core *core) {
			fprintf(stderr, "Calling pcscNaClInit(%p, %p)\n", this, core);
			pcscNaClInit(this, core);
			fprintf(stderr, "pcscNaClInit terminated\n");
		}
	public:
		explicit CACKeyInstance(PP_Instance instance, pp::Core *core) : pp::Instance(instance) {
			std::thread(&CACKeyInstance::pcscNaClInitWrapper, this, core).detach();
		}
		virtual ~CACKeyInstance() {}
		virtual void HandleMessageThread(pp::VarDictionary *message) {
			int numCertificates, i;
			struct cackey_certificate *certificates;
			pp::VarDictionary *reply;
			pp::VarArray certificatesPPArray;
			pp::VarArrayBuffer *certificateContents;
			pp::Var command;
			pp::Var command, incomingCertificateContents;
			/*
			 * Extract the command
			 */
			command = message->Get("command");
			/*
			 * Do the thing we are being asked to do
			 */
			reply = new pp::VarDictionary();
			if (command.AsString() == "listcertificates") {
				numCertificates = cackey_chrome_listCertificates(&certificates);
				reply = new pp::VarDictionary();
				certificatesPPArray.SetLength(numCertificates);
				for (i = 0; i < numCertificates; i++) {
					certificateContents = new pp::VarArrayBuffer(certificates[i].certificate_len);
					memcpy(certificateContents->Map(), certificates[i].certificate, certificates[i].certificate_len);
					certificateContents->Unmap();
					certificatesPPArray.Set(i, *certificateContents);
				}
					delete certificateContents;
				}
				cackey_chrome_freeCertificates(certificates, numCertificates);
				reply->Set("status", "success");
				reply->Set("certificates", certificatesPPArray);
			} else if (command.AsString() == "sign") {
				reply = new pp::VarDictionary();
				reply->Set("status", "success");
			} else {
				reply = new pp::VarDictionary();
				if (!message->HasKey("certificate")) {
					reply->Set("status", "error");
					reply->Set("error", "Certificate not supplied");
				} else {
					incomingCertificateContents = message->Get("certificate");
					reply->Set("status", "error");
					reply->Set("error", "This function is not yet implemented");
				}
			} else {
				reply->Set("status", "error");
				reply->Set("error", "Invalid command");
			}
			/*
			 * If a message ID was sent in the request, include it in the reply
			 */
			if (message->HasKey("id")) {
				reply->Set("id", message->Get("id"));
			}
			/*
			 * Send the reply back to the requestor, hopefully they are waiting for this message
			 */
			PostMessage(*reply);
			delete reply;
			delete message;
			return;
		}
		virtual void HandleMessage(const pp::Var& messagePlain) {
  |