diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2018-03-27 04:42:08 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-03-27 04:42:08 +0900 |
| commit | 7ee39a2f17849bd360def0437340c51583b5d720 (patch) | |
| tree | 78c43de8c51276541f956a95aa8ed3be78ac65b8 /src | |
| parent | Merge branch 'master' of https://github.com/syuilo/misskey (diff) | |
| parent | Add keypair to local account (diff) | |
| download | misskey-7ee39a2f17849bd360def0437340c51583b5d720.tar.gz misskey-7ee39a2f17849bd360def0437340c51583b5d720.tar.bz2 misskey-7ee39a2f17849bd360def0437340c51583b5d720.zip | |
Merge pull request #1303 from akihikodaki/key
Add keypair to local account
Diffstat (limited to 'src')
| -rw-r--r-- | src/api/models/user.ts | 2 | ||||
| -rw-r--r-- | src/api/private/signup.ts | 2 | ||||
| -rw-r--r-- | src/crypto_key.cc | 111 | ||||
| -rw-r--r-- | src/crypto_key.d.ts | 1 |
4 files changed, 116 insertions, 0 deletions
diff --git a/src/api/models/user.ts b/src/api/models/user.ts index 545747b50f..042f13b238 100644 --- a/src/api/models/user.ts +++ b/src/api/models/user.ts @@ -59,6 +59,7 @@ export type IUser = { is_suspended: boolean; keywords: string[]; account: { + keypair: string; email: string; links: string[]; password: string; @@ -160,6 +161,7 @@ export const pack = ( delete _user.latest_post; // Remove private properties + delete _user.account.keypair; delete _user.account.password; delete _user.account.token; delete _user.account.two_factor_temp_secret; diff --git a/src/api/private/signup.ts b/src/api/private/signup.ts index 902642425c..690f3001cc 100644 --- a/src/api/private/signup.ts +++ b/src/api/private/signup.ts @@ -1,6 +1,7 @@ import * as uuid from 'uuid'; import * as express from 'express'; import * as bcrypt from 'bcryptjs'; +import { generate as generateKeypair } from '../../crypto_key'; import recaptcha = require('recaptcha-promise'); import User, { IUser, validateUsername, validatePassword, pack } from '../models/user'; import generateUserToken from '../common/generate-native-user-token'; @@ -119,6 +120,7 @@ export default async (req: express.Request, res: express.Response) => { username: username, username_lower: username.toLowerCase(), account: { + keypair: generateKeypair(), token: secret, email: null, links: null, diff --git a/src/crypto_key.cc b/src/crypto_key.cc new file mode 100644 index 0000000000..c8e4d8f7f0 --- /dev/null +++ b/src/crypto_key.cc @@ -0,0 +1,111 @@ +#include <nan.h> +#include <openssl/bio.h> +#include <openssl/buffer.h> +#include <openssl/crypto.h> +#include <openssl/pem.h> +#include <openssl/rsa.h> +#include <openssl/x509.h> + +NAN_METHOD(extractPublic) +{ + const auto sourceString = info[0]->ToString(); + if (!sourceString->IsOneByte()) { + Nan::ThrowError("Malformed character found"); + return; + } + + size_t sourceLength = sourceString->Length(); + const auto sourceBuf = new char[sourceLength]; + + Nan::DecodeWrite(sourceBuf, sourceLength, sourceString); + + const auto source = BIO_new_mem_buf(sourceBuf, sourceLength); + if (source == nullptr) { + Nan::ThrowError("Memory allocation failed"); + delete sourceBuf; + return; + } + + const auto rsa = PEM_read_bio_RSAPrivateKey(source, nullptr, nullptr, nullptr); + + BIO_free(source); + delete sourceBuf; + + if (rsa == nullptr) { + Nan::ThrowError("Decode failed"); + return; + } + + const auto destination = BIO_new(BIO_s_mem()); + if (destination == nullptr) { + Nan::ThrowError("Memory allocation failed"); + return; + } + + const auto result = PEM_write_bio_RSAPublicKey(destination, rsa); + + RSA_free(rsa); + + if (result != 1) { + Nan::ThrowError("Public key extraction failed"); + BIO_free(destination); + return; + } + + char *pem; + const auto pemLength = BIO_get_mem_data(destination, &pem); + + info.GetReturnValue().Set(Nan::Encode(pem, pemLength)); + BIO_free(destination); +} + +NAN_METHOD(generate) +{ + const auto exponent = BN_new(); + const auto mem = BIO_new(BIO_s_mem()); + const auto rsa = RSA_new(); + char *data; + long result; + + if (exponent == nullptr || mem == nullptr || rsa == nullptr) { + Nan::ThrowError("Memory allocation failed"); + goto done; + } + + result = BN_set_word(exponent, 65537); + if (result != 1) { + Nan::ThrowError("Exponent setting failed"); + goto done; + } + + result = RSA_generate_key_ex(rsa, 2048, exponent, nullptr); + if (result != 1) { + Nan::ThrowError("Key generation failed"); + goto done; + } + + result = PEM_write_bio_RSAPrivateKey(mem, rsa, NULL, NULL, 0, NULL, NULL); + if (result != 1) { + Nan::ThrowError("Key export failed"); + goto done; + } + + result = BIO_get_mem_data(mem, &data); + info.GetReturnValue().Set(Nan::Encode(data, result)); + +done: + RSA_free(rsa); + BIO_free(mem); + BN_free(exponent); +} + +NAN_MODULE_INIT(InitAll) +{ + Nan::Set(target, Nan::New<v8::String>("extractPublic").ToLocalChecked(), + Nan::GetFunction(Nan::New<v8::FunctionTemplate>(extractPublic)).ToLocalChecked()); + + Nan::Set(target, Nan::New<v8::String>("generate").ToLocalChecked(), + Nan::GetFunction(Nan::New<v8::FunctionTemplate>(generate)).ToLocalChecked()); +} + +NODE_MODULE(crypto_key, InitAll); diff --git a/src/crypto_key.d.ts b/src/crypto_key.d.ts new file mode 100644 index 0000000000..28ac2f9683 --- /dev/null +++ b/src/crypto_key.d.ts @@ -0,0 +1 @@ +export function generate(): String; |