Smack/smack-openpgp/src/main/java/org/jivesoftware/smackx/ox/OpenPgpMessage.java

170 lines
5.7 KiB
Java
Raw Normal View History

/**
*
* Copyright 2017 Florian Schmaus.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2018-07-04 16:02:03 +02:00
package org.jivesoftware.smackx.ox;
import java.io.IOException;
2018-06-13 18:39:09 +02:00
import java.nio.charset.Charset;
import java.util.HashSet;
import java.util.Set;
2018-06-13 18:39:09 +02:00
import org.jivesoftware.smack.util.Objects;
2018-05-30 22:06:09 +02:00
import org.jivesoftware.smackx.ox.element.CryptElement;
import org.jivesoftware.smackx.ox.element.OpenPgpContentElement;
2018-05-30 22:06:09 +02:00
import org.jivesoftware.smackx.ox.element.OpenPgpElement;
import org.jivesoftware.smackx.ox.element.SignElement;
import org.jivesoftware.smackx.ox.element.SigncryptElement;
import org.jivesoftware.smackx.ox.provider.OpenPgpContentElementProvider;
import org.xmlpull.v1.XmlPullParserException;
2018-05-30 22:06:09 +02:00
/**
* This class embodies a decrypted {@link OpenPgpElement}.
*/
public class OpenPgpMessage {
2018-05-10 16:00:27 +02:00
public enum State {
2018-05-30 22:06:09 +02:00
/**
* Represents a {@link SigncryptElement}.
*/
signcrypt,
2018-05-30 22:06:09 +02:00
/**
* Represents a {@link SignElement}.
*/
sign,
2018-05-30 22:06:09 +02:00
/**
* Represents a {@link CryptElement}.
*/
crypt,
;
}
private final String element;
2018-06-13 18:39:09 +02:00
private final State state;
2018-07-04 16:02:03 +02:00
private final Metadata metadata;
private OpenPgpContentElement openPgpContentElement;
2018-05-30 22:06:09 +02:00
/**
* Constructor.
*
2018-07-04 16:02:03 +02:00
* @param metadata Metadata about the encryption
2018-05-30 22:06:09 +02:00
* @param content XML representation of the decrypted {@link OpenPgpContentElement}.
*/
2018-07-04 16:02:03 +02:00
public OpenPgpMessage(String content, Metadata metadata) {
this.metadata = Objects.requireNonNull(metadata);
this.state = Objects.requireNonNull(metadata.getState());
2018-06-13 18:39:09 +02:00
this.element = Objects.requireNonNull(content);
}
public OpenPgpMessage(byte[] bytes, Metadata metadata) {
2018-07-04 16:02:03 +02:00
this(new String(Objects.requireNonNull(bytes), Charset.forName("UTF-8")), metadata);
}
2018-05-30 22:06:09 +02:00
/**
* Return the decrypted {@link OpenPgpContentElement} of this message.
* To determine, whether the element is a {@link SignElement}, {@link CryptElement} or {@link SigncryptElement},
* please consult {@link #getState()}.
*
* @return {@link OpenPgpContentElement}
* @throws XmlPullParserException if the parser encounters an error.
* @throws IOException if the parser encounters an error.
*/
public OpenPgpContentElement getOpenPgpContentElement() throws XmlPullParserException, IOException {
ensureOpenPgpContentElementSet();
return openPgpContentElement;
}
private void ensureOpenPgpContentElementSet() throws XmlPullParserException, IOException {
if (openPgpContentElement != null)
return;
openPgpContentElement = OpenPgpContentElementProvider.parseOpenPgpContentElement(element);
2018-05-21 15:42:04 +02:00
if (openPgpContentElement == null) {
return;
}
2018-05-21 15:42:04 +02:00
2018-05-30 22:06:09 +02:00
// Determine the state of the content element.
2018-05-21 15:42:04 +02:00
if (openPgpContentElement instanceof SigncryptElement) {
2018-06-13 18:39:09 +02:00
if (state != State.signcrypt) {
throw new IllegalStateException("OpenPgpContentElement was signed and encrypted, but is not a SigncryptElement.");
}
2018-05-21 15:42:04 +02:00
} else if (openPgpContentElement instanceof SignElement) {
2018-06-13 18:39:09 +02:00
if (state != State.sign) {
throw new IllegalStateException("OpenPgpContentElement was signed and unencrypted, but is not a SignElement.");
}
2018-05-30 22:06:09 +02:00
} else if (openPgpContentElement instanceof CryptElement) {
2018-06-13 18:39:09 +02:00
if (state != State.crypt) {
throw new IllegalStateException("OpenPgpContentElement was unsigned and encrypted, but is not a CryptElement.");
}
}
}
2018-05-30 22:06:09 +02:00
/**
* Return the state of the message. This value determines, whether the message was a {@link SignElement},
* {@link CryptElement} or {@link SigncryptElement}.
*
* @return state of the content element.
* @throws IOException if the parser encounters an error.
* @throws XmlPullParserException if the parser encounters and error.
*/
public State getState() throws IOException, XmlPullParserException {
ensureOpenPgpContentElementSet();
return state;
}
2018-06-13 18:39:09 +02:00
2018-07-04 16:02:03 +02:00
public Metadata getMetadata() {
return metadata;
}
2018-06-13 18:39:09 +02:00
public static class Metadata {
private final Long encryptionKeyId;
private final Set<Long> validSignatureIds;
public Metadata(Long encryptionKeyId, Set<Long> validSignatureIds) {
this.encryptionKeyId = encryptionKeyId;
this.validSignatureIds = validSignatureIds;
}
public Long getEncryptionKeyId() {
return encryptionKeyId;
}
public Set<Long> getValidSignatureIds() {
return new HashSet<>(validSignatureIds);
}
public State getState() {
if (validSignatureIds.size() != 0) {
if (encryptionKeyId != null) {
return State.signcrypt;
} else {
return State.sign;
}
} else {
if (encryptionKeyId != null) {
return State.crypt;
} else {
throw new IllegalStateException("OpenPGP message appears to be neither encrypted, " +
"nor signed.");
}
}
}
}
}