I don't have idea how to achieve this. I refereed this link where i can match my csr with certificate.
In this tool they are getting the hash from certificate and csr and matching them.
So I also tried to get hash but i am not able to get proper value.
Here is my implementation:
BufferedInputStream bufferedIS = new BufferedInputStream(mulitpartFile.getInputStream());
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
Certificate certificate = certificateFactory.generateCertificate(bufferedIS);
X509Certificate x509Certificate = (X509Certificate) certificate;
String thumbprint = DigestUtils.sha512Hex(x509Certificate.getEncoded());
But i am not getting the exact value what i am getting in there tool.
Here is my CSR and CERTIFICATE.
CSR
-----BEGIN NEW CERTIFICATE REQUEST-----
MIIC/jCCAeYCAQAwdDENMAsGA1UEAxMEMkdSSTEMMAoGA1UECxMDQUlCMRAwDgYD
VQQLEwdEZXZpY2VzMTYwNAYDVQQKEy1FdXJvcGVhbiBBZXJvbmF1dGljIERlZmVu
Y2UgYW5kIFNwYWNlIENvbXBhbnkxCzAJBgNVBAYTAkRFMIIBIjANBgkqhkiG9w0B
AQEFAAOCAQ8AMIIBCgKCAQEA8yh1MlHMF4Q8WJAUDjaQ6pnEKEUdl85Dhyg/STF2
uxHnasR9aBavd5he20IVmzHX3X8rG2RM3lN2QP/3j0zObpPTPHvNhp/CuTFw6qrX
JovktZv0VNIZhAWbZHl4Ar+2zX44c0VbuHOZDbrYlteREdbg0x9hNj3AObQe37TQ
33rA4jABbMEODtGlPprMSsSzUCiHJ6KRSVGGuIUAIHjgIqciLOvqfgICD6mgHi58
yeM1hi3MfXIm8BAlIFMCJZgO+IDDBFkfvCPAdDmnTy9O8e6eXFQ2etFlAgYN2l16
3NqBQw5cJ5Oyg8By4xPcreRDgmqzz2YoIZnqJGW0Y46tCwIDAQABoEUwQwYJKoZI
hvcNAQkOMTYwNDAOBgNVHQ8BAf8EBAMCBsAwIgYDVR0RAQH/BBgwFogOKwYBBAHr
EIZKAv8wLg+HBMLOFp4wDQYJKoZIhvcNAQELBQADggEBALN3kYMVHlrPWO/8yv3n
imKEgXZZRPK9rVnPt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qfgICD6mgHi58yeM1
hi3MfXIm8BAlIFMCJZgO+IDDBFkfvCPAdDmnTy9O8e6eXFQ2etFlAgYN2l163NqB
Qw5cJ5Oyg8By4xPcreRDgmqzz2YoIZnqJGW0Y46tCwIDAQABo1MwUTAdBgNVHQ4E
FgQUXMHQnnIrkSavE10n8wS/ZD9yz8wwHwYDVR0jBBgwFoAUXMHQnnIrkSavE10n
8wS/ZD9yz8wwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOCAQEAujxz
tDjgy5XIJLSbBdBQorwYa51rWaA1zlRwDW9j690JncoIsNeiuWA1MzlpcWFEwcYz
KYciJcUZSwtpao9mioGrTDrChPkfeJrpaKhUNQkQbc59YpiKyShE05KKpFjSZJzZ
p5K8mQfq6OHlee4khxVJA/VirtoWlmtzzV5XyqbCDLIBZhOj005D1eu8KBq+qN09
KKiiUC16Lgj6UdBAfQWf79yN6ymxH9mex5T2HAJQir4rdx1nc5nFpC2V1pJpS8fW
EJNQgipn6CsufB58j4eWu1QAfceRvq7C7k3MbQk3wlW67m/LVQWSu49S6MmRb0EW
a9FacTxnmJFElBO5Gg==
-----END CERTIFICATE-----
Anyone having idea to achieve this please help me.
Your code creates the hash from the complete certificate x509Certificate.getEncoded() and not the public key.
Related
Tried to read the private key (test.key.pem) of the certificate
str = private key without -----BEGIN ENCRYPTED PRIVATE KEY----- and -----END ENCRYPTED PRIVATE KEY-----
byte[] bytes = Base64.getDecoder().decode(str);
EncryptedPrivateKeyInfo epk = new EncryptedPrivateKeyInfo(bytes);
But I'm getting an error
PBE parameter parsing error: expecting the object identifier for AES cipher
After I experienced making rsa aes certificate in openssl:
rsa -aes-256-cbc -in Z:\40000214_test.key -out Z:\file.txt.enc
But I catch completely different errors.
For example this:
java.io.IOException: DerInputStream.getLength(): lengthTag=5, too big.
also instead of "rsa" command I used "enc" and the result was also erroneous
as a result, I was able to achieve re-encryption of the private key. openssl
pkcs8 -v1 PBE-SHA1-3DES -topk8 -in so_privatekey_secret.pem -out so_privatekey_3des_secret.pem
But the problems did not leave me and when sending the request I received the following exception
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
The certificate is expired there, but requests go through postman and also with a new private key
I disabled all certificate checks in Java Control Panel but it didn't help, I also imported the certificate into jvm
I am receiving the following String from a certificate stored in Azure Key Vault. I am using the Secret API in order to retrieve both the certificate and the private key related to this cert.
Initially the certificate was uploaded using a .pfx file to Azure Key vault. Now I need to create a Certificate and a PrivateKey to allow client authentication to a 3rd party system and I am using the given String retrieved from the API, however I am note sure how to get around that in Java.
I took some hints from this link in C# however I am pretty certain that this method doesn't work like that in Java. In particular an X509Certificate or a Certificate in general doesn't hold any information about the PrivateKey in Java, unlike C#, and I am not sure how to extract that information from given String in Java.
This works as expected to retrieve the certificate from the String retrieved from the API
String secret = azureSecret.getValue();
byte[] certkey = Base64.getDecoder().decode(secret);
ByteArrayInputStream inputStream = new ByteArrayInputStream(certkey);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = cf.generateCertificate(inputStream);
The azureSecret.getValue() format is like the following however I am not sure how to get PrivateKey out of the given String
MIIKvgIBaaZd6Euf3EYwYdHrIIKYzCC...
YES, Java X509Certificate and Certificate is only the certificate. Instead use KeyStore which can contain multiple entries each of which is either a 'trusted' certificate (for someone else), or a privatekey plus certificate plus other chain cert(s) (if applicable) for yourself, or (not relevant here) a 'secret' (symmetric) key. PKCS12 is supported as one type of KeyStore along with others not relevant here, so after the base64-decoding you already have do something like:
KeyStore ks = KeyStore.getInstance("PKCS12");
ks.load(inputstreamfromvaultvalue, password);
// then
PrivateKey pkey = (PrivateKey) ks.getKey(alias, password);
// and
Certificate cert = ks.getCertificate(alias); // if you only need the leaf cert
// or
Certificate[] chain = ks.getCertificateChain(alias); // usually
But if you want to do client authentication in TLS/SSL (including HTTPS), you give the JSSE KeyManager the whole keystore object not the individual pieces (privatekey and certificates). Similarly to verify the peer in TLS/SSL, you give TrustManager a keystore containing trusted certificates, usually root CAs and often defaulted to a built-in set of public root CAs.
I'm trying to convert the below java code to python:
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
serverCertificate = (X509Certificate) certFactory.generateCertificate(new FileInputStream(certFile));
Cipher decryptCipher = Cipher.getInstance("RSA");
decryptCipher.init(Cipher.DECRYPT_MODE, serverCertificate.getPublicKey());
byte[] decryptedClientChallengeBytes = decryptCipher.doFinal(encryptedClientChallengeBytes);
The first part I think it's resolved with this:
from cryptography import x509
from cryptography.hazmat.backends import default_backend
cert = x509.load_pem_x509_certificate(key,default_backend())
cert = cert.public_key()
But I can't convert the decrypt part. I´ve tried various methods like PKCS1_v1_5.
I'm trying to implement this https://docs.oracle.com/en/cloud/saas/marketing/responsys-rest-api/RequestUserAuthorization.html
And the step i'm stacked says: "You should receive the following response from the server. Decrypt (using the RSA algorithm) the encrypted clientChallenge using server certificate's public key (which you should have downloaded and stored on your system)."
The clientChallenge received is like this:
"hc8w8gElQJQa45WGofZwsOAi4wwEIaFClI1A2PvvCw4YUc4rbnOpedkBgrNsr0FpZ6DVV_vnZNsAGg_ph75VqeKD-R-cy_RkQIdvn0W_nb0RvPGZI7zA98_t9-rJTiTSsrJ6j8KzlWV_MLydUcBBR1iotL6kXXuFKuXtTAniPx_b6do4D5p9iI7yhTfxp9knRRG6XoItgNGJwm8AnGsThIoZw9lSgUm3Dy9yzYvhXQDYKAUNc3tqd8WcSswlb5coN18Gc-We2E0xE7xehq15Ns4QNwX6WdRrfehSgOObdckUYAx7MyLT-vyfJ-APVEVrZmC18wo-Qr2A-n1NT0ARVQ"
And the server certificate is a .CER with this format:
-----BEGIN CERTIFICATE-----
.....
-----END CERTIFICATE-----
Any help would be awesome!
Hy Guys! I'm trying to create x.509 certificate using bouncycastle, which should be signed by another certificate and store it PEM base 64 format.
I've already have self-signed certificate (public and private key). Now I want to create new one and sign it with existing self-signed certificate.
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
keyPairGenerator.initialize(1024, new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
X500Principal dnName = new X500Principal("CN=Sergey");
certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
certGen.setSubjectDN(dnName);
certGen.setIssuerDN(caCert.getSubjectX500Principal());
certGen.setNotBefore(validityBeginDate);
certGen.setNotAfter(validityEndDate);
certGen.setPublicKey(keyPair.getPublic());
certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
certGen.addExtension(X509Extensions.AuthorityKeyIdentifier, false, new AuthorityKeyIdentifierStructure(caCert));
certGen.addExtension(X509Extensions.SubjectKeyIdentifier, false, new SubjectKeyIdentifierStructure(keyPair.getPublic()));
X509Certificate cert = certGen.generate(caCertPrivateKey, "BC");
Verification passed without exceptions, which means from my point of view that it was successfully signed by caCert:
cert.verify(caCert.getPublicKey());
Then I decode it to the PEM base 64:
PEMWriter pemWriter = new PEMWriter(new PrintWriter(System.out));
pemWriter.writeObject(cert);
pemWriter.flush();
I get something like this in the output:
-----BEGIN CERTIFICATE-----
MIIDDjCCAnegAwIBAgIBFDAN........
-----END CERTIFICATE-----
When I open it, I see the next:
Why there is no certification chain if it was successfully signed by caCert?
What need to be changed in my code to see certification chain as I expected?
I was able to find solution. Actually code works as expected. I didn't see chain of certificates because my caRoot certificate wasn't added to the trusted store. After I add my sel-signed certificate to the trusted root certified centers I see the whole certification chain as I expected.
I use a third party tool to verify signature and to get certificate detail(like serial number, CA etc..) from signature. The problem with this utility is it is licensed and works on certain machines only.
Can i validate the signature against the data using simple java or .net code?(instead of using paid application). I dont have private key to extract certificate information from signed data.
Or if someone can suggest sample code in java or .net to extract certificate detail if i have pfx file. Of from signed data.
Data is signed with asymmetric encryption.
To extract detail from certificate:
Make a string which keeps certificate data. Just ensure it has -----BEGIN CERTIFICATE----- in starting and -----END CERTIFICATE----- in end.
Now use the following code in Java to extract certificate detail.
InputStream inStream = new ByteArrayInputStream(certString.toString().getBytes("UTF-8"));
BufferedInputStream bis = new BufferedInputStream(inStream);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate cert = cf.generateCertificate(bis);
X509Certificate xCert = (X509Certificate)cert;
System.out.println("Certificate Type: "+cert.getType());
System.out.println("Public Key: \n"+cert.getPublicKey());
try{
System.out.println("Signature Algorithm"+xCert.getSigAlgName());
System.out.println("IssuerDN : "+xCert.getIssuerDN());
System.out.println("Serial Number : "+xCert.getSerialNumber());
System.out.println("SubjectDN : "+xCert.getSubjectDN());
}catch(Exception exp){
:
}
If you are having the PFX file, then that may contain the public key certificate which will be required to verify the signature.
Alternatively, if your signature is a PKCS#7 signature, then the signature itself will hold the data, signature and the certificate. Assuming PKCS#7 is not detached.
You need to ask your signer, how is he transferring his certificate for validation.