@Override public String getFormat() { return publicKey.getFormat(); }
private static boolean checkKeysEqual(PublicKey pk1, PublicKey pk2) { // first, they should have the same format // second, their encoded form must be the same // assert(pk1 != null); // assert(pk2 != null); String format1 = pk1.getFormat(); String format2; if ((pk2 == null) || (((format2 = pk2.getFormat()) != null) ^ (format1 != null)) || ((format1 != null) && !format1.equals(format2))) { return false; } return Arrays.equals(pk1.getEncoded(), pk2.getEncoded()); }
private static byte[] encodePublicKey(PublicKey publicKey) throws InvalidKeyException { byte[] encodedPublicKey = null; if ("X.509".equals(publicKey.getFormat())) { encodedPublicKey = publicKey.getEncoded(); } if (encodedPublicKey == null) { try { encodedPublicKey = KeyFactory.getInstance(publicKey.getAlgorithm()) .getKeySpec(publicKey, X509EncodedKeySpec.class) .getEncoded(); } catch (NoSuchAlgorithmException e) { throw new InvalidKeyException( "Failed to obtain X.509 encoded form of public key " + publicKey + " of class " + publicKey.getClass().getName(), e); } catch (InvalidKeySpecException e) { throw new InvalidKeyException( "Failed to obtain X.509 encoded form of public key " + publicKey + " of class " + publicKey.getClass().getName(), e); } } if ((encodedPublicKey == null) || (encodedPublicKey.length == 0)) { throw new InvalidKeyException( "Failed to obtain X.509 encoded form of public key " + publicKey + " of class " + publicKey.getClass().getName()); } return encodedPublicKey; }
entry.put( PUBLIC_KEY_FORMAT_AT, publicKey.getFormat() ); LOG.debug( "PublicKey: {}", publicKey );
entry.put( PUBLIC_KEY_FORMAT_AT, publicKey.getFormat() ); LOG.debug( "PublicKey: {}", publicKey );
@Test public void testKeysConversionMethods() throws Exception { final KeyPair kp = CertUtils.generateRandomKeyPair(2048); final PrivateKey inPrivateKey = kp.getPrivate(); final PrivateKey outPrivateKey = CertUtils.pemToPrivateKey(CertUtils.privateKeyToPem(inPrivateKey)); Assert.assertEquals(inPrivateKey.getAlgorithm(), outPrivateKey.getAlgorithm()); Assert.assertEquals(inPrivateKey.getFormat(), outPrivateKey.getFormat()); Assert.assertArrayEquals(inPrivateKey.getEncoded(), outPrivateKey.getEncoded()); final PublicKey inPublicKey = kp.getPublic(); final PublicKey outPublicKey = CertUtils.pemToPublicKey(CertUtils.publicKeyToPem(inPublicKey)); Assert.assertEquals(inPublicKey.getAlgorithm(), outPublicKey.getAlgorithm()); Assert.assertEquals(inPublicKey.getFormat(), inPublicKey.getFormat()); Assert.assertArrayEquals(inPublicKey.getEncoded(), outPublicKey.getEncoded()); }
@Override public String getFormat() { return publicKey.getFormat(); }
private static boolean checkKeysEqual(PublicKey pk1, PublicKey pk2) { // first, they should have the same format // second, their encoded form must be the same // assert(pk1 != null); // assert(pk2 != null); String format1 = pk1.getFormat(); String format2; if ((pk2 == null) || (((format2 = pk2.getFormat()) != null) ^ (format1 != null)) || ((format1 != null) && !format1.equals(format2))) { return false; } return Arrays.equals(pk1.getEncoded(), pk2.getEncoded()); }
private static boolean checkKeysEqual(PublicKey pk1, PublicKey pk2) { // first, they should have the same format // second, their encoded form must be the same // assert(pk1 != null); // assert(pk2 != null); String format1 = pk1.getFormat(); String format2; if ((pk2 == null) || (((format2 = pk2.getFormat()) != null) ^ (format1 != null)) || ((format1 != null) && !format1.equals(format2))) { return false; } return Arrays.equals(pk1.getEncoded(), pk2.getEncoded()); }
private static boolean checkKeysEqual(PublicKey pk1, PublicKey pk2) { // first, they should have the same format // second, their encoded form must be the same // assert(pk1 != null); // assert(pk2 != null); String format1 = pk1.getFormat(); String format2; if ((pk2 == null) || (((format2 = pk2.getFormat()) != null) ^ (format1 != null)) || ((format1 != null) && !format1.equals(format2))) { return false; } return Arrays.equals(pk1.getEncoded(), pk2.getEncoded()); }
private static boolean checkKeysEqual(PublicKey pk1, PublicKey pk2) { // first, they should have the same format // second, their encoded form must be the same // assert(pk1 != null); // assert(pk2 != null); String format1 = pk1.getFormat(); String format2; if ((pk2 == null) || (((format2 = pk2.getFormat()) != null) ^ (format1 != null)) || ((format1 != null) && !format1.equals(format2))) { return false; } return Arrays.equals(pk1.getEncoded(), pk2.getEncoded()); }
private static boolean checkKeysEqual(PublicKey pk1, PublicKey pk2) { // first, they should have the same format // second, their encoded form must be the same // assert(pk1 != null); // assert(pk2 != null); String format1 = pk1.getFormat(); String format2; if ((pk2 == null) || (((format2 = pk2.getFormat()) != null) ^ (format1 != null)) || ((format1 != null) && !format1.equals(format2))) { return false; } return Arrays.equals(pk1.getEncoded(), pk2.getEncoded()); }
private static boolean checkKeysEqual(PublicKey pk1, PublicKey pk2) { // first, they should have the same format // second, their encoded form must be the same // assert(pk1 != null); // assert(pk2 != null); String format1 = pk1.getFormat(); String format2; if ((pk2 == null) || (((format2 = pk2.getFormat()) != null) ^ (format1 != null)) || ((format1 != null) && !format1.equals(format2))) { return false; } return Arrays.equals(pk1.getEncoded(), pk2.getEncoded()); }
/** * Given a certificate and public key, test whether the public key in the certificate matches * the expected public key. * * @param cert certificate The certificate to test * @param pubKey the expected public key * @return {@code true} if the certificate matches the public key, {@code false} otherwise. */ public static boolean certMatchesPubKey(X509Certificate cert, PublicKey pubKey) { PublicKey extractedPubKey = cert.getPublicKey(); return extractedPubKey.getFormat() == pubKey.getFormat() && extractedPubKey.getAlgorithm() == pubKey.getAlgorithm() && Arrays.equals(extractedPubKey.getEncoded(), pubKey.getEncoded()); }
private void populateDialog() throws CryptoException { KeyInfo keyInfo = KeyPairUtil.getKeyInfo(publicKey); jtfAlgorithm.setText(keyInfo.getAlgorithm()); Integer keyLength = keyInfo.getSize(); if (keyLength != null) { jtfKeySize.setText(MessageFormat.format(res.getString("DViewPublicKey.jtfKeySize.text"), "" + keyLength)); } else { jtfKeySize.setText(MessageFormat.format(res.getString("DViewPublicKey.jtfKeySize.text"), "?")); } jtfFormat.setText(publicKey.getFormat()); jtaEncoded.setText(new BigInteger(1, publicKey.getEncoded()).toString(16).toUpperCase()); jtaEncoded.setCaretPosition(0); if ((publicKey instanceof RSAPublicKey) || (publicKey instanceof DSAPublicKey)) { jbFields.setEnabled(true); } else { jbFields.setEnabled(false); } }
/** * Method getEncodedDER * * @return the public key * @throws XMLSecurityException */ protected byte[] getEncodedDER(PublicKey publicKey) throws XMLSecurityException { try { KeyFactory keyFactory = KeyFactory.getInstance(publicKey.getAlgorithm()); X509EncodedKeySpec keySpec = keyFactory.getKeySpec(publicKey, X509EncodedKeySpec.class); return keySpec.getEncoded(); } catch (NoSuchAlgorithmException e) { Object exArgs[] = { publicKey.getAlgorithm(), publicKey.getFormat(), publicKey.getClass().getName() }; throw new XMLSecurityException(e, "DEREncodedKeyValue.UnsupportedPublicKey", exArgs); } catch (InvalidKeySpecException e) { Object exArgs[] = { publicKey.getAlgorithm(), publicKey.getFormat(), publicKey.getClass().getName() }; throw new XMLSecurityException(e, "DEREncodedKeyValue.UnsupportedPublicKey", exArgs); } }
public X509Builder subjectPublicKey(PublicKey publicKey) { try { subjectPublicKey = new CertificateX509Key(publicKey); info.set(X509CertInfo.KEY, subjectPublicKey); // CertificateException, IOException } catch(Exception e) { fault(e, "subjectPublicKey(%s)", publicKey==null?"null":String.format("%d bytes, %s %s",publicKey.getEncoded().length,publicKey.getAlgorithm(),publicKey.getFormat())); } return this; }
static void writeCertificate(final ModelNode certificateModel, final Certificate certificate) throws CertificateEncodingException, NoSuchAlgorithmException { certificateModel.get(ElytronDescriptionConstants.TYPE).set(certificate.getType()); PublicKey publicKey = certificate.getPublicKey(); certificateModel.get(ElytronDescriptionConstants.ALGORITHM).set(publicKey.getAlgorithm()); certificateModel.get(ElytronDescriptionConstants.FORMAT).set(publicKey.getFormat()); certificateModel.get(ElytronDescriptionConstants.PUBLIC_KEY).set(encodedHexString(publicKey.getEncoded())); byte[] encodedCertificate = certificate.getEncoded(); certificateModel.get(ElytronDescriptionConstants.SHA_1_DIGEST).set(encodedHexString(digest(SHA_1, encodedCertificate))); certificateModel.get(ElytronDescriptionConstants.SHA_256_DIGEST).set(encodedHexString(digest(SHA_256, encodedCertificate))); certificateModel.get(ElytronDescriptionConstants.ENCODED).set(encodedHexString(encodedCertificate)); if (certificate instanceof X509Certificate) { writeX509Certificate(certificateModel, (X509Certificate) certificate); } }
static OpenSSLKey fromPublicKey(PublicKey key) throws InvalidKeyException { if (key instanceof OpenSSLKeyHolder) { return ((OpenSSLKeyHolder) key).getOpenSSLKey(); } if (!"X.509".equals(key.getFormat())) { throw new InvalidKeyException("Unknown key format " + key.getFormat()); } final byte[] encoded = key.getEncoded(); if (encoded == null) { throw new InvalidKeyException("Key encoding is null"); } try { return new OpenSSLKey(NativeCrypto.EVP_parse_public_key(key.getEncoded())); } catch (Exception e) { throw new InvalidKeyException(e); } }
static OpenSSLKey fromPublicKey(PublicKey key) throws InvalidKeyException { if (key instanceof OpenSSLKeyHolder) { return ((OpenSSLKeyHolder) key).getOpenSSLKey(); } if (!"X.509".equals(key.getFormat())) { throw new InvalidKeyException("Unknown key format " + key.getFormat()); } final byte[] encoded = key.getEncoded(); if (encoded == null) { throw new InvalidKeyException("Key encoding is null"); } try { return new OpenSSLKey(NativeCrypto.EVP_parse_public_key(key.getEncoded())); } catch (Exception e) { throw new InvalidKeyException(e); } }