0

Example 1 : Subject: CN=eakteam.com

Example 2 : Subject: C=US, O=Let's Encrypt, CN=Let's Encrypt Authority X3

Example 3 : Subject: C=US, ST=CA, L=San Jose, O=Ubiquiti Networks Inc., OU=Technical Support, CN=UBNT-80:2A:A8:EE:B0:A3/emailAddress=support@ubnt.com

The question is how to create a regex to check all posibility strings and to get only CN=(the data that I want) ?

The original string is a X509 Certificate and wanted to parse all the details inside it.

Data:
Version: 3 (0x2)
    Serial Number : -283916479 (-0x10ec38bf)
    Signature Algorithm : sha256WithRSAEncryption
    Issuer : C = US, ST = CA, L = San Jose, O = Ubiquiti Networks Inc., OU = Technical Support, CN = UBNT - 80 : 2A : A8 : EE : B0 : A3 / emailAddress = support@ubnt.com
    Validity
    Not Before : Mar 29 15 : 17 : 00 2017 GMT
    Not After : Mar 29 15 : 17 : 00 2022 GMT
    Subject : C = US, ST = CA, L = San Jose, O = Ubiquiti Networks Inc., OU = Technical Support, CN = UBNT - 80 : 2A : A8 : EE : B0 : A3 / emailAddress = support@ubnt.com
    Subject Public Key Info :
         Public Key Algorithm : rsaEncryption
             Public - Key : (2048 bit)
             Modulus :
             00 : 8d : 2e : 15 : f1 : 57 : d9 : 9c : aa : d2 : d2 : 66 : 69 : 88 : a4 :
             12 : 38 : b2 : 9c : 2e : 35 : 2b : 8e : 12 : 98 : 55 : 8f : b2 : 21 : ef :
             c1 : dc : 40 : e0 : f1 : da : 42 : b0 : 3e : cd : 21 : 9b : f6 : a5 : 48 :
             5c : 4c : 27 : 2e : 1f : 3b : 3b : 98 : 06 : 73 : 9a : c3 : 82 : e6 : a6 :
             cd : 61 : c9 : ae : 7d : 40 : b7 : 6c : 29 : 0a : e9 : 94 : ee : 2d : f0 :
             f6 : 17 : 1c : e0 : db : f2 : a3 : 21 : 60 : d3 : 16 : b6 : 53 : 3c : 8f :
             6a : cb : 05 : 6c : ed : f6 : 24 : f2 : dc : 32 : c4 : bb : 2b : 46 : d9 :
             3a : d6 : d2 : e1 : 2a : 66 : 85 : b5 : e1 : 8d : 3b : aa : 64 : b5 : 2e :
             4e : f9 : bc : 34 : d7 : 4b : c5 : cb : a8 : 73 : 3e : d6 : 7d : f9 : dd :
             9d : 44 : eb : c8 : c8 : ef : b4 : 79 : 13 : 71 : 2d : 37 : a1 : e9 : cd :
             f5 : 41 : b8 : 3e : 0d : 53 : 3a : 20 : 60 : bb : 8d : c6 : f5 : 43 : eb :
             a1 : b7 : ea : 7f : 74 : c4 : 25 : 09 : cc : 90 : ae : a4 : 3f : 44 : 3d :
             36 : 8b : 48 : 80 : 90 : ec : 77 : 8c : 3d : 52 : da : 09 : cf : b8 : f6 :
             7e : 13 : 7b : 1d : 6b : 90 : 64 : 36 : 37 : c9 : 20 : 99 : e4 : 83 : 56 :
             90 : 6c : de : 8c : 91 : ef : fb : 5f : 49 : c6 : ca : b3 : bf : d0 : ed :
             10 : 60 : d1 : 35 : dd : dc : 0c : 84 : 53 : 74 : 6e : b0 : 8e : 9f : 31 :
             40 : 77 : 07 : 84 : 03 : f8 : 5a : 14 : 0c : d5 : 12 : 53 : 7e : 6a : e6 :
             25 : 9b
             Exponent : 65537 (0x10001)
             Signature Algorithm : sha256WithRSAEncryption
             6d : 24 : 4e : 5d : a4 : dc : 33 : cc : bf : 22 : d9 : 68 : 91 : 6b : 66 : e7 : 62 : 09 :
             35 : a6 : e3 : 38 : 5f : 87 : a9 : a8 : 66 : f4 : a8 : 99 : 5a : 40 : 01 : 75 : bf : eb :
             10 : 11 : d7 : b0 : c9 : 76 : 2b : 23 : ad : 5b : fd : 55 : 4e : f0 : fc : 02 : fe : 9e :
             47 : 78 : 1b : a2 : 3c : f0 : 34 : 9a : 0b : 25 : 44 : 7f : b4 : 0a : 79 : e8 : 54 : 28 :
             0a : 9a : 51 : e3 : e7 : a9 : 84 : 4e : 76 : 51 : b6 : 2b : 4e : b9 : f8 : 1f : 41 : 8d :
             5d : ed : 02 : 54 : 6d : 2e : b8 : cd : c4 : 4e : 85 : a8 : a8 : 86 : 33 : ba : 12 : 6d :
             2e : 65 : ef : e3 : 3d : 4a : e8 : 89 : 9d : 54 : d7 : 92 : 15 : b8 : 68 : 35 : 32 : 57 :
             9b : 0f : 12 : 17 : 0a : 64 : bf : c7 : b3 : a5 : c6 : 3c : bb : 58 : 22 : 85 : 16 : e5 :
             84 : c9 : 33 : e8 : 49 : 3f : e6 : c2 : d6 : 05 : 0c : fe : ac : e6 : 82 : cc : ec : 4a :
             aa : b4 : 57 : 48 : 71 : 98 : dd : 8a : 59 : f3 : 6a : 98 : d6 : e5 : 2d : a7 : b7 : 67 :
             06 : 87 : 9b : 04 : 02 : c6 : 13 : b9 : 69 : c5 : cc : 2c : 84 : 68 : 65 : da : b6 : f3 :
             c2 : 77 : 48 : 6b : 36 : 53 : ae : f6 : 14 : 47 : 0f : 2d : 28 : 6c : bb : 2f : 71 : c2 :
             fd : 02 : 4a : bc : 82 : cc : 3e : 16 : 9c : 0b : 7a : ef : 75 : b2 : 7f : c0 : 37 : 96 :
             b9 : 5c : b0 : 75 : f1 : d7 : 71 : c1 : ee : 52 : 5e : 32 : 46 : 83 : 40 : f5 : b2 : b1 :
             6f : bb : 4a : 35
frslm
  • 2,969
  • 3
  • 13
  • 26
EAK TEAM
  • 5,726
  • 4
  • 30
  • 52
  • 2
    Why parsing it from text output in the first place? Why not extract it with an appropriate tool, something like: `openssl x509 -noout -subject -in cert.crt`. – randomir Oct 08 '17 at 20:02
  • @randomir i'm using certificate.tostring() to get all the text from httpsConnection ... can you provide me an example how to do this with ASN.1 ? – EAK TEAM Oct 08 '17 at 20:07
  • 1
    I'm not sure if this answer is still relevant, but see https://stackoverflow.com/questions/2914521/how-to-extract-cn-from-x509certificate-in-java – randomir Oct 08 '17 at 20:12
  • @JorgeCampos I don't want to parse and get only the CN from X509 ... actualy needed to parse and get what value needed from all the certificate string – EAK TEAM Oct 08 '17 at 20:14

2 Answers2

1

You could use:

(?<key>\w+) = (?<value>[^,\n\r]+)

See a demo on regex101.com (shortened).
You may as well only use numbered groups, like so:

(\w+) = ([^,\n\r]+)

Here, you'll need to use group $1 and $2, respectively.

Jan
  • 42,290
  • 8
  • 54
  • 79
  • This is closer of what im looking about , but i can't get it work ... can you update answer to give me easier example ? Thank you ! – EAK TEAM Oct 08 '17 at 20:15
  • with this `(\w+)=([^,\n\r]+)` i'm getting `CN` from `Issuer` too , i want `CN `only from `Subject:` – EAK TEAM Oct 08 '17 at 20:20
0

It is not clear if you want only the CN key or all the keys

For CN only use

^Subject :.* CN = ([^,\n\r]+)

and you will have the value on group 1 after pattern matching matcher.group(1)

For all the keys use

^Subject :( (\w+) = ([^,]+),)*( (\w+) = ([^,\n\r]+),)

And you will have the result also in the groups, depending on how many there are.

These expressions work on single lines.

ilomambo
  • 8,290
  • 12
  • 57
  • 106