Fix RSA to JWK

This commit is contained in:
microshine 2019-03-07 03:14:39 +03:00
parent 0328753920
commit ef48c12b6c
5 changed files with 33 additions and 14 deletions

View File

@ -1,6 +1,6 @@
import { AsnIntegerArrayBufferConverter, AsnIntegerConverter, AsnProp, AsnPropTypes } from "@peculiar/asn1-schema"; import { AsnIntegerConverter, AsnProp, AsnPropTypes } from "@peculiar/asn1-schema";
import { JsonProp } from "@peculiar/json-schema"; import { JsonProp } from "@peculiar/json-schema";
import { JsonBase64UrlArrayBufferConverter } from "../converters"; import { AsnIntegerArrayBufferConverter, JsonBase64UrlArrayBufferConverter } from "../converters";
// RFC 3437 // RFC 3437
// https://tools.ietf.org/html/rfc3447#appendix-A.1.2 // https://tools.ietf.org/html/rfc3447#appendix-A.1.2

View File

@ -1,6 +1,6 @@
import { AsnIntegerArrayBufferConverter, AsnIntegerConverter, AsnProp, AsnPropTypes } from "@peculiar/asn1-schema"; import { AsnProp, AsnPropTypes } from "@peculiar/asn1-schema";
import { JsonProp } from "@peculiar/json-schema"; import { JsonProp } from "@peculiar/json-schema";
import { JsonBase64UrlArrayBufferConverter } from "../converters"; import { AsnIntegerArrayBufferConverter, JsonBase64UrlArrayBufferConverter } from "../converters";
// RFC 3437 // RFC 3437
// https://tools.ietf.org/html/rfc3447#appendix-A.1.1 // https://tools.ietf.org/html/rfc3447#appendix-A.1.1

View File

@ -1 +1,2 @@
export * from "./base64_url"; export * from "./base64_url";
export * from "./integer_converter";

View File

@ -0,0 +1,18 @@
import { IAsnConverter } from "@peculiar/asn1-schema";
// tslint:disable-next-line: no-var-requires
const asn1 = require("asn1js");
export const AsnIntegerArrayBufferConverter: IAsnConverter<ArrayBuffer> = {
fromASN: (value: any) => {
const valueHex = value.valueBlock.valueHex;
return !(new Uint8Array(valueHex)[0])
? value.valueBlock.valueHex.slice(1)
: value.valueBlock.valueHex;
},
toASN: (value: ArrayBuffer) => {
const valueHex = new Uint8Array(value)[0] > 127
? Buffer.concat([Buffer.from([0]), Buffer.from(value)])
: Buffer.from(value);
return new asn1.Integer({ valueHex: new Uint8Array(valueHex).buffer });
},
};

View File

@ -9,16 +9,16 @@ context("ASN", () => {
context("PrivateKey", () => { context("PrivateKey", () => {
const bytes = Buffer.from("30820278020100300d06092a864886f70d0101010500048202623082025e02010002818100f73bb19904c72d3a3a837425d734190a26760151bdd6e15c948b21996e64a713dcd4cf7c0ac722e4ee589ed82ba9a8ab4f17b9283c5145422a02f35f140b586a8adfeb6f40bfc223fc1c777d692126f94f8abfac63bc67f593c8cd027326f70888fb9ac20d573cf65f90f3825156a64c34d964e0b7aba3fffe38b9cd773beea3020301000102818100b52ba83c42b5165e7a2c843b8a9521d83f50f02c8f59dcb17424f48d33c94c6a10c45dfb3f06a87a6c72c28c148af20fff189a5572c5f763c1d781b265c4de975bcfaa303e365e0e78784d64ea8345d34c988018c8868edef526da3cd9a458306a3c9f37545a645490bacf8dd040aa7aab52b0cd38c4744ddc556a8b6dbeeb51024100fc66e213e9785a33417dc830534644fcb85ebc70c3b4f6d9957637e0142879e489ec5a0fc38acaa579100d1fff585d2ad13a7f4280985e47534a36a369eed0c9024100fac1f2aa44b2dd000febaf75b9dd554068b0c6f15b41fada8089d7a52c447980498dcd95546454f0860b8a7747f40c0e7362f5a1a935a61327e4fb6298d2460b02410085dbf68882e89e45d2b4e7a7a1728201d9b1fc947d668b0828393336f6d9e4936d06595944e665c4ce68d1dd2769f0b75591858e7a6ed4895545e5a652f721e9024100a7a03339c346d6198e8afcf5f3d39383c4f837656c9dc4b5802ba52e53534aed893df3ea194a20c5d0b5b2505e9733e00d1e60193b6613a8c68879cbc560fb550240169d53d05b35edae1a5b48351b7281e12ef2f281c8026d64ed3d884249788830fc23fa45d73adb3224fa26f65e51b4838572583135de65fb13d360d9e3e19c74", "hex"); const bytes = Buffer.from("308204bd020100300d06092a864886f70d0101010500048204a7308204a30201000282010100faa3f76e5daf5dc288a54b7a99d7c7a9437581a184d80bb1aaf54cdfe245b28f52edb4db4888199e78ce993407bfd93c8102231b173eca09d44bb127c152b2e7f3a806852ed1dc323a1a33a5fecc0217ea98ddeb7068c14ffbbbd14a7904b3657a663ab76d2d88b10db77bc1b22e8881e58665508af0c31da225d08f3b07ad05bd8cf4d25943bffd3bb57d4266582276089e126db030d8ef04e0c30eeb65c325558d98bfc43b2b0c52212f3294d3ad7a91fa2ec2e838b2e73a4db616ec6aad5592dfc046f7e3589741fa0f4662fcf4ffca767611475f05272b3ddbd09b757b56f0293089d39e2cdd35722cd69ea00b3181e6e7c5e531f6608db3da31ce82afad02030100010282010025698d3901adf8125e204244822b45e7dba4721da07d393da375ab2c6e139644338e3ce5508dd43925f23cc719f306a3b3e414466a715a6a1e30d0384d70a138e3536ce9bb63e2f8f2584fe652c2b3fb4aeed78d59c1a13d65a792e5896becb5549066ea53572d24b495f358a5d6b154a664a9c1dc8374b47b2c26d6026b3265b1d6e4448bd2253ce467ae99017c53af1fb085cdd5c8dc3cc66941beaa480295e907a936776f49e5e619d5e2e89e5a1bf220121c965b08b658d669464a4d0eb414efe11c8fa20987fae0758542ec69a9d335b01a78b8770b499272105629b4e81f04065644928f0b01bfb0294fbeb0e0e4e3ad6129d356fce820d35171126b1702818100fe042d914643ad84d8e5a0d0c3c7dc9b35ac60d96ef9305e74ead8419a1937c3a4d5b6b2d589f818ab0cfa4f6ee12fbeb85f7c117ba4eb489b31f0eecac4f8368b52b3339043a3160fa3535e1e45634947b582438149b062b73029bd2279793154153da0e120c48fe71c466783e6537ea9157d776bedd272e08fe4e961fe14ff02818100fc990a2846cf6c0f38ad798855a81f2386795425523f99a8660968be450564d97d62b58533bb9e65c36d56d28480b39bbbfaf7ddfdb8b08aa080740038b1786e659dfb342cdf197bcb8c40af1719814c734cec50b3e915cf927b6eb8e880d4073ab2d6c1e106c78e5add9042476f78edfb76d2d7ef5e1ae4c33f51c558691f5302818100b4a329f86e5c40700182427b534eb4add75c6f3f10b0ba59e191041a9ac82624c5fa88c2e2220c41169ad3025bda5d86a63c98d121f964ac2c593679c9ce8aa8d7290770babdaea348999ff6855658c5caede3e5b7723cb1e68da490f08c2bc80d8051642fd48a93bf09177413935e7aeb28f22153aa3b0720749397f7eca4e702818067661215589f11c1cd569d98245014a70b25e13f01c30d1834e4871ed3cc18733af34c10c1937c8c7589ed6f7153e9b1c72a3d8a7e90ba9b9485e07632bedae87dea44692031171268c8f9b572843b3c5b3a52c5da4f80611eba2e21bcf2f7581a3c18d2f6553b1cd7af389d18f6d58ebd4fef90fae80fa433145959aa0e260702818045ef370e79be4352cae716a92244f37f0b4a5133442b49de4a8bc5342d40a00eed284dcb5061d6dcde01baa12fde1ee965f66acabf58bd08d2f78c8f5f00a9156242ea971940611c8f9892335e48e211d2667aa0c5186af712cbab48802f2fc37488316e72d8dfd28a9e311e962fba79324e14d61a61d4afc4646da76dc650ae", "hex");
const json = { const json = {
n: "APc7sZkExy06OoN0Jdc0GQomdgFRvdbhXJSLIZluZKcT3NTPfArHIuTuWJ7YK6moq08XuSg8UUVCKgLzXxQLWGqK3-tvQL_CI_wcd31pISb5T4q_rGO8Z_WTyM0Ccyb3CIj7msINVzz2X5DzglFWpkw02WTgt6uj__44uc13O-6j", d: "JWmNOQGt-BJeIEJEgitF59ukch2gfTk9o3WrLG4TlkQzjjzlUI3UOSXyPMcZ8wajs-QURmpxWmoeMNA4TXChOONTbOm7Y-L48lhP5lLCs_tK7teNWcGhPWWnkuWJa-y1VJBm6lNXLSS0lfNYpdaxVKZkqcHcg3S0eywm1gJrMmWx1uREi9IlPORnrpkBfFOvH7CFzdXI3DzGaUG-qkgClekHqTZ3b0nl5hnV4uieWhvyIBIcllsItljWaUZKTQ60FO_hHI-iCYf64HWFQuxpqdM1sBp4uHcLSZJyEFYptOgfBAZWRJKPCwG_sClPvrDg5OOtYSnTVvzoINNRcRJrFw",
dp: "tKMp-G5cQHABgkJ7U060rddcbz8QsLpZ4ZEEGprIJiTF-ojC4iIMQRaa0wJb2l2GpjyY0SH5ZKwsWTZ5yc6KqNcpB3C6va6jSJmf9oV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APrB8qpEst0AD-uvdbndVUBosMbxW0H62oCJ16UsRHmASY3NlVRkVPCGC4p3R_QMDnNi9aGpNaYTJ-T7YpjSRgs", q: "_JkKKEbPbA84rXmIVagfI4Z5VCVSP5moZglovkUFZNl9YrWFM7ueZcNtVtKEgLObu_r33f24sIqggHQAOLF4bmWd-zQs3xl7y4xArxcZgUxzTOxQs-kVz5J7brjogNQHOrLWweEGx45a3ZBCR2947ft20tfvXhrkwz9RxVhpH1M",
dp: "AIXb9oiC6J5F0rTnp6FyggHZsfyUfWaLCCg5Mzb22eSTbQZZWUTmZcTOaNHdJ2nwt1WRhY56btSJVUXlplL3Iek", qi: "Re83Dnm-Q1LK5xapIkTzfwtKUTNEK0neSovFNC1AoA7tKE3LUGHW3N4BuqEv3h7pZfZqyr9YvQjS94yPXwCpFWJC6pcZQGEcj5iSM15I4hHSZnqgxRhq9xLLq0iALy_DdIgxbnLY39KKnjEeli-6eTJOFNYaYdSvxGRtp23GUK4",
dq: "AKegMznDRtYZjor89fPTk4PE-DdlbJ3EtYArpS5TU0rtiT3z6hlKIMXQtbJQXpcz4A0eYBk7ZhOoxoh5y8Vg-1U",
qi: "Fp1T0Fs17a4aW0g1G3KB4S7y8oHIAm1k7T2IQkl4iDD8I_pF1zrbMiT6JvZeUbSDhXJYMTXeZfsT02DZ4-GcdA",
}; };
it("parse", () => { it("parse", () => {
@ -45,9 +45,9 @@ context("ASN", () => {
context("PublicKey", () => { context("PublicKey", () => {
const bytes = Buffer.from("30819f300d06092a864886f70d010101050003818d0030818902818100f615b745314ffe4669255dfe68953184bb8e5db54eecd35b4c51ee899ce7e60aaf19cc765d924f94be93d6809ba506fab26b9f8ef0cf6ab2aec1942da222992f8dad2e621845f014f9e831a529665faf0a9b8ca97356a602ce8d17cd3469aafa2de82546773540fa480510d1906c78c87b81850c26fdaeccce37cd5fdeba7e050203010001", "hex"); const bytes = Buffer.from("30820122300d06092a864886f70d01010105000382010f003082010a0282010100faa3f76e5daf5dc288a54b7a99d7c7a9437581a184d80bb1aaf54cdfe245b28f52edb4db4888199e78ce993407bfd93c8102231b173eca09d44bb127c152b2e7f3a806852ed1dc323a1a33a5fecc0217ea98ddeb7068c14ffbbbd14a7904b3657a663ab76d2d88b10db77bc1b22e8881e58665508af0c31da225d08f3b07ad05bd8cf4d25943bffd3bb57d4266582276089e126db030d8ef04e0c30eeb65c325558d98bfc43b2b0c52212f3294d3ad7a91fa2ec2e838b2e73a4db616ec6aad5592dfc046f7e3589741fa0f4662fcf4ffca767611475f05272b3ddbd09b757b56f0293089d39e2cdd35722cd69ea00b3181e6e7c5e531f6608db3da31ce82afad0203010001", "hex");
const json = { const json = {
n: "APYVt0UxT_5GaSVd_miVMYS7jl21TuzTW0xR7omc5-YKrxnMdl2ST5S-k9aAm6UG-rJrn47wz2qyrsGULaIimS-NrS5iGEXwFPnoMaUpZl-vCpuMqXNWpgLOjRfNNGmq-i3oJUZ3NUD6SAUQ0ZBseMh7gYUMJv2uzM43zV_eun4F", n: "-qP3bl2vXcKIpUt6mdfHqUN1gaGE2AuxqvVM3-JFso9S7bTbSIgZnnjOmTQHv9k8gQIjGxc-ygnUS7EnwVKy5_OoBoUu0dwyOhozpf7MAhfqmN3rcGjBT_u70Up5BLNlemY6t20tiLENt3vBsi6IgeWGZVCK8MMdoiXQjzsHrQW9jPTSWUO__Tu1fUJmWCJ2CJ4SbbAw2O8E4MMO62XDJVWNmL_EOysMUiEvMpTTrXqR-i7C6Diy5zpNthbsaq1Vkt_ARvfjWJdB-g9GYvz0_8p2dhFHXwUnKz3b0Jt1e1bwKTCJ054s3TVyLNaeoAsxgebnxeUx9mCNs9oxzoKvrQ",
e: "AQAB", e: "AQAB",
}; };