use of org.spongycastle.asn1.x509.SubjectPublicKeyInfo in project xipki by xipki.
the class CaClientExample method generateDsaKeypair.
protected static MyKeypair generateDsaKeypair() throws Exception {
// plen: 2048, qlen: 256
DSAParameterSpec spec = new DSAParameterSpec(P2048_Q256_P, P2048_Q256_Q, P2048_Q256_G);
KeyPairGenerator kpGen = KeyPairGenerator.getInstance("DSA");
kpGen.initialize(spec);
KeyPair kp = kpGen.generateKeyPair();
DSAPublicKey dsaPubKey = (DSAPublicKey) kp.getPublic();
ASN1EncodableVector vec = new ASN1EncodableVector();
vec.add(new ASN1Integer(dsaPubKey.getParams().getP()));
vec.add(new ASN1Integer(dsaPubKey.getParams().getQ()));
vec.add(new ASN1Integer(dsaPubKey.getParams().getG()));
ASN1Sequence dssParams = new DERSequence(vec);
SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, dssParams), new ASN1Integer(dsaPubKey.getY()));
return new MyKeypair(kp.getPrivate(), subjectPublicKeyInfo);
}
use of org.spongycastle.asn1.x509.SubjectPublicKeyInfo in project xipki by xipki.
the class KeyUtil method createSubjectPublicKeyInfo.
public static SubjectPublicKeyInfo createSubjectPublicKeyInfo(PublicKey publicKey) throws InvalidKeyException {
ParamUtil.requireNonNull("publicKey", publicKey);
if (publicKey instanceof DSAPublicKey) {
DSAPublicKey dsaPubKey = (DSAPublicKey) publicKey;
ASN1EncodableVector vec = new ASN1EncodableVector();
vec.add(new ASN1Integer(dsaPubKey.getParams().getP()));
vec.add(new ASN1Integer(dsaPubKey.getParams().getQ()));
vec.add(new ASN1Integer(dsaPubKey.getParams().getG()));
ASN1Sequence dssParams = new DERSequence(vec);
try {
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, dssParams), new ASN1Integer(dsaPubKey.getY()));
} catch (IOException ex) {
throw new InvalidKeyException(ex.getMessage(), ex);
}
} else if (publicKey instanceof RSAPublicKey) {
RSAPublicKey rsaPubKey = (RSAPublicKey) publicKey;
try {
return new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new org.bouncycastle.asn1.pkcs.RSAPublicKey(rsaPubKey.getModulus(), rsaPubKey.getPublicExponent()));
} catch (IOException ex) {
throw new InvalidKeyException(ex.getMessage(), ex);
}
} else if (publicKey instanceof ECPublicKey) {
ECPublicKey ecPubKey = (ECPublicKey) publicKey;
ECParameterSpec paramSpec = ecPubKey.getParams();
ASN1ObjectIdentifier curveOid = detectCurveOid(paramSpec);
if (curveOid == null) {
throw new InvalidKeyException("Cannot find namedCurve of the given private key");
}
java.security.spec.ECPoint pointW = ecPubKey.getW();
BigInteger wx = pointW.getAffineX();
if (wx.signum() != 1) {
throw new InvalidKeyException("Wx is not positive");
}
BigInteger wy = pointW.getAffineY();
if (wy.signum() != 1) {
throw new InvalidKeyException("Wy is not positive");
}
int keysize = (paramSpec.getOrder().bitLength() + 7) / 8;
byte[] wxBytes = BigIntegers.asUnsignedByteArray(keysize, wx);
byte[] wyBytes = BigIntegers.asUnsignedByteArray(keysize, wy);
byte[] pubKey = new byte[1 + keysize * 2];
// uncompressed
pubKey[0] = 4;
System.arraycopy(wxBytes, 0, pubKey, 1, keysize);
System.arraycopy(wyBytes, 0, pubKey, 1 + keysize, keysize);
AlgorithmIdentifier algId = new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, curveOid);
return new SubjectPublicKeyInfo(algId, pubKey);
} else {
throw new InvalidKeyException("unknown publicKey class " + publicKey.getClass().getName());
}
}
use of org.spongycastle.asn1.x509.SubjectPublicKeyInfo in project xipki by xipki.
the class P12KeyGenerator method genRSAKeypair.
// CHECKSTYLE:SKIP
private KeyPairWithSubjectPublicKeyInfo genRSAKeypair(int keysize, BigInteger publicExponent, SecureRandom random) throws Exception {
KeyPair kp = KeyUtil.generateRSAKeypair(keysize, publicExponent, random);
java.security.interfaces.RSAPublicKey rsaPubKey = (java.security.interfaces.RSAPublicKey) kp.getPublic();
SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPublicKey(rsaPubKey.getModulus(), rsaPubKey.getPublicExponent()));
return new KeyPairWithSubjectPublicKeyInfo(kp, spki);
}
use of org.spongycastle.asn1.x509.SubjectPublicKeyInfo in project xipki by xipki.
the class P12KeyGenerator method generateIdentity.
private static P12KeyGenerationResult generateIdentity(KeyPairWithSubjectPublicKeyInfo kp, KeystoreGenerationParameters params, String selfSignedCertSubject) throws Exception {
Date now = new Date();
// 10 minutes past
Date notBefore = new Date(now.getTime() - 10 * MIN);
Date notAfter = new Date(notBefore.getTime() + 3650 * DAY);
String dnStr = (selfSignedCertSubject == null) ? "CN=DUMMY" : selfSignedCertSubject;
X500Name subjectDn = new X500Name(dnStr);
SubjectPublicKeyInfo subjectPublicKeyInfo = kp.getSubjectPublicKeyInfo();
ContentSigner contentSigner = getContentSigner(kp.getKeypair().getPrivate());
// Generate keystore
X509v3CertificateBuilder certGenerator = new X509v3CertificateBuilder(subjectDn, BigInteger.ONE, notBefore, notAfter, subjectDn, subjectPublicKeyInfo);
KeyAndCertPair identity = new KeyAndCertPair(certGenerator.build(contentSigner), kp.getKeypair().getPrivate());
KeyStore ks = KeyUtil.getKeyStore("PKCS12");
ks.load(null, params.getPassword());
ks.setKeyEntry("main", identity.getKey(), params.getPassword(), new Certificate[] { identity.getJceCert() });
ByteArrayOutputStream ksStream = new ByteArrayOutputStream();
try {
ks.store(ksStream, params.getPassword());
} finally {
ksStream.flush();
}
P12KeyGenerationResult result = new P12KeyGenerationResult(ksStream.toByteArray());
result.setKeystoreObject(ks);
return result;
}
use of org.spongycastle.asn1.x509.SubjectPublicKeyInfo in project xipki by xipki.
the class CaLoadTestEnroll method nextCertRequests.
private Map<Integer, CertRequest> nextCertRequests() {
if (maxRequests > 0) {
int num = processedRequests.getAndAdd(1);
if (num >= maxRequests) {
return null;
}
}
Map<Integer, CertRequest> certRequests = new HashMap<>();
for (int i = 0; i < num; i++) {
final int certId = i + 1;
CertTemplateBuilder certTempBuilder = new CertTemplateBuilder();
long thisIndex = index.getAndIncrement();
certTempBuilder.setSubject(loadtestEntry.getX500Name(thisIndex));
SubjectPublicKeyInfo spki = loadtestEntry.getSubjectPublicKeyInfo();
certTempBuilder.setPublicKey(spki);
CertTemplate certTemplate = certTempBuilder.build();
CertRequest certRequest = new CertRequest(certId, certTemplate, null);
certRequests.put(certId, certRequest);
}
return certRequests;
}
Aggregations