use of com.github.zhenwei.core.asn1.DEROctetString in project LinLong-Java by zhenwei1108.
the class SubjectPermissions method getInstance.
public static SubjectPermissions getInstance(Object src) {
if (src instanceof SubjectPermissions) {
return (SubjectPermissions) src;
}
ASN1TaggedObject taggedObject = ASN1TaggedObject.getInstance(src);
int item = taggedObject.getTagNo();
switch(item) {
case explicit:
return new SubjectPermissions(explicit, SequenceOfPsidSspRange.getInstance(taggedObject.getObject()));
case all:
return new SubjectPermissions(all, DERNull.INSTANCE);
case extension:
try {
return new SubjectPermissions(extension, new DEROctetString(taggedObject.getObject().getEncoded()));
} catch (IOException ioException) {
throw new RuntimeException(ioException.getMessage(), ioException);
}
}
return null;
}
use of com.github.zhenwei.core.asn1.DEROctetString in project LinLong-Java by zhenwei1108.
the class CertPathValidatorUtilities method findIssuerCerts.
/**
* Find the issuer certificates of a given certificate.
*
* @param cert The certificate for which an issuer should be found.
* @return A <code>Collection</code> object containing the issuer
* <code>X509Certificate</code>s. Never <code>null</code>.
* @throws AnnotatedException if an error occurs.
*/
static Collection findIssuerCerts(X509Certificate cert, List<CertStore> certStores, List<PKIXCertStore> pkixCertStores) throws AnnotatedException {
X509CertSelector selector = new X509CertSelector();
try {
selector.setSubject(PrincipalUtils.getIssuerPrincipal(cert).getEncoded());
} catch (Exception e) {
throw new AnnotatedException("Subject criteria for certificate selector to find issuer certificate could not be set.", e);
}
try {
byte[] akiExtensionValue = cert.getExtensionValue(AUTHORITY_KEY_IDENTIFIER);
if (akiExtensionValue != null) {
ASN1OctetString aki = ASN1OctetString.getInstance(akiExtensionValue);
byte[] authorityKeyIdentifier = AuthorityKeyIdentifier.getInstance(aki.getOctets()).getKeyIdentifier();
if (authorityKeyIdentifier != null) {
selector.setSubjectKeyIdentifier(new DEROctetString(authorityKeyIdentifier).getEncoded());
}
}
} catch (Exception e) {
// authority key identifier could not be retrieved from target cert, just search without it
}
PKIXCertStoreSelector certSelect = new PKIXCertStoreSelector.Builder(selector).build();
LinkedHashSet certs = new LinkedHashSet();
try {
CertPathValidatorUtilities.findCertificates(certs, certSelect, certStores);
CertPathValidatorUtilities.findCertificates(certs, certSelect, pkixCertStores);
} catch (AnnotatedException e) {
throw new AnnotatedException("Issuer certificate cannot be searched.", e);
}
return certs;
}
use of com.github.zhenwei.core.asn1.DEROctetString in project LinLong-Java by zhenwei1108.
the class PKCS12Util method convertToDefiniteLength.
/**
* Re-encode the PKCS#12 structure to definite length encoding at the inner layer as well,
* recomputing the MAC accordingly.
*
* @param berPKCS12File - original PKCS12 file.
* @param provider - provider to use for MAC calculation.
* @return a byte array representing the DER encoding of the PFX structure.
* @throws IOException on parsing, encoding errors.
*/
public static byte[] convertToDefiniteLength(byte[] berPKCS12File, char[] passwd, String provider) throws IOException {
Pfx pfx = Pfx.getInstance(berPKCS12File);
ContentInfo info = pfx.getAuthSafe();
ASN1OctetString content = ASN1OctetString.getInstance(info.getContent());
ASN1Primitive obj = ASN1Primitive.fromByteArray(content.getOctets());
byte[] derEncoding = obj.getEncoded(ASN1Encoding.DER);
info = new ContentInfo(info.getContentType(), new DEROctetString(derEncoding));
MacData mData = pfx.getMacData();
try {
int itCount = mData.getIterationCount().intValue();
byte[] data = ASN1OctetString.getInstance(info.getContent()).getOctets();
byte[] res = calculatePbeMac(mData.getMac().getAlgorithmId().getAlgorithm(), mData.getSalt(), itCount, passwd, data, provider);
AlgorithmIdentifier algId = new AlgorithmIdentifier(mData.getMac().getAlgorithmId().getAlgorithm(), DERNull.INSTANCE);
DigestInfo dInfo = new DigestInfo(algId, res);
mData = new MacData(dInfo, mData.getSalt(), itCount);
} catch (Exception e) {
throw new IOException("error constructing MAC: " + e.toString());
}
pfx = new Pfx(info, mData);
return pfx.getEncoded(ASN1Encoding.DER);
}
use of com.github.zhenwei.core.asn1.DEROctetString in project alpha-wallet-android by AlphaWallet.
the class TestAttestation method makeUnsignedx509Att.
private Attestation makeUnsignedx509Att() {
Attestation att = new Attestation();
// =v3 since counting starts from 0
att.setVersion(2);
att.setSerialNumber(42);
att.setSignature(OID_SHA256ECDSA);
att.setIssuer("CN=ALX");
Date now = new Date();
att.setNotValidBefore(now);
// Valid for an hour
att.setNotValidAfter(new Date(System.currentTimeMillis() + 3600000));
att.setSubject("CN=0x2042424242424564648");
att.setSubjectPublicKeyInfo(OID_SHA256ECDSA, subjectKeys.getPublic().getEncoded());
ASN1EncodableVector extensions = new ASN1EncodableVector();
extensions.add(new ASN1ObjectIdentifier(Attestation.OID_OCTETSTRING));
extensions.add(ASN1Boolean.TRUE);
extensions.add(new DEROctetString("hello world".getBytes()));
// Double Sequence is needed to be compatible with X509V3
att.setExtensions(new DERSequence(new DERSequence(extensions)));
Assert.assertTrue(att.isValidX509());
return att;
}
use of com.github.zhenwei.core.asn1.DEROctetString in project alpha-wallet-android by AlphaWallet.
the class TestAttestation method makeUnsignedAtt.
private Attestation makeUnsignedAtt() {
Attestation att = new Attestation();
// Our initial version
att.setVersion(18);
att.setSerialNumber(42);
att.setSignature(OID_SHA256ECDSA);
att.setSubject("CN=0x2042424242424564648");
att.setSmartcontracts(Arrays.asList(42L, 1337L));
ASN1EncodableVector dataObject = new ASN1EncodableVector();
dataObject.add(new DEROctetString("hello world".getBytes()));
dataObject.add(new ASN1Integer(42));
att.setDataObject(new DERSequence(dataObject));
Assert.assertTrue(att.isValid());
return att;
}
Aggregations