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:com.android.verity.BootSignature.java

License:Apache License

public BootSignature(String target, int length) {
    this.formatVersion = new ASN1Integer(0);
    this.target = new DERPrintableString(target);
    this.length = new ASN1Integer(length);
    this.algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.sha256WithRSAEncryption);
}

From source file:com.android.verity.BootKey.java

License:Apache License

public BootKey(PublicKey key) throws Exception {
    java.security.interfaces.RSAPublicKey k = (java.security.interfaces.RSAPublicKey) key;
    this.keyMaterial = new RSAPublicKey(k.getModulus(), k.getPublicExponent());
    this.algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.sha256WithRSAEncryption);
}

From source file:it.scoppelletti.spaceship.security.FakeCertificateFactory.java

License:Apache License

@SuppressWarnings({ "deprecation", "TryFinallyCanBeTryWithResources" })
public static X509Certificate create(PublicKey publicKey, FakeKeyPairGeneratorSpec spec)
        throws IOException, CertificateParsingException {
    ASN1ObjectIdentifier sigAlgOid;/* w  w w  .j av a2  s.  co m*/
    AlgorithmIdentifier sigAlgId;
    org.bouncycastle.jce.X509Principal subject;
    ASN1EncodableVector result;
    Certificate cert;
    org.bouncycastle.jce.provider.X509CertificateObject x509Cert;
    TBSCertificate tbsCertificate;
    ASN1InputStream publicKeyInfoIn = null;
    V3TBSCertificateGenerator tbsGenerator;
    byte[] signature;

    sigAlgOid = PKCSObjectIdentifiers.sha256WithRSAEncryption;
    sigAlgId = new AlgorithmIdentifier(sigAlgOid, DERNull.INSTANCE);
    signature = new byte[1];

    tbsGenerator = new V3TBSCertificateGenerator();
    try {
        publicKeyInfoIn = new ASN1InputStream(publicKey.getEncoded());
        tbsGenerator.setSubjectPublicKeyInfo(SubjectPublicKeyInfo.getInstance(publicKeyInfoIn.readObject()));
    } finally {
        if (publicKeyInfoIn != null) {
            publicKeyInfoIn.close();
        }
    }

    subject = new org.bouncycastle.jce.X509Principal(spec.getSubject().getEncoded());

    tbsGenerator.setSerialNumber(new ASN1Integer(spec.getSerialNumber()));
    tbsGenerator.setSubject(subject);
    tbsGenerator.setIssuer(subject);
    tbsGenerator.setStartDate(new Time(spec.getStartDate()));
    tbsGenerator.setEndDate(new Time(spec.getEndDate()));
    tbsGenerator.setSignature(sigAlgId);

    tbsCertificate = tbsGenerator.generateTBSCertificate();

    result = new ASN1EncodableVector();
    result.add(tbsCertificate);
    result.add(sigAlgId);
    result.add(new DERBitString(signature));

    cert = Certificate.getInstance(new DERSequence(result));
    x509Cert = new org.bouncycastle.jce.provider.X509CertificateObject(cert);
    return x509Cert;
}

From source file:org.cesecore.certificates.util.AlgorithmTools.java

License:Open Source License

/** 
 * Get the digest algorithm corresponding to the signature algorithm. This is used for the creation of
 * PKCS7 file. SHA1 shall always be used, but it is not working with GOST which needs GOST3411 digest.
 * //  ww  w  . j a  v a  2s  .c  om
 */
public static String getDigestFromSigAlg(String sigAlg) {
    if (sigAlg.toUpperCase().contains("GOST") || sigAlg.toUpperCase().contains("DSTU")) {
        return CMSSignedGenerator.DIGEST_GOST3411;
    } else {
        if (sigAlg.equals(X9ObjectIdentifiers.ecdsa_with_SHA1.getId())
                || sigAlg.equals(PKCSObjectIdentifiers.sha1WithRSAEncryption.getId())) {
            return CMSSignedGenerator.DIGEST_SHA1;
        } else if (sigAlg.equals(X9ObjectIdentifiers.ecdsa_with_SHA224.getId())
                || sigAlg.equals(PKCSObjectIdentifiers.sha224WithRSAEncryption.getId())) {
            return CMSSignedGenerator.DIGEST_SHA224;
        } else if (sigAlg.equals(X9ObjectIdentifiers.ecdsa_with_SHA256.getId())
                || sigAlg.equals(PKCSObjectIdentifiers.sha256WithRSAEncryption.getId())) {
            return CMSSignedGenerator.DIGEST_SHA256;
        } else if (sigAlg.equals(X9ObjectIdentifiers.ecdsa_with_SHA384.getId())
                || sigAlg.equals(PKCSObjectIdentifiers.sha384WithRSAEncryption.getId())) {
            return CMSSignedGenerator.DIGEST_SHA384;
        } else if (sigAlg.equals(X9ObjectIdentifiers.ecdsa_with_SHA512.getId())
                || sigAlg.equals(PKCSObjectIdentifiers.sha512WithRSAEncryption.getId())) {
            return CMSSignedGenerator.DIGEST_SHA512;
        } else if (sigAlg.equals(PKCSObjectIdentifiers.md5WithRSAEncryption.getId())) {
            return CMSSignedGenerator.DIGEST_MD5;
        } else if (sigAlg.equals(CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_2001.getId())) {
            return CMSSignedGenerator.DIGEST_GOST3411;
        }
    }
    return CMSSignedGenerator.DIGEST_SHA1;

}

From source file:org.cesecore.certificates.util.AlgorithmTools.java

License:Open Source License

/** Calculates which signature algorithm to use given a key type and a digest algorithm
 * /*from   w  ww . j a  v a2  s. c  om*/
 * @param digestAlg objectId of a digest algorithm, CMSSignedGenerator.DIGEST_SHA256 etc
 * @param keyAlg RSA, EC, DSA
 * @return ASN1ObjectIdentifier with the id of PKCSObjectIdentifiers.sha1WithRSAEncryption, X9ObjectIdentifiers.ecdsa_with_SHA1, X9ObjectIdentifiers.id_dsa_with_sha1, etc
 */
public static ASN1ObjectIdentifier getSignAlgOidFromDigestAndKey(final String digestAlg, final String keyAlg) {
    if (log.isTraceEnabled()) {
        log.trace(">getSignAlg(" + digestAlg + "," + keyAlg + ")");
    }
    // Default to SHA1WithRSA if everything else fails    
    ASN1ObjectIdentifier oid = PKCSObjectIdentifiers.sha1WithRSAEncryption;
    if (keyAlg.equals(AlgorithmConstants.KEYALGORITHM_EC)
            || keyAlg.equals(AlgorithmConstants.KEYALGORITHM_ECDSA)) {
        oid = X9ObjectIdentifiers.ecdsa_with_SHA1;
    } else if (keyAlg.equals(AlgorithmConstants.KEYALGORITHM_DSA)) {
        oid = X9ObjectIdentifiers.id_dsa_with_sha1;
    } else if (keyAlg.equals(AlgorithmConstants.KEYALGORITHM_ECGOST3410)) {
        oid = CryptoProObjectIdentifiers.gostR3411_94_with_gostR3410_2001;
    } else if (keyAlg.equals(AlgorithmConstants.KEYALGORITHM_DSTU4145)) {
        oid = new ASN1ObjectIdentifier(CesecoreConfiguration.getOidDstu4145());
    }
    if (digestAlg != null) {
        if (digestAlg.equals(CMSSignedGenerator.DIGEST_SHA256)
                && keyAlg.equals(AlgorithmConstants.KEYALGORITHM_RSA)) {
            oid = PKCSObjectIdentifiers.sha256WithRSAEncryption;
        } else if (digestAlg.equals(CMSSignedGenerator.DIGEST_SHA512)
                && keyAlg.equals(AlgorithmConstants.KEYALGORITHM_RSA)) {
            oid = PKCSObjectIdentifiers.sha512WithRSAEncryption;
        } else if (digestAlg.equals(CMSSignedGenerator.DIGEST_MD5)
                && keyAlg.equals(AlgorithmConstants.KEYALGORITHM_RSA)) {
            oid = PKCSObjectIdentifiers.md5WithRSAEncryption;
        } else if (digestAlg.equals(CMSSignedGenerator.DIGEST_SHA256)
                && (keyAlg.equals(AlgorithmConstants.KEYALGORITHM_ECDSA)
                        || keyAlg.equals(AlgorithmConstants.KEYALGORITHM_EC))) {
            oid = X9ObjectIdentifiers.ecdsa_with_SHA256;
        } else if (digestAlg.equals(CMSSignedGenerator.DIGEST_SHA224)
                && (keyAlg.equals(AlgorithmConstants.KEYALGORITHM_ECDSA)
                        || keyAlg.equals(AlgorithmConstants.KEYALGORITHM_EC))) {
            oid = X9ObjectIdentifiers.ecdsa_with_SHA224;
        } else if (digestAlg.equals(CMSSignedGenerator.DIGEST_SHA384)
                && (keyAlg.equals(AlgorithmConstants.KEYALGORITHM_ECDSA)
                        || keyAlg.equals(AlgorithmConstants.KEYALGORITHM_EC))) {
            oid = X9ObjectIdentifiers.ecdsa_with_SHA384;
        } else if (digestAlg.equals(CMSSignedGenerator.DIGEST_SHA512)
                && (keyAlg.equals(AlgorithmConstants.KEYALGORITHM_ECDSA)
                        || keyAlg.equals(AlgorithmConstants.KEYALGORITHM_EC))) {
            oid = X9ObjectIdentifiers.ecdsa_with_SHA512;
        } else if (digestAlg.equals(CMSSignedGenerator.DIGEST_SHA256)
                && keyAlg.equals(AlgorithmConstants.KEYALGORITHM_DSA)) {
            oid = NISTObjectIdentifiers.dsa_with_sha256;
        } else if (digestAlg.equals(CMSSignedGenerator.DIGEST_SHA512)
                && keyAlg.equals(AlgorithmConstants.KEYALGORITHM_DSA)) {
            oid = NISTObjectIdentifiers.dsa_with_sha512;
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("getSignAlgOidFromDigestAndKey: " + oid.getId());
    }
    return oid;
}

From source file:org.cesecore.certificates.util.AlgorithmTools.java

License:Open Source License

/**
 * Returns the name of the algorithm corresponding to the specified OID
 * @param sigAlgOid// w  w  w.  j a v a2  s .c om
 * @return The name of the algorithm corresponding sigAlgOid or null if the algorithm is not recognized.
 */
public static String getAlgorithmNameFromOID(ASN1ObjectIdentifier sigAlgOid) {

    if (sigAlgOid.equals(PKCSObjectIdentifiers.md5WithRSAEncryption)) {
        return AlgorithmConstants.SIGALG_MD5_WITH_RSA;
    }

    if (sigAlgOid.equals(PKCSObjectIdentifiers.sha1WithRSAEncryption)) {
        return AlgorithmConstants.SIGALG_SHA1_WITH_RSA;
    }

    if (sigAlgOid.equals(PKCSObjectIdentifiers.sha256WithRSAEncryption)) {
        return AlgorithmConstants.SIGALG_SHA256_WITH_RSA;
    }

    if (sigAlgOid.equals(PKCSObjectIdentifiers.sha384WithRSAEncryption)) {
        return AlgorithmConstants.SIGALG_SHA384_WITH_RSA;
    }

    if (sigAlgOid.equals(PKCSObjectIdentifiers.sha512WithRSAEncryption)) {
        return AlgorithmConstants.SIGALG_SHA512_WITH_RSA;
    }

    if (sigAlgOid.equals(X9ObjectIdentifiers.ecdsa_with_SHA1)) {
        return AlgorithmConstants.SIGALG_SHA1_WITH_ECDSA;
    }

    if (sigAlgOid.equals(X9ObjectIdentifiers.ecdsa_with_SHA224)) {
        return AlgorithmConstants.SIGALG_SHA224_WITH_ECDSA;
    }

    if (sigAlgOid.equals(X9ObjectIdentifiers.ecdsa_with_SHA256)) {
        return AlgorithmConstants.SIGALG_SHA256_WITH_ECDSA;
    }

    if (sigAlgOid.equals(X9ObjectIdentifiers.ecdsa_with_SHA384)) {
        return AlgorithmConstants.SIGALG_SHA384_WITH_ECDSA;
    }

    if (sigAlgOid.equals(X9ObjectIdentifiers.ecdsa_with_SHA512)) {
        return AlgorithmConstants.SIGALG_SHA512_WITH_ECDSA;
    }
    // GOST3410
    if (isGost3410Enabled() && sigAlgOid.getId().equalsIgnoreCase(CesecoreConfiguration.getOidGost3410())) {
        return AlgorithmConstants.SIGALG_GOST3411_WITH_ECGOST3410;
    }
    // DSTU4145
    if (isDstu4145Enabled() && sigAlgOid.getId().startsWith(CesecoreConfiguration.getOidDstu4145() + ".")) {
        return AlgorithmConstants.SIGALG_GOST3411_WITH_DSTU4145;
    }

    return null;
}

From source file:org.ejbca.core.protocol.cmp.CrmfKeyUpdateTest.java

License:Open Source License

/**
 * Tests the possibility to use different signature algorithms in CMP requests and responses.
 * /* w ww. ja  va2 s. c om*/
 * A KeyUpdate request, signed using ECDSA with SHA256, is sent to a CA that uses RSA with SHA256 as signature algorithm.
 * The expected response is signed by RSA with SHA256.
 * 
 * @throws Exception
 */
@Test
public void test15KeyUpdateMixAlgorithms() throws Exception {
    if (log.isTraceEnabled()) {
        log.trace(">test15KeyUpdateMixAlgorithms");
    }

    this.cmpConfiguration.setKurAllowAutomaticUpdate(this.cmpAlias, true);
    this.cmpConfiguration.setKurAllowSameKey(this.cmpAlias, true);
    this.globalConfigurationSession.saveConfiguration(ADMIN, this.cmpConfiguration);

    //--------------- create the user and issue his first certificate -----------------
    createUser(this.username, this.userDN.toString(), "foo123");
    KeyPair keys = KeyTools.genKeys("prime192v1", AlgorithmConstants.KEYALGORITHM_ECDSA);
    final Certificate certificate;
    try {
        certificate = this.signSession.createCertificate(ADMIN, this.username, "foo123",
                new PublicKeyWrapper(keys.getPublic()));
    } catch (ObjectNotFoundException e) {
        throw new CertificateCreationException("Error encountered when creating certificate", e);
    } catch (CADoesntExistsException e) {
        throw new CertificateCreationException("Error encountered when creating certificate", e);
    } catch (EjbcaException e) {
        throw new CertificateCreationException("Error encountered when creating certificate", e);
    } catch (AuthorizationDeniedException e) {
        throw new CertificateCreationException("Error encountered when creating certificate", e);
    } catch (CesecoreException e) {
        throw new CertificateCreationException("Error encountered when creating certificate", e);
    }
    assertNotNull("Failed to create a test certificate", certificate);

    AlgorithmIdentifier pAlg = new AlgorithmIdentifier(X9ObjectIdentifiers.ecdsa_with_SHA256);
    PKIMessage req = genRenewalReq(this.userDN, this.cacert, this.nonce, this.transid, keys, false, null, null,
            pAlg, new DEROctetString(this.nonce));
    assertNotNull("Failed to generate a CMP renewal request", req);
    CertReqMessages kur = (CertReqMessages) req.getBody().getContent();
    int reqId = kur.toCertReqMsgArray()[0].getCertReq().getCertReqId().getValue().intValue();
    CMPCertificate[] extraCert = getCMPCert(certificate);
    req = CmpMessageHelper.buildCertBasedPKIProtection(req, extraCert, keys.getPrivate(),
            CMSSignedGenerator.DIGEST_SHA256, "BC");
    assertNotNull(req);

    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    DEROutputStream out = new DEROutputStream(bao);
    out.writeObject(req);
    byte[] ba = bao.toByteArray();
    // Send request and receive response
    byte[] resp = sendCmpHttp(ba, 200, this.cmpAlias);
    checkCmpResponseGeneral(resp, this.issuerDN, this.userDN, this.cacert, this.nonce, this.transid, true, null,
            PKCSObjectIdentifiers.sha256WithRSAEncryption.getId());
    X509Certificate cert = checkKurCertRepMessage(this.userDN, this.cacert, resp, reqId);
    assertNotNull("Failed to renew the certificate", cert);
    assertTrue("The new certificate's keys are incorrect.", cert.getPublicKey().equals(keys.getPublic()));

    if (log.isTraceEnabled()) {
        log.trace("<test15KeyUpdateMixAlgorithms");
    }

}

From source file:org.ejbca.core.protocol.cmp.CrmfRequestMessageTest.java

License:Open Source License

@Test
public void testNovosecClientRequestSHA256() throws IOException, InvalidKeyException, NoSuchAlgorithmException,
        NoSuchProviderException, InvalidAlgorithmParameterException, SignatureException, IllegalStateException,
        OperatorCreationException, CertificateException {
    doNovosecClientRequest("SHA256WithRSA", CMSSignedGenerator.DIGEST_SHA256,
            PKCSObjectIdentifiers.sha256WithRSAEncryption.getId());
}

From source file:org.ejbca.core.protocol.ocsp.ProtocolOcspHttpTest.java

License:Open Source License

/**
 * This test tests that the OCSP response contains is signed by the preferred signature algorithm specified in the request.
 * /*from  ww w  .ja  v a 2 s. co  m*/
 * @throws Exception
*/
@Test
@Deprecated // This test verifies legacy behavior from EJBCA 6.1.0 and should be removed when we no longer need to support it
public void testSigAlgExtensionLegacy() throws Exception {
    loadUserCert(this.caid);

    // Try sending a request where the preferred signature algorithm in the extension is expected to be used to sign the response.

    // set ocsp configuration
    Map<String, String> map = new HashMap<String, String>();
    map.put("ocsp.signaturealgorithm",
            AlgorithmConstants.SIGALG_SHA256_WITH_RSA + ";" + AlgorithmConstants.SIGALG_SHA1_WITH_RSA);
    this.helper.alterConfig(map);

    ASN1EncodableVector algVec = new ASN1EncodableVector();
    algVec.add(X9ObjectIdentifiers.ecdsa_with_SHA256);
    algVec.add(PKCSObjectIdentifiers.sha1WithRSAEncryption);
    ASN1Sequence algSeq = new DERSequence(algVec);
    ExtensionsGenerator extgen = new ExtensionsGenerator();
    // RFC 6960: id-pkix-ocsp-pref-sig-algs   OBJECT IDENTIFIER ::= { id-pkix-ocsp 8 } 
    extgen.addExtension(new ASN1ObjectIdentifier(OCSPObjectIdentifiers.id_pkix_ocsp + ".8"), false, algSeq);
    Extensions exts = extgen.generate();
    assertNotNull(exts);

    OCSPReqBuilder gen = new OCSPReqBuilder();
    gen.addRequest(new JcaCertificateID(SHA1DigestCalculator.buildSha1Instance(), cacert,
            ocspTestCert.getSerialNumber()), exts);
    gen.setRequestExtensions(exts);
    OCSPReq req = gen.build();
    assertTrue(req.hasExtensions());

    BasicOCSPResp response = helper.sendOCSPGet(req.getEncoded(), null, OCSPRespBuilder.SUCCESSFUL, 200);
    assertNotNull("Could not retrieve response, test could not continue.", response);
    assertEquals(PKCSObjectIdentifiers.sha1WithRSAEncryption, response.getSignatureAlgOID());

    // Try sending a request where the preferred signature algorithm is not compatible with the signing key, but 
    // the configured algorithm is. Expected a response signed using the first configured algorithm

    algVec = new ASN1EncodableVector();
    algVec.add(X9ObjectIdentifiers.ecdsa_with_SHA256);
    algSeq = new DERSequence(algVec);

    extgen = new ExtensionsGenerator();
    extgen.addExtension(new ASN1ObjectIdentifier(OCSPObjectIdentifiers.id_pkix_ocsp + ".8"), false, algSeq);
    exts = extgen.generate();
    assertNotNull(exts);

    gen = new OCSPReqBuilder();
    gen.addRequest(new JcaCertificateID(SHA1DigestCalculator.buildSha1Instance(), cacert,
            ocspTestCert.getSerialNumber()), exts);
    gen.setRequestExtensions(exts);
    req = gen.build();
    assertTrue(req.hasExtensions());

    response = helper.sendOCSPGet(req.getEncoded(), null, OCSPRespBuilder.SUCCESSFUL, 200);
    assertNotNull("Could not retrieve response, test could not continue.", response);
    assertEquals(PKCSObjectIdentifiers.sha256WithRSAEncryption, response.getSignatureAlgOID());
}

From source file:org.ejbca.core.protocol.ocsp.ProtocolOcspHttpTest.java

License:Open Source License

/** Test with a preferred signature algorithm specified in the request that is incompatible with the singing key. */
@Test/*from   w  w  w .  j  a  v  a 2s. c o m*/
public void testSigAlgExtensionMismatch() throws Exception {
    log.trace(">testSigAlgExtensionNewMismatch");
    loadUserCert(caid);
    final Map<String, String> map = new HashMap<String, String>();
    map.put("ocsp.signaturealgorithm",
            AlgorithmConstants.SIGALG_SHA256_WITH_RSA + ";" + AlgorithmConstants.SIGALG_SHA1_WITH_RSA);
    helper.alterConfig(map);
    // Try sending a request where the preferred signature algorithm is not compatible with the signing key, but 
    // the configured algorithm is. Expected a response signed using the first configured algorithm
    final ASN1Sequence preferredSignatureAlgorithms = getPreferredSignatureAlgorithms(
            X9ObjectIdentifiers.ecdsa_with_SHA256);
    final ExtensionsGenerator extensionsGenerator = new ExtensionsGenerator();
    extensionsGenerator.addExtension(new ASN1ObjectIdentifier(OCSPObjectIdentifiers.id_pkix_ocsp + ".8"), false,
            preferredSignatureAlgorithms);
    final Extensions extensions = extensionsGenerator.generate();
    assertNotNull(extensions);
    final OCSPReqBuilder ocspReqBuilder = new OCSPReqBuilder();
    ocspReqBuilder.addRequest(new JcaCertificateID(SHA1DigestCalculator.buildSha1Instance(), cacert,
            ocspTestCert.getSerialNumber()));
    ocspReqBuilder.setRequestExtensions(extensions);
    final OCSPReq ocspRequest = ocspReqBuilder.build();
    assertTrue(ocspRequest.hasExtensions());
    log.debug("base64 encoded request: " + new String(Base64.encode(ocspRequest.getEncoded(), false)));
    final BasicOCSPResp response2 = helper.sendOCSPGet(ocspRequest.getEncoded(), null,
            OCSPRespBuilder.SUCCESSFUL, 200);
    assertNotNull("Could not retrieve response, test could not continue.", response2);
    assertEquals(PKCSObjectIdentifiers.sha256WithRSAEncryption, response2.getSignatureAlgOID());
}