Skip to content Skip to sidebar Skip to footer

How Do We Validate The Format Of The Csr Using Regex

I'm currently trying to validate a CSRformat via javascript regex and I'm currently stuck with this regex: ^ (-----BEGIN NEW CERTIFICATE REQUEST-----)(.*[\r\ n]) + (-----END NEW CE

Solution 1:

You can use this version of regex that will capture only the first START/END block only in case it is a unique block of this kind:

^(?:(?!-{3,}(?:BEGIN|END) NEW CERTIFICATE REQUEST)[\s\S])*(-{3,}BEGIN NEW CERTIFICATE REQUEST(?:(?!-{3,}END NEW CERTIFICATE REQUEST)[\s\S])*?-{3,}END NEW CERTIFICATE REQUEST-{3,})(?![\s\S]*?-{3,}BEGIN NEW CERTIFICATE REQUEST[\s\S]+?-{3,}END NEW CERTIFICATE REQUEST[\s\S]*?$)

See demo

EDIT:

To make sure we just match at the end, with optional spaces, you can use a shorter regex:

^(?:(?!-{3,}(?:BEGIN|END) NEW CERTIFICATE REQUEST)[\s\S])*(-{3,}BEGIN NEW CERTIFICATE REQUEST(?:(?!-{3,}BEGIN NEW CERTIFICATE REQUEST)[\s\S])*?-{3,}END NEW CERTIFICATE REQUEST-{3,})\s*$

See Demo 2

Solution 2:

Try with that regex:

^(?:\s|\R)*(?:-----BEGIN NEW CERTIFICATE REQUEST-----)(?<request>(?:.*\R)+)(?:-----END NEW CERTIFICATE REQUEST-----)(?:\s|\R)*$

Test it there

Solution 3:

While the RegEx check is useful, it would be better to actually try to decode the certificate.

Here is an example of doing that:

https://pkijs.org/examples/PKCS10_complex_example.html

Post a Comment for "How Do We Validate The Format Of The Csr Using Regex"