CsrHelper.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package com.owncloud.android.utils;
  2. import org.spongycastle.asn1.pkcs.PKCSObjectIdentifiers;
  3. import org.spongycastle.asn1.x500.X500Name;
  4. import org.spongycastle.asn1.x509.AlgorithmIdentifier;
  5. import org.spongycastle.asn1.x509.BasicConstraints;
  6. import org.spongycastle.asn1.x509.Extension;
  7. import org.spongycastle.asn1.x509.ExtensionsGenerator;
  8. import org.spongycastle.crypto.params.AsymmetricKeyParameter;
  9. import org.spongycastle.crypto.util.PrivateKeyFactory;
  10. import org.spongycastle.operator.ContentSigner;
  11. import org.spongycastle.operator.DefaultDigestAlgorithmIdentifierFinder;
  12. import org.spongycastle.operator.DefaultSignatureAlgorithmIdentifierFinder;
  13. import org.spongycastle.operator.OperatorCreationException;
  14. import org.spongycastle.operator.bc.BcRSAContentSignerBuilder;
  15. import org.spongycastle.pkcs.PKCS10CertificationRequest;
  16. import org.spongycastle.pkcs.PKCS10CertificationRequestBuilder;
  17. import org.spongycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder;
  18. import java.io.IOException;
  19. import java.security.KeyPair;
  20. /**
  21. * copied & modified from:
  22. * https://github.com/awslabs/aws-sdk-android-samples/blob/master/CreateIotCertWithCSR/src/com/amazonaws/demo/csrcert/CsrHelper.java
  23. * accessed at 31.08.17
  24. * Original parts are licensed under the Apache License, Version 2.0: http://aws.amazon.com/apache2.0
  25. * Own parts are licensed unter GPLv3+.
  26. */
  27. public class CsrHelper {
  28. /**
  29. * Generate CSR with PEM encoding
  30. *
  31. * @param keyPair the KeyPair with private and public keys
  32. * @param userId userId of CSR owner
  33. * @return PEM encoded CSR string
  34. * @throws IOException thrown if key cannot be created
  35. * @throws OperatorCreationException thrown if contentSigner cannot be build
  36. */
  37. public static String generateCsrPemEncodedString(KeyPair keyPair, String userId)
  38. throws IOException, OperatorCreationException {
  39. PKCS10CertificationRequest csr = CsrHelper.generateCSR(keyPair, userId);
  40. byte[] derCSR = csr.getEncoded();
  41. return "-----BEGIN CERTIFICATE REQUEST-----\n" + android.util.Base64.encodeToString(derCSR,
  42. android.util.Base64.NO_WRAP) + "\n-----END CERTIFICATE REQUEST-----";
  43. }
  44. /**
  45. * Create the certificate signing request (CSR) from private and public keys
  46. *
  47. * @param keyPair the KeyPair with private and public keys
  48. * @param userId userId of CSR owner
  49. * @return PKCS10CertificationRequest with the certificate signing request (CSR) data
  50. * @throws IOException thrown if key cannot be created
  51. * @throws OperatorCreationException thrown if contentSigner cannot be build
  52. */
  53. private static PKCS10CertificationRequest generateCSR(KeyPair keyPair, String userId) throws IOException,
  54. OperatorCreationException {
  55. String principal = "CN=" + userId.split("@")[0];
  56. AsymmetricKeyParameter privateKey = PrivateKeyFactory.createKey(keyPair.getPrivate().getEncoded());
  57. AlgorithmIdentifier signatureAlgorithm = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA1WITHRSA");
  58. AlgorithmIdentifier digestAlgorithm = new DefaultDigestAlgorithmIdentifierFinder().find("SHA-1");
  59. ContentSigner signer = new BcRSAContentSignerBuilder(signatureAlgorithm, digestAlgorithm).build(privateKey);
  60. PKCS10CertificationRequestBuilder csrBuilder = new JcaPKCS10CertificationRequestBuilder(new X500Name(principal),
  61. keyPair.getPublic());
  62. ExtensionsGenerator extensionsGenerator = new ExtensionsGenerator();
  63. extensionsGenerator.addExtension(Extension.basicConstraints, true, new BasicConstraints(true));
  64. csrBuilder.addAttribute(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest, extensionsGenerator.generate());
  65. return csrBuilder.build(signer);
  66. }
  67. }