Package java.security.interfaces

Examples of java.security.interfaces.RSAPrivateKey


            CMSSignedDataGenerator gen = new CMSSignedDataGenerator();

           
            AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256WITHRSAENCRYPTION");
            AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
            RSAPrivateKey privateRSAKey = (RSAPrivateKey)privateKey;
            RSAKeyParameters keyParams = new RSAKeyParameters(true, privateRSAKey.getModulus(), privateRSAKey.getPrivateExponent());
            ContentSigner sigGen = new BcRSAContentSignerBuilder(sigAlgId, digAlgId).build(keyParams);

            gen.addSignerInfoGenerator(
                    new SignerInfoGeneratorBuilder(new BcDigestCalculatorProvider())
                        .build(sigGen, new X509CertificateHolder(certificate)));
View Full Code Here


     * @param privK key to test.
     * @return true if the key is extractable.
     */
    public static boolean isPrivateKeyExtractable(final PrivateKey privK) {
        if ( privK instanceof RSAPrivateKey ) {
            final RSAPrivateKey rsa = (RSAPrivateKey)privK;
            final BigInteger result = rsa.getPrivateExponent();
            return result!=null && result.bitLength()>0;
        }
        if ( privK instanceof ECPrivateKey ) {
            final ECPrivateKey ec = (ECPrivateKey)privK;
            final BigInteger result = ec.getS();
View Full Code Here

        JAXBElement<RegisterResultType> registerResult2 = (JAXBElement<RegisterResultType>) unmarshaller.unmarshal(bais);
        registerResultType = registerResult2.getValue();
       
        PrivateKeyType privateKeyType2 = registerResultType.getPrivateKey();
        RSAPrivateKey privkey2 = XKMSUtil.getPrivateKeyFromEncryptedXML(privateKeyType2, "This is total crap");
        X509Certificate cert = CertTools.genSelfCert("CN=test", 10, null,privkey2, keys.getPublic(), "SHA1WithRSA", true);
        cert.verify(keys.getPublic());   
    }   
View Full Code Here

   * @return a java RSAPrivateKey
   * @throws StringprepException if the shared secret doesn't conform with the SASLprep profile as specified in the XKMS specification.
   * @throws XMLEncryptionException if any other exception occurs during the processing.
   */
  public static RSAPrivateKey getPrivateKeyFromEncryptedXML(PrivateKeyType privateKeyType, String sharedSecret) throws StringprepException, XMLEncryptionException{
    RSAPrivateKey privkey2 = null;
    try{
    DocumentBuilder db = dbf.newDocumentBuilder();
        Document privateKeyDoc = db.newDocument();
        marshaller.marshal(privateKeyType, privateKeyDoc);
       
View Full Code Here

    boolean optionalV3Check = false;

    /** ** get session id from the request header and *** */
    /** ** get private key from servlet context. *** */
    String id = request.getHeader(SOTSignOnToolConstants.SESSION_ID);
    RSAPrivateKey privateKey = (RSAPrivateKey) getServletConfig()
        .getServletContext().getAttribute(id);

    if (privateKey == null) {
      // Error!!
      response.setStatus(SOTSignOnToolConstants.UNKNOWN);
      return;
    }

    /** ** get X509 certificate from the request. *** */
    X509Certificate[] certs = (X509Certificate[]) request
        .getAttribute("javax.servlet.request.X509Certificate");

    // error!
    if (certs == null || certs.length == 0) {
      response.setStatus(SOTSignOnToolConstants.CERT_NOT_FOUND);
      return;
    }
    X509Certificate cert = certs[0];

    /** ** get proxy certificate data from the request. *** */
    String proxyStr = "";

    try {
      DataInputStream dis = new DataInputStream(request.getInputStream());

      proxyStr = dis.readUTF();
      dis.close();
    } catch (Exception e) {
      log.error(e.toString());
    }

    /** ** check whether proxy cert is malicious or not. *** */

    /** ** this check is optional. *** */
    optionalV3Check = (Boolean.valueOf(rb.getString("OPTIONAL_CERT_CHECK")))
        .booleanValue();
    if (optionalV3Check) {
      SOTV3Verifier verifier = new SOTV3Verifier();

      if (!verifier.verifyProxyCert(proxyStr, cert)) {
        log.error("proxy cert invalid!");
        response.setStatus(SOTSignOnToolConstants.CERT_MALICIOUS);
        return;
      }

    }

    /** ** concatenate x509 and proxy and private key and *** */

    /** ** save to a file. *** */
    byte[] converted;

    try {
      converted = convert8to1(privateKey.getEncoded());
    } catch (Exception e) {
      // should never happen.
      log.error(e.toString());
      response.setStatus(SOTSignOnToolConstants.FAILED_SAVE_PROXY);
      return;
View Full Code Here

            return new RSAPublicKeySpec(k.getModulus(), k.getPublicExponent());
       }
       else if (spec.isAssignableFrom(RSAPrivateKeySpec.class) && key instanceof RSAPrivateKey)
       {
            RSAPrivateKey    k = (RSAPrivateKey)key;

            return new RSAPrivateKeySpec(k.getModulus(), k.getPrivateExponent());
       }
       else if (spec.isAssignableFrom(RSAPrivateCrtKeySpec.class) && key instanceof RSAPrivateCrtKey)
       {
            RSAPrivateCrtKey    k = (RSAPrivateCrtKey)key;

            return new RSAPrivateCrtKeySpec(
                            k.getModulus(), k.getPublicExponent(),
                            k.getPrivateExponent(),
                            k.getPrimeP(), k.getPrimeQ(),
                            k.getPrimeExponentP(), k.getPrimeExponentQ(),
                            k.getCrtCoefficient());
       }
       else if (spec.isAssignableFrom(DHPrivateKeySpec.class) && key instanceof DHPrivateKey)
       {
           DHPrivateKey k = (DHPrivateKey)key;
          
           return new DHPrivateKeySpec(k.getX(), k.getParams().getP(), k.getParams().getG());
       }
       else if (spec.isAssignableFrom(DHPublicKeySpec.class) && key instanceof DHPublicKey)
       {
           DHPublicKey k = (DHPublicKey)key;
          
           return new DHPublicKeySpec(k.getY(), k.getParams().getP(), k.getParams().getG());
       }

        throw new RuntimeException("not implemented yet " + key + " " + spec);
    }
View Full Code Here

            KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA", "BC");

            gen.initialize(1024, new SecureRandom());

            KeyPair         pair = gen.generateKeyPair();
            RSAPrivateKey   privKey = (RSAPrivateKey)pair.getPrivate();
            RSAPublicKey    pubKey = (RSAPublicKey)pair.getPublic();
            BigInteger      modulus = privKey.getModulus();
            BigInteger      privateExponent = privKey.getPrivateExponent();
           

            //
            // distinguished name table.
            //
            Hashtable                   attrs = new Hashtable();

            attrs.put(X509Principal.C, "AU");
            attrs.put(X509Principal.O, "The Legion of the Bouncy Castle");
            attrs.put(X509Principal.L, "Melbourne");
            attrs.put(X509Principal.ST, "Victoria");
            attrs.put(X509Principal.EmailAddress, "feedback-crypto@bouncycastle.org");

            //
            // extensions
            //

            //
            // create the certificate.
            //
            X509V3CertificateGenerator  certGen = new X509V3CertificateGenerator();

            certGen.setSerialNumber(BigInteger.valueOf(1));
            certGen.setIssuerDN(new X509Principal(attrs));
            certGen.setNotBefore(new Date(System.currentTimeMillis() - 50000));
            certGen.setNotAfter(new Date(System.currentTimeMillis() + 50000));
            certGen.setSubjectDN(new X509Principal(attrs));
            certGen.setPublicKey(pubKey);
            certGen.setSignatureAlgorithm("MD5WithRSAEncryption");

            Certificate[]   chain = new Certificate[1];

            try
            {
                X509Certificate cert = certGen.generateX509Certificate(privKey);

                cert.checkValidity(new Date());

                cert.verify(pubKey);

                ByteArrayInputStream    bIn = new ByteArrayInputStream(cert.getEncoded());
                CertificateFactory      fact = CertificateFactory.getInstance("X.509", "BC");

                cert = (X509Certificate)fact.generateCertificate(bIn);

                chain[0] = cert;
            }
            catch (Exception e)
            {
                return new SimpleTestResult(false, getName() + ": error generating cert - " + e.toString());
            }

            store.setKeyEntry("private", privKey, passwd, chain);
           
            //
            // write out and read back store
            //
            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
           
            store.store(bOut, passwd);
           
            ByteArrayInputStream    bIn = new ByteArrayInputStream(bOut.toByteArray());

            //
            // start with a new key store
            //
            store = KeyStore.getInstance(storeName, "BC");

            store.load(bIn, passwd);
           
            //
            // verify public key
            //
            privKey = (RSAPrivateKey)store.getKey("private", passwd);
           
            if (!privKey.getModulus().equals(modulus))
            {
                return new SimpleTestResult(false, getName() + ": private key modulus wrong");
            }
            else if (!privKey.getPrivateExponent().equals(privateExponent))
            {
                return new SimpleTestResult(false, getName() + ": private key exponent wrong");
            }

            //
View Full Code Here

                k.getPublicExponent(), k.getPrivateExponent(),
                k.getPrimeP(), k.getPrimeQ(), k.getPrimeExponentP(),                            k.getPrimeExponentQ(), k.getCrtCoefficient());
        }
        else
        {
            RSAPrivateKey    k = key;

            return new RSAKeyParameters(true, k.getModulus(), k.getPrivateExponent());
        }
    }
View Full Code Here

   * Verify proxy
   */
  private void verify() throws GeneralSecurityException
  {
     RSAPublicKey pkey = (RSAPublicKey) this.certificates[0].getPublicKey();
     RSAPrivateKey prkey = (RSAPrivateKey) userKey;
   
     if (!pkey.getModulus().equals(prkey.getModulus())) {
       throw new GeneralSecurityException(
         "Certificate and private key specified do not match");
     }
 
  }
View Full Code Here

        if (o == this)
        {
            return true;
        }

        RSAPrivateKey key = (RSAPrivateKey)o;

        return getModulus().equals(key.getModulus())
            && getPrivateExponent().equals(key.getPrivateExponent());
    }
View Full Code Here

TOP

Related Classes of java.security.interfaces.RSAPrivateKey

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.