use of com.github.zhenwei.core.asn1.ASN1Primitive in project certmgr by hdecarne.
the class DistributionPoint method decode.
/**
* Decode {@code DistributionPoint} object from an ASN.1 data object.
*
* @param primitive The ASN.1 data object to decode.
* @return The decoded distribution point object.
* @throws IOException if an I/O error occurs during decoding.
*/
public static DistributionPoint decode(ASN1Primitive primitive) throws IOException {
ASN1Primitive[] sequence = decodeSequence(primitive, 1, Integer.MAX_VALUE);
DistributionPointName name = null;
ReasonFlags reasons = null;
GeneralNames crlIssuer = null;
for (ASN1Primitive sequenceEntry : sequence) {
ASN1TaggedObject taggedObject = decodePrimitive(sequenceEntry, ASN1TaggedObject.class);
int taggedObjectTag = taggedObject.getTagNo();
switch(taggedObjectTag) {
case 0:
name = DistributionPointName.decode(taggedObject.getObject());
break;
case 1:
reasons = ReasonFlags.decode(taggedObject.getObject());
break;
case 2:
crlIssuer = GeneralNames.decode(taggedObject.getObject());
break;
default:
throw new IOException("Unsupported tag: " + taggedObjectTag);
}
}
return new DistributionPoint(name, crlIssuer, reasons);
}
use of com.github.zhenwei.core.asn1.ASN1Primitive in project certmgr by hdecarne.
the class IPAddressName method decode.
/**
* Decode {@code IPAddressName} from an ASN.1 data object.
*
* @param primitive The ASN.1 data object to decode.
* @return The decoded IP address name object.
* @throws IOException if an I/O error occurs during decoding.
*/
public static IPAddressName decode(ASN1Primitive primitive) throws IOException {
ASN1Primitive object = decodeTagged(primitive, GeneralNameType.IP_ADDRESS_TAG);
byte[] octets = decodePrimitive(object, ASN1OctetString.class).getOctets();
InetAddress address;
InetAddress netmask;
switch(octets.length) {
case 4:
address = InetAddress.getByAddress(octets);
netmask = null;
break;
case 8:
address = InetAddress.getByAddress(Arrays.copyOfRange(octets, 0, 4));
netmask = InetAddress.getByAddress(Arrays.copyOfRange(octets, 4, 8));
break;
case 16:
address = InetAddress.getByAddress(octets);
netmask = null;
break;
case 32:
address = InetAddress.getByAddress(Arrays.copyOfRange(octets, 0, 16));
netmask = InetAddress.getByAddress(Arrays.copyOfRange(octets, 16, 32));
break;
default:
throw new IOException("Unexpected data length: " + octets.length);
}
return new IPAddressName(address, netmask);
}
use of com.github.zhenwei.core.asn1.ASN1Primitive in project certmgr by hdecarne.
the class AuthorityKeyIdentifierExtensionData method decode.
/**
* Decode {@code AuthorityKeyIdentifierExtensionData} from an ASN.1 data object.
*
* @param primitive The ASN.1 data object to decode.
* @param critical The extension's critical flag.
* @return The decoded extension data.
* @throws IOException if an I/O error occurs during decoding.
*/
public static AuthorityKeyIdentifierExtensionData decode(ASN1Primitive primitive, boolean critical) throws IOException {
ASN1Primitive[] sequence = decodeSequence(primitive, 0, Integer.MAX_VALUE);
byte[] keyIdentifier = null;
GeneralNames authorityCertIssuer = null;
BigInteger authorityCertSerialNumber = null;
for (ASN1Primitive sequenceEntry : sequence) {
ASN1TaggedObject taggedObject = decodePrimitive(sequenceEntry, ASN1TaggedObject.class);
int taggedObjectTag = taggedObject.getTagNo();
switch(taggedObjectTag) {
case 0:
keyIdentifier = decodePrimitive(taggedObject.getObject(), ASN1OctetString.class).getOctets();
break;
case 1:
authorityCertIssuer = GeneralNames.decode(taggedObject.getObject());
break;
case 2:
authorityCertSerialNumber = decodePrimitive(taggedObject.getObject(), ASN1Integer.class).getValue();
break;
default:
throw new IOException("Unsupported tag: " + taggedObjectTag);
}
}
if (keyIdentifier == null && (authorityCertIssuer == null || authorityCertSerialNumber == null)) {
throw new IOException("Invalid or incomplete extension data");
}
return new AuthorityKeyIdentifierExtensionData(critical, keyIdentifier, authorityCertIssuer, authorityCertSerialNumber);
}
use of com.github.zhenwei.core.asn1.ASN1Primitive in project vcert-java by Venafi.
the class PEMCollection method derPrivateKey.
public RawPrivateKey derPrivateKey() {
if (Objects.isNull(this.privateKey)) {
return null;
}
try {
RawPrivateKey result = new RawPrivateKey();
if (KeyType.from(this.privateKey.getAlgorithm()) == KeyType.RSA) {
PrivateKeyInfo pkInfo = PrivateKeyInfo.getInstance(this.privateKey.getEncoded());
ASN1Primitive privateKeyPKCS1ASN1 = pkInfo.parsePrivateKey().toASN1Primitive();
result.data = privateKeyPKCS1ASN1.getEncoded();
} else {
result.data = this.privateKey.getEncoded();
}
if (privateKeyPassword == null) {
return result;
} else {
result.iv = new byte[SECRET_KEY_LENGTH_BITS / 8];
new SecureRandom().nextBytes(result.iv);
SecretKeySpec secretKey = passwordToCipherSecretKey(privateKeyPassword.toCharArray(), result.iv);
Cipher c = Cipher.getInstance(CIPHER_TRANSFORMATION);
c.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(result.iv));
result.data = c.doFinal(result.data);
return result;
}
} catch (IOException | GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
use of com.github.zhenwei.core.asn1.ASN1Primitive in project tray by qzind.
the class AboutInfo method ssl.
private static JSONObject ssl(CertificateManager certificateManager) throws JSONException, CertificateEncodingException {
JSONObject ssl = new JSONObject();
JSONArray certs = new JSONArray();
for (KeyPairWrapper keyPair : new KeyPairWrapper[] { certificateManager.getCaKeyPair(), certificateManager.getSslKeyPair() }) {
X509Certificate x509 = keyPair.getCert();
if (x509 != null) {
JSONObject cert = new JSONObject();
cert.put("alias", keyPair.getAlias());
try {
ASN1Primitive ext = X509ExtensionUtil.fromExtensionValue(x509.getExtensionValue(Extension.basicConstraints.getId()));
cert.put("rootca", BasicConstraints.getInstance(ext).isCA());
} catch (IOException | NullPointerException e) {
cert.put("rootca", false);
}
cert.put("subject", x509.getSubjectX500Principal().getName());
cert.put("expires", SystemUtilities.toISO(x509.getNotAfter()));
cert.put("data", formatCert(x509.getEncoded()));
certs.put(cert);
}
}
ssl.put("certificates", certs);
return ssl;
}
Aggregations