Diff

Differences From Artifact [5c1f40635b]:

To Artifact [f6b7a728d2]:


     1         -#include "ppapi/cpp/module.h"
            1  +/*
            2  + * Google's PCSC library requires us to write our module in C++ (thanks, Google)
            3  + * This library wraps the actual library, written in C.
            4  + */
            5  +
            6  +#include <thread>
            7  +#include <ppapi/cpp/instance.h>
            8  +#include <ppapi/cpp/module.h>
            9  +#include <ppapi/cpp/core.h>
           10  +#include <ppapi/cpp/var.h>
           11  +#include <ppapi/cpp/var_dictionary.h>
           12  +#include <ppapi/cpp/var_array.h>
           13  +#include <ppapi/cpp/var_array_buffer.h>
           14  +
           15  +#include <stdio.h>
           16  +
           17  +#include "pcsc-nacl.h"
           18  +#include "cackey-chrome.h"
           19  +
           20  +class CACKeyInstance : public pp::Instance {
           21  +	private:
           22  +		void pcscNaClInitWrapper(pp::Core *core) {
           23  +			fprintf(stderr, "Calling pcscNaClInit(%p, %p)\n", this, core);
           24  +
           25  +			pcscNaClInit(this, core);
           26  +
           27  +			fprintf(stderr, "pcscNaClInit terminated\n");
           28  +		}
           29  +	public:
           30  +		explicit CACKeyInstance(PP_Instance instance, pp::Core *core) : pp::Instance(instance) {
           31  +			std::thread(&CACKeyInstance::pcscNaClInitWrapper, this, core).detach();
           32  +		}
           33  +
           34  +		virtual ~CACKeyInstance() {}
           35  +
           36  +		virtual void HandleMessage(const pp::Var& messagePlain) {
           37  +			int numCertificates, i;
           38  +			struct cackey_certificate *certificates;
           39  +			pp::VarDictionary *message;
           40  +			pp::VarDictionary *reply;
           41  +			pp::VarArray certificatesPPArray;
           42  +			pp::VarArrayBuffer *certificateContents;
           43  +			pp::Var command, target;
           44  +
           45  +			/*
           46  +			 * The incoming message must be a dictionary
           47  +			 */
           48  +			if (!messagePlain.is_dictionary()) {
           49  +				return;
           50  +			}
           51  +
           52  +			/*
           53  +			 * Process the appropriate command from the incoming message
           54  +			 */
           55  +			message = new pp::VarDictionary(messagePlain);
           56  +
           57  +			/*
           58  +			 * Verify that this message is destined for us
           59  +			 */
           60  +			if (!message->HasKey("target")) {
           61  +				return;
           62  +			}
           63  +
           64  +			target = message->Get("target");
           65  +			if (target.AsString() != "cackey") {
           66  +				return;
           67  +			}
           68  +
           69  +			/*
           70  +			 * Determine what we are being asked to do
           71  +			 */
           72  +			if (!message->HasKey("command")) {
           73  +				return;
           74  +			}
           75  +			command = message->Get("command");
           76  +
           77  +			/*
           78  +			 * Do the thing we are being asked to do
           79  +			 */
           80  +			if (command.AsString() == "listcertificates") {
           81  +				numCertificates = cackey_chrome_listCertificates(&certificates);
           82  +
           83  +				reply = new pp::VarDictionary();
           84  +
           85  +				certificatesPPArray.SetLength(numCertificates);
           86  +
           87  +				for (i = 0; i < numCertificates; i++) {
           88  +					certificateContents = new pp::VarArrayBuffer(certificates[i].certificate_len);
           89  +
           90  +					memcpy(certificateContents->Map(), certificates[i].certificate, certificates[i].certificate_len);
           91  +
           92  +					certificateContents->Unmap();
           93  +
           94  +					certificatesPPArray.Set(i, *certificateContents);
           95  +				}
           96  +
           97  +				reply->Set("status", "success");
           98  +				reply->Set("certificates", certificatesPPArray);
           99  +			} else if (command.AsString() == "sign") {
          100  +				reply = new pp::VarDictionary();
          101  +
          102  +				reply->Set("status", "success");
          103  +			} else {
          104  +				reply = new pp::VarDictionary();
          105  +
          106  +				reply->Set("status", "error");
          107  +				reply->Set("error", "Invalid command");
          108  +			}
          109  +
          110  +			/*
          111  +			 * If a message ID was sent in the request, include it in the reply
          112  +			 */
          113  +			if (message->HasKey("id")) {
          114  +				reply->Set("id", message->Get("id"));
          115  +			}
          116  +
          117  +			/*
          118  +			 * Send the reply back to the requestor, hopefully they are waiting for this message
          119  +			 */
          120  +			PostMessage(*reply);
          121  +
          122  +			return;
          123  +		}
          124  +};
     2    125   
     3    126   class CACKeyModule : public pp::Module {
     4    127   	public:
     5         -		CACKeyModule(): pp::Module() {}
          128  +		CACKeyModule() : pp::Module() {}
     6    129   		virtual ~CACKeyModule() {}
     7    130   
     8    131   		virtual pp::Instance *CreateInstance(PP_Instance instance) {
     9         -			return(NULL);
          132  +			return(new CACKeyInstance(instance, core()));
    10    133   		}
    11    134   };
    12    135   
    13    136   namespace pp {
    14    137   	Module *CreateModule() {
    15         -		return(NULL);
          138  +		return(new CACKeyModule());
    16    139   	}
    17    140   }