用于唯一标识证书的正则表达式

时间:2016-09-21 06:45:04

标签: php regex certificate

我正在尝试提供与“密钥容器”的值匹配的正则表达式,但仅当颁发者是CN = My_root_CA并且证书是非根证书时 所有上述内容都应该满足以唯一匹配证书,因为有几个类似的证书

       ================ Certificate 3 ================
   Serial Number: 5471f66c066334ab48285398b0f24b7e
   Issuer: CN=My_Root_CA, O=Experian, OU=Hunter, L=Sofia, S=CA, C=BG
    NotBefore: 20/09/2016 15:48
    NotAfter: 20/09/2024 15:48
   Subject: CN=192.168.127.107
   Non-root Certificate
   Cert Hash(sha1): 46 92 41 60 56 36 32 fa bb 82 f2 45 0b 80 37 c4 28 15 e4 ca
     Key Container = 52668461-19b4-4fef-80d6-5982ff702c3b
     Unique container name: 0893f8f220ed765fabe249818ff664b9_bba0a089-1c2b-49c8-9f49-e4b211a91a55
     Provider = Microsoft Strong Cryptographic Provider
   Encryption test passed

我试过这个CN=My_Root_CA((.|\n)*?)Non-root Certificate((.|\n)*?)Key Container =(.*\b)

但如果我们有

,这就不能正确匹配
  ================ Certificate 1 ================
   Serial Number: b4b81124ff17b6af4eb18053cd9fecd5
   Issuer: CN=My_Root_CA, O=Experian, OU=Hunter, L=Sofia, S=CA, C=BG
    NotBefore: 20/09/2016 15:48
    NotAfter: 20/09/2024 15:48
   Subject: CN=My_Root_CA, O=Experian, OU=Hunter, L=Sofia, S=CA, C=BG
   Signature matches Public Key
   Root Certificate: Subject matches Issuer
   Cert Hash(sha1): 8c 47 dd 5a 74 47 3a 74 e1 2a c4 de 6f 95 35 04 f6 2d d4 8a
     Key Container = 25ea8612-7fd3-4504-97f8-d9150e6bfd63
     Unique container name: 4e3f7aa1fb469fd4cb7b184d188059be_bba0a089-1c2b-49c8-9f49-e4b211a91a55
     Provider = Microsoft Strong Cryptographic Provider
   Encryption test passed

   ================ Certificate 2 ================
   Serial Number: XXXXXXXX
   Issuer: CN=TEST TEST FIND Root
    NotBefore: 18/08/2015 16:16
    NotAfter: 18/09/2017 16:26
   Subject: CN=TEST CA
   CA Version: V9.0
   Certificate Template Name (Certificate Type): SubCA
   Non-root Certificate
   Template: SubCA
   Cert Hash(sha1): 55 1b 22 8c c9 eb 25 58 25 3f 00 67 a3 6e 68 b1 62 43 de 75
   No key provider information
   Cannot find the certificate and private key for decryption.
   Encryption test passed

   ================ Certificate 3 ================
   Serial Number: 5471f66c066334ab48285398b0f24b7e
   Issuer: CN=My_Root_CA, O=Experian, OU=Hunter, L=Sofia, S=CA, C=BG
    NotBefore: 20/09/2016 15:48
    NotAfter: 20/09/2024 15:48
   Subject: CN=192.168.127.107
   Non-root Certificate
   Cert Hash(sha1): 46 92 41 60 56 36 32 fa bb 82 f2 45 0b 80 37 c4 28 15 e4 ca
     Key Container = 52668461-19b4-4fef-80d6-5982ff702c3b
     Unique container name: 0893f8f220ed765fabe249818ff664b9_bba0a089-1c2b-49c8-9f49-e4b211a91a55
     Provider = Microsoft Strong Cryptographic Provider
   Encryption test passed

1 个答案:

答案 0 :(得分:1)

你需要使用否定的先行断言。

CN=My_Root_CA(?:(?!={4,})[\s\S])*?Non-root Certificate(?:(?!={4,})[\s\S])*?Key Container\s+=\s+(.*\b)

(?:(?!={4,})[\s\S])*? - >不会超过四个=标志。

DEMO