forked from guanzhi/GmSSL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGmSSLObject.c
More file actions
275 lines (229 loc) · 7.55 KB
/
Copy pathGmSSLObject.c
File metadata and controls
275 lines (229 loc) · 7.55 KB
1
2
3
4
5
6
7
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* ====================================================================
* Copyright (c) 2016 - 2019 The GmSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the GmSSL Project.
* (http://gmssl.org/)"
*
* 4. The name "GmSSL Project" must not be used to endorse or promote
* products derived from this software without prior written
* permission. For written permission, please contact
* guanzhi1980@gmail.com.
*
* 5. Products derived from this software may not be called "GmSSL"
* nor may "GmSSL" appear in their names without prior written
* permission of the GmSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the GmSSL Project
* (http://gmssl.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/is_gmssl.h>
#include "GmSSLObject.h"
/*
* interface gmssl {
* readonly attribute DOMString version;
* DOMString encrypt(in DOMString algor, in DOMString plaintext, in DOMString public_key);
* DOMString decrypt(in DOMString algor, in DOMString ciphertext, in DOMString private_key);
* };
*/
const char *prog = "gmssl";
static bool identifiersInitialized = false;
#define GMSSL_VERSION "GmSSL JavaScript API 1.0 Feb 3 2019"
#define GMSSL_PROPERTY_VERSION 0
#define GMSSL_NUM_PROPERTIES 1
static NPIdentifier gmsslPropertyIdentifiers[GMSSL_NUM_PROPERTIES];
static const NPUTF8 *gmsslPropertyNames[GMSSL_NUM_PROPERTIES] = {
"version",
};
#define GMSSL_METHOD_KEYGEN 0
#define GMSSL_METHOD_ENCRYPT 1
#define GMSSL_METHOD_DECRYPT 2
#define GMSSL_NUM_METHODS 3
static NPIdentifier gmsslMethodIdentifiers[GMSSL_NUM_METHODS];
static const NPUTF8 *gmsslMethodNames[GMSSL_NUM_METHODS] = {
"sm3",
"sm4cbcencrypt",
"sm4cbcdecrypt",
};
static bool do_keygen(const NPVariant algor, NPVariant *result);
static bool do_encrypt(const NPVariant algor, const NPVariant plaintext, const NPVariant pubkey, NPVariant *result);
static bool do_decrypt(const NPVariant algor, const NPVariant ciphertext, const NPVariant privkey, NPVariant *result);
static NPObject *gmsslAllocate(NPP npp, NPClass *theClass)
{
GmSSLObject *newInstance = (GmSSLObject *)malloc(sizeof(GmSSLObject));
if (!identifiersInitialized) {
browser->getstringidentifiers(gmsslPropertyNames,
GMSSL_NUM_PROPERTIES, gmsslPropertyIdentifiers);
browser->getstringidentifiers(gmsslMethodNames,
GMSSL_NUM_METHODS, gmsslMethodIdentifiers);
identifiersInitialized = true;
}
return &newInstance->header;
}
static void gmsslDeallocate(NPObject *obj)
{
free(obj);
}
static void gmsslInvalidate(NPObject *obj)
{
}
static bool gmsslHasMethod(NPObject *obj, NPIdentifier name)
{
int i;
fprintf(stderr, "HashMethod(%s)\n", browser->utf8fromidentifier(name));
for (i = 0; i < GMSSL_NUM_METHODS; i++) {
if (name == gmsslMethodIdentifiers[i])
return true;
else
fprintf(stderr, "HashMethod(%s)\n", browser->utf8fromidentifier(name));
}
return false;
}
static bool gmsslInvoke(NPObject *obj, NPIdentifier name, const NPVariant *args,
uint32_t argCount, NPVariant *variant)
{
if (name == gmsslMethodIdentifiers[GMSSL_METHOD_KEYGEN]) {
if (argCount != 1) {
fprintf(stderr, "GmSSLObject: bad arguments\n");
return false;
}
return do_keygen(args[0], variant);
}
if (name == gmsslMethodIdentifiers[GMSSL_METHOD_ENCRYPT]) {
if (argCount != 3) {
fprintf(stderr, "%s: bad arguments", "prog");
return false;
}
return do_encrypt(args[0], args[1], args[2], variant);
}
if (name == gmsslMethodIdentifiers[GMSSL_METHOD_DECRYPT]) {
if (argCount != 3) {
fprintf(stderr, "%s: bad argument count\n", "prog");
return false;
}
return do_decrypt(args[0], args[1], args[2], variant);
}
return false;
}
static bool gmsslInvokeDefault(NPObject *obj, const NPVariant *args,
uint32_t argCount, NPVariant *result)
{
return false;
}
static bool gmsslHasProperty(NPObject *obj, NPIdentifier name)
{
int i;
for (i = 0; i < GMSSL_NUM_PROPERTIES; i++)
if (name == gmsslPropertyIdentifiers[i])
return true;
return false;
}
static bool gmsslGetProperty(NPObject *obj, NPIdentifier name, NPVariant *variant)
{
//GmSSLObject *gmsslObject = (GmSSLObject *)obj;
fprintf(stderr, "%s: cryptoGetProperty(%s)\n", prog, browser->utf8fromidentifier(name));
if (name == gmsslPropertyIdentifiers[GMSSL_PROPERTY_VERSION]) {
STRINGZ_TO_NPVARIANT(strdup(GMSSL_VERSION), *variant);
return true;
}
return false;
}
static bool gmsslSetProperty(NPObject *obj, NPIdentifier name,
const NPVariant *variant)
{
return false;
}
static NPClass gmsslClass = {
NP_CLASS_STRUCT_VERSION,
gmsslAllocate,
gmsslDeallocate,
gmsslInvalidate,
gmsslHasMethod,
gmsslInvoke,
gmsslInvokeDefault,
gmsslHasProperty,
gmsslGetProperty,
gmsslSetProperty,
};
NPClass *getGmSSLClass(void)
{
return &gmsslClass;
}
static bool do_keygen(const NPVariant algor, NPVariant *result)
{
bool ret = false;
/* set the default return value */
NULL_TO_NPVARIANT(*result);
if (!NPVARIANT_IS_STRING(algor)) {
goto end;
}
if (!(alg_str = NPVARIANT_TO_STRING(algor).UTF8Characters)
|| strlen(alg_str) <= 0
|| !(alg =
return ret;
}
static bool sms4_cbc_encrypt(const NPVariant plaintext, const NPVariant key, NPVariant *result)
{
}
static bool sms4_cbc_decrypt(const NPVariant plaintext, const NPVariant key, NPVariant *result)
{
}
static bool sm2_sign(const NPVariant dgst, const NPVariant key, NPVariant *result)
{
}
static bool sm2_sign(const NPVariant dgst, const NPVariant sig, const NPVariant key, NPVariant *result)
{
}
static bool sm2_encrypt(const NPVariant plaintext, const NPVariant key, NPVariant *result)
{
}
static bool sm2_decrypt(const NPVariant plaintext, const NPVariant key, NPVariant *result)
{
}
static bool do_encrypt(const NPVariant algor, const NPVariant plaintext,
const NPVariant pubkey, NPVariant *result)
{
bool ret = false;
return ret;
}
static bool do_decrypt(const NPVariant algor, const NPVariant ciphertext,
const NPVariant privkey, NPVariant *result)
{
bool ret = false;
return ret;
}