Я пытаюсь сгенерировать подписанный CSR с использованием библиотеки fips Bouncy Castle с закрытым ключом, присутствующим в USB-токене.
В настоящее время в оживленном замке нет возможности присоединить байты подписи (сгенерированные с помощью USB-токена) к pkcs10certificationRequest.
как мне добиться этого, используя библиотеку надувных замков?
Я использую объект PKCS10CertificationRequest для генерации Подписанного CSR, который ожидает объект ContentSigner в качестве входных данных, поэтому я создал свой собственный Contentigner, реализовав методы
1. getSignature ().
2. getoutputstream ().
3. getalgorithmidentifier ().
Где функция getsignature вызывает библиотечные вызовы PKCS11 для генерации подписи с использованием x500Name и открытого ключа в качестве ввода и подписывания входных данных с использованием закрытого ключа, присутствующего в токене.
используя этот поток, я могу сгенерировать подписанный CSR, но когда я попытался проверить сгенерированный подписанный CSR с помощью функции isvalid (), выдается ошибка "Invalid Signature"
Использование следующего кода для создания подписанного CSR Использование надувного замка с библиотекой PKCS11:
, где
publicKeyInfo is a public key retrieved from usb token.
signingPrivKey is a handle for the private key present in usb token.
CertificationRequestInfo certificateRequestInfo = new CertificationRequestInfo(subject, publicKeyInfo, new DERSet());
ContentSigner contentSigner = new ContentSigner() {
@Override
public byte[] getSignature()
{
try
{
PrivateKey signingPrivKey = null;
RSAPrivateKey templateForSignKey = new RSAPrivateKey();
templateForSignKey.getId().setByteArrayValue(id);
session.findObjectsInit(templateForSignKey);
Object[] privKeyObjects = session.findObjects(1);
if (privKeyObjects.length > 0)
{
signingPrivKey = (PrivateKey) privKeyObjects[0];
}
session.findObjectsFinal();
ByteArrayInputStream dataInputStream = new ByteArrayInputStream(certificateRequestInfo.getEncoded());
MessageDigest digestEngine = MessageDigest.getInstance("SHA-256", "BCFIPS");
Mechanism signatureMechanism = Mechanism.get(PKCS11Constants.CKM_RSA_PKCS);
//Initialize for signing
session.signInit(signatureMechanism, signingPrivKey);
byte[] dataBuffer = new byte[1024];
int bytesRead;
// feed all data from the input stream to the message digest
while ((bytesRead = dataInputStream.read(dataBuffer)) >= 0)
{
digestEngine.update(dataBuffer, 0, bytesRead);
}
byte[] digest = digestEngine.digest();
byte[] signatureValue = session.sign(digest);
return signatureValue;
}
catch (TokenException e)
{
setMsg(e.getMessage());
}
catch (NoSuchAlgorithmException e)
{
setMsg(e.getMessage());
}
catch (FileNotFoundException e)
{
setMsg(e.getMessage());
}
catch (IOException e)
{
setMsg(e.getMessage());
}
return null;
}
@Override
public OutputStream getOutputStream()
{
return null;
}
@Override
public AlgorithmIdentifier getAlgorithmIdentifier()
{
AlgorithmIdentifier algorithmIdentifier = new DefaultSignatureAlgorithmIdentifierFinder().find(hashingAlgo);
return algorithmIdentifier;
}
};
AlgorithmIdentifier algorithmId = contentSigner.getAlgorithmIdentifier();
byte[] signData = contentSigner.getSignature();
DERBitString derBitStr = new DERBitString(signData);
CertificationRequest certReq = new CertificationRequest(certificateRequestInfo, algorithmId, derBitStr);
PKCS10CertificationRequest pkcs10Req = new PKCS10CertificationRequest(certReq);