Example usage for org.bouncycastle.asn1.pkcs PKCSObjectIdentifiers sha256WithRSAEncryption

List of usage examples for org.bouncycastle.asn1.pkcs PKCSObjectIdentifiers sha256WithRSAEncryption

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs PKCSObjectIdentifiers sha256WithRSAEncryption.

Prototype

ASN1ObjectIdentifier sha256WithRSAEncryption

To view the source code for org.bouncycastle.asn1.pkcs PKCSObjectIdentifiers sha256WithRSAEncryption.

Click Source Link

Document

PKCS#1: 1.2.840.113549.1.1.11

Usage

From source file:org.ejbca.core.protocol.ws.client.NestedCrmfRequestTestCommand.java

License:Open Source License

private void init(String args[]) {

    FileInputStream file_inputstream;
    try {//from  w  ww.j ava 2 s .  co  m
        String pwd = args[ARG_KEYSTOREPASSWORD];
        String certNameInKeystore = args[ARG_CERTNAMEINKEYSTORE];
        file_inputstream = new FileInputStream(args[ARG_KEYSTOREPATH]);
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(file_inputstream, pwd.toCharArray());
        System.out.println("Keystore size " + keyStore.size());
        Enumeration aliases = keyStore.aliases();
        while (aliases.hasMoreElements()) {
            System.out.println(aliases.nextElement());
        }
        Key key = keyStore.getKey(certNameInKeystore, pwd.toCharArray());
        getPrintStream().println("Key information " + key.getAlgorithm() + " " + key.getFormat());
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(key.getEncoded());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        innerSignKey = keyFactory.generatePrivate(keySpec);
        innerCertificate = keyStore.getCertificate(certNameInKeystore);
    } catch (FileNotFoundException e2) {
        e2.printStackTrace();
    } catch (KeyStoreException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (CertificateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (UnrecoverableKeyException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    }

    try {
        KeyPair outerSignKeys = KeyTools.genKeys("1024", "RSA");
        outerSignKey = outerSignKeys.getPrivate();
        X509Certificate signCert = CertTools.genSelfCert("CN=cmpTest,C=SE", 5000, null,
                outerSignKeys.getPrivate(), outerSignKeys.getPublic(),
                PKCSObjectIdentifiers.sha256WithRSAEncryption.getId(), true, "BC");

        writeCertificate(signCert, "/opt/racerts", "cmpTest.pem");

        /*
        ArrayList<Certificate> certCollection = new ArrayList<Certificate>();
        certCollection.add(signCert);
        byte[] pemRaCert = CertTools.getPEMFromCerts(certCollection);
                
        FileOutputStream out = new FileOutputStream(new File("/opt/racerts/cmpStressTest.pem"));
        out.write(pemRaCert);
        out.close();
        */
    } catch (NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    } catch (NoSuchProviderException e1) {
        e1.printStackTrace();
    } catch (InvalidAlgorithmParameterException e1) {
        e1.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (CertificateEncodingException e) {
        e.printStackTrace();
    } catch (SignatureException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
        //} catch (FileNotFoundException e) {
        //   e.printStackTrace();
        //} catch (IOException e) {
        //   e.printStackTrace();
        //} catch (CertificateException e) {
        //   e.printStackTrace();
    }

}

From source file:org.ejbca.core.protocol.ws.client.NestedCrmfRequestTestCommand.java

License:Open Source License

/**
 * Runs the command/*ww  w .j  ava  2s  .c om*/
 *
 * @throws IllegalAdminCommandException Error in command args
 * @throws ErrorAdminCommandException Error running command
 */
public void execute() throws IllegalAdminCommandException, ErrorAdminCommandException {

    try {

        CertRequest certReq = genCertReq(userDN, null);

        PKIMessage certMsg = genPKIMessage(false, certReq);
        if (certMsg == null) {
            getPrintStream().println("No certificate request.");
            System.exit(-1);
        }
        AlgorithmIdentifier pAlg = new AlgorithmIdentifier(PKCSObjectIdentifiers.sha256WithRSAEncryption);
        certMsg.getHeader().setProtectionAlg(pAlg);
        certMsg.getHeader().setSenderKID(new DEROctetString("CMPEnduser".getBytes()));
        PKIMessage signedMsg = signPKIMessage(certMsg, innerSignKey);
        addExtraCert(signedMsg, innerCertificate);
        if (signedMsg == null) {
            getPrintStream().println("No protected message.");
            System.exit(-1);
        }

        PKIHeader myPKIHeader = new PKIHeader(new DERInteger(2),
                new GeneralName(new X509Name("CN=CMSSender,C=SE")),
                new GeneralName(new X509Name(((X509Certificate) cacert).getSubjectDN().getName())));
        myPKIHeader.setMessageTime(new DERGeneralizedTime(new Date()));
        // senderNonce
        myPKIHeader.setSenderNonce(new DEROctetString(nonce));
        // TransactionId
        myPKIHeader.setTransactionID(new DEROctetString(nonce));
        //myPKIHeader.addGeneralInfo(new InfoTypeAndValue(ASN1Sequence.getInstance(crmfMsg)));

        PKIBody myPKIBody = new PKIBody(signedMsg, 20); // NestedMessageContent
        PKIMessage myPKIMessage = new PKIMessage(myPKIHeader, myPKIBody);
        PKIMessage cmsMessage = signPKIMessage(myPKIMessage, outerSignKey);

        reqId = signedMsg.getBody().getIr().getCertReqMsg(0).getCertReq().getCertReqId().getValue().intValue();
        final ByteArrayOutputStream bao = new ByteArrayOutputStream();
        final DEROutputStream out = new DEROutputStream(bao);
        out.writeObject(cmsMessage);
        final byte[] ba = bao.toByteArray();
        // Send request and receive response
        final byte[] resp = sendCmp(ba);
        if (resp == null || resp.length <= 0) {
            getPrintStream().println("No response message.");
            System.exit(-1);
        }
        /*
        if ( !checkCmpResponseGeneral(resp, true) ) {
           System.exit(-1);
        }
        */
        final X509Certificate cert = checkCmpCertRepMessage(resp, reqId);
        if (cert == null) {
            getPrintStream().println("No certificate was created.");
            System.exit(-1);
        }
        getPrintStream().println("Certificate for " + userDN + " was created with the serialnumber: "
                + cert.getSerialNumber().toString());

        if (createsCertsPath != null) {
            String filename = CertTools.getPartFromDN(cert.getSubjectDN().toString(), "CN") + ".pem";
            writeCertificate(cert, createsCertsPath, filename);
            getPrintStream().println("Certificate was written to: " + createsCertsPath + "/" + filename);
        }

    } catch (IOException e) {
        e.printStackTrace(getPrintStream());
        System.exit(-1);
    } catch (InvalidKeyException e) {
        e.printStackTrace(getPrintStream());
        System.exit(-1);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace(getPrintStream());
        System.exit(-1);
    } catch (SignatureException e) {
        e.printStackTrace(getPrintStream());
        System.exit(-1);
    } catch (NoSuchProviderException e) {
        e.printStackTrace(getPrintStream());
        System.exit(-1);
    } catch (CertificateEncodingException e) {
        e.printStackTrace(getPrintStream());
        System.exit(-1);
    } catch (Exception e) {
        e.printStackTrace(getPrintStream());
        System.exit(-1);
    }
    getPrintStream().println("Test successfull");
}

From source file:org.keycloak.testsuite.forms.x509.OcspHandler.java

License:Open Source License

@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
    if (exchange.isInIoThread()) {
        exchange.dispatch(this);
        return;/*  w ww  .  ja  v  a 2s  . c  o m*/
    }

    final byte[] buffy = new byte[16384];
    try (InputStream requestStream = exchange.getInputStream()) {
        requestStream.read(buffy);
    }

    final OCSPReq request = new OCSPReq(buffy);
    final Req[] requested = request.getRequestList();

    final Extension nonce = request.getExtension(OCSPObjectIdentifiers.id_pkix_ocsp_nonce);

    final DigestCalculator sha1Calculator = new JcaDigestCalculatorProviderBuilder().build()
            .get(AlgorithmIdentifier.getInstance(RespID.HASH_SHA1));

    final BasicOCSPRespBuilder responseBuilder = new BasicOCSPRespBuilder(subjectPublicKeyInfo, sha1Calculator);

    if (nonce != null) {
        responseBuilder.setResponseExtensions(new Extensions(nonce));
    }

    for (final Req req : requested) {
        final CertificateID certId = req.getCertID();

        final BigInteger certificateSerialNumber = certId.getSerialNumber();
        responseBuilder.addResponse(certId, REVOKED_CERTIFICATES_STATUS.get(certificateSerialNumber));
    }

    final ContentSigner contentSigner = new BcRSAContentSignerBuilder(
            new AlgorithmIdentifier(PKCSObjectIdentifiers.sha256WithRSAEncryption),
            new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha256)).build(privateKey);

    final OCSPResp response = new OCSPRespBuilder().build(OCSPResp.SUCCESSFUL,
            responseBuilder.build(contentSigner, chain, new Date()));

    final byte[] responseBytes = response.getEncoded();

    final HeaderMap responseHeaders = exchange.getResponseHeaders();
    responseHeaders.put(Headers.CONTENT_TYPE, "application/ocsp-response");

    final Sender responseSender = exchange.getResponseSender();
    responseSender.send(ByteBuffer.wrap(responseBytes));

    exchange.endExchange();
}

From source file:org.xipki.common.util.AlgorithmUtil.java

License:Open Source License

static public String getSignatureAlgoName(final AlgorithmIdentifier sigAlgId) throws NoSuchAlgorithmException {
    ASN1ObjectIdentifier algOid = sigAlgId.getAlgorithm();

    if (X9ObjectIdentifiers.ecdsa_with_SHA1.equals(algOid)) {
        return "SHA1withECDSA";
    } else if (X9ObjectIdentifiers.ecdsa_with_SHA224.equals(algOid)) {
        return "SHA224withECDSA";
    } else if (X9ObjectIdentifiers.ecdsa_with_SHA256.equals(algOid)) {
        return "SHA256withECDSA";
    } else if (X9ObjectIdentifiers.ecdsa_with_SHA384.equals(algOid)) {
        return "SHA384withECDSA";
    } else if (X9ObjectIdentifiers.ecdsa_with_SHA512.equals(algOid)) {
        return "SHA512WITHECDSA";
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA1.equals(algOid)) {
        return "SHA1WITHPLAIN-ECDSA";
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA224.equals(algOid)) {
        return "SHA224WITHPLAIN-ECDSA";
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA256.equals(algOid)) {
        return "SHA256WITHPLAIN-ECDSA";
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA384.equals(algOid)) {
        return "SHA384WITHPLAIN-ECDSA";
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA512.equals(algOid)) {
        return "SHA512WITHPLAIN-ECDSA";
    } else if (X9ObjectIdentifiers.id_dsa_with_sha1.equals(algOid)) {
        return "SHA1withDSA";
    } else if (X9ObjectIdentifiers.id_dsa_with_sha1.equals(algOid)) {
        return "SHA1withDSA";
    } else if (NISTObjectIdentifiers.dsa_with_sha224.equals(algOid)) {
        return "SHA224withDSA";
    } else if (NISTObjectIdentifiers.dsa_with_sha256.equals(algOid)) {
        return "SHA256withDSA";
    } else if (NISTObjectIdentifiers.dsa_with_sha384.equals(algOid)) {
        return "SHA384withDSA";
    } else if (NISTObjectIdentifiers.dsa_with_sha512.equals(algOid)) {
        return "SHA512withDSA";
    } else if (PKCSObjectIdentifiers.sha1WithRSAEncryption.equals(algOid)) {
        return "SHA1withRSA";
    } else if (PKCSObjectIdentifiers.sha224WithRSAEncryption.equals(algOid)) {
        return "SHA224withRSA";
    } else if (PKCSObjectIdentifiers.sha256WithRSAEncryption.equals(algOid)) {
        return "SHA256withRSA";
    } else if (PKCSObjectIdentifiers.sha384WithRSAEncryption.equals(algOid)) {
        return "SHA384withRSA";
    } else if (PKCSObjectIdentifiers.sha512WithRSAEncryption.equals(algOid)) {
        return "SHA512withRSA";
    } else if (PKCSObjectIdentifiers.id_RSASSA_PSS.equals(algOid)) {
        RSASSAPSSparams param = RSASSAPSSparams.getInstance(sigAlgId.getParameters());
        ASN1ObjectIdentifier digestAlgOid = param.getHashAlgorithm().getAlgorithm();
        if (X509ObjectIdentifiers.id_SHA1.equals(digestAlgOid)) {
            return "SHA1withRSAandMGF1";
        } else if (NISTObjectIdentifiers.id_sha256.equals(digestAlgOid)) {
            return "SHA256withRSAandMGF1";
        } else if (NISTObjectIdentifiers.id_sha384.equals(digestAlgOid)) {
            return "SHA384withRSAandMGF1";
        } else if (NISTObjectIdentifiers.id_sha512.equals(digestAlgOid)) {
            return "SHA512withRSAandMGF1";
        } else {//from ww  w.j a v  a2 s .c  om
            throw new NoSuchAlgorithmException("unsupported digest algorithm " + digestAlgOid.getId());
        }
    } else {
        throw new NoSuchAlgorithmException("unsupported signature algorithm " + algOid.getId());
    }
}

From source file:org.xipki.common.util.AlgorithmUtil.java

License:Open Source License

static public AlgorithmIdentifier getSignatureAlgoId(final String signatureAlgoName)
        throws NoSuchAlgorithmException {
    String algoS = signatureAlgoName.replaceAll("-", "");

    AlgorithmIdentifier signatureAlgId;//from w  w  w.  j av  a2s  . c  o  m
    if ("SHA1withRSAandMGF1".equalsIgnoreCase(algoS) || "SHA224withRSAandMGF1".equalsIgnoreCase(algoS)
            || "SHA256withRSAandMGF1".equalsIgnoreCase(algoS) || "SHA384withRSAandMGF1".equalsIgnoreCase(algoS)
            || "SHA512withRSAandMGF1".equalsIgnoreCase(algoS)) {
        ASN1ObjectIdentifier hashAlgo;
        if ("SHA1withRSAandMGF1".equalsIgnoreCase(algoS)) {
            hashAlgo = X509ObjectIdentifiers.id_SHA1;
        } else if ("SHA224withRSAandMGF1".equalsIgnoreCase(algoS)) {
            hashAlgo = NISTObjectIdentifiers.id_sha224;
        } else if ("SHA256withRSAandMGF1".equalsIgnoreCase(algoS)) {
            hashAlgo = NISTObjectIdentifiers.id_sha256;
        } else if ("SHA384withRSAandMGF1".equalsIgnoreCase(algoS)) {
            hashAlgo = NISTObjectIdentifiers.id_sha384;
        } else if ("SHA512withRSAandMGF1".equalsIgnoreCase(algoS)) {
            hashAlgo = NISTObjectIdentifiers.id_sha512;
        } else {
            throw new NoSuchAlgorithmException("should not reach here, unknown algorithm " + algoS);
        }

        signatureAlgId = AlgorithmUtil.buildRSAPSSAlgorithmIdentifier(hashAlgo);
    } else {
        boolean withNullParam = false;
        ASN1ObjectIdentifier algOid;
        if ("SHA1withRSA".equalsIgnoreCase(algoS) || "RSAwithSHA1".equalsIgnoreCase(algoS)
                || PKCSObjectIdentifiers.sha1WithRSAEncryption.getId().equals(algoS)) {
            algOid = PKCSObjectIdentifiers.sha1WithRSAEncryption;
            withNullParam = true;
        } else if ("SHA224withRSA".equalsIgnoreCase(algoS) || "RSAwithSHA224".equalsIgnoreCase(algoS)
                || PKCSObjectIdentifiers.sha224WithRSAEncryption.getId().equals(algoS)) {
            algOid = PKCSObjectIdentifiers.sha224WithRSAEncryption;
            withNullParam = true;
        } else if ("SHA256withRSA".equalsIgnoreCase(algoS) || "RSAwithSHA256".equalsIgnoreCase(algoS)
                || PKCSObjectIdentifiers.sha256WithRSAEncryption.getId().equals(algoS)) {
            algOid = PKCSObjectIdentifiers.sha256WithRSAEncryption;
            withNullParam = true;
        } else if ("SHA384withRSA".equalsIgnoreCase(algoS) || "RSAwithSHA384".equalsIgnoreCase(algoS)
                || PKCSObjectIdentifiers.sha384WithRSAEncryption.getId().equals(algoS)) {
            algOid = PKCSObjectIdentifiers.sha384WithRSAEncryption;
            withNullParam = true;
        } else if ("SHA512withRSA".equalsIgnoreCase(algoS) || "RSAwithSHA512".equalsIgnoreCase(algoS)
                || PKCSObjectIdentifiers.sha512WithRSAEncryption.getId().equals(algoS)) {
            algOid = PKCSObjectIdentifiers.sha512WithRSAEncryption;
            withNullParam = true;
        } else if ("SHA1withECDSA".equalsIgnoreCase(algoS) || "ECDSAwithSHA1".equalsIgnoreCase(algoS)
                || X9ObjectIdentifiers.ecdsa_with_SHA1.getId().equals(algoS)) {
            algOid = X9ObjectIdentifiers.ecdsa_with_SHA1;
        } else if ("SHA224withECDSA".equalsIgnoreCase(algoS) || "ECDSAwithSHA224".equalsIgnoreCase(algoS)
                || X9ObjectIdentifiers.ecdsa_with_SHA224.getId().equals(algoS)) {
            algOid = X9ObjectIdentifiers.ecdsa_with_SHA224;
        } else if ("SHA256withECDSA".equalsIgnoreCase(algoS) || "ECDSAwithSHA256".equalsIgnoreCase(algoS)
                || X9ObjectIdentifiers.ecdsa_with_SHA256.getId().equals(algoS)) {
            algOid = X9ObjectIdentifiers.ecdsa_with_SHA256;
        } else if ("SHA384withECDSA".equalsIgnoreCase(algoS) || "ECDSAwithSHA384".equalsIgnoreCase(algoS)
                || X9ObjectIdentifiers.ecdsa_with_SHA384.getId().equals(algoS)) {
            algOid = X9ObjectIdentifiers.ecdsa_with_SHA384;
        } else if ("SHA512withECDSA".equalsIgnoreCase(algoS) || "ECDSAwithSHA512".equalsIgnoreCase(algoS)
                || X9ObjectIdentifiers.ecdsa_with_SHA512.getId().equals(algoS)) {
            algOid = X9ObjectIdentifiers.ecdsa_with_SHA512;
        } else if ("SHA1withPlainECDSA".equalsIgnoreCase(algoS) || "PlainECDSAwithSHA1".equalsIgnoreCase(algoS)
                || BSIObjectIdentifiers.ecdsa_plain_SHA1.getId().equals(algoS)) {
            algOid = BSIObjectIdentifiers.ecdsa_plain_SHA1;
        } else if ("SHA224withPlainECDSA".equalsIgnoreCase(algoS)
                || "PlainECDSAwithSHA224".equalsIgnoreCase(algoS)
                || BSIObjectIdentifiers.ecdsa_plain_SHA224.getId().equals(algoS)) {
            algOid = BSIObjectIdentifiers.ecdsa_plain_SHA224;
        } else if ("SHA256withPlainECDSA".equalsIgnoreCase(algoS)
                || "PlainECDSAwithSHA256".equalsIgnoreCase(algoS)
                || BSIObjectIdentifiers.ecdsa_plain_SHA256.getId().equals(algoS)) {
            algOid = BSIObjectIdentifiers.ecdsa_plain_SHA256;
        } else if ("SHA384withPlainECDSA".equalsIgnoreCase(algoS)
                || "PlainECDSAwithSHA384".equalsIgnoreCase(algoS)
                || BSIObjectIdentifiers.ecdsa_plain_SHA384.getId().equals(algoS)) {
            algOid = BSIObjectIdentifiers.ecdsa_plain_SHA384;
        } else if ("SHA512withPlainECDSA".equalsIgnoreCase(algoS)
                || "PlainECDSAwithSHA512".equalsIgnoreCase(algoS)
                || BSIObjectIdentifiers.ecdsa_plain_SHA512.getId().equals(algoS)) {
            algOid = BSIObjectIdentifiers.ecdsa_plain_SHA512;
        } else if ("SHA1withDSA".equalsIgnoreCase(algoS) || "DSAwithSHA1".equalsIgnoreCase(algoS)
                || X9ObjectIdentifiers.id_dsa_with_sha1.getId().equals(algoS)) {
            algOid = X9ObjectIdentifiers.id_dsa_with_sha1;
        } else if ("SHA224withDSA".equalsIgnoreCase(algoS) || "DSAwithSHA224".equalsIgnoreCase(algoS)
                || NISTObjectIdentifiers.dsa_with_sha224.getId().equals(algoS)) {
            algOid = NISTObjectIdentifiers.dsa_with_sha224;
        } else if ("SHA256withDSA".equalsIgnoreCase(algoS) || "DSAwithSHA256".equalsIgnoreCase(algoS)
                || NISTObjectIdentifiers.dsa_with_sha256.getId().equals(algoS)) {
            algOid = NISTObjectIdentifiers.dsa_with_sha256;
        } else if ("SHA384withDSA".equalsIgnoreCase(algoS) || "DSAwithSHA384".equalsIgnoreCase(algoS)
                || NISTObjectIdentifiers.dsa_with_sha384.getId().equals(algoS)) {
            algOid = NISTObjectIdentifiers.dsa_with_sha384;
        } else if ("SHA512withDSA".equalsIgnoreCase(algoS) || "DSAwithSHA512".equalsIgnoreCase(algoS)
                || NISTObjectIdentifiers.dsa_with_sha512.getId().equals(algoS)) {
            algOid = NISTObjectIdentifiers.dsa_with_sha512;
        } else {
            throw new NoSuchAlgorithmException("unsupported signature algorithm " + algoS);
        }

        signatureAlgId = withNullParam ? new AlgorithmIdentifier(algOid, DERNull.INSTANCE)
                : new AlgorithmIdentifier(algOid);
    }

    return signatureAlgId;
}

From source file:org.xipki.common.util.AlgorithmUtil.java

License:Open Source License

static public AlgorithmIdentifier getRSASignatureAlgoId(final String hashAlgo, final boolean mgf1)
        throws NoSuchAlgorithmException {
    if (mgf1) {/*from  w  w w  .j av  a 2 s.co m*/
        ASN1ObjectIdentifier hashAlgoOid = AlgorithmUtil.getHashAlg(hashAlgo);
        return AlgorithmUtil.buildRSAPSSAlgorithmIdentifier(hashAlgoOid);
    }

    ASN1ObjectIdentifier sigAlgoOid;
    if ("SHA1".equalsIgnoreCase(hashAlgo)) {
        sigAlgoOid = PKCSObjectIdentifiers.sha1WithRSAEncryption;
    } else if ("SHA224".equalsIgnoreCase(hashAlgo)) {
        sigAlgoOid = PKCSObjectIdentifiers.sha224WithRSAEncryption;
    } else if ("SHA256".equalsIgnoreCase(hashAlgo)) {
        sigAlgoOid = PKCSObjectIdentifiers.sha256WithRSAEncryption;
    } else if ("SHA384".equalsIgnoreCase(hashAlgo)) {
        sigAlgoOid = PKCSObjectIdentifiers.sha384WithRSAEncryption;
    } else if ("SHA512".equalsIgnoreCase(hashAlgo)) {
        sigAlgoOid = PKCSObjectIdentifiers.sha512WithRSAEncryption;
    } else {
        throw new RuntimeException("unsupported hash algorithm " + hashAlgo);
    }

    return new AlgorithmIdentifier(sigAlgoOid, DERNull.INSTANCE);
}

From source file:org.xipki.common.util.AlgorithmUtil.java

License:Open Source License

static public AlgorithmIdentifier extractDigesetAlgorithmIdentifier(final AlgorithmIdentifier sigAlgId)
        throws NoSuchAlgorithmException {
    ASN1ObjectIdentifier algOid = sigAlgId.getAlgorithm();

    ASN1ObjectIdentifier digestAlgOid;/*from  w w  w  .  j av  a2s.c o  m*/
    if (X9ObjectIdentifiers.ecdsa_with_SHA1.equals(algOid)) {
        digestAlgOid = X509ObjectIdentifiers.id_SHA1;
    } else if (X9ObjectIdentifiers.ecdsa_with_SHA224.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha224;
    } else if (X9ObjectIdentifiers.ecdsa_with_SHA256.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha256;
    } else if (X9ObjectIdentifiers.ecdsa_with_SHA384.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha384;
    } else if (X9ObjectIdentifiers.ecdsa_with_SHA512.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha512;
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA1.equals(algOid)) {
        digestAlgOid = X509ObjectIdentifiers.id_SHA1;
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA224.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha224;
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA256.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha256;
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA384.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha384;
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA512.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha512;
    } else if (X9ObjectIdentifiers.id_dsa_with_sha1.equals(algOid)) {
        digestAlgOid = X509ObjectIdentifiers.id_SHA1;
    } else if (NISTObjectIdentifiers.dsa_with_sha224.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha224;
    } else if (NISTObjectIdentifiers.dsa_with_sha256.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha256;
    } else if (NISTObjectIdentifiers.dsa_with_sha384.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha384;
    } else if (NISTObjectIdentifiers.dsa_with_sha512.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha512;
    } else if (PKCSObjectIdentifiers.sha1WithRSAEncryption.equals(algOid)) {
        digestAlgOid = X509ObjectIdentifiers.id_SHA1;
    } else if (PKCSObjectIdentifiers.sha224WithRSAEncryption.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha224;
    } else if (PKCSObjectIdentifiers.sha256WithRSAEncryption.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha256;
    } else if (PKCSObjectIdentifiers.sha384WithRSAEncryption.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha384;
    } else if (PKCSObjectIdentifiers.sha512WithRSAEncryption.equals(algOid)) {
        digestAlgOid = NISTObjectIdentifiers.id_sha512;
    } else if (PKCSObjectIdentifiers.id_RSASSA_PSS.equals(algOid)) {
        ASN1Encodable asn1Encodable = sigAlgId.getParameters();
        RSASSAPSSparams param = RSASSAPSSparams.getInstance(asn1Encodable);
        digestAlgOid = param.getHashAlgorithm().getAlgorithm();
    } else {
        throw new NoSuchAlgorithmException("unknown signature algorithm" + algOid.getId());
    }

    return new AlgorithmIdentifier(digestAlgOid, DERNull.INSTANCE);
}

From source file:org.xipki.commons.security.pkcs11.P11RSAContentSigner.java

License:Open Source License

P11RSAContentSigner(final P11CryptService cryptService, final P11EntityIdentifier identityId,
        final AlgorithmIdentifier signatureAlgId) throws XiSecurityException, P11TokenException {
    this.cryptService = ParamUtil.requireNonNull("cryptService", cryptService);
    this.identityId = ParamUtil.requireNonNull("identityId", identityId);
    this.algorithmIdentifier = ParamUtil.requireNonNull("signatureAlgId", signatureAlgId);

    ASN1ObjectIdentifier algOid = signatureAlgId.getAlgorithm();
    HashAlgoType hashAlgo;/*  ww  w.ja  va 2 s.com*/
    if (PKCSObjectIdentifiers.sha1WithRSAEncryption.equals(algOid)) {
        hashAlgo = HashAlgoType.SHA1;
    } else if (PKCSObjectIdentifiers.sha224WithRSAEncryption.equals(algOid)) {
        hashAlgo = HashAlgoType.SHA224;
    } else if (PKCSObjectIdentifiers.sha256WithRSAEncryption.equals(algOid)) {
        hashAlgo = HashAlgoType.SHA256;
    } else if (PKCSObjectIdentifiers.sha384WithRSAEncryption.equals(algOid)) {
        hashAlgo = HashAlgoType.SHA384;
    } else if (PKCSObjectIdentifiers.sha512WithRSAEncryption.equals(algOid)) {
        hashAlgo = HashAlgoType.SHA512;
    } else if (NISTObjectIdentifiers.id_rsassa_pkcs1_v1_5_with_sha3_224.equals(algOid)) {
        hashAlgo = HashAlgoType.SHA3_224;
    } else if (NISTObjectIdentifiers.id_rsassa_pkcs1_v1_5_with_sha3_256.equals(algOid)) {
        hashAlgo = HashAlgoType.SHA3_256;
    } else if (NISTObjectIdentifiers.id_rsassa_pkcs1_v1_5_with_sha3_384.equals(algOid)) {
        hashAlgo = HashAlgoType.SHA3_384;
    } else if (NISTObjectIdentifiers.id_rsassa_pkcs1_v1_5_with_sha3_512.equals(algOid)) {
        hashAlgo = HashAlgoType.SHA3_512;
    } else {
        throw new XiSecurityException("unsupported signature algorithm " + algOid.getId());
    }

    P11SlotIdentifier slotId = identityId.getSlotId();
    P11Slot slot = cryptService.getSlot(slotId);
    if (slot.supportsMechanism(P11Constants.CKM_RSA_PKCS)) {
        this.mechanism = P11Constants.CKM_RSA_PKCS;
    } else if (slot.supportsMechanism(P11Constants.CKM_RSA_X_509)) {
        this.mechanism = P11Constants.CKM_RSA_X_509;
    } else {
        switch (hashAlgo) {
        case SHA1:
            this.mechanism = P11Constants.CKM_SHA1_RSA_PKCS;
            break;
        case SHA224:
            this.mechanism = P11Constants.CKM_SHA224_RSA_PKCS;
            break;
        case SHA256:
            this.mechanism = P11Constants.CKM_SHA256_RSA_PKCS;
            break;
        case SHA384:
            this.mechanism = P11Constants.CKM_SHA384_RSA_PKCS;
            break;
        case SHA512:
            this.mechanism = P11Constants.CKM_SHA512_RSA_PKCS;
            break;
        case SHA3_224:
            this.mechanism = P11Constants.CKM_SHA3_224_RSA_PKCS;
            break;
        case SHA3_256:
            this.mechanism = P11Constants.CKM_SHA3_256_RSA_PKCS;
            break;
        case SHA3_384:
            this.mechanism = P11Constants.CKM_SHA3_384_RSA_PKCS;
            break;
        case SHA3_512:
            this.mechanism = P11Constants.CKM_SHA3_512_RSA_PKCS;
            break;
        default:
            throw new RuntimeException("should not reach here, unknown HashAlgoType " + hashAlgo);
        }

        if (!slot.supportsMechanism(this.mechanism)) {
            throw new XiSecurityException("unsupported signature algorithm " + algOid.getId());
        }
    }

    if (mechanism == P11Constants.CKM_RSA_PKCS || mechanism == P11Constants.CKM_RSA_X_509) {
        this.digestPkcsPrefix = SignerUtil.getDigestPkcsPrefix(hashAlgo);
        Digest digest = SignerUtil.getDigest(hashAlgo);
        this.outputStream = new DigestOutputStream(digest);
    } else {
        this.digestPkcsPrefix = null;
        this.outputStream = new ByteArrayOutputStream();
    }

    RSAPublicKey rsaPubKey = (RSAPublicKey) cryptService.getIdentity(identityId).getPublicKey();
    this.modulusBitLen = rsaPubKey.getModulus().bitLength();
}

From source file:org.xipki.commons.security.pkcs12.test.Pkcs12SHA256withRSATest.java

License:Open Source License

@Override
protected AlgorithmIdentifier getSignatureAlgorithm() {
    return new AlgorithmIdentifier(PKCSObjectIdentifiers.sha256WithRSAEncryption, DERNull.INSTANCE);
}

From source file:org.xipki.commons.security.util.AlgorithmUtil.java

License:Open Source License

public static String getSignatureAlgoName(final AlgorithmIdentifier sigAlgId) throws NoSuchAlgorithmException {
    ParamUtil.requireNonNull("sigAlgId", sigAlgId);
    ASN1ObjectIdentifier algOid = sigAlgId.getAlgorithm();

    if (X9ObjectIdentifiers.ecdsa_with_SHA1.equals(algOid)) {
        return "SHA1withECDSA";
    } else if (X9ObjectIdentifiers.ecdsa_with_SHA224.equals(algOid)) {
        return "SHA224withECDSA";
    } else if (X9ObjectIdentifiers.ecdsa_with_SHA256.equals(algOid)) {
        return "SHA256withECDSA";
    } else if (X9ObjectIdentifiers.ecdsa_with_SHA384.equals(algOid)) {
        return "SHA384withECDSA";
    } else if (X9ObjectIdentifiers.ecdsa_with_SHA512.equals(algOid)) {
        return "SHA512withECDSA";
    } else if (NISTObjectIdentifiers.id_ecdsa_with_sha3_224.equals(algOid)) {
        return "SHA3-224withECDSA";
    } else if (NISTObjectIdentifiers.id_ecdsa_with_sha3_256.equals(algOid)) {
        return "SHA3-256withECDSA";
    } else if (NISTObjectIdentifiers.id_ecdsa_with_sha3_384.equals(algOid)) {
        return "SHA3-384withECDSA";
    } else if (NISTObjectIdentifiers.id_ecdsa_with_sha3_512.equals(algOid)) {
        return "SHA3-512withECDSA";
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA1.equals(algOid)) {
        return "SHA1withPLAIN-ECDSA";
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA224.equals(algOid)) {
        return "SHA224withPLAIN-ECDSA";
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA256.equals(algOid)) {
        return "SHA256withPLAIN-ECDSA";
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA384.equals(algOid)) {
        return "SHA384withPLAIN-ECDSA";
    } else if (BSIObjectIdentifiers.ecdsa_plain_SHA512.equals(algOid)) {
        return "SHA512withPLAIN-ECDSA";
    } else if (X9ObjectIdentifiers.id_dsa_with_sha1.equals(algOid)) {
        return "SHA1withDSA";
    } else if (X9ObjectIdentifiers.id_dsa_with_sha1.equals(algOid)) {
        return "SHA1withDSA";
    } else if (NISTObjectIdentifiers.dsa_with_sha224.equals(algOid)) {
        return "SHA224withDSA";
    } else if (NISTObjectIdentifiers.dsa_with_sha256.equals(algOid)) {
        return "SHA256withDSA";
    } else if (NISTObjectIdentifiers.dsa_with_sha384.equals(algOid)) {
        return "SHA384withDSA";
    } else if (NISTObjectIdentifiers.dsa_with_sha512.equals(algOid)) {
        return "SHA512withDSA";
    } else if (NISTObjectIdentifiers.id_dsa_with_sha3_224.equals(algOid)) {
        return "SHA3-224withDSA";
    } else if (NISTObjectIdentifiers.id_dsa_with_sha3_256.equals(algOid)) {
        return "SHA3-256withDSA";
    } else if (NISTObjectIdentifiers.id_dsa_with_sha3_384.equals(algOid)) {
        return "SHA3-384withDSA";
    } else if (NISTObjectIdentifiers.id_dsa_with_sha3_512.equals(algOid)) {
        return "SHA3-512withDSA";
    } else if (PKCSObjectIdentifiers.sha1WithRSAEncryption.equals(algOid)) {
        return "SHA1withRSA";
    } else if (PKCSObjectIdentifiers.sha224WithRSAEncryption.equals(algOid)) {
        return "SHA224withRSA";
    } else if (PKCSObjectIdentifiers.sha256WithRSAEncryption.equals(algOid)) {
        return "SHA256withRSA";
    } else if (PKCSObjectIdentifiers.sha384WithRSAEncryption.equals(algOid)) {
        return "SHA384withRSA";
    } else if (PKCSObjectIdentifiers.sha512WithRSAEncryption.equals(algOid)) {
        return "SHA512withRSA";
    } else if (NISTObjectIdentifiers.id_rsassa_pkcs1_v1_5_with_sha3_224.equals(algOid)) {
        return "SHA3-224withRSA";
    } else if (NISTObjectIdentifiers.id_rsassa_pkcs1_v1_5_with_sha3_256.equals(algOid)) {
        return "SHA3-256withRSA";
    } else if (NISTObjectIdentifiers.id_rsassa_pkcs1_v1_5_with_sha3_384.equals(algOid)) {
        return "SHA3-384withRSA";
    } else if (NISTObjectIdentifiers.id_rsassa_pkcs1_v1_5_with_sha3_512.equals(algOid)) {
        return "SHA3-512withRSA";
    } else if (PKCSObjectIdentifiers.id_RSASSA_PSS.equals(algOid)) {
        RSASSAPSSparams param = RSASSAPSSparams.getInstance(sigAlgId.getParameters());
        ASN1ObjectIdentifier digestAlgOid = param.getHashAlgorithm().getAlgorithm();
        if (X509ObjectIdentifiers.id_SHA1.equals(digestAlgOid)) {
            return "SHA1withRSAandMGF1";
        } else if (NISTObjectIdentifiers.id_sha256.equals(digestAlgOid)) {
            return "SHA256withRSAandMGF1";
        } else if (NISTObjectIdentifiers.id_sha384.equals(digestAlgOid)) {
            return "SHA384withRSAandMGF1";
        } else if (NISTObjectIdentifiers.id_sha512.equals(digestAlgOid)) {
            return "SHA512withRSAandMGF1";
        } else if (NISTObjectIdentifiers.id_sha3_224.equals(digestAlgOid)) {
            return "SHA3-224withRSAandMGF1";
        } else if (NISTObjectIdentifiers.id_sha3_256.equals(digestAlgOid)) {
            return "SHA3-256withRSAandMGF1";
        } else if (NISTObjectIdentifiers.id_sha3_384.equals(digestAlgOid)) {
            return "SHA3-384withRSAandMGF1";
        } else if (NISTObjectIdentifiers.id_sha3_512.equals(digestAlgOid)) {
            return "SHA3-512withRSAandMGF1";
        } else {//from w w w  .j a va2  s  .co  m
            throw new NoSuchAlgorithmException("unsupported digest algorithm " + digestAlgOid.getId());
        }
    } else {
        throw new NoSuchAlgorithmException("unsupported signature algorithm " + algOid.getId());
    }
}