PeculiarVentures / ASN1.js

ASN1js is a pure JavaScript library implementing a full ASN.1 BER decoder and encoder.
https://asn1js.org
Other
271 stars 57 forks source link

How to load ASN.1 description in json structure #76

Open pedefe opened 2 years ago

pedefe commented 2 years ago

Hello, Do you have a function (or a link on a library in js) to load an ASN.1 description and convert it in json structure ? For exemple :

ASN.1 Description :

DENM ::= SEQUENCE { 
  header ItsPduHeader, 
  denm DecentralizedEnvironmentalNotificationMessage 
} 

DecentralizedEnvironmentalNotificationMessage ::= SEQUENCE { 
  management ManagementContainer, 
  situation SituationContainer OPTIONAL, 
  location LocationContainer OPTIONAL, 
  alacarte AlacarteContainer OPTIONAL 
} 

converted in something like...

{
    DENM: {
        _type: "SEQUENCE",
        _content: {
            header: "ItsPduHeader",
            denm: "DecentralizedEnvironmentalNotificationMessage"
            }
        },
    ItsPduHeader: {
        _type: "SEQUENCE",
        _sequence: {
            protocolVersion: {
                _type: "INTEGER",
                _range: [0,255]
                },
            messageID: {
                _type: "INTEGER",
                _choice: {
                    1: "denm",
                    2: "cam",
                    3: "poi"
                    }
                }
            stationID: {
                _type: "StationID"
                }
            },
        DecentralizedEnvironmentalNotificationMessage: {
            ...
            }
        },
    ...
}

Best regards.

microshine commented 2 years ago

Have you seen @peculiar/asn1-schema? It uses asn1js and allows describing ASN.1 schemas via property and class decorators.

https://github.com/PeculiarVentures/asn1-schema/tree/master/packages/schema#typescript-examples

Here is X509 certificate in JS structure after AsnCovert.parse

Certificate {
  tbsCertificate: TBSCertificate {
    version: 2,
    serialNumber: ArrayBuffer {
      [Uint8Contents]: <48 44 dc c6 d4 70 0f fa b3 7f 41 63 56 f1>,
      byteLength: 14
    },
    signature: AlgorithmIdentifier {
      algorithm: '1.2.840.113549.1.1.11',
      parameters: null
    },
    issuer: Name(4) [
      [RelativeDistinguishedName],
      [RelativeDistinguishedName],
      [RelativeDistinguishedName],
      [RelativeDistinguishedName]
    ],
    validity: Validity { notBefore: [Time], notAfter: [Time] },
    subject: Name(4) [
      [RelativeDistinguishedName],
      [RelativeDistinguishedName],
      [RelativeDistinguishedName],
      [RelativeDistinguishedName]
    ],
    subjectPublicKeyInfo: SubjectPublicKeyInfo {
      algorithm: [AlgorithmIdentifier],
      subjectPublicKey: [ArrayBuffer]
    },
    extensions: Extensions(3) [ [Extension], [Extension], [Extension] ]
  },
  signatureAlgorithm: AlgorithmIdentifier {
    algorithm: '1.2.840.113549.1.1.11',
    parameters: null
  },
  signatureValue: ArrayBuffer {
    [Uint8Contents]: <00 40 8a 2a 92 f4 03 62 3e 86 c6 3c f3 5b 48 cf b5 aa 90 b0 ee 2e 1f c8 4b 34 55 76 c0 0b 60 0c 4b 85 50 d4 1c f7 24 87 95 f2 55 19 29 6d 39 a2 ae 7f 9b e4 ca 6e c0 02 36 47 c0 3b 2d de 9a 59 68 a2 6d ce f3 18 46 4d bc 42 ac 50 46 28 26 86 11 d1 fb 90 b3 67 05 ed e8 22 62 38 45 c0 53 17 ef 9c 3e 92 ... 156 more bytes>,
    byteLength: 256
  }
}
YuryStrozhevsky commented 2 years ago

@pedefe Each ASN1.js object (except internal-specific objects like "Any" and "Repeated") has toJSON function, which allows you to convert any ASN.1 schemas to JSON right ater making them:

import * as pkijs from "pkijs";
import * as asn1js from "asn1js";
import * as pvutils from "pvutils";

const b64 = "MIIGuDCCBKCgAwIBAgIUUk/B8W400XArhKE/sEK7zHw8kDIwDQYJKoZIhvcNAQELBQAwSDELMAkGA1UEBhMCQk0xGTAXBgNVBAoTEFF1b1ZhZGlzIExpbWl0ZWQxHjAcBgNVBAMTFVF1b1ZhZGlzIFJvb3QgQ0EgMiBHMzAeFw0xNjExMzAxNjIxMDFaFw0yNjExMzAxNjIxMDFaMEkxCzAJBgNVBAYTAkJNMRkwFwYDVQQKDBBRdW9WYWRpcyBMaW1pdGVkMR8wHQYDVQQDDBZRdW9WYWRpcyBFViBTU0wgSUNBIEczMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAonyczmwRSnw5BhWIrfcD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YDVR0jBBgwFoAU7edvdlq/YOxJW8ald7tyFnGbxD0wOwYDVR0fBDQwMjAwoC6gLIYqaHR0cDovL2NybC5xdW92YWRpc2dsb2JhbC5jb20vcXZyY2EyZzMuY3JsMB0GA1UdDgQWBBTlhFTQkEmfOLryyeEqCMVOn6BIPzANBgkqhkiG9w0BAQsFAAOCAgEAY/EHWbpNwCgGVQ1B7cIn530n6Rnht8ryN6E4Sis2GG09801seCVMoGUB1uBCWm7uqQqydjTbjLhuub7hTjSJ1J30SOK1CZbk+c1VP9DcjY46hycytUKQ2WbgkaY+l/tZNDKu0djc2hA5apljQCmiIzckbcHr6yRnFK7ZPjSPCAUKm20DvORQ7hsIaomsIlqXm5BPssMcxjI48Ezgv/s8ynASI8S5P2vOnBo08sJBM/a0Kbuw351SubTzjxG+o1SHe6lAzvIQMuSwxUca8YkiB19w5YZt+Ss2JXNc6F2jZwpr0htoIXe+N9/x0CohYRRa+IivRGgdDQc3w2P+pffNQP/qdPuUYyMkYWiuHH/YvwXyuDxvyGQfvKmHr1uq/qiqbK1bDSUoEq4Su8yX8YoF9TuxYraIpp9iErO5rarDO6GTNVHh1OXAJ/ePhOWzqo3flLTlAdTcs3Mq97kKW8XWCnu/cjJJglf2zVfLAlv95p56B9If0pXbN74qDkYEC8TdLOwryhcv8yyimh90/AvW9LpB7swkWnUUYNTep/XMX/RLpHLnJOVtnRpn3coVfSR/0rz0XKVXeZGnKztGdIMQhWMTxvZ1UpmRAH2Ab2QnVo1fkPVyqNSJces5Y/VKpIvLBk5Jj55fvK8ME/9ASa+LtLrIms8iYHl75cupuYZZlg8=";

const asn1 = asn1js.fromBER(pvutils.stringToArrayBuffer(pvutils.fromBase64(b64)));

const json = JSON.stringify(asn1.result);

This code would work on version before TypeScript versions current team just made.