Dealing With X500 Names

From OpenCA::Wiki

This example provides a simple usage for relative distinguished names in LibPKI. Many objects in X.509 have Distinguished Names (DNs). Here's how you can actually parse them easily:


 #include <libpki/pki.h>
 
 int main () {
       char * st = "DC=Dartmouth, DC=EDU, O=Dartmouth,"
                   " OU=Computer Science, ST=NH, CN=Massimiliano Pala,"
                   " emailAddress=pala@cs.dartmouth.edu, emailAddress=madwolf@openca.org";
  
       // This is the new X509 name
       PKI_X509_NAME *name = NULL;
       // This will carry the list of Relative DNs
       PKI_X509_NAME_RDN **list = NULL;
       // This is a pointer to a RDN
       PKI_X509_NAME_RDN *pnt = NULL;
  
       // Some useful variables
       char *st2 = NULL;
       int i = 0;
       PKI_OID *oid = NULL;
 
       // Let's init LibPKI
       PKI_init_all();
  
       // Let's Create the new X509 name
       if((name = PKI_X509_NAME_new ( st )) == NULL ) {
               printf("ERROR 1\n");
               exit(0);
       }
 
       // Let's Get the List of RDNs
       if (( list = PKI_X509_NAME_get_list ( name,
                               PKI_X509_NAME_TYPE_NONE )) == NULL ) {
               printf("ERROR 2\n");
               exit(0);
       }
   
       // Let's print out the RDNs properties
       while ( pnt = list[i] ) {
               printf( " TYPE => %s (%s) [%d] VALUE => %s\n\n",
                       PKI_X509_NAME_RDN_type_descr(pnt),
                       PKI_X509_NAME_RDN_type_text(pnt),
                       PKI_X509_NAME_RDN_type_id(pnt),
                       PKI_X509_NAME_RDN_value(pnt));
               i++;
       }
  
       // Now, release the memory allocated for the list
       PKI_X509_NAME_list_free ( list );
  
       printf("Done.\n");
       return 0;
  }