use of org.spongycastle.pkcs.PKCS10CertificationRequestBuilder in project android by nextcloud.
the class CsrHelper method generateCSR.
/**
* Create the certificate signing request (CSR) from private and public keys
*
* @param keyPair the KeyPair with private and public keys
* @param userId userId of CSR owner
* @return PKCS10CertificationRequest with the certificate signing request (CSR) data
* @throws IOException thrown if key cannot be created
* @throws OperatorCreationException thrown if contentSigner cannot be build
*/
private static PKCS10CertificationRequest generateCSR(KeyPair keyPair, String userId) throws IOException, OperatorCreationException {
String principal = "CN=" + userId;
AsymmetricKeyParameter privateKey = PrivateKeyFactory.createKey(keyPair.getPrivate().getEncoded());
AlgorithmIdentifier signatureAlgorithm = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA1WITHRSA");
AlgorithmIdentifier digestAlgorithm = new DefaultDigestAlgorithmIdentifierFinder().find("SHA-1");
ContentSigner signer = new BcRSAContentSignerBuilder(signatureAlgorithm, digestAlgorithm).build(privateKey);
PKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(new X500Name(principal), keyPair.getPublic());
ExtensionsGenerator extensionsGenerator = new ExtensionsGenerator();
extensionsGenerator.addExtension(Extension.basicConstraints, true, new BasicConstraints(true));
csrBuilder.addAttribute(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, extensionsGenerator.generate());
return csrBuilder.build(signer);
}
use of org.spongycastle.pkcs.PKCS10CertificationRequestBuilder in project android-mdm-agent by flyve-mdm.
the class AndroidCryptoProvider method generateRequest.
/**
* Generate the certificated Key pair
* @param callback
*/
public void generateRequest(GenerateCallback callback) {
byte[] snBytes = new byte[8];
new SecureRandom().nextBytes(snBytes);
KeyPair keyPair = null;
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
keyPairGenerator.initialize(4096);
keyPair = keyPairGenerator.generateKeyPair();
if (keyPair == null) {
callback.onGenerate(false);
return;
}
} catch (Exception ex) {
FlyveLog.wtf("KeyPairGenerator fail: %s", ex.getMessage());
callback.onGenerate(false);
return;
}
X500Principal subjectName = new X500Principal("CN=mydevice.stork-mdm.com");
ContentSigner signGen;
try {
signGen = new JcaContentSignerBuilder("SHA1withRSA").build(keyPair.getPrivate());
if (signGen == null) {
callback.onGenerate(false);
return;
}
} catch (Exception ex) {
FlyveLog.e("generateRequest", ex);
callback.onGenerate(false);
return;
}
PKCS10CertificationRequestBuilder builder = new JcaPKCS10CertificationRequestBuilder(subjectName, keyPair.getPublic());
csr = builder.build(signGen);
try {
key = (RSAPrivateKey) keyPair.getPrivate();
} catch (Exception ex) {
FlyveLog.wtf("generateRequest", ex);
callback.onGenerate(false);
return;
}
// Save the resulting pair
saveCsrKey();
// true or false
boolean bvar = loadCsr();
callback.onGenerate(bvar);
}
Aggregations