private static PublicKey parsePemPublicKey(String type, ByteIterator byteIterator) throws IllegalArgumentException { if (! type.equals(PUBLIC_KEY_FORMAT)) { throw log.invalidPemType(PUBLIC_KEY_FORMAT, type); } try { byte[] der = byteIterator.drain(); DERDecoder derDecoder = new DERDecoder(der); derDecoder.startSequence(); switch (derDecoder.peekType()) { case ASN1.SEQUENCE_TYPE: derDecoder.startSequence(); String algorithm = derDecoder.decodeObjectIdentifierAsKeyAlgorithm(); if (algorithm != null) { return KeyFactory.getInstance(algorithm).generatePublic(new X509EncodedKeySpec(der)); } throw log.asnUnrecognisedAlgorithm(algorithm); default: throw log.asnUnexpectedTag(); } } catch (Exception cause) { throw log.publicKeyParseError(cause); } }
private static PrivateKey parsePemPrivateKey(String type, ByteIterator byteIterator) throws IllegalArgumentException { if (! type.equals(PRIVATE_KEY_FORMAT)) { throw log.invalidPemType(PRIVATE_KEY_FORMAT, type); } try { byte[] der = byteIterator.drain(); DERDecoder derDecoder = new DERDecoder(der); derDecoder.startSequence(); // Version if (derDecoder.peekType() != ASN1.INTEGER_TYPE) throw log.asnUnexpectedTag(); derDecoder.skipElement(); // AlgorithmIdentifier derDecoder.startSequence(); String algorithm = derDecoder.decodeObjectIdentifierAsKeyAlgorithm(); if (algorithm != null) { return KeyFactory.getInstance(algorithm).generatePrivate(new PKCS8EncodedKeySpec(der)); } throw log.asnUnrecognisedAlgorithm(algorithm); } catch (Exception cause) { throw log.privateKeyParseError(cause); } }
private static PublicKey parsePemPublicKey(String type, ByteIterator byteIterator) throws IllegalArgumentException { if (! type.equals(PUBLIC_KEY_FORMAT)) { throw log.invalidPemType(PUBLIC_KEY_FORMAT, type); } try { byte[] der = byteIterator.drain(); DERDecoder derDecoder = new DERDecoder(der); derDecoder.startSequence(); switch (derDecoder.peekType()) { case ASN1.SEQUENCE_TYPE: derDecoder.startSequence(); String algorithm = derDecoder.decodeObjectIdentifierAsKeyAlgorithm(); if (algorithm != null) { return KeyFactory.getInstance(algorithm).generatePublic(new X509EncodedKeySpec(der)); } throw log.asnUnrecognisedAlgorithm(algorithm); default: throw log.asnUnexpectedTag(); } } catch (Exception cause) { throw log.publicKeyParseError(cause); } }
private static PublicKey parsePemPublicKey(String type, ByteIterator byteIterator) throws IllegalArgumentException { if (! type.equals(PUBLIC_KEY_FORMAT)) { throw log.invalidPemType(PUBLIC_KEY_FORMAT, type); } try { byte[] der = byteIterator.drain(); DERDecoder derDecoder = new DERDecoder(der); derDecoder.startSequence(); switch (derDecoder.peekType()) { case ASN1.SEQUENCE_TYPE: derDecoder.startSequence(); String algorithm = derDecoder.decodeObjectIdentifierAsKeyAlgorithm(); if (algorithm != null) { return KeyFactory.getInstance(algorithm).generatePublic(new X509EncodedKeySpec(der)); } throw log.asnUnrecognisedAlgorithm(algorithm); default: throw log.asnUnexpectedTag(); } } catch (Exception cause) { throw log.publicKeyParseError(cause); } }
private static PrivateKey parsePemPrivateKey(String type, ByteIterator byteIterator) throws IllegalArgumentException { if (! type.equals(PRIVATE_KEY_FORMAT)) { throw log.invalidPemType(PRIVATE_KEY_FORMAT, type); } try { byte[] der = byteIterator.drain(); DERDecoder derDecoder = new DERDecoder(der); derDecoder.startSequence(); // Version if (derDecoder.peekType() != ASN1.INTEGER_TYPE) throw log.asnUnexpectedTag(); derDecoder.skipElement(); // AlgorithmIdentifier derDecoder.startSequence(); String algorithm = derDecoder.decodeObjectIdentifierAsKeyAlgorithm(); if (algorithm != null) { return KeyFactory.getInstance(algorithm).generatePrivate(new PKCS8EncodedKeySpec(der)); } throw log.asnUnrecognisedAlgorithm(algorithm); } catch (Exception cause) { throw log.privateKeyParseError(cause); } }
private static PublicKey parsePemPublicKey(String type, ByteIterator byteIterator) throws IllegalArgumentException { if (! type.equals(PUBLIC_KEY_FORMAT)) { throw log.invalidPemType(PUBLIC_KEY_FORMAT, type); } try { byte[] der = byteIterator.drain(); DERDecoder derDecoder = new DERDecoder(der); derDecoder.startSequence(); switch (derDecoder.peekType()) { case ASN1.SEQUENCE_TYPE: derDecoder.startSequence(); String algorithm = derDecoder.decodeObjectIdentifierAsKeyAlgorithm(); if (algorithm != null) { return KeyFactory.getInstance(algorithm).generatePublic(new X509EncodedKeySpec(der)); } throw log.asnUnrecognisedAlgorithm(algorithm); default: throw log.asnUnexpectedTag(); } } catch (Exception cause) { throw log.publicKeyParseError(cause); } }
private static PrivateKey parsePemPrivateKey(String type, ByteIterator byteIterator) throws IllegalArgumentException { if (! type.equals(PRIVATE_KEY_FORMAT)) { throw log.invalidPemType(PRIVATE_KEY_FORMAT, type); } try { byte[] der = byteIterator.drain(); DERDecoder derDecoder = new DERDecoder(der); derDecoder.startSequence(); // Version if (derDecoder.peekType() != ASN1.INTEGER_TYPE) throw log.asnUnexpectedTag(); derDecoder.skipElement(); // AlgorithmIdentifier derDecoder.startSequence(); String algorithm = derDecoder.decodeObjectIdentifierAsKeyAlgorithm(); if (algorithm != null) { return KeyFactory.getInstance(algorithm).generatePrivate(new PKCS8EncodedKeySpec(der)); } throw log.asnUnrecognisedAlgorithm(algorithm); } catch (Exception cause) { throw log.privateKeyParseError(cause); } }
private static PrivateKey parsePemPrivateKey(String type, ByteIterator byteIterator) throws IllegalArgumentException { if (! type.equals(PRIVATE_KEY_FORMAT)) { throw log.invalidPemType(PRIVATE_KEY_FORMAT, type); } try { byte[] der = byteIterator.drain(); DERDecoder derDecoder = new DERDecoder(der); derDecoder.startSequence(); // Version if (derDecoder.peekType() != ASN1.INTEGER_TYPE) throw log.asnUnexpectedTag(); derDecoder.skipElement(); // AlgorithmIdentifier derDecoder.startSequence(); String algorithm = derDecoder.decodeObjectIdentifierAsKeyAlgorithm(); if (algorithm != null) { return KeyFactory.getInstance(algorithm).generatePrivate(new PKCS8EncodedKeySpec(der)); } throw log.asnUnrecognisedAlgorithm(algorithm); } catch (Exception cause) { throw log.privateKeyParseError(cause); } }