Ephenodrom / Dart-Basic-Utils

A dart package for many helper methods fitting common situations
MIT License
364 stars 77 forks source link

help to decode generateRsaCsrPem with x509CertificateFromPem #21

Closed CyberDepot closed 4 years ago

CyberDepot commented 4 years ago

Hi, Thank you for this library i use X509Utils version ^2.6.2

i need to generate a certificate and keep public key on the certificate for share to other i want to use certificate for sign, verifiy sign and key exchange i wnat to give the certificate to customers (certificate with public key) i want to generate a qr code for sharing certificate then when i sign a string and send for them, customers will find out if I signed it or not or i want to use asymmetric encryption with certificate, i encrypt a string with certificate private key and send for customers and they can decrypt string with my certificate (if i give to them In the form of qr code)

i see the web, you examples, issues and you test but i cant decrypt pem certificate and find public key

i use this code

var dn = {
    'CN': 'basic-utils.dev',
    'O': 'Magic Company',
    'OU': 'org unit',
    'L': 'Fakecity',
    'S': 'FakeState',
    'C': 'DE',
};

keypair = CryptoUtils.generateRSAKeyPair();
var csr = X509Utils.generateRsaCsrPem(dn, keypair.privateKey, keypair.publicKey);

i generate certificate and check this on this link https://www.sslchecker.com/csr/decode

i put the certificate for you

-----BEGIN CERTIFICATE REQUEST-----
MIICvjCCAaYCAQAweTEYMBYGA1UEAwwPYmFzaWMtdXRpbHMuZGV2MRYwFAYDVQQK
DA1NYWdpYyBDb21wYW55MREwDwYDVQQLDAhvcmcgdW5pdDERMA8GA1UEBwwIRmFr
ZWNpdHkxEjAQBgNVBAgMCUZha2VTdGF0ZTELMAkGA1UEBhMCREUwggEiMA0GCSqG
SIb3DQEBAQUAA4IBDwAwggEKAoIBAQC2s27seyvJHmW5d3VZK1D9RMeK6oNio5N4
OJCOGB5LsQbthSjuREDErUzqGRqepdlRu4jQJaAXDiuKjO1w4wDAkBwGP9Lk1Eb9
Sz1+BopddUIh8IY5T4goixHm8Ggw7lB3TTY5W2HbzMiu5SvZ4JyBokutaITHpMPw
pYk5Uj7W283wamLc5POQs6+jhQ3DvrqSzSI7+gki9p+S5SDURfBIySKHXM0HkPaV
8bg1111Tu1jmv/VU7D5R/WyIdk81FT4G/Wf+2a67iR2BbftX3zMC3SvwBb0BbfcU
Y9ijtEjvh47C5wQixetZs5oZfG6r1t4P3lxXquP+BkJhFI8db/I5AgMBAAGgADAN
BgkqhkiG9w0BAQsFAAOCAQEAfufnpVds2kPQpE6b/8RHDc63MIYWbT+etu65gFHo
M212hGJiC9pVvlNbnjOy7CEYC9jJ2jxRkovTd/A7BS7NUzyQeBY7IsML9AYCqBtm
ycnKEXG0Q3Np0Ght1Zia/CbUAOELlzmIpODzFaT0vJxFDDgGAF0By8P5AreP4jAb
12HmqXPv/HC4es4umbL9LazlobN81bsK8fRu48eENmpElllLmO+bRBbxSx4FiuYk
eoBKJIIrPLnPVm63JJOGHEzVYC/Cy35y+m2LSkgB8RD5mwGfXcNOSns2oXLnKgAp
RVHI7dZJODNWfajnENel1acyNOrzDfmZymklrSj8P7J5Tw==
-----END CERTIFICATE REQUEST-----

i try to decode certificate with this code

X509CertificateData data = X509Utils.x509CertificateFromPem(csr);

String commonName = data.subject["2.5.4.3"];
print(commonName);

but give me this error

E/flutter ( 8688): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type 'ASN1Set' is not a subtype of type 'ASN1ObjectIdentifier' in type cast
E/flutter ( 8688): #0      X509Utils.x509CertificateFromPem (package:basic_utils/src/X509Utils.dart:245:53)

i use from this website and generate a certificate https://www.samltool.com/self_signed_certs.php

when i use CSR thats give me error but when i use X.509 cert thats work very good so i put X.509 cert for you

-----BEGIN CERTIFICATE-----
MIICfjCCAeegAwIBAgIBADANBgkqhkiG9w0BAQ0FADBcMQswCQYDVQQGEwJhZTEP
MA0GA1UECAwGVGVocmFuMQ8wDQYDVQQKDAZEYXJwb3UxDzANBgNVBAMMBk1vaHNl
bjEMMAoGA1UEBwwDZHNmMQwwCgYDVQQLDANzZGYwHhcNMjAwODE3MTUyNzAwWhcN
MjEwODE3MTUyNzAwWjBcMQswCQYDVQQGEwJhZTEPMA0GA1UECAwGVGVocmFuMQ8w
DQYDVQQKDAZEYXJwb3UxDzANBgNVBAMMBk1vaHNlbjEMMAoGA1UEBwwDZHNmMQww
CgYDVQQLDANzZGYwgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMGlklLCqPWU
afjzaj5fpszDYSbrL0YJ+y4pwtMuFevReuk7amM+JVxw2nKlIhYaQH1KNPGNfbmK
P4kWMeaXdRxsS6W4mu75TFGXhlufiIO1U75d5T/rLx72AXZGqzBfEGoaaR7LnPRJ
parv5m2R1E0N77ZbrVtxzSeSH6sh3B/3AgMBAAGjUDBOMB0GA1UdDgQWBBSXSqGK
ux8IE5m4NSriAEU9f35KuTAfBgNVHSMEGDAWgBSXSqGKux8IE5m4NSriAEU9f35K
uTAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBDQUAA4GBAKxX53UR2zUjhO4RWdG6
VJSFN462GixNVuMLNCUsHP+2an69xbS3uiFlZajhhjR51dWHo93kKfR/37afTtN4
UbevBk4LgAIS0CLXDTFQkzxQJDLUHTLBwQWtxFDHFc3TcBJmp5tUl5BsZASahVwS
NlYU3RWImVRogRKX9wt5WRt4
-----END CERTIFICATE-----

i dont know what is the problem but i need to generate a certificate Just like the top X.509 cert certificate if you know a package for generate this type please say to me if toy can help me to generate this certificate i will be very grateful please help me for solved the problem thank you

Ephenodrom commented 4 years ago

Hello @CyberDepot

This package is not for GENERATING X509 certificates. This package only helps you doing the following things :

So for me it looks like you want to create a self signed certificate, is this right ?

Tienisto commented 1 year ago

In case somebody come accross this. Here is an example to generate a X509 certificate using basic_utils v5.4.2:

  final keyPair = CryptoUtils.generateRSAKeyPair();
  final privateKey = keyPair.privateKey as RSAPrivateKey;
  final publicKey = keyPair.publicKey as RSAPublicKey;
  final dn = {
    'CN': 'Mr A.',
    'O': '',
    'OU': '',
    'L': '',
    'S': '',
    'C': '',
  };
  final csr = X509Utils.generateRsaCsrPem(dn, privateKey, publicKey);

 // this is the certificate in PEM format
  final certificate = X509Utils.generateSelfSignedCertificate(keyPair.privateKey, csr, 30);
CyberDepot commented 1 year ago

In case somebody come accross this. Here is an example to generate a X509 certificate using basic_utils v5.4.2:

  final keyPair = CryptoUtils.generateRSAKeyPair();
  final privateKey = keyPair.privateKey as RSAPrivateKey;
  final publicKey = keyPair.publicKey as RSAPublicKey;
  final dn = {
    'CN': 'Mr A.',
    'O': '',
    'OU': '',
    'L': '',
    'S': '',
    'C': '',
  };
  final csr = X509Utils.generateRsaCsrPem(dn, privateKey, publicKey);

 // this is the certificate in PEM format
  final certificate = X509Utils.generateSelfSignedCertificate(keyPair.privateKey, csr, 30);

thank you, i check it