public static byte[] EMSA_PKCS1_v1_5_encoding(byte[] encodedDigestInfo, int modulusBigLength) throws XiSecurityException { Args.notNull(encodedDigestInfo, "encodedDigestInfo"); int msgLen = encodedDigestInfo.length; int blockSize = (modulusBigLength + 7) / 8; if (msgLen + 3 > blockSize) { throw new XiSecurityException("data too long (maximal " + (blockSize - 3) + " allowed): " + msgLen); } byte[] block = new byte[blockSize]; block[0] = 0x00; // type code 1 block[1] = 0x01; int offset = 2; while (offset < block.length - msgLen - 1) { block[offset++] = (byte) 0xFF; } // mark the end of the padding block[offset++] = 0x00; System.arraycopy(encodedDigestInfo, 0, block, offset, encodedDigestInfo.length); return block; }
@Override public void refreshTokenForSignerType(String signerType) throws CaMgmtException { try { securityFactory.refreshTokenForSignerType(signerType); } catch (XiSecurityException ex) { throw new CaMgmtException("could not refresh token for signer type " + signerType + ": " + ex.getMessage(), ex); } }
public static byte[] EMSA_PSS_ENCODE(HashAlgo contentDigest, byte[] hashValue, HashAlgo mgfDigest, int saltLen, int modulusBitLength, SecureRandom random) throws XiSecurityException { final int hLen = contentDigest.getLength(); final byte[] salt = new byte[saltLen]; final byte[] mDash = new byte[8 + saltLen + hLen]; final byte trailer = (byte)0xBC; if (hashValue.length != hLen) { throw new XiSecurityException("hashValue.length is incorrect: " + hashValue.length + " != " + hLen); } int emBits = modulusBitLength - 1; if (emBits < (8 * hLen + 8 * saltLen + 9)) { throw new IllegalArgumentException("key too small for specified hash and salt lengths"); } System.arraycopy(hashValue, 0, mDash, mDash.length - hLen - saltLen, hLen); random.nextBytes(salt); System.arraycopy(salt, 0, mDash, mDash.length - saltLen, saltLen); byte[] hv = contentDigest.hash(mDash); byte[] block = new byte[(emBits + 7) / 8]; block[block.length - saltLen - 1 - hLen - 1] = 0x01; System.arraycopy(salt, 0, block, block.length - saltLen - hLen - 1, saltLen); byte[] dbMask = maskGeneratorFunction1(mgfDigest, hv, block.length - hLen - 1); for (int i = 0; i != dbMask.length; i++) { block[i] ^= dbMask[i]; } block[0] &= (0xff >> ((block.length * 8) - emBits)); System.arraycopy(hv, 0, block, block.length - hLen - 1, hLen); block[block.length - 1] = trailer; return block; }
@Override public byte[] getSignature() { try { byte[] plainSignature = getPlainSignature(); return plain ? plainSignature : SignerUtil.dsaSigPlainToX962(plainSignature); } catch (XiSecurityException ex) { LogUtil.warn(LOG, ex); throw new RuntimeCryptoException("XiSecurityException: " + ex.getMessage()); } catch (Throwable th) { LogUtil.warn(LOG, th); throw new RuntimeCryptoException(th.getClass().getName() + ": " + th.getMessage()); } }
@Override public void refreshToken(String type) throws XiSecurityException { if (!TYPE.equalsIgnoreCase(type)) { // Nothing to do return; } Set<String> errorModules = new HashSet<>(2); for (String name : p11CryptServiceFactory.getModuleNames()) { try { p11CryptServiceFactory.getP11CryptService(name).refresh(); } catch (P11TokenException ex) { LogUtil.error(LOG, ex, "could not refresh PKCS#11 module " + name); errorModules.add(name); } } if (!errorModules.isEmpty()) { throw new XiSecurityException("could not refreshed modules " + errorModules); } }
@Override public byte[] getSignature() { try { byte[] plainSignature = getPlainSignature(); return plain ? plainSignature : SignerUtil.dsaSigPlainToX962(plainSignature); } catch (XiSecurityException ex) { LogUtil.warn(LOG, ex); throw new RuntimeCryptoException("XiSecurityException: " + ex.getMessage()); } catch (Throwable th) { LogUtil.warn(LOG, th); throw new RuntimeCryptoException(th.getClass().getName() + ": " + th.getMessage()); } }
public static byte[] dsaSigToPlain(BigInteger sigR, BigInteger sigS, int keyBitLen) throws XiSecurityException { Args.notNull(sigR, "sigR"); Args.notNull(sigS, "sigS"); final int blockSize = (keyBitLen + 7) / 8; int bitLenOfR = sigR.bitLength(); int bitLenOfS = sigS.bitLength(); int bitLen = Math.max(bitLenOfR, bitLenOfS); if ((bitLen + 7) / 8 > blockSize) { throw new XiSecurityException("signature is too large"); } byte[] plainSignature = new byte[2 * blockSize]; bigIntToBytes(sigR, plainSignature, 0, blockSize); bigIntToBytes(sigS, plainSignature, blockSize, blockSize); return plainSignature; }
@Override public byte[] getSignature() { try { byte[] plainSignature = getPlainSignature(); return SignerUtil.dsaSigPlainToX962(plainSignature); } catch (XiSecurityException ex) { LogUtil.warn(LOG, ex); throw new RuntimeCryptoException("XiSecurityException: " + ex.getMessage()); } catch (Throwable th) { LogUtil.warn(LOG, th); throw new RuntimeCryptoException(th.getClass().getName() + ": " + th.getMessage()); } }
public static List<String[]> splitCaSignerConfs(String conf) throws XiSecurityException { ConfPairs pairs = new ConfPairs(conf); String str = pairs.value("algo"); if (str == null) { throw new XiSecurityException("no algo is defined in CA signerConf"); } List<String> list = StringUtil.split(str, ":"); if (CollectionUtil.isEmpty(list)) { throw new XiSecurityException("empty algo is defined in CA signerConf"); } List<String[]> signerConfs = new ArrayList<>(list.size()); for (String n : list) { String c14nAlgo; try { c14nAlgo = AlgorithmUtil.canonicalizeSignatureAlgo(n); } catch (NoSuchAlgorithmException ex) { throw new XiSecurityException(ex.getMessage(), ex); } pairs.putPair("algo", c14nAlgo); signerConfs.add(new String[]{c14nAlgo, pairs.getEncoded()}); } return signerConfs; }
private byte[] rsaPkcsSign(byte[] contentToSign, HashAlgo hashAlgo) throws P11TokenException { int modulusBitLen = getSignatureKeyBitLength(); byte[] paddedHash; try { if (hashAlgo == null) { paddedHash = SignerUtil.EMSA_PKCS1_v1_5_encoding(contentToSign, modulusBitLen); } else { byte[] hash = hashAlgo.hash(contentToSign); paddedHash = SignerUtil.EMSA_PKCS1_v1_5_encoding(hash, modulusBitLen, hashAlgo); } } catch (XiSecurityException ex) { throw new P11TokenException("XiSecurityException: " + ex.getMessage(), ex); } return rsaX509Sign(paddedHash); }
public static byte[] dsaSigPlainToX962(byte[] signature) throws XiSecurityException { Args.notNull(signature, "signature"); if (signature.length % 2 != 0) { throw new XiSecurityException("signature.lenth must be even, but is odd"); } byte[] ba = new byte[signature.length / 2]; ASN1EncodableVector sigder = new ASN1EncodableVector(); System.arraycopy(signature, 0, ba, 0, ba.length); sigder.add(new ASN1Integer(new BigInteger(1, ba))); System.arraycopy(signature, ba.length, ba, 0, ba.length); sigder.add(new ASN1Integer(new BigInteger(1, ba))); DERSequence seq = new DERSequence(sigder); try { return seq.getEncoded(); } catch (IOException ex) { throw new XiSecurityException("IOException, message: " + ex.getMessage(), ex); } }
@Override protected Signer createSigner(AlgorithmIdentifier sigAlgId) throws OperatorCreationException { if (PKCSObjectIdentifiers.id_RSASSA_PSS.equals(sigAlgId.getAlgorithm())) { try { return SignerUtil.createPSSRSASigner(sigAlgId); } catch (XiSecurityException ex) { throw new OperatorCreationException(ex.getMessage(), ex); } } else { AlgorithmIdentifier digAlg = digestAlgorithmFinder.find(sigAlgId); return new RSADigestSigner(digestProvider.get(digAlg)); } }
throw new XiSecurityException("unknown key named " + tmpKeyname); } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | UnrecoverableKeyException | ClassCastException ex) { throw new XiSecurityException(ex.getMessage(), ex);
private byte[] dsaAndEcdsaSign(byte[] dataToSign, HashAlgo hashAlgo) throws P11TokenException { byte[] hash = (hashAlgo == null) ? dataToSign : hashAlgo.hash(dataToSign); ConcurrentBagEntry<Signature> sig0; try { sig0 = dsaSignatures.borrow(5000, TimeUnit.MILLISECONDS); } catch (InterruptedException ex) { throw new P11TokenException("InterruptedException occurs while retrieving idle signature"); } if (sig0 == null) { throw new P11TokenException("no idle DSA Signature available"); } try { Signature sig = sig0.value(); sig.update(hash); byte[] x962Signature = sig.sign(); return SignerUtil.dsaSigX962ToPlain(x962Signature, getSignatureKeyBitLength()); } catch (SignatureException ex) { throw new P11TokenException("SignatureException: " + ex.getMessage(), ex); } catch (XiSecurityException ex) { throw new P11TokenException("XiSecurityException: " + ex.getMessage(), ex); } finally { dsaSignatures.requite(sig0); } }
cipher0 = Cipher.getInstance("AES/GCM/NoPadding"); } catch (NoSuchAlgorithmException | NoSuchPaddingException ex2) { throw new XiSecurityException(ex2); this.sigAlgIdTemplate = new AlgorithmIdentifier(oid, params).getEncoded(); } catch (IOException ex) { throw new XiSecurityException("could not encode AlgorithmIdentifier", ex); if (keyLen == 16) { if (!oid.equals(NISTObjectIdentifiers.id_aes128_GCM)) { throw new XiSecurityException("oid and singingKey do not match"); throw new XiSecurityException("oid and singingKey do not match"); throw new XiSecurityException("oid and singingKey do not match"); throw new XiSecurityException("invalid AES key length: " + keyLen); cipher.init(Cipher.ENCRYPT_MODE, signingKey, new GCMParameterSpec(tagByteLen << 3, nonce)); } catch (InvalidKeyException | InvalidAlgorithmParameterException ex) { throw new XiSecurityException(ex);
protected Signer createSigner(AlgorithmIdentifier sigAlgId, AlgorithmIdentifier digAlgId) throws OperatorCreationException { if (!AlgorithmUtil.isRSASigAlgId(sigAlgId)) { throw new OperatorCreationException("the given algorithm is not a valid RSA signature " + "algirthm '" + sigAlgId.getAlgorithm().getId() + "'"); } if (!PKCSObjectIdentifiers.id_RSASSA_PSS.equals(sigAlgId.getAlgorithm())) { Digest dig = digestProvider.get(digAlgId); return new RSADigestSigner(dig); } try { return SignerUtil.createPSSRSASigner(sigAlgId); } catch (XiSecurityException ex) { throw new OperatorCreationException(ex.getMessage(), ex); } }
public XiWrappedContentSigner(ContentSigner signer, boolean fixedAlgorithmIdentifier) throws XiSecurityException { this.signer = Args.notNull(signer, "signer"); if (fixedAlgorithmIdentifier) { try { this.encodedAlgorithmIdentifier = signer.getAlgorithmIdentifier().getEncoded(); } catch (IOException ex) { throw new XiSecurityException("could not encode AlgorithmIdentifier", ex); } } }
private byte[] sm2SignHash(byte[] hash) throws P11TokenException { ConcurrentBagEntry<SM2Signer> sig0; try { sig0 = sm2Signers.borrow(5000, TimeUnit.MILLISECONDS); } catch (InterruptedException ex) { throw new P11TokenException("InterruptedException occurs while retrieving idle signature"); } if (sig0 == null) { throw new P11TokenException("no idle SM2 Signer available"); } try { SM2Signer sig = sig0.value(); byte[] x962Signature = sig.generateSignatureForHash(hash); return SignerUtil.dsaSigX962ToPlain(x962Signature, getSignatureKeyBitLength()); } catch (CryptoException ex) { throw new P11TokenException("CryptoException: " + ex.getMessage(), ex); } catch (XiSecurityException ex) { throw new P11TokenException("XiSecurityException: " + ex.getMessage(), ex); } finally { sm2Signers.requite(sig0); } }
throw new XiSecurityException("unknown key named " + tmpKeyname); throw new XiSecurityException("unsupported key " + key.getClass().getName()); } catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException | UnrecoverableKeyException | ClassCastException ex) { throw new XiSecurityException(ex.getMessage(), ex);
private byte[] rsaPkcsPssSign(P11Params parameters, byte[] contentToSign, HashAlgo hashAlgo) throws P11TokenException { if (!(parameters instanceof P11Params.P11RSAPkcsPssParams)) { throw new P11TokenException("the parameters is not of " + P11Params.P11RSAPkcsPssParams.class.getName()); } P11Params.P11RSAPkcsPssParams pssParam = (P11Params.P11RSAPkcsPssParams) parameters; HashAlgo contentHash = getHashAlgoForPkcs11HashMech(pssParam.getHashAlgorithm()); if (contentHash == null) { throw new P11TokenException("unsupported HashAlgorithm " + pssParam.getHashAlgorithm()); } else if (hashAlgo != null && contentHash != hashAlgo) { throw new P11TokenException("Invalid parameters: invalid hash algorithm"); } HashAlgo mgfHash = getHashAlgoForPkcs11MgfMech( pssParam.getMaskGenerationFunction()); if (mgfHash == null) { throw new P11TokenException( "unsupported MaskGenerationFunction " + pssParam.getHashAlgorithm()); } byte[] hashValue = (hashAlgo == null) ? contentToSign : hashAlgo.hash(contentToSign); byte[] encodedHashValue; try { encodedHashValue = SignerUtil.EMSA_PSS_ENCODE(contentHash, hashValue, mgfHash, (int) pssParam.getSaltLength(), getSignatureKeyBitLength(), random); } catch (XiSecurityException ex) { throw new P11TokenException("XiSecurityException: " + ex.getMessage(), ex); } return rsaX509Sign(encodedHashValue); }