Package gnu.java.security.der

Examples of gnu.java.security.der.DERValue


    BigInteger n, e;
    DERReader der = new DERReader(input);
    try
      {
        DERValue derSPKI = der.read();
        DerUtil.checkIsConstructed(derSPKI, "Wrong SubjectPublicKeyInfo field");

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

        DERValue derOID = der.read();
        if (! (derOID.getValue() instanceof OID))
          throw new InvalidParameterException("Wrong Algorithm field");

        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();

        if (! (val.getValue() instanceof BitString))
          throw new InvalidParameterException("Wrong SubjectPublicKey field");

        byte[] spkBytes = ((BitString) val.getValue()).toByteArray();

        der = new DERReader(spkBytes);
        val = der.read();
        DerUtil.checkIsConstructed(derAlgorithmID, "Wrong subjectPublicKey field");

        val = der.read();
        DerUtil.checkIsBigInteger(val, "Wrong modulus field");
        n = (BigInteger) val.getValue();
        val = der.read();
        DerUtil.checkIsBigInteger(val, "Wrong publicExponent field");
        e = (BigInteger) val.getValue();
      }
    catch (IOException x)
      {
        InvalidParameterException y = new InvalidParameterException(x.getMessage());
        y.initCause(x);
View Full Code Here


   * EncryptedDigest ::= OCTET STRING
   * </pre>
   */
  public SignerInfo(BERReader ber) throws IOException
  {
    DERValue val = ber.read();
    if (Configuration.DEBUG)
      log.fine("SignerInfo: " + val);
    if (!val.isConstructed())
      throw new BEREncodingException("malformed SignerInfo");

    val = ber.read();
    if (val.getTag() != BER.INTEGER)
      throw new BEREncodingException("malformed Version");

    version = (BigInteger) val.getValue();
    if (Configuration.DEBUG)
      log.fine("  Version: " + version);

    val = ber.read();
    if (!val.isConstructed())
      throw new BEREncodingException("malformed IssuerAndSerialNumber");
    if (Configuration.DEBUG)
      log.fine("  IssuerAndSerialNumber: " + val);

    val = ber.read();
    if (!val.isConstructed())
      throw new BEREncodingException("malformed Issuer");

    issuer = new X500Principal(val.getEncoded());
    ber.skip(val.getLength());
    if (Configuration.DEBUG)
      log.fine("    Issuer: " + issuer);

    val = ber.read();
    if (val.getTag() != BER.INTEGER)
      throw new BEREncodingException("malformed SerialNumber");

    serialNumber = (BigInteger) val.getValue();
    if (Configuration.DEBUG)
      log.fine("    SerialNumber: " + serialNumber);

    val = ber.read();
    if (!val.isConstructed())
      throw new BEREncodingException("malformed DigestAlgorithmIdentifier");
    if (Configuration.DEBUG)
      log.fine("  DigestAlgorithmIdentifier: " + val);

    int count = 0;
    DERValue val2 = ber.read();
    if (val2.getTag() != BER.OBJECT_IDENTIFIER)
      throw new BEREncodingException("malformed AlgorithmIdentifier");

    digestAlgorithmId = (OID) val2.getValue();
    if (Configuration.DEBUG)
      log.fine("    digestAlgorithm OID: " + digestAlgorithmId);

    if (BERValue.isIndefinite(val))
      {
        val2 = ber.read();
        if (val2 != BER.END_OF_SEQUENCE)
          {
            digestAlgorithmParams = val2.getEncoded();
            val2 = ber.read();
            if (val2 != BER.END_OF_SEQUENCE)
              throw new BEREncodingException("expecting BER end-of-sequence");
          }
        else
          digestAlgorithmParams = null;
      }
    else if (val2.getEncodedLength() < val.getLength())
      {
        val2 = ber.read();
        digestAlgorithmParams = val2.getEncoded();
        if (val2.isConstructed())
          ber.skip(val2.getLength());
      }
    else
      digestAlgorithmParams = null;

    if (Configuration.DEBUG)
      {
        log.fine("    digestAlgorithm params: ");
        log.fine(Util.dumpString(digestAlgorithmParams,
                                 "    digestAlgorithm params: "));
      }
    val = ber.read();
    if (val.getTag() == 0)
      {
        authenticatedAttributes = val.getEncoded();
        val = ber.read();
        if (val.isConstructed())
          ber.skip(val.getLength());

        val = ber.read();
      }
    else
      authenticatedAttributes = null;

    if (Configuration.DEBUG)
      {
        log.fine("  AuthenticatedAttributes: ");
        log.fine(Util.dumpString(authenticatedAttributes,
                                 "  AuthenticatedAttributes: "));
      }
    if (!val.isConstructed())
      throw new BEREncodingException("malformed DigestEncryptionAlgorithmIdentifier");
    if (Configuration.DEBUG)
      log.fine("  DigestEncryptionAlgorithmIdentifier: " + val);
    count = 0;
    val2 = ber.read();
    if (val2.getTag() != BER.OBJECT_IDENTIFIER)
      throw new BEREncodingException("malformed AlgorithmIdentifier");

    digestEncryptionAlgorithmId = (OID) val2.getValue();
    if (Configuration.DEBUG)
      log.fine("    digestEncryptionAlgorithm OID: " + digestEncryptionAlgorithmId);

    if (BERValue.isIndefinite(val))
      {
        val2 = ber.read();
        if (val2 != BER.END_OF_SEQUENCE)
          {
            digestEncryptionAlgorithmParams = val2.getEncoded();
            val2 = ber.read();
            if (val2 != BER.END_OF_SEQUENCE)
              throw new BEREncodingException("expecting BER end-of-sequence");
          }
        else
          digestEncryptionAlgorithmParams = null;
      }
    else if (val2.getEncodedLength() < val.getLength())
      {
        val2 = ber.read();
        digestEncryptionAlgorithmParams = val2.getEncoded();
        if (val2.isConstructed())
          ber.skip(val2.getLength());
      }
    else
      digestEncryptionAlgorithmParams = null;

    if (Configuration.DEBUG)
View Full Code Here

   * @param out the destination output stream.
   * @throws IOException if an I/O related exception occurs during the process.
   */
  public void encode(OutputStream out) throws IOException
  {
    DERValue derVersion = new DERValue(DER.INTEGER, version);

    ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
    baos.write(issuer.getEncoded());
    DERValue derSerialNumber = new DERValue(DER.INTEGER, serialNumber);
    DERWriter.write(baos, derSerialNumber);
    baos.flush();
    byte[] b = baos.toByteArray();
    DERValue derIssuerAndSerialNumber =
        new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, b.length, b, null);

    DERValue derDigestAlgorithmOID = new DERValue(DER.OBJECT_IDENTIFIER,
                                                  digestAlgorithmId);
    ArrayList digestAlgorithmIdentifier = new ArrayList(1);
    digestAlgorithmIdentifier.add(derDigestAlgorithmOID);
    DERValue derDigestAlgorithmIdentifier =
        new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, digestAlgorithmIdentifier);

    DERValue derAuthenticatedAttributes;
    if (authenticatedAttributes == null)
      derAuthenticatedAttributes = new DERValue(DER.NULL, null);
    else
      derAuthenticatedAttributes = new DERValue(DER.CONSTRUCTED | DER.SET,
                                                authenticatedAttributes);

    DERValue derDigestEncryptionAlgorithmOID =
        new DERValue(DER.OBJECT_IDENTIFIER, digestEncryptionAlgorithmId);
    ArrayList digestEncryptionAlgorithmIdentifier = new ArrayList(1);
    digestEncryptionAlgorithmIdentifier.add(derDigestEncryptionAlgorithmOID);
    DERValue derDigestEncryptionAlgorithmIdentifier =
        new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, digestEncryptionAlgorithmIdentifier);

    DERValue derEncryptedDigest = new DERValue(DER.OCTET_STRING, encryptedDigest);

    DERValue derUnauthenticatedAttributes;
    if (unauthenticatedAttributes == null)
      derUnauthenticatedAttributes = new DERValue(DER.NULL, null);
    else
      derUnauthenticatedAttributes = new DERValue(DER.CONSTRUCTED | DER.SET,
                                                  unauthenticatedAttributes);

    ArrayList signerInfo = new ArrayList(5);
    signerInfo.add(derVersion);
    signerInfo.add(derIssuerAndSerialNumber);
    signerInfo.add(derDigestAlgorithmIdentifier);
    signerInfo.add(derDigestEncryptionAlgorithmIdentifier);
    signerInfo.add(derEncryptedDigest);
    DERValue derSignerInfo = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
                                          signerInfo);
    DERWriter.write(out, derSignerInfo);
  }
View Full Code Here

   */
  public PKCS7SignedData(BERReader ber)
    throws CRLException, CertificateException, IOException
  {
    CertificateFactory x509 = CertificateFactory.getInstance("X509");
    DERValue val = ber.read();
    if (!val.isConstructed())
      throw new BEREncodingException("malformed ContentInfo");

    val = ber.read();
    if (val.getTag() != BER.OBJECT_IDENTIFIER)
      throw new BEREncodingException("malformed ContentType");

    if (!PKCS7_SIGNED_DATA.equals(val.getValue()))
      throw new BEREncodingException("content is not SignedData");

    val = ber.read();
    if (val.getTag() != 0)
      throw new BEREncodingException("malformed Content");

    val = ber.read();
    if (!val.isConstructed())
      throw new BEREncodingException("malformed SignedData");

    if (Configuration.DEBUG)
      log.fine("SignedData: " + val);

    val = ber.read();
    if (val.getTag() != BER.INTEGER)
      throw new BEREncodingException("expecting Version");
    version = (BigInteger) val.getValue();
    if (Configuration.DEBUG)
      log.fine("  Version: " + version);

    digestAlgorithms = new HashSet();
    val = ber.read();
    if (!val.isConstructed())
      throw new BEREncodingException("malformed DigestAlgorithmIdentifiers");
    if (Configuration.DEBUG)
      log.fine("  DigestAlgorithmIdentifiers: " + val);
    int count = 0;
    DERValue val2 = ber.read();
    while (val2 != BER.END_OF_SEQUENCE &&
           (val.getLength() > 0 && val.getLength() > count))
      {
        if (!val2.isConstructed())
          throw new BEREncodingException("malformed AlgorithmIdentifier");
        if (Configuration.DEBUG)
          log.fine("    AlgorithmIdentifier: " + val2);
        count += val2.getEncodedLength();
        val2 = ber.read();
        if (val2.getTag() != BER.OBJECT_IDENTIFIER)
          throw new BEREncodingException("malformed AlgorithmIdentifier");
        if (Configuration.DEBUG)
          log.fine("      digestAlgorithmIdentifiers OID: " + val2.getValue());
        List algId = new ArrayList(2);
        algId.add(val2.getValue());
        val2 = ber.read();
        if (val2 != BER.END_OF_SEQUENCE)
          {
            count += val2.getEncodedLength();
            if (val2.getTag() == BER.NULL)
              algId.add(null);
            else
              algId.add(val2.getEncoded());

            if (val2.isConstructed())
              ber.skip(val2.getLength());

            if (BERValue.isIndefinite(val))
              val2 = ber.read();
          }
        else
          algId.add(null);

        if (Configuration.DEBUG)
          {
            log.fine("      digestAlgorithmIdentifiers params: ");
            log.fine(Util.dumpString((byte[]) algId.get(1),
                                     "      digestAlgorithmIdentifiers params: "));
          }
        digestAlgorithms.add(algId);
      }

    val = ber.read();
    if (!val.isConstructed())
      throw new BEREncodingException("malformed ContentInfo");
    if (Configuration.DEBUG)
      log.fine("  ContentInfo: " + val);
    val2 = ber.read();
    if (val2.getTag() != BER.OBJECT_IDENTIFIER)
      throw new BEREncodingException("malformed ContentType");

    contentType = (OID) val2.getValue();
    if (Configuration.DEBUG)
      log.fine("    ContentType OID: " + contentType);
    if (BERValue.isIndefinite(val)
        || (val.getLength() > 0 && val.getLength() > val2.getEncodedLength()))
      {
        val2 = ber.read();
        if (val2 != BER.END_OF_SEQUENCE)
          {
            content = val2.getEncoded();
            if (BERValue.isIndefinite(val))
              val2 = ber.read();
          }
      }
    if (Configuration.DEBUG)
      {
        log.fine("    Content: ");
        log.fine(Util.dumpString(content, "    Content: "));
      }
    val = ber.read();
    if (val.getTag() == 0)
      {
        if (!val.isConstructed())
          throw new BEREncodingException("malformed ExtendedCertificatesAndCertificates");
        if (Configuration.DEBUG)
          log.fine("  ExtendedCertificatesAndCertificates: " + val);
        count = 0;
        val2 = ber.read();
        List certs = new LinkedList();
        while (val2 != BER.END_OF_SEQUENCE &&
               (val.getLength() > 0 && val.getLength() > count))
          {
            Certificate cert =
              x509.generateCertificate(new ByteArrayInputStream(val2.getEncoded()));
            if (Configuration.DEBUG)
              log.fine("    Certificate: " + cert);
            certs.add(cert);
            count += val2.getEncodedLength();
            ber.skip(val2.getLength());
            if (BERValue.isIndefinite(val) || val.getLength() > count)
              val2 = ber.read();
          }
        certificates = (Certificate[]) certs.toArray(new Certificate[certs.size()]);
        val = ber.read();
      }

    if (val.getTag() == 1)
      {
        if (!val.isConstructed())
          throw new BEREncodingException("malformed CertificateRevocationLists");
        if (Configuration.DEBUG)
          log.fine("  CertificateRevocationLists: " + val);
        count = 0;
        val2 = ber.read();
        List crls = new LinkedList();
        while (val2 != BER.END_OF_SEQUENCE &&
               (val.getLength() > 0 && val.getLength() > count))
          {
            CRL crl = x509.generateCRL(new ByteArrayInputStream(val2.getEncoded()));
            if (Configuration.DEBUG)
              log.fine("    CRL: " + crl);
            crls.add(crl);
            count += val2.getEncodedLength();
            ber.skip(val2.getLength());
            if (BERValue.isIndefinite(val) || val.getLength() > count)
              val2 = ber.read();
          }
        this.crls = (CRL[]) crls.toArray(new CRL[crls.size()]);
        val = ber.read();
View Full Code Here

   * the certificate chains associated with this instance.
   */
  public void encode(OutputStream out) throws IOException, CRLException,
      CertificateEncodingException
  {
    DERValue derVersion = new DERValue(DER.INTEGER, version);

    DERValue derDigestAlgorithms = new DERValue(DER.CONSTRUCTED | DER.SET,
                                                digestAlgorithms);

    DERValue derContentType = new DERValue(DER.OBJECT_IDENTIFIER,
                                           PKCS7Data.PKCS7_DATA);
    ArrayList contentInfo = new ArrayList(2);
    contentInfo.add(derContentType);
    if (content == null)
      contentInfo.add(new DERValue(DER.NULL, null));
    else
      contentInfo.add(content);

    DERValue derContentInfo = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
                                           contentInfo);

    ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
    for (int i = 0; i < certificates.length; i++)
      baos.write(certificates[i].getEncoded());

    baos.flush();
    byte[] b = baos.toByteArray();
    DERValue derExtendedCertificatesAndCertificates =
        new DERValue(DER.CONSTRUCTED | DER.CONTEXT | 0, b.length, b, null);

    DERValue derCertificateRevocationLists = null;
    if (crls != null && crls.length > 0)
      {
        baos.reset();
        for (int i = 0; i < crls.length; i++)
          baos.write(((X509CRL) crls[i]).getEncoded());

        baos.flush();
        byte[] b2 = baos.toByteArray();
        derCertificateRevocationLists =
            new DERValue(DER.CONSTRUCTED | DER.CONTEXT | 1, b2.length, b2, null);
      }

    baos.reset();
    for (Iterator it = signerInfos.iterator(); it.hasNext();)
      {
        SignerInfo signerInfo = (SignerInfo) it.next();
        signerInfo.encode(baos);
      }
    baos.flush();
    byte[] b3 = baos.toByteArray();
    DERValue derSignerInfos = new DERValue(DER.CONSTRUCTED | DER.SET,
                                           b3.length, b3, null);

    ArrayList signedData = new ArrayList(6);
    signedData.add(derVersion);
    signedData.add(derDigestAlgorithms);
    signedData.add(derContentInfo);
    signedData.add(derExtendedCertificatesAndCertificates);
    if (derCertificateRevocationLists != null)
      signedData.add(derCertificateRevocationLists);

    signedData.add(derSignerInfos);
    DERValue derSignedData = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE,
                                          signedData);
    // now the outer contents
    ArrayList outer = new ArrayList(3);
    outer.add(new DERValue(DER.OBJECT_IDENTIFIER, PKCS7_SIGNED_DATA));
    outer.add(new DERValue(DER.CONTEXT | 0, null));
    outer.add(derSignedData);
    DERValue derOuter = new DERValue(DER.CONSTRUCTED | DER.SEQUENCE, outer);

    DERWriter.write(out, derOuter);
  }
View Full Code Here

  {
    if (encoded == null)
      throw new IOException("null bytes");
    this.encoded = (byte[]) encoded.clone();
    DERReader in = new DERReader(new ByteArrayInputStream(this.encoded));
    DERValue qualInfo = in.read();
    if (!qualInfo.isConstructed())
      throw new ASN1ParsingException("malformed PolicyQualifierInfo");
    DERValue val = in.read();
    if (!(val.getValue() instanceof OID))
      throw new ASN1ParsingException("value read not an OBJECT IDENTIFIER");
    oid = (OID) val.getValue();
    if (val.getEncodedLength() < val.getLength())
      qualifier = in.read();
  }
View Full Code Here

        byte[] b = crl.getExtensionValue(CRL_NUMBER_ID);
        if (b == null)
          return false;
        try
          {
            DERValue val = DERReader.read(b);
            if (!(val.getValue() instanceof BigInteger))
              return false;
            crlNumber = (BigInteger) val.getValue();
          }
        catch (IOException ioe)
          {
            return false;
          }
        if (maxCrlNumber.compareTo(crlNumber) < 0)
          return false;
      }
    if (minCrlNumber != null)
      {
        if (crlNumber == null)
          {
            byte[] b = crl.getExtensionValue(CRL_NUMBER_ID);
            if (b == null)
              return false;
            try
              {
                DERValue val = DERReader.read(b);
                if (!(val.getValue() instanceof BigInteger))
                  return false;
                crlNumber = (BigInteger) val.getValue();
              }
            catch (IOException ioe)
              {
                return false;
              }
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.