java 生成 X509 证书时得到“数据不是对象 ID(标签 = 49)”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41512915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Got "data isn't an object ID (tag = 49)" while generating X509 cert
提问by Kaninchen
I'm trying to generate my own CSR for my keystore, but it didn't go well and that error is confusing me. Here is my code:
我正在尝试为我的密钥库生成我自己的 CSR,但它进行得并不顺利,这个错误让我感到困惑。这是我的代码:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x500.X500NameBuilder;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.openssl.PEMWriter;
import org.bouncycastle.operator.ContentSigner;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder;
import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder;
public class CreateKeyTest {
public static void main(String[] args) throws OperatorCreationException, IOException, GeneralSecurityException {
KeyPairGenerator kpg;
KeyPair kp;
RSAPublicKey pubKey;
RSAPrivateKey privKey;
FileOutputStream out;
KeyStore ks;
FileInputStream in;
FileInputStream bFis;
try {
ks = KeyStore.getInstance("JKS");
kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
kp = kpg.generateKeyPair();
pubKey = (RSAPublicKey) kp.getPublic();
privKey = (RSAPrivateKey) kp.getPrivate();
// generate CSR
ContentSigner sign = new JcaContentSignerBuilder("SHA1withRSA").build(privKey);
X500NameBuilder nBuilder = new X500NameBuilder();
nBuilder.addRDN(BCStyle.CN, "TestCSR");
nBuilder.addRDN(BCStyle.C, "ER");
nBuilder.addRDN(BCStyle.E, "[email protected]");
X500Name name = nBuilder.build();
PKCS10CertificationRequestBuilder cerReq = new JcaPKCS10CertificationRequestBuilder(name, pubKey);
PKCS10CertificationRequest request = cerReq.build(sign);
PEMWriter pWr = new PEMWriter(new FileWriter(new File("D:\test.csr")));
pWr.writeObject(request);
pWr.flush();
pWr.close();
bFis = new FileInputStream("D:\test.csr");
BufferedInputStream ksbufin = new BufferedInputStream(bFis);
X509Certificate certificate = (X509Certificate) CertificateFactory.getInstance("X.509")
.generateCertificate(ksbufin);
ks.setKeyEntry("RSA_key", kp.getPrivate(), "changeit".toCharArray(),
new java.security.cert.Certificate[] { certificate });
out = new FileOutputStream("key.store");
ks.store(out, "changeit".toCharArray());
System.out.println("New Keystore Generated");
out.close();
} catch (KeyStoreException | IOException | CertificateException | NoSuchAlgorithmException
| OperatorCreationException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
When I execute it, it showed me the exception:X509.ObjectIdentifier() -- data isn't an object ID (tag = 49)
, and it could be back-traced to generateCertificate(ksbufin)
. But I checked test.cer
and it do have certificate data in there, and that exception message confused me, don't even know what does that mean(object ID
? tag = 49
? I didn't see I generated an ID in my code.).
当我执行它时,它向我显示了异常:X509.ObjectIdentifier() -- data isn't an object ID (tag = 49)
,并且可以回溯到generateCertificate(ksbufin)
. 但我检查test.cer
,它确实有存在的证书数据,而且异常消息我感到困惑,甚至不知道这是什么意思(object ID
?tag = 49
?我没看到我产生在我的代码的ID)。
Can anyone help me out this mud?
谁能帮我解决这个泥巴?
回答by pedrofb
The error message is correct,test.csr
does not contain a certificate. You have built it using a PKCS10CertificationRequest
, so it consenquently contains a Certificate Signing Request (CSR).
错误信息是正确的,test.csr
不包含证书。您已使用 构建它PKCS10CertificationRequest
,因此它随后包含一个证书签名请求 (CSR)。
You have generated a key pair, private and public, and a CSR. The CSR is a request of a certificate to a Certification Authority (CA). It contains the public key and some expected attributes for the certificate (CN, C, OU, etc). CSR is signed with the private key and has to be sent to CA. The CA will extract the public key, generates a certificate and signs it. See Certificate enrollment process
您已经生成了一个密钥对,私有的和公共的,以及一个 CSR。CSR 是对证书颁发机构 (CA) 的证书请求。它包含公钥和证书的一些预期属性(CN、C、OU 等)。CSR 是用私钥签名的,必须发送给 CA。CA 将提取公钥,生成证书并对其进行签名。查看证书注册过程
If you want a Certificate, you need to get signed the certificate by the CA
如果你想要一个证书,你需要得到 CA 签署的证书