简体   繁体   中英

How to add PrivateKeyUsage extension to a certificate using bouncycastle in java?

我有一个X509Certificate,我想向其中添加扩展名...我想添加PrivateKeyUsage扩展名,但我不知道如何创建PrivateKeyUsage对象或如何为其赋予notBefore&notAfter值...谢谢

Date from = new Date();
Date to = new Date(System.currentTimeMillis()+ 30*1000*60*60l);

ASN1EncodableVector v = new ASN1EncodableVector();
DERGeneralizedTime fromTime = new DERGeneralizedTime(from);    
v.add(new DERTaggedObject(false, 0, fromTime));

DERGeneralizedTime toTime = new DERGeneralizedTime(to);    
v.add(new DERTaggedObject(false, 1, toTime));

DERObject o = new DERSequence(v);    
PrivateKeyUsagePeriod pkup = PrivateKeyUsagePeriod.getInstance(o);    
v3CertGen.addExtension(x509Extensions.PrivateKeyUsagePeriod, false, pkup);

You can do something like this,

    Date notBeforeDate = new Date();
    Date notAfterDate = new Date(System.currentMillis() + 24L*3600*365*1000);

    PrivateKeyUsagePeriod pkup = new PrivateKeyUsagePeriod(notBeforeDate,
            notAfterDate);
    V3Extension[] v3 = {pkup};

    cert = CertificateFactory.createCertificate(subject, kp.getPublic(), issuer,
           kp.getPrivate(), algorithm, 1, v3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM