Parsing Strings ( .crt files )

Posted by user1661521 on Stack Overflow See other posts from Stack Overflow or by user1661521
Published on 2013-06-27T21:02:50Z Indexed on 2013/06/28 10:21 UTC
Read the original article Hit count: 306

Filed under:
|
|
|

Base Knowledge : I have a .crt file ( certification authoritie file ) and he is composed of many fields but in one line that resumes this question i have this :

    Certificate:
       ...(alot of stuff before)...
       Subject: C=US, ST=Maryland, L=Pasadena, O=Brent Baccala,
                OU=FreeSoft, CN=www.freesoft.org/[email protected]
       Subject Public Key Info:
          ...(alot of stuff after)

and i need to parse the file to populate a .csv file and i have that done the problem that i need help is, i need to get the field:

CN=www.fresoft.org

but when i get this kind of CN=...(Value instead of the ...) with alot of slashes i get a error in the parsing like the raw string is:

CN=foo/bar/the/hell/emailAddress=blablabla

and i need only:

foo/bar/the/hell

and for a moment i got that in the correct column but when i dont have the emailAddress something just fail in my parsing and i then get in my CN .csv column the information wrong instead of

|CN|
foo/bar/the/hell

i get:

|CN|
OU=FreeSoft, foo/bar/the/hell.

I have this code doing the CN parsing:

#!/bin/bash

subject_line=$(echo $cert | grep -o "Subject:.*Subject Public Key Info")

cn=$(echo $subject_line | grep -o "CN=.*" )

if [ $(echo $cn | grep -c ".*email.*") -gt 0 ]; then
    end_cn=$(echo $cn | grep -b -o emailAddress)
    end_cn_idx=$(echo $end_cn | grep -o .*:)
    final_end_cn=${end_cn_idx:0:-1}
    common_name=${cn:3:$final_end_cn-4}

    echo $common_name

else
    end_cn=$(echo $cn | grep -b -o "Subject Public Key Info")
        end_cn_idx=$(echo $end_cn | grep -o .*:)
        final_end_cn=${end_cn_idx:0:-1}
        common_name=${cn:3:$final_end_cn-5}

        echo $common_name
fi

© Stack Overflow or respective owner

Related posts about bash

Related posts about parsing