Sponsored Content
Full Discussion: ASN.1 Decoder UNIX Code
Top Forums Programming ASN.1 Decoder UNIX Code Post 302888614 by fpmurphy on Sunday 16th of February 2014 01:15:15 AM
Old 02-16-2014
Given your apparent difficulties following my GNU_EFI ASN.1 X.509 parser code, here is an example of another way to parse X509 .pem files using the OpenSSL development library.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>


int 
main(int argc, char *argv[])
{
    FILE *fp = NULL;
    char *filename;
    struct stat fstat;

    if (argc != 2) {
        fprintf(stdout, "ERROR - No filename provided\n");
        exit(1);
    }

    filename = argv[1];

    if ((stat(filename, &fstat) != 0) || (fp = fopen(filename, "r")) == NULL) {
        fprintf(stdout, "ERROR - File could not be opened\n");
        exit(1);
    }

    // parse certificate
    X509 *cert = PEM_read_X509(fp, NULL, NULL, NULL);
    if (!cert) {
        fprintf(stderr, "ERROR - Unable to parse certificate\n");
        fclose(fp);
        exit(1);
    }
    
    // version number
    int version = ((int) X509_get_version(cert)) + 1;
    printf("Version: %d\n", version);

    // serial number
    ASN1_INTEGER *serial = X509_get_serialNumber(cert);
    
    BIGNUM *bn = ASN1_INTEGER_to_BN(serial, NULL);
    if (!bn) {
        fprintf(stderr, "ERROR - Unable to convert ASN1INTEGER to BN\n");
        exit(1);
    }
    
    char *tmp = BN_bn2dec(bn);
    if (!tmp) {
        fprintf(stderr, "ERROR - Unable to convert BN to decimal string\n");
        BN_free(bn);
        exit(1);
    }
    
    printf("Serial Number: %s\n", tmp);

    BN_free(bn);
    OPENSSL_free(tmp);

    // subject
    tmp = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
    printf("Subject: %s\n", tmp);
    OPENSSL_free(tmp);

    // issuer
    tmp = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0);
    printf("Issuer: %s\n", tmp);
    OPENSSL_free(tmp);

    // signature algorithm
    int pkey_nid = OBJ_obj2nid(cert->cert_info->key->algor->algorithm);
    if (pkey_nid == NID_undef) {
        fprintf(stderr, "ERROR - Signature algorithm name\n");
        exit(1);
    }

    const char* buf  = OBJ_nid2ln(pkey_nid);
    printf("Algorithm: %s\n", buf);

    X509_free(cert);
    fclose(fp);
}

This User Gave Thanks to fpmurphy For This Post:
 

4 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

core file decoder needed

All, Remotely logged in to the UNIX server (HP B1000 Visual Server) (Version HP-UX 10.20) by using the program Xapplauncher. This is a application runs under Exceed. (Exceed "version 6.1" is a Windows application to communicate with UNIX servers) With no pre warnings the application was... (2 Replies)
Discussion started by: pbekker
2 Replies

2. Programming

ASN.1 decoder

Hi All, I am fairly new to this so please forgive me, Currently I have an ASN.1 which I would like the ability to load this to my work server in order to enter a string of output decode and display the output. There are methods online as shown on ASN.1 JavaScript decoder however I have... (3 Replies)
Discussion started by: mutley2202
3 Replies

3. Solaris

ASN Binary to ASCII

Dears, I need help to convert the binary file into ASCII format. Actually we have CDRs which is generated by telecom switch at this is in ASN1 format or binary format, I need to convert those binary formatted file into ASCII format using Perl, or shell scripting. Is there any way to solve... (3 Replies)
Discussion started by: PRINCESS_RORO
3 Replies

4. UNIX for Advanced & Expert Users

Python Programming for ASN.1 file

Hi. Has anyone here got an experience doing conversion from asn1 format to a readable format so that it can be processed by Oracle? I want to load the data into a table. This is a CDR file. Attached is the pairing file. Please remove the .txt at the end. Someone said that it is possible... (1 Reply)
Discussion started by: aimy
1 Replies
DDIS(5) 							File Formats Manual							   DDIS(5)

Name
       DDIS - Digital Data Interchange Syntax / ISO ASN.1 (DDIS/ASN.1) files

Description
       DDIS/ASN.1 files conform to Digital's Digital Data Interchange Syntax.  DDIS conforms to syntaxes that can be defined within the specifica-
       tions of International Standards Organization Abstract Syntax Notation One (ISO ASN.1), but is not itself an  implementation  of  full  ISO
       ASN.1 syntax.

       DDIS/ASN.1 files conform to the DDIS/ASN.1 syntax.  The DDIS/ASN.1 syntax is itself used to define other syntaxes.  The following are among
       the syntaxes that are subsets of DDIS/ASN.1:

	      DDIF   Digital Document Interchange Format

	      DTIF   Digital Table Interchange Format

	      DOTS   Data Object Transport Syntax

       Files that conform to one of the DDIS/ASN.1 family of syntaxes are described as DDIS/ASN.1 files.  More specifically,  however,	the  files
       are typed according to a particular DDIS/ASN.1 syntax.  For example, a file that conforms to the DDIF syntax is a DDIF file, and is identi-
       fied by the command as a ddis/ddif file. The command includes the string ddis/ as a part of its output if a file belongs in the	DDIS  fam-
       ily.

See Also
       ctod(1), dtoc(1), DDIF(5), DOTS(5), DTIF(5), CDA(5)

																	   DDIS(5)
All times are GMT -4. The time now is 07:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy