use of com.github.zhenwei.core.asn1.DLSequence in project LinLong-Java by zhenwei1108.
the class SafeBag method toASN1Primitive.
public ASN1Primitive toASN1Primitive() {
ASN1EncodableVector v = new ASN1EncodableVector(3);
v.add(bagId);
v.add(new DLTaggedObject(true, 0, bagValue));
if (bagAttributes != null) {
v.add(bagAttributes);
}
return new DLSequence(v);
}
use of com.github.zhenwei.core.asn1.DLSequence in project LinLong-Java by zhenwei1108.
the class TimeStampResponseGenerator method generateGrantedResponse.
/**
* Return a granted response, if the passed in request passes validation with the passed in status
* string and extra extensions.
* <p>
* If genTime is null a timeNotAvailable or a validation exception occurs a TSPValidationException
* will be thrown. The parent TSPException will only occur on some sort of system failure.
* </p>
*
* @param request the request this response is for.
* @param serialNumber serial number for the response token.
* @param genTime generation time for the response token.
* @param additionalExtensions extra extensions to be added to the response token.
* @return the TimeStampResponse with a status of PKIStatus.GRANTED
* @throws TSPException on validation exception or internal error.
*/
public TimeStampResponse generateGrantedResponse(TimeStampRequest request, BigInteger serialNumber, Date genTime, String statusString, Extensions additionalExtensions) throws TSPException {
if (genTime == null) {
throw new TSPValidationException("The time source is not available.", PKIFailureInfo.timeNotAvailable);
}
request.validate(acceptedAlgorithms, acceptedPolicies, acceptedExtensions);
status = PKIStatus.GRANTED;
statusStrings = new ASN1EncodableVector();
if (statusString != null) {
this.addStatusString(statusString);
}
PKIStatusInfo pkiStatusInfo = getPKIStatusInfo();
ContentInfo tstTokenContentInfo;
try {
tstTokenContentInfo = tokenGenerator.generate(request, serialNumber, genTime, additionalExtensions).toCMSSignedData().toASN1Structure();
} catch (TSPException e) {
throw e;
} catch (Exception e) {
throw new TSPException("Timestamp token received cannot be converted to ContentInfo", e);
}
try {
return new TimeStampResponse(new DLSequence(new ASN1Encodable[] { pkiStatusInfo.toASN1Primitive(), tstTokenContentInfo.toASN1Primitive() }));
} catch (IOException e) {
throw new TSPException("created badly formatted response!");
}
}
use of com.github.zhenwei.core.asn1.DLSequence in project LinLong-Java by zhenwei1108.
the class PKCS12PfxPduBuilder method build.
/**
* Build the Pfx structure, protecting it with a MAC calculated against the passed in password.
*
* @param macCalcBuilder a builder for a PKCS12 mac calculator.
* @param password the password to use.
* @return a Pfx object.
* @throws PKCSException on a encoding or processing error.
*/
public PKCS12PfxPdu build(PKCS12MacCalculatorBuilder macCalcBuilder, char[] password) throws PKCSException {
AuthenticatedSafe auth = AuthenticatedSafe.getInstance(new DLSequence(dataVector));
byte[] encAuth;
try {
encAuth = auth.getEncoded();
} catch (IOException e) {
throw new PKCSException("unable to encode AuthenticatedSafe: " + e.getMessage(), e);
}
ContentInfo mainInfo = new ContentInfo(PKCSObjectIdentifiers.data, new DEROctetString(encAuth));
MacData mData = null;
if (macCalcBuilder != null) {
MacDataGenerator mdGen = new MacDataGenerator(macCalcBuilder);
mData = mdGen.build(password, encAuth);
}
//
// output the Pfx
//
Pfx pfx = new Pfx(mainInfo, mData);
return new PKCS12PfxPdu(pfx);
}
use of com.github.zhenwei.core.asn1.DLSequence in project LinLong-Java by zhenwei1108.
the class KeyBuilder method getRealPrivateKey.
/**
* @param [privateKey]
* @return byte[]
* @author zhangzhenwei
* @description 获取裸私钥
* @date 2022/2/11 23:06
* @since 1.0
*/
public byte[] getRealPrivateKey(byte[] privateKey) throws WeGooCryptoException {
try {
PrivateKeyInfo info = PrivateKeyInfo.getInstance(privateKey);
if (info == null) {
throw new WeGooKeyException(IExceptionEnum.params_err);
}
KeyPairAlgEnum algEnum = KeyPairAlgEnum.match(info.getPrivateKeyAlgorithm().getAlgorithm());
// SM2 算法
if (algEnum.getAlg().equals(KeyPairAlgEnum.SM2_256.getAlg())) {
DLSequence dlSequence = (DLSequence) DLSequence.fromByteArray(privateKey);
byte[] priKeys = ((DEROctetString) dlSequence.getObjectAt(2)).getOctets();
dlSequence = (DLSequence) DLSequence.fromByteArray(priKeys);
DEROctetString derPriKey = (DEROctetString) dlSequence.getObjectAt(1);
return derPriKey.getOctets();
} else {
return info.getPrivateKey().getOctets();
}
} catch (WeGooKeyException e) {
throw e;
} catch (Exception e) {
throw new WeGooKeyException(KeyExceptionMessageEnum.parse_private_key_err, e);
}
}
use of com.github.zhenwei.core.asn1.DLSequence in project xrpl4j by XRPLF.
the class EcDsaSignature method fromDer.
/**
* Create an {@link EcDsaSignature} from a DER encoded byte array signature.
*
* @param bytes A DER encoded byte array containing a signature.
*
* @return An {@link EcDsaSignature}.
*/
static EcDsaSignature fromDer(byte[] bytes) {
try {
ASN1InputStream decoder = new ASN1InputStream(bytes);
DLSequence seq = (DLSequence) decoder.readObject();
ASN1Integer r;
ASN1Integer s;
try {
r = (ASN1Integer) seq.getObjectAt(0);
s = (ASN1Integer) seq.getObjectAt(1);
} catch (ClassCastException e) {
return null;
} finally {
decoder.close();
}
// Thus, we always use the positive versions. See: http://r6.ca/blog/20111119T211504Z.html
return EcDsaSignature.builder().r(r.getPositiveValue()).s(s.getPositiveValue()).build();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations