use of com.github.zhenwei.pkix.operator.RuntimeOperatorException in project LinLong-Java by zhenwei1108.
the class JcaEACSignatureVerifierBuilder method build.
public EACSignatureVerifier build(final ASN1ObjectIdentifier usageOid, PublicKey pubKey) throws OperatorCreationException {
Signature sig;
try {
sig = helper.getSignature(usageOid);
sig.initVerify(pubKey);
} catch (NoSuchAlgorithmException e) {
throw new OperatorCreationException("unable to find algorithm: " + e.getMessage(), e);
} catch (NoSuchProviderException e) {
throw new OperatorCreationException("unable to find provider: " + e.getMessage(), e);
} catch (InvalidKeyException e) {
throw new OperatorCreationException("invalid key: " + e.getMessage(), e);
}
final SignatureOutputStream sigStream = new SignatureOutputStream(sig);
return new EACSignatureVerifier() {
public ASN1ObjectIdentifier getUsageIdentifier() {
return usageOid;
}
public OutputStream getOutputStream() {
return sigStream;
}
public boolean verify(byte[] expected) {
try {
if (usageOid.on(EACObjectIdentifiers.id_TA_ECDSA)) {
try {
byte[] reencoded = derEncode(expected);
return sigStream.verify(reencoded);
} catch (Exception e) {
return false;
}
} else {
return sigStream.verify(expected);
}
} catch (SignatureException e) {
throw new RuntimeOperatorException("exception obtaining signature: " + e.getMessage(), e);
}
}
};
}
use of com.github.zhenwei.pkix.operator.RuntimeOperatorException in project LinLong-Java by zhenwei1108.
the class PKMACBuilder method genCalculator.
private MacCalculator genCalculator(final PBMParameter params, char[] password) throws CRMFException {
// From RFC 4211
//
// 1. Generate a random salt value S
//
// 2. Append the salt to the pw. K = pw || salt.
//
// 3. Hash the value of K. K = HASH(K)
//
// 4. Iter = Iter - 1. If Iter is greater than zero. Goto step 3.
//
// 5. Compute an HMAC as documented in [HMAC].
//
// MAC = HASH( K XOR opad, HASH( K XOR ipad, data) )
//
// Where opad and ipad are defined in [HMAC].
byte[] pw = Strings.toUTF8ByteArray(password);
byte[] salt = params.getSalt().getOctets();
byte[] K = new byte[pw.length + salt.length];
System.arraycopy(pw, 0, K, 0, pw.length);
System.arraycopy(salt, 0, K, pw.length, salt.length);
calculator.setup(params.getOwf(), params.getMac());
int iter = params.getIterationCount().intValueExact();
do {
K = calculator.calculateDigest(K);
} while (--iter > 0);
final byte[] key = K;
return new MacCalculator() {
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
public AlgorithmIdentifier getAlgorithmIdentifier() {
return new AlgorithmIdentifier(CMPObjectIdentifiers.passwordBasedMac, params);
}
public GenericKey getKey() {
return new GenericKey(getAlgorithmIdentifier(), key);
}
public OutputStream getOutputStream() {
return bOut;
}
public byte[] getMac() {
try {
return calculator.calculateMac(key, bOut.toByteArray());
} catch (CRMFException e) {
throw new RuntimeOperatorException("exception calculating mac: " + e.getMessage(), e);
}
}
};
}
use of com.github.zhenwei.pkix.operator.RuntimeOperatorException in project LinLong-Java by zhenwei1108.
the class JcaContentSignerBuilder method build.
public ContentSigner build(PrivateKey privateKey) throws OperatorCreationException {
if (privateKey instanceof CompositePrivateKey) {
return buildComposite((CompositePrivateKey) privateKey);
}
try {
final Signature sig = helper.createSignature(sigAlgId);
final AlgorithmIdentifier signatureAlgId = sigAlgId;
if (random != null) {
sig.initSign(privateKey, random);
} else {
sig.initSign(privateKey);
}
return new ContentSigner() {
private OutputStream stream = OutputStreamFactory.createStream(sig);
public AlgorithmIdentifier getAlgorithmIdentifier() {
return signatureAlgId;
}
public OutputStream getOutputStream() {
return stream;
}
public byte[] getSignature() {
try {
return sig.sign();
} catch (SignatureException e) {
throw new RuntimeOperatorException("exception obtaining signature: " + e.getMessage(), e);
}
}
};
} catch (GeneralSecurityException e) {
throw new OperatorCreationException("cannot create signer: " + e.getMessage(), e);
}
}
use of com.github.zhenwei.pkix.operator.RuntimeOperatorException in project LinLong-Java by zhenwei1108.
the class JcaContentSignerBuilder method buildComposite.
private ContentSigner buildComposite(CompositePrivateKey privateKey) throws OperatorCreationException {
try {
List<PrivateKey> privateKeys = privateKey.getPrivateKeys();
final ASN1Sequence sigAlgIds = ASN1Sequence.getInstance(sigAlgId.getParameters());
final Signature[] sigs = new Signature[sigAlgIds.size()];
for (int i = 0; i != sigAlgIds.size(); i++) {
sigs[i] = helper.createSignature(AlgorithmIdentifier.getInstance(sigAlgIds.getObjectAt(i)));
if (random != null) {
sigs[i].initSign(privateKeys.get(i), random);
} else {
sigs[i].initSign(privateKeys.get(i));
}
}
OutputStream sStream = OutputStreamFactory.createStream(sigs[0]);
for (int i = 1; i != sigs.length; i++) {
sStream = new TeeOutputStream(sStream, OutputStreamFactory.createStream(sigs[i]));
}
final OutputStream sigStream = sStream;
return new ContentSigner() {
OutputStream stream = sigStream;
public AlgorithmIdentifier getAlgorithmIdentifier() {
return sigAlgId;
}
public OutputStream getOutputStream() {
return stream;
}
public byte[] getSignature() {
try {
ASN1EncodableVector sigV = new ASN1EncodableVector();
for (int i = 0; i != sigs.length; i++) {
sigV.add(new DERBitString(sigs[i].sign()));
}
return new DERSequence(sigV).getEncoded(ASN1Encoding.DER);
} catch (IOException e) {
throw new RuntimeOperatorException("exception encoding signature: " + e.getMessage(), e);
} catch (SignatureException e) {
throw new RuntimeOperatorException("exception obtaining signature: " + e.getMessage(), e);
}
}
};
} catch (GeneralSecurityException e) {
throw new OperatorCreationException("cannot create signer: " + e.getMessage(), e);
}
}
use of com.github.zhenwei.pkix.operator.RuntimeOperatorException in project LinLong-Java by zhenwei1108.
the class JcaEACSignerBuilder method build.
public EACSigner build(final ASN1ObjectIdentifier usageOid, PrivateKey privKey) throws OperatorCreationException {
Signature sig;
try {
sig = helper.getSignature(usageOid);
sig.initSign(privKey);
} catch (NoSuchAlgorithmException e) {
throw new OperatorCreationException("unable to find algorithm: " + e.getMessage(), e);
} catch (NoSuchProviderException e) {
throw new OperatorCreationException("unable to find provider: " + e.getMessage(), e);
} catch (InvalidKeyException e) {
throw new OperatorCreationException("invalid key: " + e.getMessage(), e);
}
final SignatureOutputStream sigStream = new SignatureOutputStream(sig);
return new EACSigner() {
public ASN1ObjectIdentifier getUsageIdentifier() {
return usageOid;
}
public OutputStream getOutputStream() {
return sigStream;
}
public byte[] getSignature() {
try {
byte[] signature = sigStream.getSignature();
if (usageOid.on(EACObjectIdentifiers.id_TA_ECDSA)) {
return reencode(signature);
}
return signature;
} catch (SignatureException e) {
throw new RuntimeOperatorException("exception obtaining signature: " + e.getMessage(), e);
}
}
};
}
Aggregations