Search in sources :

Example 71 with DerValue

use of sun.security.util.DerValue in project Bytecoder by mirkosertic.

the class ResponderId method keyIdToBytes.

/**
 * Convert the responderKeyId data member into its DER-encoded form
 *
 * @return the DER encoding for a responder ID byKey option, including
 *      explicit context-specific tagging.
 *
 * @throws IOException if any encoding error occurs
 */
private byte[] keyIdToBytes() throws IOException {
    // Place the KeyIdentifier bytes into an OCTET STRING
    DerValue inner = new DerValue(DerValue.tag_OctetString, responderKeyId.getIdentifier());
    // Mark the OCTET STRING-wrapped KeyIdentifier bytes
    // as EXPLICIT CONTEXT 2
    DerValue outer = new DerValue(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) Type.BY_KEY.value()), inner.toByteArray());
    return outer.toByteArray();
}
Also used : DerValue(sun.security.util.DerValue)

Example 72 with DerValue

use of sun.security.util.DerValue in project dbeaver by serge-rider.

the class PKCS1Util method loadPrivateKeyFromPKCS1.

public static PrivateKey loadPrivateKeyFromPKCS1(String privateKeyPem) throws GeneralSecurityException, IOException {
    DerInputStream derReader = new DerInputStream(Base64.decode(privateKeyPem));
    DerValue[] seq = derReader.getSequence(0);
    if (seq.length < 9) {
        throw new GeneralSecurityException("Could not parse a PKCS1 private key.");
    }
    // skip version seq[0];
    BigInteger modulus = seq[1].getBigInteger();
    BigInteger publicExp = seq[2].getBigInteger();
    BigInteger privateExp = seq[3].getBigInteger();
    BigInteger prime1 = seq[4].getBigInteger();
    BigInteger prime2 = seq[5].getBigInteger();
    BigInteger exp1 = seq[6].getBigInteger();
    BigInteger exp2 = seq[7].getBigInteger();
    BigInteger crtCoef = seq[8].getBigInteger();
    RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef);
    KeyFactory factory = KeyFactory.getInstance("RSA");
    return factory.generatePrivate(keySpec);
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) DerValue(sun.security.util.DerValue) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger) DerInputStream(sun.security.util.DerInputStream) KeyFactory(java.security.KeyFactory)

Example 73 with DerValue

use of sun.security.util.DerValue in project j2objc by google.

the class ESSCertId method parse.

public void parse(byte[] bytes) throws IOException {
    // Parse signingCertificate
    DerValue derValue = new DerValue(bytes);
    if (derValue.tag != DerValue.tag_Sequence) {
        throw new IOException("Bad encoding for signingCertificate");
    }
    // Parse certs
    DerValue[] certs = derValue.data.getSequence(1);
    certId = new ESSCertId[certs.length];
    for (int i = 0; i < certs.length; i++) {
        certId[i] = new ESSCertId(certs[i]);
    }
    // Parse policies, if present
    if (derValue.data.available() > 0) {
        DerValue[] policies = derValue.data.getSequence(1);
        for (int i = 0; i < policies.length; i++) {
        // parse PolicyInformation
        }
    }
}
Also used : DerValue(sun.security.util.DerValue) IOException(java.io.IOException)

Example 74 with DerValue

use of sun.security.util.DerValue in project j2objc by google.

the class PKCS9Attributes method decode.

/**
 * Decode this set of PKCS9 attributes from the contents of its
 * DER encoding. Ignores unsupported attributes when directed.
 *
 * @param in
 * the contents of the DER encoding of the attribute set.
 *
 * @exception IOException
 * on i/o error, encoding syntax error, unacceptable or
 * unsupported attribute, or duplicate attribute.
 */
private byte[] decode(DerInputStream in) throws IOException {
    DerValue val = in.getDerValue();
    // save the DER encoding with its proper tag byte.
    byte[] derEncoding = val.toByteArray();
    derEncoding[0] = DerValue.tag_SetOf;
    DerInputStream derIn = new DerInputStream(derEncoding);
    DerValue[] derVals = derIn.getSet(3, true);
    PKCS9Attribute attrib;
    ObjectIdentifier oid;
    boolean reuseEncoding = true;
    for (int i = 0; i < derVals.length; i++) {
        try {
            attrib = new PKCS9Attribute(derVals[i]);
        } catch (ParsingException e) {
            if (ignoreUnsupportedAttributes) {
                // cannot reuse supplied DER encoding
                reuseEncoding = false;
                // skip
                continue;
            } else {
                throw e;
            }
        }
        oid = attrib.getOID();
        if (attributes.get(oid) != null)
            throw new IOException("Duplicate PKCS9 attribute: " + oid);
        if (permittedAttributes != null && !permittedAttributes.containsKey(oid))
            throw new IOException("Attribute " + oid + " not permitted in this attribute set");
        attributes.put(oid, attrib);
    }
    return reuseEncoding ? derEncoding : generateDerEncoding();
}
Also used : DerValue(sun.security.util.DerValue) DerInputStream(sun.security.util.DerInputStream) IOException(java.io.IOException) ObjectIdentifier(sun.security.util.ObjectIdentifier)

Example 75 with DerValue

use of sun.security.util.DerValue in project j2objc by google.

the class X509CertPath method parsePKIPATH.

/**
 * Parse a PKIPATH format CertPath from an InputStream. Return an
 * unmodifiable List of the certificates.
 *
 * @param is the <code>InputStream</code> to read the data from
 * @return an unmodifiable List of the certificates
 * @exception CertificateException if an exception occurs
 */
private static List<X509Certificate> parsePKIPATH(InputStream is) throws CertificateException {
    List<X509Certificate> certList = null;
    CertificateFactory certFac = null;
    if (is == null) {
        throw new CertificateException("input stream is null");
    }
    try {
        DerInputStream dis = new DerInputStream(readAllBytes(is));
        DerValue[] seq = dis.getSequence(3);
        if (seq.length == 0) {
            return Collections.<X509Certificate>emptyList();
        }
        certFac = CertificateFactory.getInstance("X.509");
        certList = new ArrayList<X509Certificate>(seq.length);
        // append certs in reverse order (target to trust anchor)
        for (int i = seq.length - 1; i >= 0; i--) {
            certList.add((X509Certificate) certFac.generateCertificate(new ByteArrayInputStream(seq[i].toByteArray())));
        }
        return Collections.unmodifiableList(certList);
    } catch (IOException ioe) {
        throw new CertificateException("IOException parsing PkiPath data: " + ioe, ioe);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DerValue(sun.security.util.DerValue) CertificateException(java.security.cert.CertificateException) DerInputStream(sun.security.util.DerInputStream) IOException(java.io.IOException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Aggregations

DerValue (sun.security.util.DerValue)76 IOException (java.io.IOException)30 DerInputStream (sun.security.util.DerInputStream)26 ObjectIdentifier (sun.security.util.ObjectIdentifier)17 CertificateException (java.security.cert.CertificateException)14 DerOutputStream (sun.security.util.DerOutputStream)11 BigInteger (java.math.BigInteger)10 KeyStoreException (java.security.KeyStoreException)10 UnrecoverableKeyException (java.security.UnrecoverableKeyException)10 X509Certificate (java.security.cert.X509Certificate)10 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)9 UnrecoverableEntryException (java.security.UnrecoverableEntryException)8 CertificateFactory (java.security.cert.CertificateFactory)8 X500Principal (javax.security.auth.x500.X500Principal)7 DestroyFailedException (javax.security.auth.DestroyFailedException)6 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)5 AlgorithmId (sun.security.x509.AlgorithmId)5 AlgorithmParameters (java.security.AlgorithmParameters)4 KeyFactory (java.security.KeyFactory)4 PrivateKey (java.security.PrivateKey)4