|
| 1 | +/* pkcs7.cpp |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2022 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSL. |
| 6 | + * |
| 7 | + * wolfSSL 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; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfSSL is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | +#include "./h/pkcs7.h" |
| 22 | + |
| 23 | +Napi::Number sizeof_PKCS7(const Napi::CallbackInfo& info) |
| 24 | +{ |
| 25 | + Napi::Env env = info.Env(); |
| 26 | + |
| 27 | + return Napi::Number::New( env, sizeof( PKCS7 ) ); |
| 28 | +} |
| 29 | + |
| 30 | +Napi::Number typeof_Key_Sum(const Napi::CallbackInfo& info) |
| 31 | +{ |
| 32 | + int ret = -1; |
| 33 | + Napi::Env env = info.Env(); |
| 34 | + std::string type = info[0].As<Napi::String>().Utf8Value(); |
| 35 | + |
| 36 | + if ( strcmp( type.c_str(), "DSA" ) == 0 ) |
| 37 | + { |
| 38 | + ret = DSAk; |
| 39 | + } |
| 40 | + else if ( strcmp( type.c_str(), "RSA" ) == 0 ) |
| 41 | + { |
| 42 | + ret = RSAk; |
| 43 | + } |
| 44 | + else if ( strcmp( type.c_str(), "ECDSA" ) == 0 ) |
| 45 | + { |
| 46 | + ret = ECDSAk; |
| 47 | + } |
| 48 | + else if ( strcmp( type.c_str(), "ED25519" ) == 0 ) |
| 49 | + { |
| 50 | + ret = ED25519k; |
| 51 | + } |
| 52 | + else if ( strcmp( type.c_str(), "X25519" ) == 0 ) |
| 53 | + { |
| 54 | + ret = X25519k; |
| 55 | + } |
| 56 | + else if ( strcmp( type.c_str(), "ED448" ) == 0 ) |
| 57 | + { |
| 58 | + ret = ED448k; |
| 59 | + } |
| 60 | + else if ( strcmp( type.c_str(), "X448" ) == 0 ) |
| 61 | + { |
| 62 | + ret = X448k; |
| 63 | + } |
| 64 | + else if ( strcmp( type.c_str(), "DH" ) == 0 ) |
| 65 | + { |
| 66 | + ret = DHk; |
| 67 | + } |
| 68 | + else if ( strcmp( type.c_str(), "FALCON_LEVEL1" ) == 0 ) |
| 69 | + { |
| 70 | + ret = FALCON_LEVEL1k; |
| 71 | + } |
| 72 | + else if ( strcmp( type.c_str(), "FALCON_LEVEL5" ) == 0 ) |
| 73 | + { |
| 74 | + ret = FALCON_LEVEL5k; |
| 75 | + } |
| 76 | + |
| 77 | + return Napi::Number::New( env, ret ); |
| 78 | +} |
| 79 | + |
| 80 | +Napi::Number typeof_Hash_Sum(const Napi::CallbackInfo& info) |
| 81 | +{ |
| 82 | + int ret = -1; |
| 83 | + Napi::Env env = info.Env(); |
| 84 | + std::string type = info[0].As<Napi::String>().Utf8Value(); |
| 85 | + |
| 86 | + if ( strcmp( type.c_str(), "MD2" ) == 0 ) |
| 87 | + { |
| 88 | + ret = MD2h; |
| 89 | + } |
| 90 | + else if ( strcmp( type.c_str(), "MD5" ) == 0 ) |
| 91 | + { |
| 92 | + ret = MD5h; |
| 93 | + } |
| 94 | + else if ( strcmp( type.c_str(), "SHA" ) == 0 ) |
| 95 | + { |
| 96 | + ret = SHAh; |
| 97 | + } |
| 98 | + else if ( strcmp( type.c_str(), "SHA224" ) == 0 ) |
| 99 | + { |
| 100 | + ret = SHA224h; |
| 101 | + } |
| 102 | + else if ( strcmp( type.c_str(), "SHA256" ) == 0 ) |
| 103 | + { |
| 104 | + ret = SHA256h; |
| 105 | + } |
| 106 | + else if ( strcmp( type.c_str(), "SHA384" ) == 0 ) |
| 107 | + { |
| 108 | + ret = SHA384h; |
| 109 | + } |
| 110 | + else if ( strcmp( type.c_str(), "SHA512" ) == 0 ) |
| 111 | + { |
| 112 | + ret = SHA512h; |
| 113 | + } |
| 114 | + else if ( strcmp( type.c_str(), "SHA512_224" ) == 0 ) |
| 115 | + { |
| 116 | + ret = SHA512_224h; |
| 117 | + } |
| 118 | + else if ( strcmp( type.c_str(), "SHA512_256" ) == 0 ) |
| 119 | + { |
| 120 | + ret = SHA512_256h; |
| 121 | + } |
| 122 | + else if ( strcmp( type.c_str(), "SHA3_224" ) == 0 ) |
| 123 | + { |
| 124 | + ret = SHA3_224h; |
| 125 | + } |
| 126 | + else if ( strcmp( type.c_str(), "SHA3_256" ) == 0 ) |
| 127 | + { |
| 128 | + ret = SHA3_256h; |
| 129 | + } |
| 130 | + else if ( strcmp( type.c_str(), "SHA3_384" ) == 0 ) |
| 131 | + { |
| 132 | + ret = SHA3_384h; |
| 133 | + } |
| 134 | + else if ( strcmp( type.c_str(), "SHA3_512" ) == 0 ) |
| 135 | + { |
| 136 | + ret = SHA3_512h; |
| 137 | + } |
| 138 | + else if ( strcmp( type.c_str(), "SHAKE128" ) == 0 ) |
| 139 | + { |
| 140 | + ret = SHAKE128h; |
| 141 | + } |
| 142 | + else if ( strcmp( type.c_str(), "SHAKE256" ) == 0 ) |
| 143 | + { |
| 144 | + ret = SHAKE256h; |
| 145 | + } |
| 146 | + |
| 147 | + return Napi::Number::New( env, ret ); |
| 148 | +} |
| 149 | + |
| 150 | +Napi::Number bind_wc_PKCS7_Init(const Napi::CallbackInfo& info) |
| 151 | +{ |
| 152 | + int ret; |
| 153 | + Napi::Env env = info.Env(); |
| 154 | + PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() ); |
| 155 | + |
| 156 | + ret = wc_PKCS7_Init( pkcs7, NULL, INVALID_DEVID ); |
| 157 | + |
| 158 | + return Napi::Number::New( env, ret ); |
| 159 | +} |
| 160 | + |
| 161 | +Napi::Number bind_wc_PKCS7_InitWithCert(const Napi::CallbackInfo& info) |
| 162 | +{ |
| 163 | + int ret; |
| 164 | + Napi::Env env = info.Env(); |
| 165 | + PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() ); |
| 166 | + uint8_t* cert = info[1].As<Napi::Uint8Array>().Data(); |
| 167 | + int cert_size = info[2].As<Napi::Number>().Int32Value(); |
| 168 | + |
| 169 | + ret = wc_PKCS7_InitWithCert( pkcs7, cert, cert_size ); |
| 170 | + |
| 171 | + return Napi::Number::New( env, ret ); |
| 172 | +} |
| 173 | + |
| 174 | +Napi::Number bind_wc_PKCS7_AddCertificate(const Napi::CallbackInfo& info) |
| 175 | +{ |
| 176 | + int ret; |
| 177 | + Napi::Env env = info.Env(); |
| 178 | + PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() ); |
| 179 | + uint8_t* cert = info[1].As<Napi::Uint8Array>().Data(); |
| 180 | + int cert_size = info[2].As<Napi::Number>().Int32Value(); |
| 181 | + |
| 182 | + ret = wc_PKCS7_AddCertificate( pkcs7, cert, cert_size ); |
| 183 | + |
| 184 | + return Napi::Number::New( env, ret ); |
| 185 | +} |
| 186 | + |
| 187 | +Napi::Number bind_wc_PKCS7_EncodeData(const Napi::CallbackInfo& info) |
| 188 | +{ |
| 189 | + int ret; |
| 190 | + Napi::Env env = info.Env(); |
| 191 | + PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() ); |
| 192 | + uint8_t* data = info[1].As<Napi::Uint8Array>().Data(); |
| 193 | + int data_size = info[2].As<Napi::Number>().Int32Value(); |
| 194 | + uint8_t* key = info[3].As<Napi::Uint8Array>().Data(); |
| 195 | + int key_size = info[4].As<Napi::Number>().Int32Value(); |
| 196 | + uint8_t* output = info[5].As<Napi::Uint8Array>().Data(); |
| 197 | + int output_size = info[6].As<Napi::Number>().Int32Value(); |
| 198 | + |
| 199 | + pkcs7->content = data; |
| 200 | + pkcs7->contentSz = data_size; |
| 201 | + pkcs7->privateKey = key; |
| 202 | + pkcs7->privateKeySz = key_size; |
| 203 | + |
| 204 | + ret = wc_PKCS7_EncodeData( pkcs7, output, output_size ); |
| 205 | + |
| 206 | + return Napi::Number::New( env, ret ); |
| 207 | +} |
| 208 | + |
| 209 | +Napi::Number bind_wc_PKCS7_EncodeSignedData(const Napi::CallbackInfo& info) |
| 210 | +{ |
| 211 | + int ret; |
| 212 | + Napi::Env env = info.Env(); |
| 213 | + WC_RNG rng; |
| 214 | + PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() ); |
| 215 | + uint8_t* data = info[1].As<Napi::Uint8Array>().Data(); |
| 216 | + int data_size = info[2].As<Napi::Number>().Int32Value(); |
| 217 | + uint8_t* key = info[3].As<Napi::Uint8Array>().Data(); |
| 218 | + int key_size = info[4].As<Napi::Number>().Int32Value(); |
| 219 | + int key_sum = info[5].As<Napi::Number>().Int32Value(); |
| 220 | + int hash_sum = info[6].As<Napi::Number>().Int32Value(); |
| 221 | + uint8_t* output = info[7].As<Napi::Uint8Array>().Data(); |
| 222 | + int output_size = info[8].As<Napi::Number>().Int32Value(); |
| 223 | + |
| 224 | + wc_InitRng( &rng ); |
| 225 | + |
| 226 | + pkcs7->content = data; |
| 227 | + pkcs7->contentSz = data_size; |
| 228 | + pkcs7->privateKey = key; |
| 229 | + pkcs7->privateKeySz = key_size; |
| 230 | + pkcs7->encryptOID = key_sum; |
| 231 | + pkcs7->hashOID = hash_sum; |
| 232 | + pkcs7->rng = &rng; |
| 233 | + |
| 234 | + ret = wc_PKCS7_EncodeSignedData( pkcs7, output, output_size ); |
| 235 | + |
| 236 | + wc_FreeRng( &rng ); |
| 237 | + |
| 238 | + return Napi::Number::New( env, ret ); |
| 239 | +} |
| 240 | + |
| 241 | +Napi::Number bind_wc_PKCS7_VerifySignedData(const Napi::CallbackInfo& info) |
| 242 | +{ |
| 243 | + int ret; |
| 244 | + Napi::Env env = info.Env(); |
| 245 | + PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() ); |
| 246 | + uint8_t* in = info[1].As<Napi::Uint8Array>().Data(); |
| 247 | + int in_size = info[2].As<Napi::Number>().Int32Value(); |
| 248 | + |
| 249 | + ret = wc_PKCS7_VerifySignedData( pkcs7, in, in_size ); |
| 250 | + |
| 251 | + return Napi::Number::New( env, ret ); |
| 252 | +} |
| 253 | + |
| 254 | +void bind_wc_PKCS7_Free(const Napi::CallbackInfo& info) |
| 255 | +{ |
| 256 | + PKCS7* pkcs7 = (PKCS7*)( info[0].As<Napi::Uint8Array>().Data() ); |
| 257 | + |
| 258 | + wc_PKCS7_Free( pkcs7 ); |
| 259 | +} |
0 commit comments