use of com.github.zhenwei.pkix.util.asn1.cms.ContentInfo in project gdmatrix by gdmatrix.
the class CMSUtils method sendData.
private static TimeStampResp sendData(InputStream dataToBeSent, String serviceURI) throws Exception {
URL url = new URL(serviceURI);
URLConnection conn = url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
// post request data
OutputStream os = conn.getOutputStream();
byte[] buffer = new byte[4096];
int numRead = dataToBeSent.read(buffer);
while (numRead > 0) {
os.write(buffer, 0, numRead);
numRead = dataToBeSent.read(buffer);
}
os.flush();
// read response
InputStream response = conn.getInputStream();
ASN1InputStream asn1Is = new ASN1InputStream(response);
// TimeStampResp tspResp = new TimeStampResp((ASN1Sequence)asn1Is.readObject());
Enumeration e = ((ASN1Sequence) asn1Is.readObject()).getObjects();
PKIStatusInfo pkiStatusInfo = PKIStatusInfo.getInstance(e.nextElement());
ContentInfo timeStampToken = null;
if (e.hasMoreElements()) {
timeStampToken = ContentInfo.getInstance(e.nextElement());
}
TimeStampResp tspResp = new TimeStampResp(pkiStatusInfo, timeStampToken);
return tspResp;
}
use of com.github.zhenwei.pkix.util.asn1.cms.ContentInfo 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.pkix.util.asn1.cms.ContentInfo in project vnfsdk-validation by onap.
the class CmsSignatureDataFactory method getCMSSignedData.
private CMSSignedData getCMSSignedData(byte[] innerPackageFileCSAR, byte[] signatureStream) throws IOException, CmsSignatureLoadingException, CMSException {
ContentInfo signature = signatureFactory.createSignature(signatureStream);
CMSTypedData signedContent = new CMSProcessableByteArray(innerPackageFileCSAR);
return new CMSSignedData(signedContent, signature);
}
use of com.github.zhenwei.pkix.util.asn1.cms.ContentInfo in project vnfsdk-validation by onap.
the class SignatureFactoryTest method shouldCreateContentInfoWithProperContentForPEM.
@Test
public void shouldCreateContentInfoWithProperContentForPEM() throws IOException, CmsSignatureLoadingException {
// when
ContentInfo contentInfo = signatureFactory.createSignature(testPemSignature.getBytes());
// then
final String contentInfoSignature = getContentInfoSignatureAsPem(contentInfo);
assertThat(testPemSignature).contains(contentInfoSignature);
}
use of com.github.zhenwei.pkix.util.asn1.cms.ContentInfo in project vnfsdk-validation by onap.
the class SignatureFactoryTest method shouldCreateContentInfoWithProperContentForPKCS7.
@Test
public void shouldCreateContentInfoWithProperContentForPKCS7() throws IOException, CmsSignatureLoadingException {
// when
ContentInfo contentInfo = signatureFactory.createSignature(testPkcs7Signature.getBytes());
// then
final String contentInfoSignature = getContentInfoSignatureAsPem(contentInfo);
assertThat(testPkcs7Signature).contains(contentInfoSignature);
}
Aggregations