Package gnu.java.security.der

Examples of gnu.java.security.der.DERValue


  {
    this.encoded = (byte[]) encoded.clone();
    DERReader der = new DERReader(encoded);

    // Extension ::= SEQUENCE {
    DERValue val = der.read();
    if (Configuration.DEBUG)
      log.fine("read val  tag == " + val.getTag() + " len == " + val.getLength());
    if (!val.isConstructed())
      throw new IOException("malformed Extension");

    //   extnID    OBJECT IDENTIFIER,
    val = der.read();
    if (val.getTag() != DER.OBJECT_IDENTIFIER)
      throw new IOException("expecting OBJECT IDENTIFIER");
    oid = (OID) val.getValue();
    if (Configuration.DEBUG)
      log.fine("read oid == " + oid);

    //   critical  BOOLEAN DEFAULT FALSE,
    val = der.read();
    if (val.getTag() == DER.BOOLEAN)
      {
        critical = ((Boolean) val.getValue()).booleanValue();
        val = der.read();
      }
    else
      critical = false;
    if (Configuration.DEBUG)
      log.fine("is critical == " + critical);

    //   extnValue OCTET STRING }
    if (val.getTag() != DER.OCTET_STRING)
      throw new IOException("expecting OCTET STRING");
    byte[] encval = (byte[]) val.getValue();
    isSupported = true;
    if (oid.equals(AuthorityKeyIdentifier.ID))
      {
        value = new AuthorityKeyIdentifier(encval);
      }
View Full Code Here


  }

  public DERValue getDerValue()
  {
    List ext = new ArrayList (3);
    ext.add (new DERValue (DER.OBJECT_IDENTIFIER, oid));
    ext.add (new DERValue (DER.BOOLEAN, Boolean.valueOf (critical)));
    ext.add (new DERValue (DER.OCTET_STRING, value.getEncoded()));
    return new DERValue (DER.CONSTRUCTED|DER.SEQUENCE, ext);
  }
View Full Code Here

    BigInteger q = pk.getPrimeQ();
    BigInteger dP = pk.getPrimeExponentP();
    BigInteger dQ = pk.getPrimeExponentQ();
    BigInteger qInv = pk.getCrtCoefficient();

    DERValue derVersion = new DERValue(DER.INTEGER, BigInteger.ZERO);

    DERValue derOID = new DERValue(DER.OBJECT_IDENTIFIER, RSA_ALG_OID);

    ArrayList algorithmID = new ArrayList(2);
    algorithmID.add(derOID);
    algorithmID.add(new DERValue(DER.NULL, null));
    DERValue derAlgorithmID = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
                                           algorithmID);

    DERValue derRSAVersion = new DERValue(DER.INTEGER, BigInteger.ZERO);
    DERValue derN = new DERValue(DER.INTEGER, n);
    DERValue derE = new DERValue(DER.INTEGER, e);
    DERValue derD = new DERValue(DER.INTEGER, d);
    DERValue derP = new DERValue(DER.INTEGER, p);
    DERValue derQ = new DERValue(DER.INTEGER, q);
    DERValue derDP = new DERValue(DER.INTEGER, dP);
    DERValue derDQ = new DERValue(DER.INTEGER, dQ);
    DERValue derQInv = new DERValue(DER.INTEGER, qInv);

    ArrayList rsaPrivateKey = new ArrayList();
    rsaPrivateKey.add(derRSAVersion);
    rsaPrivateKey.add(derN);
    rsaPrivateKey.add(derE);
    rsaPrivateKey.add(derD);
    rsaPrivateKey.add(derP);
    rsaPrivateKey.add(derQ);
    rsaPrivateKey.add(derDP);
    rsaPrivateKey.add(derDQ);
    rsaPrivateKey.add(derQInv);
    DERValue derRSAPrivateKey = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
                                             rsaPrivateKey);
    byte[] pkBytes = derRSAPrivateKey.getEncoded();
    DERValue derPrivateKey = new DERValue(DER.OCTET_STRING, pkBytes);

    ArrayList pki = new ArrayList(3);
    pki.add(derVersion);
    pki.add(derAlgorithmID);
    pki.add(derPrivateKey);
    DERValue derPKI = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, pki);

    byte[] result;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try
      {
View Full Code Here

    BigInteger version, n, e, d, p, q, dP, dQ, qInv;
    DERReader der = new DERReader(input);
    try
      {
        DERValue derPKI = der.read();
        DerUtil.checkIsConstructed(derPKI, "Wrong PrivateKeyInfo field");

        DERValue derVersion = der.read();
        DerUtil.checkIsBigInteger(derVersion, "Wrong Version field");
        version = (BigInteger) derVersion.getValue();
        if (version.compareTo(BigInteger.ZERO) != 0)
          throw new InvalidParameterException("Unexpected Version: " + version);

        DERValue derAlgoritmID = der.read();
        DerUtil.checkIsConstructed(derAlgoritmID, "Wrong AlgorithmIdentifier field");

        DERValue derOID = der.read();
        OID algOID = (OID) derOID.getValue();
        if (! algOID.equals(RSA_ALG_OID))
          throw new InvalidParameterException("Unexpected OID: " + algOID);

        // rfc-2459 states that this field is OPTIONAL but NULL if/when present
        DERValue val = der.read();
        if (val.getTag() == DER.NULL)
          val = der.read();

        byte[] pkBytes = (byte[]) val.getValue();
        der = new DERReader(pkBytes);
        DERValue derRSAPrivateKey = der.read();
        DerUtil.checkIsConstructed(derRSAPrivateKey, "Wrong RSAPrivateKey field");

        val = der.read();
        DerUtil.checkIsBigInteger(val, "Wrong RSAPrivateKey Version field");
        version = (BigInteger) val.getValue();
View Full Code Here

  private void parse(InputStream encoded) throws Exception
  {
    DERReader der = new DERReader(encoded);

    // Certificate ::= SEQUENCE {
    DERValue cert = der.read();
    Logger logger = logger();
    logger.log (Component.X509, "start Certificate  len == {0}",
                Integer.valueOf(cert.getLength()));

    this.encoded = cert.getEncoded();
    if (!cert.isConstructed())
      {
        throw new IOException("malformed Certificate");
      }

    // TBSCertificate ::= SEQUENCE {
    DERValue tbsCert = der.read();
    if (tbsCert.getValue() != DER.CONSTRUCTED_VALUE)
      {
        throw new IOException("malformed TBSCertificate");
      }
    tbsCertBytes = tbsCert.getEncoded();
    logger.log (Component.X509, "start TBSCertificate  len == {0}",
                Integer.valueOf(tbsCert.getLength()));

    // Version ::= INTEGER [0] { v1(0), v2(1), v3(2) }
    DERValue val = der.read();
    if (val.getTagClass() == DER.CONTEXT && val.getTag() == 0)
      {
        version = ((BigInteger) der.read().getValue()).intValue() + 1;
        val = der.read();
      }
    else
      {
        version = 1;
      }
    logger.log (Component.X509, "read version == {0}",
                Integer.valueOf(version));

    // SerialNumber ::= INTEGER
    serialNo = (BigInteger) val.getValue();
    logger.log (Component.X509, "read serial number == {0}", serialNo);

    // AlgorithmIdentifier ::= SEQUENCE {
    val = der.read();
    if (!val.isConstructed())
      {
        throw new IOException("malformed AlgorithmIdentifier");
      }
    int certAlgLen = val.getLength();
    logger.log (Component.X509, "start AlgorithmIdentifier  len == {0}",
                Integer.valueOf(certAlgLen));
    val = der.read();

    //   algorithm    OBJECT IDENTIFIER,
    algId = (OID) val.getValue();
    logger.log (Component.X509, "read algorithm ID == {0}", algId);

    //   parameters   ANY DEFINED BY algorithm OPTIONAL }
    if (certAlgLen > val.getEncodedLength())
      {
        val = der.read();
        if (val == null)
          {
            algVal = null;
          }
        else
          {
            algVal = val.getEncoded();

            if (val.isConstructed())
              encoded.skip(val.getLength());
          }
        logger.log (Component.X509, "read algorithm parameters == {0}", algVal);
      }

    // issuer   Name,
    val = der.read();
    issuer = new X500DistinguishedName(val.getEncoded());
    der.skip(val.getLength());
    logger.log (Component.X509, "read issuer == {0}", issuer);

    // Validity ::= SEQUENCE {
    //   notBefore   Time,
    //   notAfter    Time }
    if (!der.read().isConstructed())
      {
        throw new IOException("malformed Validity");
      }
    notBefore = (Date) der.read().getValue();
    logger.log (Component.X509, "read notBefore == {0}", notBefore);
    notAfter  = (Date) der.read().getValue();
    logger.log (Component.X509, "read notAfter == {0}", notAfter);

    // subject   Name,
    val = der.read();
    subject = new X500DistinguishedName(val.getEncoded());
    der.skip(val.getLength());
    logger.log (Component.X509, "read subject == {0}", subject);

    // SubjectPublicKeyInfo ::= SEQUENCE {
    //   algorithm         AlgorithmIdentifier,
    //   subjectPublicKey  BIT STRING }
    DERValue spki = der.read();
    if (!spki.isConstructed())
      {
        throw new IOException("malformed SubjectPublicKeyInfo");
      }
    KeyFactory spkFac = KeyFactory.getInstance("X.509");
    subjectKey = spkFac.generatePublic(new X509EncodedKeySpec(spki.getEncoded()));
    der.skip(spki.getLength());
    logger.log (Component.X509, "read subjectPublicKey == {0}", subjectKey);

    val = der.read();
    if (version >= 2 && val.getTagClass() != DER.UNIVERSAL && val.getTag() == 1)
      {
        byte[] b = (byte[]) val.getValue();
        issuerUniqueId = new BitString(b, 1, b.length-1, b[0] & 0xFF);
        logger.log (Component.X509, "read issuerUniqueId == {0}", issuerUniqueId);
        val = der.read();
      }
    if (version >= 2 && val.getTagClass() != DER.UNIVERSAL && val.getTag() == 2)
      {
        byte[] b = (byte[]) val.getValue();
        subjectUniqueId = new BitString(b, 1, b.length-1, b[0] & 0xFF);
        logger.log (Component.X509, "read subjectUniqueId == {0}", subjectUniqueId);
        val = der.read();
      }
    if (version >= 3 && val.getTagClass() != DER.UNIVERSAL && val.getTag() == 3)
      {
        val = der.read();
        logger.log (Component.X509, "start Extensions  len == {0}",
                    Integer.valueOf(val.getLength()));
        int len = 0;
        while (len < val.getLength())
          {
            DERValue ext = der.read();
            logger.log (Component.X509, "start extension  len == {0}",
                        Integer.valueOf(ext.getLength()));
            Extension e = new Extension(ext.getEncoded());
            extensions.put(e.getOid(), e);
            der.skip(ext.getLength());
            len += ext.getEncodedLength();
            logger.log (Component.X509, "read extension {0} == {1}",
                        new Object[] { e.getOid (), e });
            logger.log (Component.X509, "count == {0}", Integer.valueOf(len));
          }
View Full Code Here

  // -------------------------------------------------------------------------

  private void parse(int version, DERReader der) throws Exception
  {
    // RevokedCertificate ::= SEQUENCE {
    DERValue entry = der.read();
    if (Configuration.DEBUG)
      log.fine("start CRL entry   len == " + entry.getLength());
    if (!entry.isConstructed())
      throw new IOException("malformed revokedCertificate");
    encoded = entry.getEncoded();
    int len = 0;
    if (Configuration.DEBUG)
      log.fine("encoded entry:\n" + Util.hexDump(encoded, ">>>> "));

    //   userCertificate   CertificateSerialNumber,
    DERValue val = der.read();
    serialNo = (BigInteger) val.getValue();
    len += val.getEncodedLength();
    if (Configuration.DEBUG)
      log.fine("userCertificate == " + serialNo + "  current count == " + len);

    //   revocationDate   Time,
    val = der.read();
    revocationDate = (Date) val.getValue();
    len += val.getEncodedLength();
    if (Configuration.DEBUG)
      log.fine("revocationDate == " + revocationDate + "  current count == "
               + len);
    //   crlEntryExtensions   Extensions OPTIONAL
    //                          -- if present MUST be v2
    if (len < entry.getLength())
      {
        if (version < 2)
          throw new IOException("extra data in CRL entry");
        DERValue exts = der.read();
        if (!exts.isConstructed())
          throw new IOException("malformed Extensions");
        if (Configuration.DEBUG)
          log.fine("start Extensions  len == " + exts.getLength());
        len = 0;
        while (len < exts.getLength())
          {
            val = der.read();
            if (!val.isConstructed())
              throw new IOException("malformed Extension");
            if (Configuration.DEBUG)
View Full Code Here

  private void parse(InputStream in) throws Exception
  {
    // CertificateList ::= SEQUENCE {
    DERReader der = new DERReader(in);
    DERValue val = der.read();
    if (Configuration.DEBUG)
      log.fine("start CertificateList len == " + val.getLength());
    if (!val.isConstructed())
      throw new IOException("malformed CertificateList");
    encoded = val.getEncoded();

    //   tbsCertList ::= SEQUENCE {  -- TBSCertList
    val = der.read();
    if (!val.isConstructed())
      throw new IOException("malformed TBSCertList");
    if (Configuration.DEBUG)
      log.fine("start tbsCertList  len == " + val.getLength());
    tbsCRLBytes = val.getEncoded();

    //     version    Version OPTIONAL,
    //                  -- If present must be v2
    val = der.read();
    if (val.getValue() instanceof BigInteger)
      {
        version = ((BigInteger) val.getValue()).intValue() + 1;
        val = der.read();
      }
    else
      version = 1;
    if (Configuration.DEBUG)
      log.fine("read version == " + version);

    //     signature   AlgorithmIdentifier,
    if (Configuration.DEBUG)
      log.fine("start AlgorithmIdentifier len == " + val.getLength());
    if (!val.isConstructed())
      throw new IOException("malformed AlgorithmIdentifier");
    DERValue algIdVal = der.read();
    algId = (OID) algIdVal.getValue();
    if (Configuration.DEBUG)
      log.fine("read object identifier == " + algId);
    if (val.getLength() > algIdVal.getEncodedLength())
      {
        val = der.read();
        if (Configuration.DEBUG)
          log.fine("read parameters  len == " + val.getEncodedLength());
        algParams = val.getEncoded();
        if (val.isConstructed())
          in.skip(val.getLength());
      }

    //     issuer   Name,
    val = der.read();
    issuerDN = new X500DistinguishedName(val.getEncoded());
    der.skip(val.getLength());
    if (Configuration.DEBUG)
      log.fine("read issuer == " + issuerDN);

    //     thisUpdate   Time,
    thisUpdate = (Date) der.read().getValue();
    if (Configuration.DEBUG)
      log.fine("read thisUpdate == " + thisUpdate);

    //     nextUpdate   Time OPTIONAL,
    val = der.read();
    if (val.getValue() instanceof Date)
      {
        nextUpdate = (Date) val.getValue();
        if (Configuration.DEBUG)
          log.fine("read nextUpdate == " + nextUpdate);
        val = der.read();
      }

    //     revokedCertificates SEQUENCE OF SEQUENCE {
    //       -- X509CRLEntry objects...
    //     } OPTIONAL,
    if (val.getTag() != 0)
      {
        int len = 0;
        while (len < val.getLength())
          {
            X509CRLEntry entry = new X509CRLEntry(version, der);
            revokedCerts.put(entry.getSerialNumber(), entry);
            len += entry.getEncoded().length;
          }
        val = der.read();
      }

    //    crlExtensions   [0] EXPLICIT Extensions OPTIONAL
    //                        -- if present MUST be v2
    if (val.getTagClass() != DER.UNIVERSAL && val.getTag() == 0)
      {
        if (version < 2)
          throw new IOException("extra data in CRL");
        DERValue exts = der.read();
        if (!exts.isConstructed())
          throw new IOException("malformed Extensions");
        if (Configuration.DEBUG)
          log.fine("start Extensions  len == " + exts.getLength());
        int len = 0;
        while (len < exts.getLength())
          {
            DERValue ext = der.read();
            if (!ext.isConstructed())
              throw new IOException("malformed Extension");
            Extension e = new Extension(ext.getEncoded());
            extensions.put(e.getOid(), e);
            der.skip(ext.getLength());
            len += ext.getEncodedLength();
            if (Configuration.DEBUG)
              log.fine("current count == " + len);
          }
        val = der.read();
      }

    if (Configuration.DEBUG)
      log.fine("read tag == " + val.getTag());
    if (!val.isConstructed())
      throw new IOException("malformed AlgorithmIdentifier");
    if (Configuration.DEBUG)
      log.fine("start AlgorithmIdentifier  len == " + val.getLength());
    DERValue sigAlgVal = der.read();
    if (Configuration.DEBUG)
      log.fine("read tag == " + sigAlgVal.getTag());
    if (sigAlgVal.getTag() != DER.OBJECT_IDENTIFIER)
      throw new IOException("malformed AlgorithmIdentifier");
    sigAlg = (OID) sigAlgVal.getValue();
    if (Configuration.DEBUG)
      {
        log.fine("signature id == " + sigAlg);
        log.fine("sigAlgVal length == " + sigAlgVal.getEncodedLength());
      }
    if (val.getLength() > sigAlgVal.getEncodedLength())
      {
        val = der.read();
        if (Configuration.DEBUG)
          log.fine("sig params tag = " + val.getTag() + " len == "
                   + val.getEncodedLength());
View Full Code Here

  public byte[] encodePrivateKey(PrivateKey key)
  {
    if (! (key instanceof DSSPrivateKey))
      throw new InvalidParameterException("Wrong key type");

    DERValue derVersion = new DERValue(DER.INTEGER, BigInteger.ZERO);

    DERValue derOID = new DERValue(DER.OBJECT_IDENTIFIER, DSA_ALG_OID);

    DSSPrivateKey pk = (DSSPrivateKey) key;
    BigInteger p = pk.getParams().getP();
    BigInteger q = pk.getParams().getQ();
    BigInteger g = pk.getParams().getG();
    BigInteger x = pk.getX();

    ArrayList params = new ArrayList(3);
    params.add(new DERValue(DER.INTEGER, p));
    params.add(new DERValue(DER.INTEGER, q));
    params.add(new DERValue(DER.INTEGER, g));
    DERValue derParams = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, params);

    ArrayList algorithmID = new ArrayList(2);
    algorithmID.add(derOID);
    algorithmID.add(derParams);
    DERValue derAlgorithmID = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
                                           algorithmID);

    // The OCTET STRING is the DER encoding of an INTEGER.
    DERValue derX = new DERValue(DER.INTEGER, x);
    DERValue derPrivateKey = new DERValue(DER.OCTET_STRING, derX.getEncoded());

    ArrayList pki = new ArrayList(3);
    pki.add(derVersion);
    pki.add(derAlgorithmID);
    pki.add(derPrivateKey);
    DERValue derPKI = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, pki);

    byte[] result;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try
      {
View Full Code Here

    BigInteger version, p, q, g, x;
    DERReader der = new DERReader(input);
    try
      {
        DERValue derPKI = der.read();
        DerUtil.checkIsConstructed(derPKI, "Wrong PrivateKeyInfo field");

        DERValue derVersion = der.read();
        if (! (derVersion.getValue() instanceof BigInteger))
          throw new InvalidParameterException("Wrong Version field");

        version = (BigInteger) derVersion.getValue();
        if (version.compareTo(BigInteger.ZERO) != 0)
          throw new InvalidParameterException("Unexpected Version: " + version);

        DERValue derAlgoritmID = der.read();
        DerUtil.checkIsConstructed(derAlgoritmID, "Wrong AlgorithmIdentifier field");

        DERValue derOID = der.read();
        OID algOID = (OID) derOID.getValue();
        if (! algOID.equals(DSA_ALG_OID))
          throw new InvalidParameterException("Unexpected OID: " + algOID);

        DERValue derParams = der.read();
        DerUtil.checkIsConstructed(derParams, "Wrong DSS Parameters field");

        DERValue val = der.read();
        DerUtil.checkIsBigInteger(val, "Wrong P field");
        p = (BigInteger) val.getValue();
        val = der.read();
        DerUtil.checkIsBigInteger(val, "Wrong Q field");
        q = (BigInteger) val.getValue();
        val = der.read();
        DerUtil.checkIsBigInteger(val, "Wrong G field");
        g = (BigInteger) val.getValue();

        val = der.read();
        if (Configuration.DEBUG)
          log.fine("val = " + val);
        byte[] xBytes = (byte[]) val.getValue();
        if (Configuration.DEBUG)
          log.fine(Util.dumpString(xBytes, "xBytes: "));
        DERReader der2 = new DERReader(xBytes);
        val = der2.read();
        DerUtil.checkIsBigInteger(val, "Wrong X field");
        x = (BigInteger) val.getValue();
      }
    catch (IOException e)
      {
        InvalidParameterException y = new InvalidParameterException(e.getMessage());
        y.initCause(e);
View Full Code Here

    if (Configuration.DEBUG)
      log.entering(this.getClass().getName(), "encodePublicKey()", key);
    if (! (key instanceof GnuRSAPublicKey))
      throw new InvalidParameterException("key");

    DERValue derOID = new DERValue(DER.OBJECT_IDENTIFIER, RSA_ALG_OID);

    GnuRSAPublicKey rsaKey = (GnuRSAPublicKey) key;
    BigInteger n = rsaKey.getN();
    BigInteger e = rsaKey.getE();

    DERValue derN = new DERValue(DER.INTEGER, n);
    DERValue derE = new DERValue(DER.INTEGER, e);

    ArrayList algorithmID = new ArrayList(2);
    algorithmID.add(derOID);
    algorithmID.add(new DERValue(DER.NULL, null));
    DERValue derAlgorithmID = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
                                           algorithmID);

    ArrayList publicKey = new ArrayList(2);
    publicKey.add(derN);
    publicKey.add(derE);
    DERValue derPublicKey = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
                                         publicKey);
    byte[] spkBytes = derPublicKey.getEncoded();
    DERValue derSPK = new DERValue(DER.BIT_STRING, new BitString(spkBytes));

    ArrayList spki = new ArrayList(2);
    spki.add(derAlgorithmID);
    spki.add(derSPK);
    DERValue derSPKI = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, spki);

    byte[] result;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try
      {
View Full Code Here

TOP

Related Classes of gnu.java.security.der.DERValue

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.