use of com.github.zhenwei.core.asn1.DEROctetString in project PdfBox-Android by TomRoush.
the class PublicKeySecurityHandler method createDERForRecipient.
private ASN1Primitive createDERForRecipient(byte[] in, X509Certificate cert) throws IOException, GeneralSecurityException {
String algorithm = PKCSObjectIdentifiers.RC2_CBC.getId();
AlgorithmParameterGenerator apg;
KeyGenerator keygen;
Cipher cipher;
try {
apg = AlgorithmParameterGenerator.getInstance(algorithm, SecurityProvider.getProvider());
keygen = KeyGenerator.getInstance(algorithm, SecurityProvider.getProvider());
cipher = Cipher.getInstance(algorithm, SecurityProvider.getProvider());
} catch (NoSuchAlgorithmException e) {
// happens when using the command line app .jar file
throw new IOException("Could not find a suitable javax.crypto provider for algorithm " + algorithm + "; possible reason: using an unsigned .jar file", e);
} catch (NoSuchPaddingException e) {
// should never happen, if this happens throw IOException instead
throw new RuntimeException("Could not find a suitable javax.crypto provider", e);
}
AlgorithmParameters parameters = apg.generateParameters();
ASN1InputStream input = new ASN1InputStream(parameters.getEncoded("ASN.1"));
ASN1Primitive object = input.readObject();
input.close();
keygen.init(128);
SecretKey secretkey = keygen.generateKey();
cipher.init(1, secretkey, parameters);
byte[] bytes = cipher.doFinal(in);
KeyTransRecipientInfo recipientInfo = computeRecipientInfo(cert, secretkey.getEncoded());
DERSet set = new DERSet(new RecipientInfo(recipientInfo));
AlgorithmIdentifier algorithmId = new AlgorithmIdentifier(new ASN1ObjectIdentifier(algorithm), object);
EncryptedContentInfo encryptedInfo = new EncryptedContentInfo(PKCSObjectIdentifiers.data, algorithmId, new DEROctetString(bytes));
EnvelopedData enveloped = new EnvelopedData(null, set, encryptedInfo, (ASN1Set) null);
ContentInfo contentInfo = new ContentInfo(PKCSObjectIdentifiers.envelopedData, enveloped);
return contentInfo.toASN1Primitive();
}
use of com.github.zhenwei.core.asn1.DEROctetString in project PdfBox-Android by TomRoush.
the class PublicKeySecurityHandler method computeRecipientInfo.
private KeyTransRecipientInfo computeRecipientInfo(X509Certificate x509certificate, byte[] abyte0) throws IOException, CertificateEncodingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
ASN1InputStream input = new ASN1InputStream(x509certificate.getTBSCertificate());
TBSCertificate certificate = TBSCertificate.getInstance(input.readObject());
input.close();
AlgorithmIdentifier algorithmId = certificate.getSubjectPublicKeyInfo().getAlgorithm();
IssuerAndSerialNumber serial = new IssuerAndSerialNumber(certificate.getIssuer(), certificate.getSerialNumber().getValue());
Cipher cipher;
try {
cipher = Cipher.getInstance(algorithmId.getAlgorithm().getId(), SecurityProvider.getProvider());
} catch (NoSuchAlgorithmException e) {
// should never happen, if this happens throw IOException instead
throw new RuntimeException("Could not find a suitable javax.crypto provider", e);
} catch (NoSuchPaddingException e) {
// should never happen, if this happens throw IOException instead
throw new RuntimeException("Could not find a suitable javax.crypto provider", e);
}
cipher.init(1, x509certificate.getPublicKey());
DEROctetString octets = new DEROctetString(cipher.doFinal(abyte0));
RecipientIdentifier recipientId = new RecipientIdentifier(serial);
return new KeyTransRecipientInfo(recipientId, algorithmId, octets);
}
use of com.github.zhenwei.core.asn1.DEROctetString in project attestation by TokenScript.
the class HelperTest method makeMinimalAtt.
public static Attestation makeMinimalAtt() {
Attestation att = new Attestation();
// Our initial version
att.setVersion(IdentifierAttestation.HIDDEN_IDENTIFIER_VERSION);
att.setSerialNumber(42);
// Blank subject info
att.setSubject("CN=");
att.setSigningAlgorithm(IdentifierAttestation.DEFAULT_SIGNING_ALGORITHM);
ASN1EncodableVector dataObject = new ASN1EncodableVector();
dataObject.add(new DEROctetString("hello world".getBytes()));
att.setDataObject(new DERSequence(dataObject));
assertTrue(att.checkValidity());
return att;
}
use of com.github.zhenwei.core.asn1.DEROctetString in project attestation by TokenScript.
the class HelperTest method makeMaximalAtt.
public static IdentifierAttestation makeMaximalAtt(AsymmetricKeyParameter key) throws IOException {
IdentifierAttestation att = new IdentifierAttestation("205521676", "https://www.deviantart.com/some_user", key);
att.setSerialNumber(42);
att.setSigningAlgorithm(IdentifierAttestation.DEFAULT_SIGNING_ALGORITHM);
att.setIssuer("CN=ALX");
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));
assertTrue(att.checkValidity());
return att;
}
use of com.github.zhenwei.core.asn1.DEROctetString in project attestation by TokenScript.
the class HelperTest method makeUnsignedx509Att.
/* the unsigned x509 attestation will have a subject of "CN=0x2042424242424564648" */
public static Attestation makeUnsignedx509Att(AsymmetricKeyParameter key) throws IOException {
Attestation att = new Attestation();
// =v3 since counting starts from 0
att.setVersion(2);
att.setSerialNumber(42);
// ECDSA with SHA256 which is needed for a proper x509
att.setSigningAlgorithm(SignedIdentifierAttestation.ECDSA_WITH_SHA256);
att.setIssuer("CN=ALX");
Date now = new Date();
att.setNotValidBefore(now);
att.setNotValidAfter(new Date(System.currentTimeMillis() + VALIDITY));
att.setSubject("CN=0x2042424242424564648");
SubjectPublicKeyInfo spki = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(key);
att.setSubjectPublicKeyInfo(spki);
ASN1EncodableVector extensions = new ASN1EncodableVector();
extensions.add(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)));
assertTrue(att.isValidX509());
return att;
}
Aggregations