node icon indicating copy to clipboard operation
node copied to clipboard

crypto: add extensions property to X509Certificate

Open mertcanaltin opened this issue 1 year ago • 28 comments

This pull request adds an enhanced property to the X509Certificate class, providing additional functionality for X.509 certificates. The new property improves the handling of certificate subjects, issuers, and other related information. This enhancement enhances the overall capabilities and usability of the X509Certificate class

issue:#48730 fyi @jeffsec-aws

mertcanaltin avatar Jul 15 '23 12:07 mertcanaltin

Review requested:

  • [ ] @nodejs/crypto

nodejs-github-bot avatar Jul 15 '23 12:07 nodejs-github-bot

This needs tests.

mscdex avatar Jul 15 '23 13:07 mscdex

I believe the tests belong better to test/parallel/test-crypto-x509.js. A test for accessing the property when there are no extensions would also be welcome.

Not being familiar with extensions myself so apologizies if the following questions are just plain irrelevant. I wonder if an extension can be present multiple times, in which case how would such extension be returned? Are there structured extensions? Or extensions who's value is not a string?

@tniessen is there a reason you didn't expose this in the first place?

panva avatar Jul 15 '23 17:07 panva

When I look before the test, I get the output of x509.extensions undefined. Could it be because I cannot access it because it is undefined?

mertcanaltin avatar Jul 16 '23 08:07 mertcanaltin

I am unfamiliar with the underlying implementation.

panva avatar Jul 16 '23 10:07 panva

Nice work.

Extensions can be of various different types and content. Now they only appear one in terms of key (an OID see for example: https://access.redhat.com/documentation/en-us/red_hat_certificate_system/9/html/administration_guide/standard_x.509_v3_certificate_extensions) but can have multiple values in a form of an array.

jeffsec-aws avatar Jul 16 '23 14:07 jeffsec-aws

@mscdex @panva @tniessen @jeffsec-aws hello I will be very happy if you review it, thank you very much

mertcanaltin avatar Jul 17 '23 13:07 mertcanaltin

@panva How can I generate a certificate with extensions? I will use it in my test instead of hardcoded.

i will be using it in my test code

mertcanaltin avatar Jul 18 '23 13:07 mertcanaltin

@mertcanaltin I am really not able to be of that kind of assistance.

panva avatar Jul 18 '23 13:07 panva

What method do you want to use to generate those certificates? Would OpenSSL commands help?

jeffsec-aws avatar Jul 18 '23 13:07 jeffsec-aws

What method do you want to use to generate those certificates? Would OpenSSL commands help?

yes it will help

mertcanaltin avatar Jul 18 '23 14:07 mertcanaltin

Yes, I added this certificate to fixtures instead of hardcoding it and tried to fix my test @panva

mertcanaltin avatar Jul 31 '23 11:07 mertcanaltin

I'll quote my earlier concern which needs to be settled before this can progress.

Not being familiar with extensions myself so apologizies if the following questions are just plain irrelevant. I wonder if an extension can be present multiple times, in which case how would such extension be returned? Are there structured extensions? Or extensions who's value is not a string?

These examples would need tests added and the documentation for this method also has to be added.

And I answered to you. Extensions are referenced by OID so the key always appears only once. Now the value can be array of value for some extensions like OCSP Responder URL

jeffsec-aws avatar Jul 31 '23 12:07 jeffsec-aws

Hello @jeffsec-aws,@panva I would appreciate it if you could review this Pull Request. Thank you!

mertcanaltin avatar Aug 02 '23 10:08 mertcanaltin

@tniessen can you take a look?

panva avatar Aug 02 '23 14:08 panva

@anonrig Thank you very much for your suggestion, it gave me a new perspective

mertcanaltin avatar Aug 06 '23 11:08 mertcanaltin

Hello, I've added a GetCertificateExtensions method on the C++ side to populate extensions. After using this method, the extensions object looks like this:

 extensions: {
    subjectKeyIdentifier: '\x04\x14\x16J`?15x���K\x0Fq\x1D ����5',
    authorityKeyIdentifier: '0\x16�\x14\x16J`?15x���K\x0Fq\x1D ����5',
    basicConstraints: '0\x03\x01\x01�',
    subjectAltName: '0\x12�\x10evil.example.com'
  }

However, I'm not sure if I need to convert this to UTF-8. I would greatly appreciate your feedback and advice on this matter. Thank you once again

mertcanaltin avatar Aug 20 '23 13:08 mertcanaltin

I think I have a character encoding issue, and I need to solve this.

NOTE: The test started as a child_process using these flags: [ '--expose-internals' ] Use NODE_SKIP_FLAG_CHECK to run the test with the original flags.
node:assert:125
  throw new AssertionError(obj);
  ^

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
+ actual - expected

+ '0\x12�\x10evil.example.com'
- 'DNS:example.com, DNS:www.example.com'
    at Object.<anonymous> (/Users/mert/Desktop/nodejs/node/test/parallel/test-crypto-x509.js:370:10)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47 {
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: '0\x12�\x10evil.example.com',
  expected: 'DNS:example.com, DNS:www.example.com',
  operator: 'strictEqual'
}

Node.js v21.0.0-pre
➜  node git:(dev-48730) ./node test/parallel/test-crypto-x509.js
NOTE: The test started as a child_process using these flags: [ '--expose-internals' ] Use NODE_SKIP_FLAG_CHECK to run the test with the original flags.
node:assert:125
  throw new AssertionError(obj);
  ^

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
+ actual - expected

+ '0\x1E�\x0Bexample.com�\x0Fwww.example.com'
- 'DNS:example.com, DNS:www.example.com'
    at Object.<anonymous> (/Users/mert/Desktop/nodejs/node/test/parallel/test-crypto-x509.js:370:10)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    at node:internal/main/run_main_module:23:47 {
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: '0\x1E�\x0Bexample.com�\x0Fwww.example.com',
  expected: 'DNS:example.com, DNS:www.example.com',
  operator: 'strictEqual'
}
Node.js v21.0.0-pre

mertcanaltin avatar Aug 21 '23 13:08 mertcanaltin

Hello, I've solved the issue and improved my code. I'm very happy about it!

  • Implemented BIO objects for secure data handling.
  • Used OpenSSL functions for proper extension data printing.
  • Added error handling for potential issues.
  • Ensured careful memory management.
  • Checked and adjusted character encoding settings as needed.

mertcanaltin avatar Aug 21 '23 14:08 mertcanaltin

@nodejs/crypto This could probably use some reviews (or re-reviews for the people that have already reviewed it).

Trott avatar Sep 19 '23 15:09 Trott

greetings I triggered you guys again very sorry I wonder if I have a chance to get an update here

mertcanaltin avatar Nov 03 '23 07:11 mertcanaltin

I am unable to review the implementation, aside of that this lacks sufficient test coverage and update to documentation.

thank you very much for your suggestion, I updated the document @panva

mertcanaltin avatar Nov 03 '23 18:11 mertcanaltin

greetings I updated the document and I think I got stuck on a flakky test @panva

mertcanaltin avatar Nov 07 '23 15:11 mertcanaltin

Aside from my above suggestion to fix and simplify the docs entry I believe this lacks sufficient test coverage.

Note that I can not review the c++ implementation

panva avatar Nov 08 '23 22:11 panva

Aside from my above suggestion to fix and simplify the docs entry I believe this lacks sufficient test coverage.

Note that I can not review the c++ implementation

thank you very much for your suggestions, here I created a crypto-extensions.pem certificate for testing and tested the subjectAltName in the extension, in fact, I would be very happy if you have a suggestion.

mertcanaltin avatar Nov 12 '23 13:11 mertcanaltin

thank you very much for your suggestions I have applied a few changes and I hope I have followed the correct steps

mertcanaltin avatar Nov 19 '23 16:11 mertcanaltin

@tniessen greetings, I would be very happy if you could take a look when you have time, again many thanks for your support

mertcanaltin avatar Jan 28 '24 14:01 mertcanaltin

Greetings, I want to improve this place very much, do not hesitate to make suggestions when appropriate ❤️

mertcanaltin avatar Feb 09 '24 20:02 mertcanaltin

There are still a bunch of tests failing and some comments from @tniessen that have not been addressed

marco-ippolito avatar Feb 27 '24 22:02 marco-ippolito

@tniessen Hello, I would like to move this place forward, do you have an update?

mertcanaltin avatar Aug 08 '24 13:08 mertcanaltin