Search in sources :

Example 26 with DLSequence

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);
}
Also used : DLTaggedObject(com.github.zhenwei.core.asn1.DLTaggedObject) DLSequence(com.github.zhenwei.core.asn1.DLSequence) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector)

Example 27 with DLSequence

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!");
    }
}
Also used : DLSequence(com.github.zhenwei.core.asn1.DLSequence) ContentInfo(com.github.zhenwei.pkix.util.asn1.cms.ContentInfo) PKIStatusInfo(com.github.zhenwei.pkix.util.asn1.cmp.PKIStatusInfo) ASN1EncodableVector(com.github.zhenwei.core.asn1.ASN1EncodableVector) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) IOException(java.io.IOException) IOException(java.io.IOException)

Example 28 with DLSequence

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);
}
Also used : MacData(com.github.zhenwei.core.asn1.pkcs.MacData) Pfx(com.github.zhenwei.core.asn1.pkcs.Pfx) DLSequence(com.github.zhenwei.core.asn1.DLSequence) ContentInfo(com.github.zhenwei.core.asn1.pkcs.ContentInfo) AuthenticatedSafe(com.github.zhenwei.core.asn1.pkcs.AuthenticatedSafe) IOException(java.io.IOException) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString)

Example 29 with DLSequence

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);
    }
}
Also used : WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) DLSequence(com.github.zhenwei.core.asn1.DLSequence) KeyPairAlgEnum(com.github.zhenwei.core.enums.KeyPairAlgEnum) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) WeGooCryptoException(com.github.zhenwei.core.exception.WeGooCryptoException) WeGooKeyException(com.github.zhenwei.core.exception.WeGooKeyException) BaseWeGooException(com.github.zhenwei.core.exception.BaseWeGooException)

Example 30 with DLSequence

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);
    }
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) DLSequence(org.bouncycastle.asn1.DLSequence) ASN1Integer(org.bouncycastle.asn1.ASN1Integer) IOException(java.io.IOException)

Aggregations

DLSequence (org.bouncycastle.asn1.DLSequence)59 IOException (java.io.IOException)27 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)21 DERTaggedObject (org.bouncycastle.asn1.DERTaggedObject)21 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)18 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)18 DEROctetString (org.bouncycastle.asn1.DEROctetString)13 ASN1Encodable (org.bouncycastle.asn1.ASN1Encodable)12 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)12 BigInteger (java.math.BigInteger)9 DERUTF8String (org.bouncycastle.asn1.DERUTF8String)9 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)8 DERIA5String (org.bouncycastle.asn1.DERIA5String)8 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)7 DLSequence (com.github.zhenwei.core.asn1.DLSequence)6 Pair (android.util.Pair)5 CertificateEncodingException (java.security.cert.CertificateEncodingException)5 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)4 ASN1OutputStream (org.bouncycastle.asn1.ASN1OutputStream)4 Extension (org.bouncycastle.asn1.x509.Extension)4