001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.asn1.ASN1Null;
004: import org.bouncycastle.asn1.ASN1Sequence;
005: import org.bouncycastle.asn1.DEREncodable;
006: import org.bouncycastle.asn1.DERNull;
007: import org.bouncycastle.asn1.DERObjectIdentifier;
008: import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
009: import org.bouncycastle.asn1.nist.NISTObjectIdentifiers;
010: import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
011: import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
012: import org.bouncycastle.asn1.pkcs.RSASSAPSSparams;
013: import org.bouncycastle.asn1.teletrust.TeleTrusTObjectIdentifiers;
014: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
015: import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
016:
017: import java.io.IOException;
018: import java.security.AlgorithmParameters;
019: import java.security.GeneralSecurityException;
020: import java.security.InvalidKeyException;
021: import java.security.NoSuchAlgorithmException;
022: import java.security.Signature;
023: import java.security.SignatureException;
024: import java.security.spec.PSSParameterSpec;
025:
026: class X509SignatureUtil {
027: private static final ASN1Null derNull = new DERNull();
028:
029: static void setSignatureParameters(Signature signature,
030: DEREncodable params) throws NoSuchAlgorithmException,
031: SignatureException, InvalidKeyException {
032: if (params != null && !derNull.equals(params)) {
033: AlgorithmParameters sigParams = AlgorithmParameters
034: .getInstance(signature.getAlgorithm(), signature
035: .getProvider());
036:
037: try {
038: sigParams.init(params.getDERObject().getDEREncoded());
039: } catch (IOException e) {
040: throw new SignatureException(
041: "IOException decoding parameters: "
042: + e.getMessage());
043: }
044:
045: if (signature.getAlgorithm().endsWith("MGF1")) {
046: try {
047: signature.setParameter(sigParams
048: .getParameterSpec(PSSParameterSpec.class));
049: } catch (GeneralSecurityException e) {
050: throw new SignatureException(
051: "Exception extracting parameters: "
052: + e.getMessage());
053: }
054: }
055: }
056: }
057:
058: static String getSignatureName(AlgorithmIdentifier sigAlgId) {
059: DEREncodable params = sigAlgId.getParameters();
060:
061: if (params != null && !derNull.equals(params)) {
062: if (sigAlgId.getObjectId().equals(
063: PKCSObjectIdentifiers.id_RSASSA_PSS)) {
064: RSASSAPSSparams rsaParams = RSASSAPSSparams
065: .getInstance(params);
066:
067: return getDigestAlgName(rsaParams.getHashAlgorithm()
068: .getObjectId())
069: + "withRSAandMGF1";
070: }
071: if (sigAlgId.getObjectId().equals(
072: X9ObjectIdentifiers.ecdsa_with_SHA2)) {
073: ASN1Sequence ecDsaParams = ASN1Sequence
074: .getInstance(params);
075:
076: return getDigestAlgName((DERObjectIdentifier) ecDsaParams
077: .getObjectAt(0))
078: + "withECDSA";
079: }
080: }
081:
082: return sigAlgId.getObjectId().getId();
083: }
084:
085: /**
086: * Return the digest algorithm using one of the standard JCA string
087: * representations rather the the algorithm identifier (if possible).
088: */
089: private static String getDigestAlgName(
090: DERObjectIdentifier digestAlgOID) {
091: if (PKCSObjectIdentifiers.md5.equals(digestAlgOID)) {
092: return "MD5";
093: } else if (OIWObjectIdentifiers.idSHA1.equals(digestAlgOID)) {
094: return "SHA1";
095: } else if (NISTObjectIdentifiers.id_sha224.equals(digestAlgOID)) {
096: return "SHA224";
097: } else if (NISTObjectIdentifiers.id_sha256.equals(digestAlgOID)) {
098: return "SHA256";
099: } else if (NISTObjectIdentifiers.id_sha384.equals(digestAlgOID)) {
100: return "SHA384";
101: } else if (NISTObjectIdentifiers.id_sha512.equals(digestAlgOID)) {
102: return "SHA512";
103: } else if (TeleTrusTObjectIdentifiers.ripemd128
104: .equals(digestAlgOID)) {
105: return "RIPEMD128";
106: } else if (TeleTrusTObjectIdentifiers.ripemd160
107: .equals(digestAlgOID)) {
108: return "RIPEMD160";
109: } else if (TeleTrusTObjectIdentifiers.ripemd256
110: .equals(digestAlgOID)) {
111: return "RIPEMD256";
112: } else if (CryptoProObjectIdentifiers.gostR3411
113: .equals(digestAlgOID)) {
114: return "GOST3411";
115: } else {
116: return digestAlgOID.getId();
117: }
118: }
119: }
|