use of sun.security.util.BitArray in project jdk8u_jdk by JetBrains.
the class DistributionPoint method encode.
/**
* Write the DistributionPoint value to the DerOutputStream.
*
* @param out the DerOutputStream to write the extension to.
* @exception IOException on error.
*/
public void encode(DerOutputStream out) throws IOException {
DerOutputStream tagged = new DerOutputStream();
// NOTE: only one of pointNames and pointRDN can be set
if ((fullName != null) || (relativeName != null)) {
DerOutputStream distributionPoint = new DerOutputStream();
if (fullName != null) {
DerOutputStream derOut = new DerOutputStream();
fullName.encode(derOut);
distributionPoint.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_FULL_NAME), derOut);
} else if (relativeName != null) {
DerOutputStream derOut = new DerOutputStream();
relativeName.encode(derOut);
distributionPoint.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_REL_NAME), derOut);
}
tagged.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_DIST_PT), distributionPoint);
}
if (reasonFlags != null) {
DerOutputStream reasons = new DerOutputStream();
BitArray rf = new BitArray(reasonFlags);
reasons.putTruncatedUnalignedBitString(rf);
tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, false, TAG_REASONS), reasons);
}
if (crlIssuer != null) {
DerOutputStream issuer = new DerOutputStream();
crlIssuer.encode(issuer);
tagged.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT, true, TAG_ISSUER), issuer);
}
out.write(DerValue.tag_Sequence, tagged);
}
use of sun.security.util.BitArray in project jdk8u_jdk by JetBrains.
the class IPAddressName method parseIPv6.
private void parseIPv6(String name) throws IOException {
int slashNdx = name.indexOf('/');
if (slashNdx == -1) {
address = InetAddress.getByName(name).getAddress();
} else {
address = new byte[32];
byte[] base = InetAddress.getByName(name.substring(0, slashNdx)).getAddress();
System.arraycopy(base, 0, address, 0, 16);
// append a mask corresponding to the num of prefix bits specified
int prefixLen = Integer.parseInt(name.substring(slashNdx + 1));
if (prefixLen < 0 || prefixLen > 128) {
throw new IOException("IPv6Address prefix length (" + prefixLen + ") in out of valid range [0,128]");
}
// create new bit array initialized to zeros
BitArray bitArray = new BitArray(MASKSIZE * 8);
// set all most significant bits up to prefix length
for (int i = 0; i < prefixLen; i++) bitArray.set(i, true);
byte[] maskArray = bitArray.toByteArray();
// copy mask bytes into mask portion of address
for (int i = 0; i < MASKSIZE; i++) address[MASKSIZE + i] = maskArray[i];
}
}
use of sun.security.util.BitArray in project jdk8u_jdk by JetBrains.
the class NamedBitList method main.
public static void main(String[] args) throws Exception {
boolean[] bb = (new boolean[] { true, false, true, false, false, false });
GeneralNames gns = new GeneralNames();
gns.add(new GeneralName(new DNSName("dns")));
DerOutputStream out;
// length should be 5 since only {T,F,T} should be encoded
KeyUsageExtension x1 = new KeyUsageExtension(bb);
check(new DerValue(x1.getExtensionValue()).getUnalignedBitString().length(), 3);
NetscapeCertTypeExtension x2 = new NetscapeCertTypeExtension(bb);
check(new DerValue(x2.getExtensionValue()).getUnalignedBitString().length(), 3);
ReasonFlags r = new ReasonFlags(bb);
out = new DerOutputStream();
r.encode(out);
check(new DerValue(out.toByteArray()).getUnalignedBitString().length(), 3);
// Read sun.security.x509.DistributionPoint for ASN.1 definition
DistributionPoint dp = new DistributionPoint(gns, bb, gns);
out = new DerOutputStream();
dp.encode(out);
DerValue v = new DerValue(out.toByteArray());
// skip distributionPoint
v.data.getDerValue();
// read reasons
DerValue v2 = v.data.getDerValue();
// reset to BitString since it's context-specfic[1] encoded
v2.resetTag(DerValue.tag_BitString);
// length should be 5 since only {T,F,T} should be encoded
check(v2.getUnalignedBitString().length(), 3);
BitArray ba;
ba = new BitArray(new boolean[] { false, false, false });
check(ba.length(), 3);
ba = ba.truncate();
check(ba.length(), 1);
ba = new BitArray(new boolean[] { true, true, true, true, true, true, true, true, false, false });
check(ba.length(), 10);
check(ba.toByteArray().length, 2);
ba = ba.truncate();
check(ba.length(), 8);
check(ba.toByteArray().length, 1);
ba = new BitArray(new boolean[] { true, true, true, true, true, true, true, true, true, false });
check(ba.length(), 10);
check(ba.toByteArray().length, 2);
ba = ba.truncate();
check(ba.length(), 9);
check(ba.toByteArray().length, 2);
}
use of sun.security.util.BitArray in project jdk8u_jdk by JetBrains.
the class V3Certificate method test.
public static boolean test(String algorithm, String sigAlg, int keyLength) throws IOException, NoSuchAlgorithmException, InvalidKeyException, CertificateException, NoSuchProviderException, SignatureException {
byte[] issuerId = { 1, 2, 3, 4, 5 };
byte[] subjectId = { 6, 7, 8, 9, 10 };
boolean testResult = true;
// Subject and Issuer
X500Name subject = new X500Name("test", "Oracle", "Santa Clara", "US");
X500Name issuer = subject;
// Generate keys and sign
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(algorithm);
keyGen.initialize(keyLength);
KeyPair pair = keyGen.generateKeyPair();
PublicKey publicKey = pair.getPublic();
PrivateKey privateKey = pair.getPrivate();
MessageDigest md = MessageDigest.getInstance("SHA");
byte[] keyId = md.digest(publicKey.getEncoded());
Signature signature = Signature.getInstance(sigAlg);
signature.initSign(privateKey);
// Validity interval
Date firstDate = new Date();
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("PST"));
cal.set(2014, 03, 10, 12, 30, 30);
Date lastDate = cal.getTime();
CertificateValidity interval = new CertificateValidity(firstDate, lastDate);
// Certificate Info
X509CertInfo cert = new X509CertInfo();
cert.set(X509CertInfo.VERSION, new CertificateVersion(CertificateVersion.V3));
cert.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber((int) (firstDate.getTime() / 1000)));
cert.set(X509CertInfo.ALGORITHM_ID, new CertificateAlgorithmId(AlgorithmId.get(sigAlg)));
cert.set(X509CertInfo.SUBJECT, subject);
cert.set(X509CertInfo.KEY, new CertificateX509Key(publicKey));
cert.set(X509CertInfo.VALIDITY, interval);
cert.set(X509CertInfo.ISSUER, issuer);
cert.set(X509CertInfo.ISSUER_ID, new UniqueIdentity(new BitArray(issuerId.length * 8 - 2, issuerId)));
cert.set(X509CertInfo.SUBJECT_ID, new UniqueIdentity(subjectId));
// Create Extensions
CertificateExtensions exts = new CertificateExtensions();
GeneralNameInterface mailInf = new RFC822Name("test@Oracle.com");
GeneralName mail = new GeneralName(mailInf);
GeneralNameInterface dnsInf = new DNSName("Oracle.com");
GeneralName dns = new GeneralName(dnsInf);
GeneralNameInterface uriInf = new URIName("http://www.Oracle.com");
GeneralName uri = new GeneralName(uriInf);
// localhost
byte[] address = new byte[] { 127, 0, 0, 1 };
GeneralNameInterface ipInf = new IPAddressName(address);
GeneralName ip = new GeneralName(ipInf);
int[] oidData = new int[] { 1, 2, 3, 4 };
GeneralNameInterface oidInf = new OIDName(new ObjectIdentifier(oidData));
GeneralName oid = new GeneralName(oidInf);
SubjectAlternativeNameExtension subjectName = new SubjectAlternativeNameExtension();
IssuerAlternativeNameExtension issuerName = new IssuerAlternativeNameExtension();
GeneralNames subjectNames = (GeneralNames) subjectName.get(SubjectAlternativeNameExtension.SUBJECT_NAME);
GeneralNames issuerNames = (GeneralNames) issuerName.get(IssuerAlternativeNameExtension.ISSUER_NAME);
subjectNames.add(mail);
subjectNames.add(dns);
subjectNames.add(uri);
issuerNames.add(ip);
issuerNames.add(oid);
cal.set(2000, 11, 15, 12, 30, 30);
lastDate = cal.getTime();
PrivateKeyUsageExtension pkusage = new PrivateKeyUsageExtension(firstDate, lastDate);
KeyUsageExtension usage = new KeyUsageExtension();
usage.set(KeyUsageExtension.CRL_SIGN, true);
usage.set(KeyUsageExtension.DIGITAL_SIGNATURE, true);
usage.set(KeyUsageExtension.NON_REPUDIATION, true);
KeyIdentifier kid = new KeyIdentifier(keyId);
SerialNumber sn = new SerialNumber(42);
AuthorityKeyIdentifierExtension aki = new AuthorityKeyIdentifierExtension(kid, subjectNames, sn);
SubjectKeyIdentifierExtension ski = new SubjectKeyIdentifierExtension(keyId);
BasicConstraintsExtension cons = new BasicConstraintsExtension(true, 10);
PolicyConstraintsExtension pce = new PolicyConstraintsExtension(2, 4);
exts.set(SubjectAlternativeNameExtension.NAME, subjectName);
exts.set(IssuerAlternativeNameExtension.NAME, issuerName);
exts.set(PrivateKeyUsageExtension.NAME, pkusage);
exts.set(KeyUsageExtension.NAME, usage);
exts.set(AuthorityKeyIdentifierExtension.NAME, aki);
exts.set(SubjectKeyIdentifierExtension.NAME, ski);
exts.set(BasicConstraintsExtension.NAME, cons);
exts.set(PolicyConstraintsExtension.NAME, pce);
cert.set(X509CertInfo.EXTENSIONS, exts);
// Generate and sign X509CertImpl
X509CertImpl crt = new X509CertImpl(cert);
crt.sign(privateKey, sigAlg);
crt.verify(publicKey);
try (FileOutputStream fos = new FileOutputStream(new File(V3_FILE));
FileOutputStream fos_b64 = new FileOutputStream(new File(V3_B64_FILE));
PrintWriter pw = new PrintWriter(fos_b64)) {
crt.encode((OutputStream) fos);
fos.flush();
// Certificate boundaries/
pw.println("-----BEGIN CERTIFICATE-----");
pw.flush();
new BASE64Encoder().encodeBuffer(crt.getEncoded(), fos_b64);
fos_b64.flush();
pw.println("-----END CERTIFICATE-----");
}
out.println("*** Certificate ***");
out.println(crt);
out.println("*** End Certificate ***");
X509Certificate x2 = generateCertificate(V3_FILE);
if (!x2.equals(crt)) {
out.println("*** Certificate mismatch ***");
testResult = false;
}
X509Certificate x3 = generateCertificate(V3_B64_FILE);
if (!x3.equals(crt)) {
out.println("*** Certificate mismatch ***");
testResult = false;
}
return testResult;
}
use of sun.security.util.BitArray in project j2objc by google.
the class IPAddressName method parseIPv6.
private void parseIPv6(String name) throws IOException {
int slashNdx = name.indexOf('/');
if (slashNdx == -1) {
address = InetAddress.getByName(name).getAddress();
} else {
address = new byte[32];
byte[] base = InetAddress.getByName(name.substring(0, slashNdx)).getAddress();
System.arraycopy(base, 0, address, 0, 16);
// append a mask corresponding to the num of prefix bits specified
int prefixLen = Integer.parseInt(name.substring(slashNdx + 1));
if (prefixLen > 128)
throw new IOException("IPv6Address prefix is longer than 128");
// create new bit array initialized to zeros
BitArray bitArray = new BitArray(MASKSIZE * 8);
// set all most significant bits up to prefix length
for (int i = 0; i < prefixLen; i++) bitArray.set(i, true);
byte[] maskArray = bitArray.toByteArray();
// copy mask bytes into mask portion of address
for (int i = 0; i < MASKSIZE; i++) address[MASKSIZE + i] = maskArray[i];
}
}
Aggregations