AttrCertExample.java
上传用户:mbg588933
上传日期:2022-08-05
资源大小:10k
文件大小:12k
源码类别:

加密解密

开发平台:

Java

  1. import java.security.cert.*;
  2. import java.security.spec.RSAPrivateCrtKeySpec;
  3. import java.security.spec.RSAPublicKeySpec;
  4. import java.security.*;
  5. import java.math.*;
  6. import java.util.Date;
  7. import java.util.Hashtable;
  8. import java.util.Vector;
  9. import org.bouncycastle.jce.X509Principal;
  10. import org.bouncycastle.jce.provider.*;
  11. import org.bouncycastle.asn1.*;
  12. import org.bouncycastle.asn1.misc.MiscObjectIdentifiers;
  13. import org.bouncycastle.asn1.misc.NetscapeCertType;
  14. import org.bouncycastle.asn1.x509.GeneralName;
  15. import org.bouncycastle.x509.*;
  16. /**
  17.  * A simple example that generates an attribute certificate.
  18.  */
  19. public class AttrCertExample
  20. {
  21.     static X509V1CertificateGenerator  v1CertGen = new X509V1CertificateGenerator();
  22.     static X509V3CertificateGenerator  v3CertGen = new X509V3CertificateGenerator();
  23.     
  24.     /**
  25.      * we generate the AC issuer's certificate
  26.      */
  27.     public static X509Certificate createAcIssuerCert(
  28.         PublicKey       pubKey,
  29.         PrivateKey      privKey)
  30.         throws Exception
  31.     {
  32.         //
  33.         // signers name 
  34.         //
  35.         String  issuer = "C=AU, O=The Legion of the Bouncy Castle, OU=Bouncy Primary Certificate";
  36.         //
  37.         // subjects name - the same as we are self signed.
  38.         //
  39.         String  subject = "C=AU, O=The Legion of the Bouncy Castle, OU=Bouncy Primary Certificate";
  40.         //
  41.         // create the certificate - version 1
  42.         //f
  43.         v1CertGen.setSerialNumber(BigInteger.valueOf(10));
  44.         v1CertGen.setIssuerDN(new X509Principal(issuer));
  45.         v1CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30));
  46.         v1CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 30)));
  47.         v1CertGen.setSubjectDN(new X509Principal(subject));
  48.         v1CertGen.setPublicKey(pubKey);
  49.         v1CertGen.setSignatureAlgorithm("SHA1WithRSAEncryption");
  50.         X509Certificate cert = v1CertGen.generateX509Certificate(privKey);
  51.         cert.checkValidity(new Date());
  52.         cert.verify(pubKey);
  53.         return cert;
  54.     }
  55.     
  56.     /**
  57.      * we generate a certificate signed by our CA's intermediate certficate
  58.      */
  59.     public static X509Certificate createClientCert(
  60.         PublicKey       pubKey,
  61.         PrivateKey      caPrivKey,
  62.         PublicKey       caPubKey)
  63.         throws Exception
  64.     {
  65.         //
  66.         // issuer
  67.         //
  68.         String  issuer = "C=AU, O=The Legion of the Bouncy Castle, OU=Bouncy Primary Certificate";
  69.         //
  70.         // subjects name table.
  71.         //
  72.         Hashtable                   attrs = new Hashtable();
  73.         Vector                      order = new Vector();
  74.         attrs.put(X509Principal.C, "AU");
  75.         attrs.put(X509Principal.O, "The Legion of the Bouncy Castle");
  76.         attrs.put(X509Principal.L, "Melbourne");
  77.         attrs.put(X509Principal.CN, "Eric H. Echidna");
  78.         attrs.put(X509Principal.EmailAddress, "feedback-crypto@bouncycastle.org");
  79.         order.addElement(X509Principal.C);
  80.         order.addElement(X509Principal.O);
  81.         order.addElement(X509Principal.L);
  82.         order.addElement(X509Principal.CN);
  83.         order.addElement(X509Principal.EmailAddress);
  84.         //
  85.         // create the certificate - version 3
  86.         //
  87.         v3CertGen.reset();
  88.         v3CertGen.setSerialNumber(BigInteger.valueOf(20));
  89.         v3CertGen.setIssuerDN(new X509Principal(issuer));
  90.         v3CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30));
  91.         v3CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 30)));
  92.         v3CertGen.setSubjectDN(new X509Principal(order, attrs));
  93.         v3CertGen.setPublicKey(pubKey);
  94.         v3CertGen.setSignatureAlgorithm("SHA1WithRSAEncryption");
  95.         //
  96.         // add the extensions
  97.         //
  98.         v3CertGen.addExtension(
  99.             MiscObjectIdentifiers.netscapeCertType,
  100.             false,
  101.             new NetscapeCertType(NetscapeCertType.objectSigning | NetscapeCertType.smime));
  102.         X509Certificate cert = v3CertGen.generateX509Certificate(caPrivKey);
  103.         cert.checkValidity(new Date());
  104.         cert.verify(caPubKey);
  105.         return cert;
  106.     }
  107.     
  108.     public static void main(String args[])
  109.         throws Exception
  110.     {
  111.         Security.addProvider(new BouncyCastleProvider());
  112.         //
  113.         // personal keys
  114.         //
  115.         RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(
  116.             new BigInteger("b4a7e46170574f16a97082b22be58b6a2a629798419be12872a4bdba626cfae9900f76abfb12139dce5de56564fab2b6543165a040c606887420e33d91ed7ed7", 16),
  117.             new BigInteger("11", 16));
  118.         RSAPrivateCrtKeySpec privKeySpec = new RSAPrivateCrtKeySpec(
  119.             new BigInteger("b4a7e46170574f16a97082b22be58b6a2a629798419be12872a4bdba626cfae9900f76abfb12139dce5de56564fab2b6543165a040c606887420e33d91ed7ed7", 16),
  120.             new BigInteger("11", 16),
  121.             new BigInteger("9f66f6b05410cd503b2709e88115d55daced94d1a34d4e32bf824d0dde6028ae79c5f07b580f5dce240d7111f7ddb130a7945cd7d957d1920994da389f490c89", 16),
  122.             new BigInteger("c0a0758cdf14256f78d4708c86becdead1b50ad4ad6c5c703e2168fbf37884cb", 16),
  123.             new BigInteger("f01734d7960ea60070f1b06f2bb81bfac48ff192ae18451d5e56c734a5aab8a5", 16),
  124.             new BigInteger("b54bb9edff22051d9ee60f9351a48591b6500a319429c069a3e335a1d6171391", 16),
  125.             new BigInteger("d3d83daf2a0cecd3367ae6f8ae1aeb82e9ac2f816c6fc483533d8297dd7884cd", 16),
  126.             new BigInteger("b8f52fc6f38593dabb661d3f50f8897f8106eee68b1bce78a95b132b4e5b5d19", 16));
  127.         //
  128.         // ca keys
  129.         //
  130.         RSAPublicKeySpec caPubKeySpec = new RSAPublicKeySpec(
  131.             new BigInteger("b259d2d6e627a768c94be36164c2d9fc79d97aab9253140e5bf17751197731d6f7540d2509e7b9ffee0a70a6e26d56e92d2edd7f85aba85600b69089f35f6bdbf3c298e05842535d9f064e6b0391cb7d306e0a2d20c4dfb4e7b49a9640bdea26c10ad69c3f05007ce2513cee44cfe01998e62b6c3637d3fc0391079b26ee36d5", 16),
  132.             new BigInteger("11", 16));
  133.         RSAPrivateCrtKeySpec   caPrivKeySpec = new RSAPrivateCrtKeySpec(
  134.             new BigInteger("b259d2d6e627a768c94be36164c2d9fc79d97aab9253140e5bf17751197731d6f7540d2509e7b9ffee0a70a6e26d56e92d2edd7f85aba85600b69089f35f6bdbf3c298e05842535d9f064e6b0391cb7d306e0a2d20c4dfb4e7b49a9640bdea26c10ad69c3f05007ce2513cee44cfe01998e62b6c3637d3fc0391079b26ee36d5", 16),
  135.             new BigInteger("11", 16),
  136.             new BigInteger("92e08f83cc9920746989ca5034dcb384a094fb9c5a6288fcc4304424ab8f56388f72652d8fafc65a4b9020896f2cde297080f2a540e7b7ce5af0b3446e1258d1dd7f245cf54124b4c6e17da21b90a0ebd22605e6f45c9f136d7a13eaac1c0f7487de8bd6d924972408ebb58af71e76fd7b012a8d0e165f3ae2e5077a8648e619", 16),
  137.             new BigInteger("f75e80839b9b9379f1cf1128f321639757dba514642c206bbbd99f9a4846208b3e93fbbe5e0527cc59b1d4b929d9555853004c7c8b30ee6a213c3d1bb7415d03", 16),
  138.             new BigInteger("b892d9ebdbfc37e397256dd8a5d3123534d1f03726284743ddc6be3a709edb696fc40c7d902ed804c6eee730eee3d5b20bf6bd8d87a296813c87d3b3cc9d7947", 16),
  139.             new BigInteger("1d1a2d3ca8e52068b3094d501c9a842fec37f54db16e9a67070a8b3f53cc03d4257ad252a1a640eadd603724d7bf3737914b544ae332eedf4f34436cac25ceb5", 16),
  140.             new BigInteger("6c929e4e81672fef49d9c825163fec97c4b7ba7acb26c0824638ac22605d7201c94625770984f78a56e6e25904fe7db407099cad9b14588841b94f5ab498dded", 16),
  141.             new BigInteger("dae7651ee69ad1d081ec5e7188ae126f6004ff39556bde90e0b870962fa7b926d070686d8244fe5a9aa709a95686a104614834b0ada4b10f53197a5cb4c97339", 16));
  142.         //
  143.         // set up the keys
  144.         //
  145.         KeyFactory          fact = KeyFactory.getInstance("RSA", "BC");
  146.         PrivateKey          caPrivKey = fact.generatePrivate(caPrivKeySpec);
  147.         PublicKey           caPubKey = fact.generatePublic(caPubKeySpec);
  148.         PrivateKey          privKey = fact.generatePrivate(privKeySpec);
  149.         PublicKey           pubKey = fact.generatePublic(pubKeySpec);
  150.         //
  151.         // note in this case we are using the CA certificate for both the client cetificate
  152.         // and the attribute certificate. This is to make the vcode simpler to read, in practice
  153.         // the CA for the attribute certificate should be different to that of the client certificate
  154.         //
  155.         X509Certificate     caCert = createAcIssuerCert(caPubKey, caPrivKey);
  156.         X509Certificate     clientCert = createClientCert(pubKey, caPrivKey, caPubKey);
  157.         // Instantiate a new AC generator
  158.         X509V2AttributeCertificateGenerator acGen = new X509V2AttributeCertificateGenerator();
  159.         acGen.reset();
  160.         //
  161.         // Holder: here we use the IssuerSerial form
  162.         //
  163.         acGen.setHolder(new AttributeCertificateHolder(clientCert));
  164.         // set the Issuer
  165.         acGen.setIssuer(new AttributeCertificateIssuer(caCert.getSubjectX500Principal()));
  166.         //
  167.         // serial number (as it's an example we don't have to keep track of the
  168.         // serials anyway
  169.         //
  170.         acGen.setSerialNumber(new BigInteger("1"));
  171.         // not Before
  172.         acGen.setNotBefore(new Date(System.currentTimeMillis() - 50000));
  173.         // not After
  174.         acGen.setNotAfter(new Date(System.currentTimeMillis() + 50000));
  175.         // signature Algorithmus
  176.         acGen.setSignatureAlgorithm("SHA1WithRSAEncryption");
  177.         // the actual attributes
  178.         GeneralName roleName = new GeneralName(GeneralName.rfc822Name, "DAU123456789");
  179.         ASN1EncodableVector roleSyntax = new ASN1EncodableVector();
  180.         roleSyntax.add(roleName);
  181.         // roleSyntax OID: 2.5.24.72
  182.         X509Attribute attributes = new X509Attribute("2.5.24.72",
  183.                 new DERSequence(roleSyntax));
  184.         acGen.addAttribute(attributes);
  185.         //      finally create the AC
  186.         X509V2AttributeCertificate att = (X509V2AttributeCertificate)acGen
  187.                 .generateCertificate(caPrivKey, "BC");
  188.         //
  189.         // starting here, we parse the newly generated AC
  190.         //
  191.         // Holder
  192.         AttributeCertificateHolder h = att.getHolder();
  193.         if (h.match(clientCert))
  194.         {
  195.             if (h.getEntityNames() != null)
  196.             {
  197.                 System.out.println(h.getEntityNames().length + " entity names found");
  198.             }
  199.             if (h.getIssuer() != null)
  200.             {
  201.                 System.out.println(h.getIssuer().length + " issuer names found, serial number " + h.getSerialNumber());
  202.             }
  203.             System.out.println("Matches original client x509 cert");
  204.         }
  205.         // Issuer
  206.         
  207.         AttributeCertificateIssuer issuer = att.getIssuer();
  208.         if (issuer.match(caCert))
  209.         {
  210.             if (issuer.getPrincipals() != null)
  211.             {
  212.                 System.out.println(issuer.getPrincipals().length + " entity names found");
  213.             }
  214.             System.out.println("Matches original ca x509 cert");
  215.         }
  216.         
  217.         // Dates
  218.         System.out.println("valid not before: " + att.getNotBefore());
  219.         System.out.println("valid not before: " + att.getNotAfter());
  220.         // check the dates, an exception is thrown in checkValidity()...
  221.         try
  222.         {
  223.             att.checkValidity();
  224.             att.checkValidity(new Date());
  225.         }
  226.         catch (Exception e)
  227.         {
  228.             System.out.println(e);
  229.         }
  230.         // verify
  231.         try
  232.         {
  233.             att.verify(caPubKey, "BC");
  234.         }
  235.         catch (Exception e)
  236.         {
  237.             System.out.println(e);
  238.         }
  239.         // Attribute
  240.         X509Attribute[] attribs = att.getAttributes();
  241.         System.out.println("cert has " + attribs.length + " attributes:");
  242.         for (int i = 0; i < attribs.length; i++)
  243.         {
  244.             X509Attribute a = attribs[i];
  245.             System.out.println("OID: " + a.getOID());
  246.             
  247.             // currently we only check for the presence of a 'RoleSyntax' attribute
  248.             if (a.getOID().equals("2.5.24.72"))
  249.             {
  250.                 System.out.println("rolesyntax read from cert!");
  251.             }
  252.         }
  253.     }
  254. }