Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ASN.1 RSAPublicKey type as defined by PKCS#1 #257

Merged
merged 4 commits into from
Oct 17, 2022

Conversation

HwangTaehyun
Copy link
Contributor

@HwangTaehyun HwangTaehyun commented Aug 20, 2022

Currently, the jsencrypt package only supports ASN.1 SubjectPublicKeyInfo type as defined by X.509.
However, many native platforms' RSA key libraries still only support ASN.1 RSAPublicKey type as defined by PKCS#1. Thus, we also need to support ASN.1 RSAPublicKey type as defined by PKCS#1.
Thanks @travist for the JavaScript RSA Encryption package!

@HwangTaehyun HwangTaehyun changed the title Handle ASN.1 RSAPublicKey type as defined by PKCS#1 Support ASN.1 RSAPublicKey type as defined by PKCS#1 Aug 20, 2022
@HwangTaehyun
Copy link
Contributor Author

@travist Could you review this PR?

@HwangTaehyun
Copy link
Contributor Author

ping @travist

@travist
Copy link
Owner

travist commented Sep 19, 2022

Looks good! Thank you!

Since this is a pretty large change, I would like to see some automated tests written for these changes for me to accept this into the library. Please add some tests and I would be happy to include this contribution.

Thanks.

Travis.

@HwangTaehyun
Copy link
Contributor Author

HwangTaehyun commented Sep 24, 2022

@travist Thanks! Could you let me know how to run the test code? Docs have no information about the test. Also, there's no test script in the package.json

@HwangTaehyun
Copy link
Contributor Author

HwangTaehyun commented Sep 26, 2022

Test code does not work, so I enable test code for local using chai & dirty-chai & ts-mocha and add test script to package.json. Also, I add the unit test for pkcs#1 format pub key. As the OpenSSL only exports pubkey by X509 format, I generate pkcs#1 format pub key using ssh-keygen with type RSA. I refer to https://www.rfc-editor.org/rfc/rfc4716#section-3.4, https://stackoverflow.com/questions/18039401/how-can-i-transform-between-the-two-styles-of-public-key-format-one-begin-rsa.
Obviously, the last 2 unit test failed without the first code changes supporting PKCS#1.
Could you review the above code? @travist

@HwangTaehyun
Copy link
Contributor Author

HwangTaehyun commented Oct 3, 2022

I would like to do this PR as part of hacktoberfest. Would you add the label "HACKTOBERFEST-ACCEPTED" to this PR?

@HwangTaehyun
Copy link
Contributor Author

ping @travist
Thanks!

@travist travist changed the title Support ASN.1 RSAPublicKey type as defined by PKCS#1 HACKTOBERFEST-ACCEPTED: Support ASN.1 RSAPublicKey type as defined by PKCS#1 Oct 4, 2022
@travist
Copy link
Owner

travist commented Oct 4, 2022

Done! Thank you and have fun at hacktoberfest!

@HwangTaehyun
Copy link
Contributor Author

HwangTaehyun commented Oct 4, 2022

@travist Oh, thanks! However, it was not the title but this label!
image

@HwangTaehyun
Copy link
Contributor Author

HwangTaehyun commented Oct 4, 2022

Also, Could you review this PR? @travist

@travist travist changed the title HACKTOBERFEST-ACCEPTED: Support ASN.1 RSAPublicKey type as defined by PKCS#1 Support ASN.1 RSAPublicKey type as defined by PKCS#1 Oct 5, 2022
@travist
Copy link
Owner

travist commented Oct 8, 2022

I tried to pull this in and build but ran into issues.

First one was

➜  jsencrypt git:(master) ✗ npm run build

> [email protected] build
> npm run build:dev && npm run build:prod


> [email protected] build:dev
> tsc && tsc --project tsconfig-def.json && webpack

src/JSEncrypt.ts:3:38 - error TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' or 'nodenext'.

3 import version from './version.json' assert { type: 'json' };
                                       ~~~~~~~~~~~~~~~~~~~~~~~


Found 1 error in src/JSEncrypt.ts:3

I then changed the module to "esnext" in tsconfig-def.json but then ran into this

➜  jsencrypt git:(master) ✗ npm run build

> [email protected] build
> npm run build:dev && npm run build:prod


> [email protected] build:dev
> tsc && tsc --project tsconfig-def.json && webpack

[webpack-cli] Failed to load '/Users/travistidwell/Documents/projects/jsencrypt/webpack.config.js' config
[webpack-cli] ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/Users/travistidwell/Documents/projects/jsencrypt/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:https:///Users/travistidwell/Documents/projects/jsencrypt/webpack.config.js:1:14
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at async WebpackCLI.tryRequireThenImport (/Users/travistidwell/Documents/projects/jsencrypt/node_modules/webpack-cli/lib/webpack-cli.js:213:26)
    at async loadConfigByPath (/Users/travistidwell/Documents/projects/jsencrypt/node_modules/webpack-cli/lib/webpack-cli.js:1404:27)
    at async WebpackCLI.loadConfig (/Users/travistidwell/Documents/projects/jsencrypt/node_modules/webpack-cli/lib/webpack-cli.js:1510:38)
    at async WebpackCLI.createCompiler (/Users/travistidwell/Documents/projects/jsencrypt/node_modules/webpack-cli/lib/webpack-cli.js:1785:22)
    at async WebpackCLI.runWebpack (/Users/travistidwell/Documents/projects/jsencrypt/node_modules/webpack-cli/lib/webpack-cli.js:1890:20)

I then tried to change the "require" statements to "import" and then received the following

➜  jsencrypt git:(master) ✗ npm run build

> [email protected] build
> npm run build:dev && npm run build:prod


> [email protected] build:dev
> tsc && tsc --project tsconfig-def.json && webpack

(node:56866) ExperimentalWarning: Importing JSON modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
asset jsencrypt.js 3.73 KiB [compared for emit] (name: main)
runtime modules 670 bytes 3 modules
./lib/index.js 89 bytes [built] [code generated]

ERROR in ./lib/index.js 1:0-40
Module not found: Error: Can't resolve './JSEncrypt' in '/Users/travistidwell/Documents/projects/jsencrypt/lib'
Did you mean 'JSEncrypt.js'?
BREAKING CHANGE: The request './JSEncrypt' failed to resolve only because it was resolved as fully specified
(probably because the origin is a '*.mjs' file or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.
resolve './JSEncrypt' in '/Users/travistidwell/Documents/projects/jsencrypt/lib'
  using description file: /Users/travistidwell/Documents/projects/jsencrypt/package.json (relative path: ./lib)
    Field 'browser' doesn't contain a valid alias configuration
    using description file: /Users/travistidwell/Documents/projects/jsencrypt/package.json (relative path: ./lib/JSEncrypt)
      Field 'browser' doesn't contain a valid alias configuration
      /Users/travistidwell/Documents/projects/jsencrypt/lib/JSEncrypt doesn't exist

webpack 5.47.1 compiled with 1 error in 39 ms

I think this PR still needs some work.

HwangTaehyun and others added 4 commits October 9, 2022 00:09
…nit test

- Fix "to.have.keys" to "to.include.all.keys" for superset of [n, e] in pubkey verification unit test
- Change webpack.config.js and webpack.prod.js to esm
- Change tsconfig-def.json's module to esnext
- Get version from process.env.npm_package_version
@HwangTaehyun
Copy link
Contributor Author

HwangTaehyun commented Oct 8, 2022

@travist Oh, Sorry for that. I changed package.json's type to module due to test code's esm.
Currently, I fixed all problems. Could you check this again? Thank you!

  • Change webpack.config.js and webpack.prod.js to esm
  • Change tsconfig-def.json's module to esnext
  • Get version from process.env.npm_package_version

@HwangTaehyun
Copy link
Contributor Author

@travist Could you review that? I checked them and all scripts currently work!

@HwangTaehyun
Copy link
Contributor Author

ping @travist

@travist
Copy link
Owner

travist commented Oct 17, 2022

Looks good! Thanks!

@travist travist merged commit 7b14257 into travist:master Oct 17, 2022
@travist
Copy link
Owner

travist commented Oct 17, 2022

So I am not able to release this because it broke the application.

Try the following to reproduce...

Uncaught TypeError: JSEncrypt is not a constructor
    at generateKeys (index.html:183:19)
    at HTMLDocument.<anonymous> (index.html:213:5)
    at c (jquery.min.js:4:26036)
    at Object.fireWith [as resolveWith] (jquery.min.js:4:26840)
    at Function.ready (jquery.min.js:4:3305)
    at HTMLDocument.q (jquery.min.js:4:717)
generateKeys @ index.html:183
(anonymous) @ index.html:213
c @ jquery.min.js:4
fireWith @ jquery.min.js:4
ready @ jquery.min.js:4
q @ jquery.min.js:4

Not sure what you changed that would affect that but it will block release...

@HwangTaehyun
Copy link
Contributor Author

I'll check it now.
Sorry for that not checking Jekyll build.
Also, I'm going to add a pre-hook for checking commits later.

@HwangTaehyun
Copy link
Contributor Author

I found out that bin/jsencrypt.min.js is breaking when build.

@HwangTaehyun
Copy link
Contributor Author

HwangTaehyun commented Oct 17, 2022

When I remove the next line in webpack.config.js, then build successfully.

experiments: {
     outputModule: true,
 },

@HwangTaehyun
Copy link
Contributor Author

HwangTaehyun commented Oct 17, 2022

I run into a problem getting the version in the browser using the process variable.

process.env.npm_package_version;

I changed the original code which gets the version from package.json because the test script does not work well.

I will fix it today.

@HwangTaehyun
Copy link
Contributor Author

HwangTaehyun commented Oct 18, 2022

I opened a new PR! #263 @travist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants