use of com.github.zhenwei.core.asn1.DERUTF8String in project signer by demoiselle.
the class FieldOfApplication method parse.
@Override
public void parse(ASN1Primitive derObject) {
if (derObject instanceof DERUTF8String) {
DERUTF8String derUTF8String = (DERUTF8String) derObject;
this.setValue(derUTF8String.getString());
} else {
this.setValue(derObject.toString());
}
}
use of com.github.zhenwei.core.asn1.DERUTF8String in project signer by demoiselle.
the class OIDGeneric method getInstance.
/**
* Instance for OIDGeneric.
*
* @param data Set of bytes with the contents of the certificate.
* @return Object GenericOID
* @throws IOException exception of input/output
* @throws Exception general exception
*/
public static OIDGeneric getInstance(byte[] data) throws IOException, Exception {
is = new ASN1InputStream(data);
DLSequence sequence = (DLSequence) is.readObject();
ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) sequence.getObjectAt(0);
DERTaggedObject taggedObject = (DERTaggedObject) sequence.getObjectAt(1);
DERTaggedObject taggedObject2 = (DERTaggedObject) taggedObject.getObject();
DEROctetString octet = null;
DERPrintableString print = null;
DERUTF8String utf8 = null;
DERIA5String ia5 = null;
try {
octet = (DEROctetString) taggedObject2.getObject();
} catch (Exception e) {
try {
print = (DERPrintableString) taggedObject2.getObject();
} catch (Exception e1) {
try {
utf8 = (DERUTF8String) taggedObject2.getObject();
} catch (Exception e2) {
ia5 = (DERIA5String) taggedObject2.getObject();
}
}
}
String className = getPackageName() + oid.getId().replaceAll("[.]", "_");
OIDGeneric oidGenerico;
try {
oidGenerico = (OIDGeneric) Class.forName(className).newInstance();
} catch (InstantiationException e) {
throw new Exception(coreMessagesBundle.getString("error.class.instance", className), e);
} catch (IllegalAccessException e) {
throw new Exception(coreMessagesBundle.getString("error.class.illegal.access", className), e);
} catch (ClassNotFoundException e) {
oidGenerico = new OIDGeneric();
}
oidGenerico.oid = oid.getId();
if (octet != null) {
oidGenerico.data = new String(octet.getOctets());
} else {
if (print != null) {
oidGenerico.data = print.getString();
} else {
if (utf8 != null) {
oidGenerico.data = utf8.getString();
} else {
oidGenerico.data = ia5.getString();
}
}
}
oidGenerico.initialize();
return oidGenerico;
}
use of com.github.zhenwei.core.asn1.DERUTF8String in project candlepin by candlepin.
the class JSSPKIUtilityTest method testCustomExtensions.
@Test
public void testCustomExtensions() throws Exception {
JSSPKIUtility pki = this.buildJSSPKIUtility();
Date start = new Date();
Date end = Date.from(LocalDate.now().plusDays(365).atStartOfDay(ZoneId.systemDefault()).toInstant());
String extOid = OIDUtil.REDHAT_OID + "." + OIDUtil.TOPLEVEL_NAMESPACES.get(OIDUtil.ENTITLEMENT_TYPE_KEY);
X509ExtensionWrapper typeExtension = new X509ExtensionWrapper(extOid, false, "OrgLevel");
Set<X509ExtensionWrapper> exts = Set.of(typeExtension);
String byteExtOid = OIDUtil.REDHAT_OID + "." + OIDUtil.TOPLEVEL_NAMESPACES.get(OIDUtil.ENTITLEMENT_DATA_KEY);
byte[] someBytes = new byte[] { 0xd, 0xe, 0xf, 0xa, 0xc, 0xe, 0xa, 0xc, 0xe };
X509ByteExtensionWrapper byteExtension = new X509ByteExtensionWrapper(byteExtOid, false, someBytes);
Set<X509ByteExtensionWrapper> byteExtensions = Set.of(byteExtension);
X509Certificate cert = pki.createX509Certificate("cn=candlepinproject.org", exts, byteExtensions, start, end, subjectKeyPair, BigInteger.valueOf(2000L), "altName");
assertNotNull(cert.getExtensionValue(extOid));
ASN1OctetString value = (ASN1OctetString) ASN1OctetString.fromByteArray(cert.getExtensionValue(extOid));
DERUTF8String actual = DERUTF8String.getInstance(value.getOctets());
assertEquals("OrgLevel", actual.getString());
value = (ASN1OctetString) ASN1OctetString.fromByteArray(cert.getExtensionValue(byteExtOid));
ASN1OctetString actualBytes = ASN1OctetString.getInstance(value.getOctets());
assertArrayEquals(someBytes, actualBytes.getOctets());
}
use of com.github.zhenwei.core.asn1.DERUTF8String 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();
}
use of com.github.zhenwei.core.asn1.DERUTF8String 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);
}
}
Aggregations