Package org.apache.commons.ssl.asn1

Examples of org.apache.commons.ssl.asn1.DERInteger


            version = DERInteger.getInstance(seq.getObjectAt(0));
        }
        else
        {
            seqStart = -1;          // field 0 is missing!
            version = new DERInteger(0);
        }

        serialNumber = DERInteger.getInstance(seq.getObjectAt(seqStart + 1));

        signature = AlgorithmIdentifier.getInstance(seq.getObjectAt(seqStart + 2));
View Full Code Here


     */
    public DERObject getDERObject()
    {
        ASN1EncodableVector  v = new ASN1EncodableVector();

        v.add(new DERInteger(getModulus()));
        v.add(new DERInteger(getPublicExponent()));

        return new DERSequence(v);
    }
View Full Code Here

        int     pathLenConstraint)
    {
      if (cA )
      {
          this.cA = new DERBoolean(cA);
          this.pathLenConstraint = new DERInteger(pathLenConstraint);
      }
      else
      {
        this.cA = null;
        this.pathLenConstraint = null;
View Full Code Here

   */
  public BasicConstraints(
    int     pathLenConstraint)
  {
    this.cA = new DERBoolean(true);
    this.pathLenConstraint = new DERInteger(pathLenConstraint);
  }
View Full Code Here

            PrivateKey key = (PrivateKey) ks.getKey(alias, password);
            Certificate[] chain = ks.getCertificateChain(alias);
            byte[] pkcs8DerBytes = null;
            if (key instanceof RSAPrivateCrtKey) {
                RSAPrivateCrtKey rsa = (RSAPrivateCrtKey) key;
                ASN1EncodableVector vec = new ASN1EncodableVector();
                vec.add(new DERInteger(BigInteger.ZERO));
                vec.add(new DERInteger(rsa.getModulus()));
                vec.add(new DERInteger(rsa.getPublicExponent()));
                vec.add(new DERInteger(rsa.getPrivateExponent()));
                vec.add(new DERInteger(rsa.getPrimeP()));
                vec.add(new DERInteger(rsa.getPrimeQ()));
                vec.add(new DERInteger(rsa.getPrimeExponentP()));
                vec.add(new DERInteger(rsa.getPrimeExponentQ()));
                vec.add(new DERInteger(rsa.getCrtCoefficient()));
                DERSequence seq = new DERSequence(vec);
                byte[] derBytes = PKCS8Key.encode(seq);
                PKCS8Key pkcs8 = new PKCS8Key(derBytes, null);
                pkcs8DerBytes = pkcs8.getDecryptedBytes();
            } else if (key instanceof DSAPrivateKey) {
                DSAPrivateKey dsa = (DSAPrivateKey) key;
                DSAParams params = dsa.getParams();
                BigInteger g = params.getG();
                BigInteger p = params.getP();
                BigInteger q = params.getQ();
                BigInteger x = dsa.getX();
                BigInteger y = q.modPow(x, p);

                ASN1EncodableVector vec = new ASN1EncodableVector();
                vec.add(new DERInteger(BigInteger.ZERO));
                vec.add(new DERInteger(p));
                vec.add(new DERInteger(q));
                vec.add(new DERInteger(g));
                vec.add(new DERInteger(y));
                vec.add(new DERInteger(x));
                DERSequence seq = new DERSequence(vec);
                byte[] derBytes = PKCS8Key.encode(seq);
                PKCS8Key pkcs8 = new PKCS8Key(derBytes, null);
                pkcs8DerBytes = pkcs8.getDecryptedBytes();
            }
View Full Code Here

    }

    public static byte[] formatAsPKCS8(byte[] privateKey, String oid,
                                       ASN1Structure pkcs8) {
        DERInteger derZero = new DERInteger(BigInteger.ZERO);
        ASN1EncodableVector outterVec = new ASN1EncodableVector();
        ASN1EncodableVector innerVec = new ASN1EncodableVector();
        DEROctetString octetsToAppend;
        try {
            DERObjectIdentifier derOID = new DERObjectIdentifier(oid);
            innerVec.add(derOID);
            if (DSA_OID.equals(oid)) {
                if (pkcs8 == null) {
                    try {
                        pkcs8 = ASN1Util.analyze(privateKey);
                    }
                    catch (Exception e) {
                        throw new RuntimeException("asn1 parse failure " + e);
                    }
                }
                if (pkcs8.derIntegers == null || pkcs8.derIntegers.size() < 6) {
                    throw new RuntimeException("invalid DSA key - can't find P, Q, G, X");
                }

                DERInteger[] ints = new DERInteger[pkcs8.derIntegers.size()];
                pkcs8.derIntegers.toArray(ints);
                DERInteger p = ints[1];
                DERInteger q = ints[2];
                DERInteger g = ints[3];
                DERInteger x = ints[5];

                byte[] encodedX = encode(x);
                octetsToAppend = new DEROctetString(encodedX);
                ASN1EncodableVector pqgVec = new ASN1EncodableVector();
                pqgVec.add(p);
                pqgVec.add(q);
                pqgVec.add(g);
                DERSequence pqg = new DERSequence(pqgVec);
                innerVec.add(pqg);
            } else {
                innerVec.add(DERNull.INSTANCE);
                octetsToAppend = new DEROctetString(privateKey);
View Full Code Here

    public final static BigInteger BIGGEST =
        new BigInteger(Integer.toString(Integer.MAX_VALUE));

    public static ASN1Structure analyze(byte[] asn1)
        throws IOException {
        ASN1InputStream asn = new ASN1InputStream(asn1);
        DERSequence seq = (DERSequence) asn.readObject();
        ASN1Structure pkcs8 = new ASN1Structure();
        ASN1Util.analyze(seq, pkcs8, 0);
        return pkcs8;
    }
View Full Code Here

        return true;
    }

    public static byte[] encode(DEREncodable der) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
        ASN1OutputStream out = new ASN1OutputStream(baos);
        out.writeObject(der);
        out.close();
        return baos.toByteArray();
    }
View Full Code Here

            en = v.elements();
        } else {
            throw new IllegalArgumentException("DEREncodable must be one of: DERSequence, DERSet, DERTaggedObject");
        }
        while (en != null && en.hasMoreElements()) {
            DEREncodable obj = (DEREncodable) en.nextElement();
            if (!(obj instanceof DERSequence) &&
                !(obj instanceof DERSet) &&
                !(obj instanceof DERTaggedObject)) {
                String str = obj.toString();
                String name = obj.getClass().getName();
                name = name.substring(name.lastIndexOf('.') + 1);
                if (tag != null) {
                    name = " [tag=" + tag + "] " + name;
                }
                for (int i = 0; i < depth; i++) {
                    name = "  " + name;
                }
                if (obj instanceof DERInteger) {
                    DERInteger dInt = (DERInteger) obj;
                    if (pkcs8.derIntegers != null) {
                        pkcs8.derIntegers.add(dInt);
                    }
                    BigInteger big = dInt.getValue();
                    int intValue = big.intValue();
                    if (BIGGEST.compareTo(big) >= 0 && intValue > 0) {
                        if (pkcs8.iterationCount == 0) {
                            pkcs8.iterationCount = intValue;
                        } else if (pkcs8.keySize == 0) {
                            pkcs8.keySize = intValue;
                        }
                    }
                    str = dInt.getValue().toString();
                } else if (obj instanceof DERObjectIdentifier) {
                    DERObjectIdentifier id = (DERObjectIdentifier) obj;
                    str = id.getId();
                    pkcs8.oids.add(str);
                    if (pkcs8.oid1 == null) {
                        pkcs8.oid1 = str;
                    } else if (pkcs8.oid2 == null) {
                        pkcs8.oid2 = str;
                    } else if (pkcs8.oid3 == null) {
                        pkcs8.oid3 = str;
                    }
                } else {
                    pkcs8.derIntegers = null;
                    if (obj instanceof DEROctetString) {
                        DEROctetString oct = (DEROctetString) obj;
                        byte[] octets = oct.getOctets();
                        int len = Math.min(10, octets.length);
                        boolean probablyBinary = false;
                        for (int i = 0; i < len; i++) {
                            byte b = octets[i];
                            boolean isBinary = b > 128 || b < 0;
                            if (isBinary) {
                                probablyBinary = true;
                                break;
                            }
                        }
                        if (probablyBinary && octets.length > 64) {
                            if (pkcs8.bigPayload == null) {
                                pkcs8.bigPayload = octets;
                            }
                            str = "probably binary";
                        } else {
                            str = Hex.encode(octets);
                            if (octets.length <= 64) {
                                if (octets.length % 8 == 0) {
                                    if (pkcs8.salt == null) {
                                        pkcs8.salt = octets;
                                    } else if (pkcs8.iv == null) {
                                        pkcs8.iv = octets;
                                    }
                                } else {
                                    if (pkcs8.smallPayload == null) {
                                        pkcs8.smallPayload = octets;
                                    }
                                }
                            }
                        }
                        str += " (length=" + octets.length + ")";
                    } else if (obj instanceof DERPrintableString) {
                        DERPrintableString dps = (DERPrintableString) obj;
                        str = dps.getString();
                    }
                }

                if (DEBUG) {
                    System.out.println(name + ": [" + str + "]");
                }
            } else {
                if (tag != null && DEBUG) {
                    String name = obj.getClass().getName();
                    name = name.substring(name.lastIndexOf('.') + 1);
                    name = " [tag=" + tag + "] " + name;
                    for (int i = 0; i < depth; i++) {
                        name = "  " + name;
                    }
View Full Code Here

                }
                for (int i = 0; i < depth; i++) {
                    name = "  " + name;
                }
                if (obj instanceof DERInteger) {
                    DERInteger dInt = (DERInteger) obj;
                    if (pkcs8.derIntegers != null) {
                        pkcs8.derIntegers.add(dInt);
                    }
                    BigInteger big = dInt.getValue();
                    int intValue = big.intValue();
                    if (BIGGEST.compareTo(big) >= 0 && intValue > 0) {
                        if (pkcs8.iterationCount == 0) {
                            pkcs8.iterationCount = intValue;
                        } else if (pkcs8.keySize == 0) {
                            pkcs8.keySize = intValue;
                        }
                    }
                    str = dInt.getValue().toString();
                } else if (obj instanceof DERObjectIdentifier) {
                    DERObjectIdentifier id = (DERObjectIdentifier) obj;
                    str = id.getId();
                    pkcs8.oids.add(str);
                    if (pkcs8.oid1 == null) {
View Full Code Here

TOP

Related Classes of org.apache.commons.ssl.asn1.DERInteger

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.