use of org.spongycastle.asn1.DEROctetString in project android_packages_apps_Settings by SudaMod.
the class CertInstallerHelper method isCa.
private boolean isCa(X509Certificate cert) {
try {
byte[] asn1EncodedBytes = cert.getExtensionValue("2.5.29.19");
if (asn1EncodedBytes == null) {
return false;
}
DEROctetString derOctetString = (DEROctetString) new ASN1InputStream(asn1EncodedBytes).readObject();
byte[] octets = derOctetString.getOctets();
ASN1Sequence sequence = (ASN1Sequence) new ASN1InputStream(octets).readObject();
return BasicConstraints.getInstance(sequence).isCA();
} catch (IOException e) {
return false;
}
}
use of org.spongycastle.asn1.DEROctetString in project fabric-sdk-java by hyperledger.
the class SDKUtils method calculateBlockHash.
/**
* used asn1 and get hash
*
* @param blockNumber
* @param previousHash
* @param dataHash
* @return byte[]
* @throws IOException
* @throws InvalidArgumentException
*/
public static byte[] calculateBlockHash(HFClient client, long blockNumber, byte[] previousHash, byte[] dataHash) throws IOException, InvalidArgumentException {
if (previousHash == null) {
throw new InvalidArgumentException("previousHash parameter is null.");
}
if (dataHash == null) {
throw new InvalidArgumentException("dataHash parameter is null.");
}
if (null == client) {
throw new InvalidArgumentException("client parameter is null.");
}
CryptoSuite cryptoSuite = client.getCryptoSuite();
if (null == client) {
throw new InvalidArgumentException("Client crypto suite has not been set.");
}
ByteArrayOutputStream s = new ByteArrayOutputStream();
DERSequenceGenerator seq = new DERSequenceGenerator(s);
seq.addObject(new ASN1Integer(blockNumber));
seq.addObject(new DEROctetString(previousHash));
seq.addObject(new DEROctetString(dataHash));
seq.close();
return cryptoSuite.hash(s.toByteArray());
}
use of org.spongycastle.asn1.DEROctetString in project platform_packages_apps_Settings by BlissRoms.
the class CertInstallerHelper method isCa.
private boolean isCa(X509Certificate cert) {
try {
byte[] asn1EncodedBytes = cert.getExtensionValue("2.5.29.19");
if (asn1EncodedBytes == null) {
return false;
}
DEROctetString derOctetString = (DEROctetString) new ASN1InputStream(asn1EncodedBytes).readObject();
byte[] octets = derOctetString.getOctets();
ASN1Sequence sequence = (ASN1Sequence) new ASN1InputStream(octets).readObject();
return BasicConstraints.getInstance(sequence).isCA();
} catch (IOException e) {
return false;
}
}
use of org.spongycastle.asn1.DEROctetString in project jruby-openssl by jruby.
the class PEMInputOutput method readAuxCertificate.
private static X509AuxCertificate readAuxCertificate(final BufferedReader in, final String endMarker) throws IOException {
final byte[] bytes = readBase64Bytes(in, endMarker);
final ASN1InputStream asn1 = new ASN1InputStream(bytes);
ByteArrayInputStream certBytes = new ByteArrayInputStream((asn1.readObject()).getEncoded());
try {
final X509Certificate cert = (X509Certificate) getX509CertificateFactory().generateCertificate(certBytes);
final ASN1Sequence auxSeq = (ASN1Sequence) asn1.readObject();
final X509Aux aux;
if (auxSeq != null) {
// X509Aux fields :
final List<String> trust;
final List<String> reject;
final String alias;
final byte[] keyid;
final List<ASN1Primitive> other;
int ix = 0;
ASN1Encodable obj = null;
if (auxSeq.size() > ix)
obj = auxSeq.getObjectAt(ix);
if (obj instanceof ASN1Sequence) {
trust = new ArrayList<String>();
final ASN1Sequence trustSeq = (ASN1Sequence) obj;
for (int i = 0; i < trustSeq.size(); i++) {
trust.add(((ASN1ObjectIdentifier) trustSeq.getObjectAt(i)).getId());
}
// next obj
obj = (auxSeq.size() > ++ix) ? auxSeq.getObjectAt(ix) : null;
} else
trust = Collections.emptyList();
if (obj instanceof ASN1TaggedObject && ((ASN1TaggedObject) obj).getTagNo() == 0) {
reject = new ArrayList<String>();
final ASN1Sequence rejectSeq = (ASN1Sequence) ((ASN1TaggedObject) obj).getObject();
for (int i = 0; i < rejectSeq.size(); i++) {
reject.add(((ASN1ObjectIdentifier) rejectSeq.getObjectAt(i)).getId());
}
// next obj
obj = (auxSeq.size() > ++ix) ? auxSeq.getObjectAt(ix) : null;
} else
reject = Collections.emptyList();
if (obj instanceof DERUTF8String) {
alias = ((DERUTF8String) obj).getString();
// next obj
obj = (auxSeq.size() > ++ix) ? auxSeq.getObjectAt(ix) : null;
} else
alias = null;
if (obj instanceof DEROctetString) {
keyid = ((DEROctetString) obj).getOctets();
// next obj
obj = (auxSeq.size() > ++ix) ? auxSeq.getObjectAt(ix) : null;
} else
keyid = null;
if (obj instanceof ASN1TaggedObject && ((ASN1TaggedObject) obj).getTagNo() == 1) {
other = new ArrayList<ASN1Primitive>();
final ASN1Sequence otherSeq = (ASN1Sequence) ((ASN1TaggedObject) obj).getObject();
for (int i = 0; i < otherSeq.size(); i++) {
other.add((ASN1Primitive) otherSeq.getObjectAt(i));
}
// obj = ( auxSeq.size() > ++ix ) ? auxSeq.getObjectAt(ix) : null; // next obj
} else
other = Collections.emptyList();
aux = new X509Aux(alias, keyid, Collections.unmodifiableList(trust), Collections.unmodifiableList(reject), Collections.unmodifiableList(other));
} else {
aux = null;
}
return new X509AuxCertificate(cert, aux);
} catch (CertificateException e) {
throw new IOException("failed to read aux cert: " + e, e);
}
}
use of org.spongycastle.asn1.DEROctetString in project jruby-openssl by jruby.
the class PEMInputOutput method writeX509Aux.
public static void writeX509Aux(final Writer _out, final X509AuxCertificate cert) throws IOException {
BufferedWriter out = makeBuffered(_out);
final byte[] encoding;
final int encLen;
try {
if (cert.aux == null) {
encoding = cert.getEncoded();
encLen = encoding.length;
} else {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] enc = cert.getEncoded();
baos.write(enc, 0, enc.length);
final X509Aux aux = cert.aux;
ASN1EncodableVector a1 = new ASN1EncodableVector();
if (aux.trust.size() > 0) {
ASN1EncodableVector a2 = new ASN1EncodableVector();
for (String trust : aux.trust) {
a2.add(new ASN1ObjectIdentifier(trust));
}
a1.add(new DLSequence(a2));
}
if (aux.reject.size() > 0) {
ASN1EncodableVector a2 = new ASN1EncodableVector();
for (String reject : aux.reject) {
a2.add(new ASN1ObjectIdentifier(reject));
}
a1.add(new DERTaggedObject(0, new DLSequence(a2)));
}
if (aux.alias != null) {
a1.add(new DERUTF8String(aux.alias));
}
if (aux.keyid != null) {
a1.add(new DEROctetString(aux.keyid));
}
if (aux.other.size() > 0) {
ASN1EncodableVector a2 = new ASN1EncodableVector();
for (ASN1Primitive other : aux.other) a2.add(other);
a1.add(new DERTaggedObject(1, new DLSequence(a2)));
}
enc = new DLSequence(a1).getEncoded();
baos.write(enc, 0, enc.length);
encoding = baos.buffer();
encLen = baos.size();
}
} catch (CertificateEncodingException e) {
throw new IOException("problem with encoding object in write_X509_AUX", e);
}
out.write(BEF_G + PEM_STRING_X509_TRUSTED + AFT);
out.newLine();
writeEncoded(out, encoding, encLen);
out.write(BEF_E + PEM_STRING_X509_TRUSTED + AFT);
out.newLine();
out.flush();
}
Aggregations