2014-11-14 20 views
2

我是pyOpenSSL的新用戶,我希望做一個證書模板用下面的代碼crlDistributionPoints DIRNAME

from OpenSSL import crypto as c 

cert = c.X509() 
cert.add_extensions([ 
    c.X509Extension('crlDistributionPoints', False, 'dirName:/C=US/O=TEST'), 
]) 

此代碼不能工作,任何人都可以幫我嗎?pyOpenSSL好像不支持目錄名稱

cert.add_extensions([ 
    c.X509Extension('crlDistributionPoints', False, 'URI:http://somesite') can work 
]) 
+0

試圖定義一個想法如文檔中所述的dirName https://www.openssl.org/docs/apps/x509v3_config.html#Subject_Alternative_Name_ – 2014-11-15 00:48:57

回答

0

我有完全相同的問題,但是,我也找不到真正的解決方案,我設法有一種解決方法來通過Python完成它。 在此頁面中,格式化被解釋爲http://openssl.org/docs/apps/x509v3_config.html#CRL-distribution-points,也是使用原始DER字節的選項。 (Section:ARBITRARY EXTENSIONS)

首先從已具有正確URI和dirName的證書中「收集」DER字節。替代使用openssl和正確的crlDistributionPoint創建一個證書,本例中的tmpcert就是這個證書。還要弄清楚使用哪個擴展索引。 get_short_name會給出擴展的'key',所以搜索crlDistributionPoint。 使用收集它:

from binascii import hexlify 
print tmpcert.get_extension(5).get_short_name() 
print hexlify(tmpcert.get_extension(5).get_data()) 

而且事後格式化這個輸出和在X509Extension()

crypto.X509Extension('crlDistributionPoints', False, 
"DER:30:6a:xx:xx:xx:..........:xx:xx") 

作爲一個理解的初始化劑使用它,這是quitte一個「硬編碼的」解決方案,沒有這種方式直接改變了這個領域的內容。

0

這裏是您可以在其中產生的DER的方式......它不包括目錄名稱的代碼,但我希望它讓你可以如何構建DER

from pyasn1.codec.der import encoder as der_encoder 
from pyasn1.type import tag 
from pyasn1_modules import rfc2459 

class GeneralNames(rfc2459.GeneralNames): 
    """ 
    rfc2459 has wrong tagset. 
    """ 
    tagSet = tag.TagSet(
     (), 
     tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0), 
     ) 

class DistributionPointName(rfc2459.DistributionPointName): 
    """ 
    rfc2459 has wrong tagset. 
    """ 
    tagSet = tag.TagSet(
     (), 
     tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0), 
     ) 

cdps = [('uri', 'http://something'), ('dns', 'some.domain.com')] 

cdp = rfc2459.CRLDistPointsSyntax() 
values = [] 
position = 0 
for cdp_type, cdp_value in cdps: 
    cdp_entry = rfc2459.DistributionPoint() 

    general_name = rfc2459.GeneralName() 

    if cdp_type == 'uri': 
     general_name.setComponentByName(
      'uniformResourceIdentifier', 
      cdp_value, 
      ) 
    elif cdp_type == 'dns': 
     general_name.setComponentByName(
      'dNSName', 
      cdp_value, 
      ) 

    general_names = GeneralNames() 
    general_names.setComponentByPosition(0, general_name) 

    name = DistributionPointName() 
    name.setComponentByName('fullName', general_names) 
    cdp_entry.setComponentByName('distributionPoint', name) 

    cdp.setComponentByPosition(position, cdp_entry) 
    position += 1 

cdp_der = der_encoder.encode(cdp) 

extensions.append(
    crypto.X509Extension(
     b'crlDistributionPoints', 
     False, 
     'DER:' + cdp_der.encode('hex'), 
     ), 
    ) 
相關問題