Example usage for org.bouncycastle.asn1 DLSequence getObjectAt

List of usage examples for org.bouncycastle.asn1 DLSequence getObjectAt

Introduction

In this page you can find the example usage for org.bouncycastle.asn1 DLSequence getObjectAt.

Prototype

public ASN1Encodable getObjectAt(int index) 

Source Link

Document

Return the object at the sequence position indicated by index.

Usage

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static String extractHashId(byte[] sign) throws Exception {
    String ret = null;//from   ww w  . j a  v  a 2s  . c o m
    ASN1InputStream is = new ASN1InputStream(new ByteArrayInputStream(sign));
    ASN1Primitive topLevel = is.readObject();
    LOG.debug("top level:" + topLevel.getClass().getName());

    if (topLevel instanceof org.bouncycastle.asn1.DLSequence) {
        DLSequence topLevelDLS = (DLSequence) topLevel;
        if (topLevelDLS.size() == 2) {
            ASN1Encodable level1 = topLevelDLS.getObjectAt(1);
            LOG.debug("level1:" + level1.getClass().getName());
            if (level1 instanceof org.bouncycastle.asn1.DERTaggedObject) {
                DERTaggedObject level1TO = (DERTaggedObject) level1;
                ASN1Primitive level2 = level1TO.getObject();
                LOG.debug("level2:" + level2.getClass().getName());
                if (level2 instanceof org.bouncycastle.asn1.DERSequence) {
                    DERSequence level2DS = (DERSequence) level2;
                    LOG.debug("level2 len:" + level2DS.size());

                    ASN1Encodable level3_1 = level2DS.getObjectAt(1);
                    LOG.debug("level3_1:" + level3_1.getClass().getName());

                    if (level3_1 instanceof org.bouncycastle.asn1.DERSet) {
                        DERSet level3_1Set = (DERSet) level3_1;
                        ASN1Encodable level4_1 = level3_1Set.getObjectAt(0);
                        LOG.debug("level4_1:" + level4_1.getClass().getName());

                        if (level4_1 instanceof org.bouncycastle.asn1.DERSequence) {
                            DERSequence level4_1Seq = (DERSequence) level4_1;
                            ASN1Encodable level5_0 = level4_1Seq.getObjectAt(0);

                            LOG.debug("level5_0:" + level5_0.getClass().getName());

                            if (level5_0 instanceof org.bouncycastle.asn1.ASN1ObjectIdentifier) {
                                ASN1ObjectIdentifier level5_0Seq = (ASN1ObjectIdentifier) level5_0;
                                LOG.debug(level5_0Seq.toString());
                                ret = level5_0Seq.toString();

                            } else {
                                throw new Exception("DER enconding error");
                            }

                        } else {
                            throw new Exception("DER enconding error");
                        }

                    } else {
                        throw new Exception("DER enconding error");
                    }
                } else {
                    throw new Exception("DER enconding error");
                }

            } else {
                throw new Exception("DER enconding error");
            }
        } else {
            throw new Exception("DER enconding error");
        }

    } else {
        throw new Exception("DER enconding error");
    }

    return ret;
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static byte[] extractSignature(byte[] sign) throws Exception {
    byte[] ret = null;
    ASN1InputStream is = new ASN1InputStream(new ByteArrayInputStream(sign));
    ASN1Primitive topLevel = is.readObject();
    LOG.debug("top level:" + topLevel.getClass().getName());

    if (topLevel instanceof org.bouncycastle.asn1.DLSequence) {
        DLSequence topLevelDLS = (DLSequence) topLevel;
        if (topLevelDLS.size() == 2) {
            ASN1Encodable level1 = topLevelDLS.getObjectAt(1);
            LOG.debug("level1:" + level1.getClass().getName());
            if (level1 instanceof org.bouncycastle.asn1.DERTaggedObject) {
                DERTaggedObject level1TO = (DERTaggedObject) level1;
                ASN1Primitive level2 = level1TO.getObject();
                LOG.debug("level2:" + level2.getClass().getName());
                if (level2 instanceof org.bouncycastle.asn1.DERSequence) {
                    DERSequence level2DS = (DERSequence) level2;
                    LOG.debug("level2 len:" + level2DS.size());
                    ASN1Encodable level3_4 = level2DS.getObjectAt(level2DS.size() - 1);
                    LOG.debug("level3_4:" + level3_4.getClass().getName());
                    if (level3_4 instanceof org.bouncycastle.asn1.DERSet) {
                        DERSet level3_4DS = (DERSet) level3_4;
                        ASN1Encodable level3_4_0 = level3_4DS.getObjectAt(0);
                        LOG.debug("level3_4_0:" + level3_4_0.getClass().getName());
                        if (level3_4_0 instanceof org.bouncycastle.asn1.DERSequence) {
                            DERSequence level3_4_0DS = (DERSequence) level3_4_0;
                            LOG.debug("level3_4_0DS len:" + level3_4_0DS.size());
                            ASN1Encodable signature = level3_4_0DS.getObjectAt(level3_4_0DS.size() - 1);
                            LOG.debug("signature:" + signature.getClass().getName());
                            if (signature instanceof org.bouncycastle.asn1.DEROctetString) {
                                DEROctetString signDOS = (DEROctetString) signature;
                                ret = signDOS.getOctets();
                            }/*  w w w  .  j av a2  s.  c o m*/
                        } else {
                            throw new Exception("DER enconding error");
                        }

                    } else {
                        throw new Exception("DER enconding error");
                    }
                } else {
                    throw new Exception("DER enconding error");
                }

            } else {
                throw new Exception("DER enconding error");
            }
        } else {
            throw new Exception("DER enconding error");
        }

    } else {
        throw new Exception("DER enconding error");
    }

    return ret;
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static DERTaggedObject extractDTOSignPolicyOid(byte[] sign, SignCompare signCompare) throws Exception {

    DERTaggedObject ret = null;/*from   w ww .j  a  va 2  s .com*/
    ASN1InputStream is = new ASN1InputStream(new ByteArrayInputStream(sign));
    ASN1Primitive topLevel = is.readObject();
    LOG.debug("top level:" + topLevel.getClass().getName());

    if (topLevel instanceof org.bouncycastle.asn1.DLSequence) {
        DLSequence topLevelDLS = (DLSequence) topLevel;
        if (topLevelDLS.size() == 2) {
            ASN1Encodable level1 = topLevelDLS.getObjectAt(1);
            LOG.debug("level1:" + level1.getClass().getName());
            if (level1 instanceof org.bouncycastle.asn1.DERTaggedObject) {
                DERTaggedObject level1TO = (DERTaggedObject) level1;
                ASN1Primitive level2 = level1TO.getObject();
                LOG.debug("level2:" + level2.getClass().getName());
                if (level2 instanceof org.bouncycastle.asn1.DERSequence) {
                    DERSequence level2DS = (DERSequence) level2;
                    LOG.debug("level2 len:" + level2DS.size());
                    signCompare.setNumCerts(extractCertCount(level2DS));
                    ret = extractSignedAttributes(level2DS);
                } else {
                    throw new Exception("DER enconding error");
                }

            } else {
                throw new Exception("DER enconding error");
            }
        } else {
            throw new Exception("DER enconding error");
        }

    } else {
        throw new Exception("DER enconding error");
    }

    return ret;
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static void extractSignCompare2(byte[] sign, SignCompare2 signCompare) throws Exception {
    saveDebug(sign);/*w w w.ja va  2 s .c  o m*/
    DERTaggedObject ret = null;
    ASN1InputStream is = new ASN1InputStream(new ByteArrayInputStream(sign));
    ASN1Primitive topLevel = is.readObject();
    LOG.debug("top level:" + topLevel.getClass().getName());

    if (topLevel instanceof org.bouncycastle.asn1.DLSequence) {
        DLSequence topLevelDLS = (DLSequence) topLevel;
        if (topLevelDLS.size() == 2) {
            ASN1Encodable level1 = topLevelDLS.getObjectAt(1);
            LOG.debug("level1:" + level1.getClass().getName());
            if (level1 instanceof org.bouncycastle.asn1.DERTaggedObject) {
                DERTaggedObject level1TO = (DERTaggedObject) level1;
                ASN1Primitive level2 = level1TO.getObject();
                LOG.debug("level2:" + level2.getClass().getName());
                if (level2 instanceof org.bouncycastle.asn1.DERSequence) {
                    DERSequence level2DS = (DERSequence) level2;
                    LOG.debug("level2 len:" + level2DS.size());
                    signCompare.setNumCerts(extractCertCount(level2DS));
                    ret = extractSignedAttributes(level2DS);
                } else {
                    throw new Exception("DER enconding error");
                }

            } else {
                throw new Exception("DER enconding error");
            }
        } else {
            throw new Exception("DER enconding error");
        }

    } else {
        throw new Exception("DER enconding error");
    }

    //      return ret;
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static List<byte[]> extractCertList(byte[] sign) throws Exception {
    List<byte[]> ret = null;
    ASN1InputStream is = new ASN1InputStream(new ByteArrayInputStream(sign));
    ASN1Primitive topLevel = is.readObject();
    LOG.debug("top level:" + topLevel.getClass().getName());

    if (topLevel instanceof org.bouncycastle.asn1.DLSequence) {
        DLSequence topLevelDLS = (DLSequence) topLevel;
        if (topLevelDLS.size() == 2) {
            ASN1Encodable level1 = topLevelDLS.getObjectAt(1);
            LOG.debug("level1:" + level1.getClass().getName());
            if (level1 instanceof org.bouncycastle.asn1.DERTaggedObject) {
                DERTaggedObject level1TO = (DERTaggedObject) level1;
                ASN1Primitive level2 = level1TO.getObject();
                LOG.debug("level2:" + level2.getClass().getName());
                if (level2 instanceof org.bouncycastle.asn1.DERSequence) {
                    DERSequence level2DS = (DERSequence) level2;
                    LOG.debug("level2 len:" + level2DS.size());
                    ret = extractCertArray(level2DS);
                } else {
                    throw new Exception("DER enconding error");
                }/*  ww w  .  ja v a  2  s  .  c o m*/

            } else {
                throw new Exception("DER enconding error");
            }
        } else {
            throw new Exception("DER enconding error");
        }

    } else {
        throw new Exception("DER enconding error");
    }

    return ret;
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static void extractSignPolicyRefFromSignedAttrib(DERTaggedObject signedAttribsDTO,
        SignCompare signCompare) throws Exception {
    //      String SignCompare = null;
    ASN1Primitive dtoObj = signedAttribsDTO.getObject();
    if (dtoObj instanceof DLSequence) {
        DLSequence topSeq = (DLSequence) dtoObj;
        List<String> signedAttribOid = new ArrayList<String>();
        signCompare.setSignedAttribs(signedAttribOid);
        for (int i = 0; i < topSeq.size(); i++) {
            // treat each SIGNED ATTRIBUTE
            ASN1Encodable objL1 = topSeq.getObjectAt(i);
            if (objL1 instanceof DERSequence) {
                DERSequence seqL1 = (DERSequence) objL1;
                ASN1Encodable objL2 = seqL1.getObjectAt(0);
                if (objL2 instanceof ASN1ObjectIdentifier) {
                    ASN1ObjectIdentifier saOid = (ASN1ObjectIdentifier) objL2;
                    String saOIdStr = saOid.toString();
                    // System.out.println(saOIdStr);
                    signedAttribOid.add(saOIdStr);

                    if (saOIdStr.compareTo(DerEncoder.ID_SIG_POLICY) == 0) {
                        ASN1Encodable objL21 = seqL1.getObjectAt(1);
                        if (objL21 instanceof DERSet) {
                            DERSet objL21Set = (DERSet) objL21;
                            ASN1Encodable objL3 = objL21Set.getObjectAt(0);
                            if (objL3 instanceof DERSequence) {
                                DERSequence objL3Seq = (DERSequence) objL3;
                                ASN1Encodable objL4 = objL3Seq.getObjectAt(0);
                                if (objL4 instanceof ASN1ObjectIdentifier) {
                                    ASN1ObjectIdentifier objL4Oid = (ASN1ObjectIdentifier) objL4;
                                    signCompare.setPsOid(objL4Oid.toString());
                                }//from   w w  w . jav a  2s .  c  o m
                                ASN1Encodable objL42 = getAt(objL3Seq, 2);
                                if (objL42 instanceof DERSequence) {
                                    DERSequence objL42DerSeq = (DERSequence) objL42;
                                    ASN1Encodable objL420 = getAt(objL42DerSeq, 0);
                                    if (objL420 instanceof DERSequence) {
                                        DERSequence objL420DerSeq = (DERSequence) objL420;
                                        ASN1Encodable psUrl = getAt(objL420DerSeq, 1);
                                        if (psUrl instanceof DERIA5String) {
                                            DERIA5String psUrlIA5 = (DERIA5String) psUrl;
                                            signCompare.setPsUrl(psUrlIA5.getString());
                                        }
                                    }
                                }

                            }
                        }
                    } else if (saOIdStr.compareTo(DerEncoder.ID_SIGNING_TIME) == 0) {
                        ASN1Encodable objL2SetTime = seqL1.getObjectAt(1);
                        if (objL2SetTime instanceof DERSet) {
                            DERSet objL2SetTimeDer = (DERSet) objL2SetTime;
                            ASN1Encodable objL2SignTime = objL2SetTimeDer.getObjectAt(0);
                            if (objL2SignTime instanceof ASN1UTCTime) {
                                ASN1UTCTime objL2SignTimeUTC = (ASN1UTCTime) objL2SignTime;
                                signCompare.setSigningTime(objL2SignTimeUTC.getDate());
                            }

                        }

                    }
                }
            }
        }
    }

}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static SignPolicyRef extractVerifyRefence(byte[] policy) throws IOException, ParseException {
    SignPolicyRef ret = new SignPolicyRef();

    ASN1InputStream is = new ASN1InputStream(new ByteArrayInputStream(policy));
    ASN1Primitive topLevel = is.readObject();
    // SignaturePolicy ::= SEQUENCE {
    // signPolicyHashAlg AlgorithmIdentifier,
    // signPolicyInfo SignPolicyInfo,
    // signPolicyHash SignPolicyHash OPTIONAL }
    if (topLevel instanceof DLSequence) {
        DLSequence topLevelDLS = (DLSequence) topLevel;
        ASN1Encodable dseqL10 = topLevelDLS.getObjectAt(0);
        ASN1Encodable psHashAlg = null;/*from w  w w  .j a v a2s.  c o m*/
        if (dseqL10 instanceof DLSequence) {
            DLSequence dseqL10DLS = (DLSequence) dseqL10;
            psHashAlg = dseqL10DLS.getObjectAt(0);

        } else if (dseqL10 instanceof ASN1ObjectIdentifier) {
            psHashAlg = (ASN1ObjectIdentifier) dseqL10;
        } else
            return null;

        if (psHashAlg instanceof ASN1ObjectIdentifier) {
            ASN1ObjectIdentifier psHashAlgOid = (ASN1ObjectIdentifier) psHashAlg;
            ret.setPsHashAlg(psHashAlgOid.toString());
        }

        ASN1Encodable dseqL11 = topLevelDLS.getObjectAt(1);
        if (dseqL11 instanceof DLSequence) {
            // SignPolicyInfo ::= SEQUENCE {
            DLSequence dseqL11DLS = (DLSequence) dseqL11;
            ASN1Encodable psOid = dseqL11DLS.getObjectAt(0);
            // signPolicyIdentifier SignPolicyId,
            // 2.16.76.1.7.1.6.2.1
            if (psOid instanceof ASN1ObjectIdentifier) {
                ASN1ObjectIdentifier psOidOid = (ASN1ObjectIdentifier) psOid;
                ret.setPsOid(psOidOid.toString());

            }
            ASN1Encodable dateOfIssue = dseqL11DLS.getObjectAt(1);
            // dateOfIssue GeneralizedTime,
            // 2012-03-22
            if (dateOfIssue instanceof ASN1GeneralizedTime) {
                ASN1GeneralizedTime dateOfIssueGT = (ASN1GeneralizedTime) dateOfIssue;
                ret.setDateOfIssue(dateOfIssueGT.getDate());
            }

            ASN1Encodable policyIssuerName = dseqL11DLS.getObjectAt(2);
            // policyIssuerName PolicyIssuerName,
            // C=BR, O=ICP-Brasil, OU=Instituto Nacional de Tecnologia da
            // Informacao
            // - ITI
            if (policyIssuerName instanceof DLSequence) {
                DLSequence policyIssuerNameDLSeq = (DLSequence) policyIssuerName;
                ASN1Encodable policyIssuerName2 = policyIssuerNameDLSeq.getObjectAt(0);
                if (policyIssuerName2 instanceof DERTaggedObject) {
                    DERTaggedObject policyIssuerName2DTO = (DERTaggedObject) policyIssuerName2;
                    ASN1Primitive polIssuerNameObj = policyIssuerName2DTO.getObject();
                    if (polIssuerNameObj instanceof DEROctetString) {
                        String polIssuerNameStr = new String(((DEROctetString) polIssuerNameObj).getOctets());
                        ret.setPolIssuerName(polIssuerNameStr);
                    }
                }

            }

            ASN1Encodable fieldOfApplication = dseqL11DLS.getObjectAt(3);
            // fieldOfApplication FieldOfApplication,
            // Este tipo de assinatura deve ser utilizado em aplicacoes ou
            // processos
            // de negocio nos quais a assinatura digital agrega seguranca a
            // autenticacao de entidades e verificacao de integridade,
            // permitindo
            // sua validacao durante o prazo de, validade dos certificados
            // dos
            // signatarios. Uma vez que nao sao usados carimbos do tempo, a
            // validacao posterior so sera possivel se existirem referencias
            // temporais que identifiquem o momento em que ocorreu a
            // assinatura
            // digital. Nessas situacoes, deve existir legislacao especifica
            // ou um
            // acordo previo entre as partes definindo as referencias a
            // serem
            // utilizadas. Segundo esta PA, e permitido o emprego de
            // multiplas
            // assinaturas.
            if (fieldOfApplication instanceof DEROctetString) {
                DERUTF8String fieldOfApplicationDUS = (DERUTF8String) fieldOfApplication;
                ret.setFieldOfApplication(fieldOfApplicationDUS.getString());
            }
            // signatureValidationPolicy SignatureValidationPolicy,
            // signPolExtensions SignPolExtensions OPTIONAL
            // SignatureValidationPolicy ::= SEQUENCE {
            ASN1Encodable signatureValidationPolicy = dseqL11DLS.getObjectAt(4);
            if (signatureValidationPolicy instanceof DLSequence) {
                DLSequence signatureValidationPolicyDLS = (DLSequence) signatureValidationPolicy;
                // signingPeriod SigningPeriod,
                // NotBefore 2012-03-22
                // NotAfter 2023-06-21
                ASN1Encodable signingPeriod = signatureValidationPolicyDLS.getObjectAt(0);
                if (signingPeriod instanceof DLSequence) {
                    DLSequence signingPeriodDLS = (DLSequence) signingPeriod;
                    ASN1Encodable notBefore = signingPeriodDLS.getObjectAt(0);
                    if (notBefore instanceof ASN1GeneralizedTime) {
                        ASN1GeneralizedTime notBeforeAGT = (ASN1GeneralizedTime) notBefore;
                        ret.setNotBefore(notBeforeAGT.getDate());

                    }

                    ASN1Encodable notAfter = signingPeriodDLS.getObjectAt(1);
                    if (notAfter instanceof ASN1GeneralizedTime) {
                        ASN1GeneralizedTime notAfterAGT = (ASN1GeneralizedTime) notAfter;
                        ret.setNotAfter(notAfterAGT.getDate());
                    }

                }

                //
                // commonRules CommonRules,
                ASN1Encodable commonRules = getAt(signatureValidationPolicyDLS, 1);
                if (commonRules instanceof DLSequence) {
                    DLSequence commonRulesDLS = (DLSequence) commonRules;
                    // CommonRules ::= SEQUENCE {
                    // signerAndVeriferRules [0] SignerAndVerifierRules
                    // OPTIONAL,
                    // signingCertTrustCondition [1]
                    // SigningCertTrustCondition OPTIONAL,
                    // timeStampTrustCondition [2] TimestampTrustCondition
                    // OPTIONAL,
                    // attributeTrustCondition [3] AttributeTrustCondition
                    // OPTIONAL,
                    // algorithmConstraintSet [4] AlgorithmConstraintSet
                    // OPTIONAL,
                    // signPolExtensions [5] SignPolExtensions OPTIONAL
                    // }
                    ASN1Encodable signerAndVeriferRules = getAt(commonRulesDLS, 0);

                    // SignerAndVerifierRules ::= SEQUENCE {
                    // signerRules SignerRules,
                    // verifierRules VerifierRules }

                    if (signerAndVeriferRules instanceof DERTaggedObject) {
                        DERTaggedObject signerAndVeriferRulesDTO = (DERTaggedObject) signerAndVeriferRules;
                        ASN1Encodable signerAndVeriferRulesTmp = signerAndVeriferRulesDTO.getObject();
                        if (signerAndVeriferRulesTmp instanceof DERSequence) {
                            DERSequence signerAndVeriferRulesDERSeq = (DERSequence) signerAndVeriferRulesTmp;
                            ASN1Encodable signerRules = getAt(signerAndVeriferRulesDERSeq, 0);
                            if (signerRules instanceof DERSequence) {
                                DERSequence signerRulesDERSeq = (DERSequence) signerRules;
                                // SignerRules ::= SEQUENCE {
                                // externalSignedData BOOLEAN OPTIONAL,
                                // -- True if signed data is external to CMS
                                // structure
                                // -- False if signed data part of CMS
                                // structure
                                // -- not present if either allowed
                                // mandatedSignedAttr CMSAttrs,
                                // -- Mandated CMS signed attributes
                                // 1.2.840.113549.1.9.3
                                // 1.2.840.113549.1.9.4
                                // 1.2.840.113549.1.9.16.2.15
                                // 1.2.840.113549.1.9.16.2.47
                                // mandatedUnsignedAttr CMSAttrs,
                                // <empty sequence>
                                // -- Mandated CMS unsigned attributed
                                // mandatedCertificateRef [0] CertRefReq
                                // DEFAULT signerOnly,
                                // (1)
                                // -- Mandated Certificate Reference
                                // mandatedCertificateInfo [1] CertInfoReq
                                // DEFAULT none,
                                // -- Mandated Certificate Info
                                // signPolExtensions [2] SignPolExtensions
                                // OPTIONAL}

                                // CMSAttrs ::= SEQUENCE OF OBJECT
                                // IDENTIFIER
                                ASN1Encodable mandatedSignedAttr = getAt(signerRulesDERSeq, 0);
                                if (mandatedSignedAttr instanceof DERSequence) {
                                    DERSequence mandatedSignedAttrDERSeq = (DERSequence) mandatedSignedAttr;
                                    for (int i = 0; i < mandatedSignedAttrDERSeq.size(); i++) {
                                        ASN1Encodable at = getAt(mandatedSignedAttrDERSeq, i);
                                        ret.addMandatedSignedAttr(at.toString());
                                    }
                                }
                                ASN1Encodable mandatedUnsignedAttr = getAt(signerRulesDERSeq, 1);
                                if (mandatedUnsignedAttr instanceof DERSequence) {
                                    DERSequence mandatedUnsignedAttrDERSeq = (DERSequence) mandatedUnsignedAttr;
                                }
                                ASN1Encodable mandatedCertificateRef = getAt(signerRulesDERSeq, 2);
                                if (mandatedCertificateRef instanceof DERTaggedObject) {
                                    DERTaggedObject mandatedCertificateRefDERSeq = (DERTaggedObject) mandatedCertificateRef;
                                    // CertRefReq ::= ENUMERATED {
                                    // signerOnly (1),
                                    // -- Only reference to signer cert
                                    // mandated
                                    // fullpath (2)
                                    //
                                    // -- References for full cert path up
                                    // to a trust point required
                                    // }
                                    ASN1Encodable mandatedCertificateRefTmp = mandatedCertificateRefDERSeq
                                            .getObject();
                                    ASN1Enumerated mandatedCertificateRefEnum = (ASN1Enumerated) mandatedCertificateRefTmp;
                                    BigInteger valEnum = mandatedCertificateRefEnum.getValue();
                                    int mandatedCertificateRefInt = valEnum.intValue();
                                    ret.setMandatedCertificateRef(mandatedCertificateRefInt);
                                    int x = 0;
                                }
                            }

                            ASN1Encodable verifierRules = getAt(signerAndVeriferRulesDERSeq, 1);
                            if (verifierRules instanceof DERSequence) {
                                DERSequence verifierRulesDERSeq = (DERSequence) verifierRules;

                            }

                        }

                    }

                    ASN1Encodable signingCertTrustCondition = getAt(commonRulesDLS, 1);
                    if (signingCertTrustCondition instanceof DERTaggedObject) {
                        DERTaggedObject signingCertTrustConditionDTO = (DERTaggedObject) signingCertTrustCondition;
                        ASN1Encodable signingCertTrustConditionTmp = signingCertTrustConditionDTO.getObject();
                        if (signingCertTrustConditionTmp instanceof DERSequence) {
                            DERSequence signingCertTrustConditionDERSeq = (DERSequence) signingCertTrustConditionTmp;
                        }

                    }
                    ASN1Encodable timeStampTrustCondition = getAt(commonRulesDLS, 2);
                    if (timeStampTrustCondition instanceof DERTaggedObject) {
                        DERTaggedObject timeStampTrustConditionDTO = (DERTaggedObject) timeStampTrustCondition;
                        ASN1Encodable timeStampTrustConditionTmp = timeStampTrustConditionDTO.getObject();
                        if (timeStampTrustConditionTmp instanceof DERSequence) {
                            DERSequence timeStampTrustConditionDERSeq = (DERSequence) timeStampTrustConditionTmp;
                        }

                    }
                    ASN1Encodable attributeTrustCondition = getAt(commonRulesDLS, 3);
                    if (attributeTrustCondition instanceof DERTaggedObject) {
                        DERTaggedObject attributeTrustConditionDTO = (DERTaggedObject) attributeTrustCondition;
                        ASN1Encodable attributeTrustConditionTmp = attributeTrustConditionDTO.getObject();
                        if (attributeTrustConditionTmp instanceof DERSequence) {
                            DERSequence attributeTrustConditionDERSeq = (DERSequence) attributeTrustConditionTmp;
                        }

                    }

                    // *****************************
                    ASN1Encodable algorithmConstraintSet = getAt(commonRulesDLS, 4);
                    ASN1Encodable signPolExtensions = getAt(commonRulesDLS, 5);

                }
                // commitmentRules CommitmentRules,
                ASN1Encodable commitmentRules = getAt(signatureValidationPolicyDLS, 2);
                if (commitmentRules instanceof DLSequence) {

                }

                // signPolExtensions SignPolExtensions
                // OPTIONAL
                ASN1Encodable signPolExtensions = getAt(signatureValidationPolicyDLS, 3);
                if (signPolExtensions instanceof DLSequence) {

                }
                // }
            }
        }

    }

    // CertInfoReq ::= ENUMERATED {
    // none (0) ,
    // -- No mandatory requirements
    // signerOnly (1) ,
    // -- Only reference to signer cert mandated
    // fullpath (2)
    // -- References for full cert path up to a
    // -- trust point mandated
    // }

    is.close();
    return ret;

}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static Map<String, String> createSanMap(byte[] extensionValue, int index) {
    Map<String, String> ret = new HashMap<String, String>();
    try {/*w w w.j  a v  a2s.co m*/
        if (extensionValue == null) {
            return null;
        }
        ASN1InputStream oAsnInStream = new ASN1InputStream(new ByteArrayInputStream(extensionValue));
        ASN1Primitive derObjCP = oAsnInStream.readObject();
        DLSequence derSeq = (DLSequence) derObjCP;
        // int seqLen = derSeq.size();
        ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) derSeq.getObjectAt(0);
        String sanOid = oid.getId();

        DERTaggedObject derTO = (DERTaggedObject) derSeq.getObjectAt(1);
        // int tag = derTO.getTagNo();
        ASN1Primitive derObjA = derTO.getObject();

        DERTaggedObject derTO2 = (DERTaggedObject) derObjA;
        // int tag2 = derTO2.getTagNo();
        ASN1Primitive derObjB = derTO2.getObject();
        String contentStr = "";
        if (derObjB instanceof DEROctetString) {
            DEROctetString derOCStr = (DEROctetString) derObjB;
            contentStr = new String(derOCStr.getOctets(), "UTF8");
        } else if (derObjB instanceof DERPrintableString) {
            DERPrintableString derOCStr = (DERPrintableString) derObjB;
            contentStr = new String(derOCStr.getOctets(), "UTF8");
        } else {
            System.err.println("FORMAT OF SAN: UNRECOGNIZED -> " + derObjB.getClass().getCanonicalName());
        }
        LOG.debug(sanOid + " -> " + contentStr);

        String value = "";
        String name = "";

        if (sanOid.compareTo(PF_PF_ID) == 0 || sanOid.compareTo(PJ_PF_ID) == 0) {
            value = contentStr.substring(BIRTH_DATE_INI, BIRTH_DATE_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.BIRTH_DATE_D, index);
                ret.put(name, value);
            }

            value = contentStr.substring(CPF_INI, CPF_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.CPF_D, index);
                ret.put(name, value);
            }

            value = contentStr.substring(PIS_INI, PIS_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.PIS_D, index);
                ret.put(name, value);
            }

            value = contentStr.substring(RG_INI, RG_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.RG_D, index);
                ret.put(name, value);
            }

            int rgOrgUfLen = RG_ORG_UF_LEN > contentStr.length() ? contentStr.length() : RG_ORG_UF_LEN;
            if (rgOrgUfLen > RG_ORG_UF_INI) {
                value = contentStr.substring(RG_ORG_UF_INI, rgOrgUfLen);

                String rgOrg = value.substring(0, value.length() - 2);
                String rgUf = value.substring(value.length() - 2, value.length());
                if (isValidValue(rgOrg)) {
                    name = String.format(CertConstants.RG_ORG_D, index);
                    ret.put(name, rgOrg);
                }
                if (isValidValue(rgUf)) {
                    name = String.format(CertConstants.RG_UF_D, index);
                    ret.put(name, rgUf);
                }
            }

        } else if (sanOid.compareTo(PERSON_NAME_OID) == 0) {
            value = contentStr;
            if (isValidValue(value)) {
                name = String.format(CertConstants.PERSON_NAME_D, index);
                ret.put(name, value);
            }

        } else if (sanOid.compareTo(CNPJ_OID) == 0) {
            name = String.format(CERT_TYPE_FMT, index);
            ret.put(name, ICP_BRASIL_PJ);
            value = contentStr;
            if (isValidValue(value)) {
                name = String.format(CertConstants.CNPJ_D, index);
                ret.put(name, value);
            }

        } else if (sanOid.compareTo(ELEITOR_OID) == 0) {
            name = String.format(CERT_TYPE_FMT, index);
            ret.put(name, ICP_BRASIL_PF);
            value = contentStr.substring(ELEITOR_INI, ELEITOR_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.ELEITOR_D, index);
                ret.put(name, value);
            }

            int zonaLen = ZONA_LEN > contentStr.length() ? contentStr.length() : ZONA_LEN;
            if (zonaLen > ZONA_LEN) {

                value = contentStr.substring(ZONA_INI, zonaLen);
                if (isValidValue(value)) {
                    name = String.format(CertConstants.ZONA_D, index);
                    ret.put(name, value);
                }
            }

            int secaoLen = SECAO_LEN > contentStr.length() ? contentStr.length() : SECAO_LEN;
            if (secaoLen > SECAO_LEN) {
                value = contentStr.substring(SECAO_INI, SECAO_LEN);
                if (isValidValue(value)) {
                    name = String.format(CertConstants.SECAO_D, index);
                    ret.put(name, value);
                }
            }

        } else if (sanOid.compareTo(PF_PF_INSS_OID) == 0 || sanOid.compareTo(PJ_PF_INSS_OID) == 0) {
            value = contentStr.substring(INSS_INI, INSS_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.INSS_D, index);
                ret.put(name, value);
            }

        } else if (sanOid.compareTo(OAB_OID) == 0) {
            value = contentStr.substring(OAB_REG_INI, OAB_REG_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.OAB_REG_D, index);
                ret.put(name, value);
            }
            value = contentStr.substring(OAB_UF_INI, OAB_UF_LEN);
            if (isValidValue(value)) {
                name = String.format(CertConstants.OAB_UF_D, index);
                ret.put(name, value);
            }

        } else if (sanOid.startsWith(PROFESSIONAL_OID)) {
            value = contentStr;
            if (isValidValue(value)) {
                name = String.format(CertConstants.PROFESSIONAL_D, index);
                ret.put(name, value);
            }
        } else if (sanOid.startsWith(UPN)) {
            value = contentStr;
            if (isValidValue(value)) {
                name = String.format(CertConstants.UPN_D, index);
                ret.put(name, value);
            }
        } else {
            System.err.println("SAN:OTHER NAME NOT RECOGNIZED");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static byte[] getAKI(byte[] extensionValue, int index) {
    byte[] ret = null;
    try {//from   www .  j av  a 2  s . com
        if (extensionValue == null) {
            return null;
        }
        ASN1InputStream oAsnInStream = new ASN1InputStream(new ByteArrayInputStream(extensionValue));
        ASN1Primitive derObjCP = oAsnInStream.readObject();
        DEROctetString dosCP = (DEROctetString) derObjCP;
        byte[] cpOctets = dosCP.getOctets();
        ASN1InputStream oAsnInStream2 = new ASN1InputStream(new ByteArrayInputStream(cpOctets));
        ASN1Primitive derObj2 = oAsnInStream2.readObject();
        // derObj2 = oAsnInStream2.readObject();
        DLSequence derSeq = (DLSequence) derObj2;
        int seqLen = derSeq.size();
        // for(int i = 0; i < seqLen; i++){
        ASN1Encodable derObj3 = derSeq.getObjectAt(0);
        DERTaggedObject derTO = (DERTaggedObject) derObj3;
        int tag = derTO.getTagNo();
        boolean empty = derTO.isEmpty();
        ASN1Primitive derObj4 = derTO.getObject();
        DEROctetString ocStr4 = (DEROctetString) derObj4;
        ret = ocStr4.getOctets();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return ret;
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static Map<String, String> getCertPolicies(byte[] certPols, int index)
        throws CertificateParsingException, IOException {
    Map<String, String> ret = new HashMap<String, String>();
    if (certPols == null) {
        return null;
    }//from www  .  j  a va  2 s . co m
    ASN1InputStream oAsnInStream = new ASN1InputStream(new ByteArrayInputStream(certPols));
    ASN1Primitive derObjCP = oAsnInStream.readObject();
    DEROctetString dosCP = (DEROctetString) derObjCP;
    byte[] cpOctets = dosCP.getOctets();
    ASN1InputStream oAsnInStream2 = new ASN1InputStream(new ByteArrayInputStream(cpOctets));
    ASN1Primitive derObj2 = oAsnInStream2.readObject();
    DLSequence dlCP = (DLSequence) derObj2;
    int seqLen = dlCP.size();
    for (int i = 0; i < seqLen; i++) {
        ASN1Encodable nextObj = dlCP.getObjectAt(i);
        DLSequence dlCP2 = (DLSequence) nextObj;
        // for(int j = 0; j < dlCP2.size(); j++){
        ASN1Encodable nextObj2 = dlCP2.getObjectAt(0);
        ASN1ObjectIdentifier pcOID = (ASN1ObjectIdentifier) nextObj2;
        ret.put(String.format(CERT_POL_OID, index), pcOID.toString());
        if (pcOID.toString().startsWith(ICP_BRASIL_PC_PREFIX_OID)) {

            ret.put(String.format(CertConstants.CERT_USAGE_D, index), getCertUsage(pcOID.toString()));
        }

        if (dlCP2.size() == 2) {
            nextObj2 = dlCP2.getObjectAt(1);

            ASN1Encodable nextObj3 = null;
            if (nextObj2 instanceof DLSequence) {
                DLSequence dlCP3 = (DLSequence) nextObj2;
                nextObj3 = dlCP3.getObjectAt(0);
            } else if (nextObj2 instanceof DERSequence) {
                DERSequence dlCP3 = (DERSequence) nextObj2;
                if (dlCP3.size() > 1) {
                    nextObj3 = dlCP3.getObjectAt(0);
                }

            }
            if (nextObj3 != null) {
                DLSequence dlCP4 = (DLSequence) nextObj3;
                ASN1Encodable nextObj4a = dlCP4.getObjectAt(0);
                ASN1Encodable nextObj4b = dlCP4.getObjectAt(1);

                ret.put(String.format(CERT_POL_QUALIFIER, index), nextObj4b.toString());
            }
        }
    }
    return ret;

}