001/** 002 * 003 * Copyright 2017 Florian Schmaus 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.eme.element; 018 019import java.util.HashMap; 020import java.util.Map; 021 022import org.jivesoftware.smack.packet.ExtensionElement; 023import org.jivesoftware.smack.packet.Message; 024import org.jivesoftware.smack.util.StringUtils; 025import org.jivesoftware.smack.util.XmlStringBuilder; 026 027public class ExplicitMessageEncryptionElement implements ExtensionElement { 028 029 private static final Map<String, ExplicitMessageEncryptionProtocol> PROTOCOL_LUT = new HashMap<>(); 030 031 public static final String ELEMENT = "encryption"; 032 033 public static final String NAMESPACE = "urn:xmpp:eme:0"; 034 035 public enum ExplicitMessageEncryptionProtocol { 036 037 /** 038 * The encryption method specified in <a href="https://xmpp.org/extensions/xep-0373.html">XEP-0373: OpenPGP for 039 * XMPP</a>. 040 */ 041 openpgpV0("urn:xmpp:openpgp:0", "OpenPGP for XMPP (XEP-0373)"), 042 043 otrV0("urn:xmpp:otr:0", "Off-the-Record Messaging (XEP-0364)"), 044 045 omemoVAxolotl("eu.siacs.conversations.axolotl", "OMEMO Multi End Message and Object Encryption (XEP-0384)"), 046 047 legacyOpenPGP("jabber:x:encrypted", "Legacy OpenPGP for XMPP [DANGEROUS, DO NOT USE!]"), 048 ; 049 050 private final String namespace; 051 private final String name; 052 053 ExplicitMessageEncryptionProtocol(String namespace, String name) { 054 this.namespace = namespace; 055 this.name = name; 056 PROTOCOL_LUT.put(namespace, this); 057 } 058 059 public String getNamespace() { 060 return namespace; 061 } 062 063 public String getName() { 064 return name; 065 } 066 067 public static ExplicitMessageEncryptionProtocol from(String namespace) { 068 return PROTOCOL_LUT.get(namespace); 069 } 070 } 071 072 private final String encryptionNamespace; 073 074 private final String name; 075 076 private boolean isUnknownProtocol; 077 078 private ExplicitMessageEncryptionProtocol protocolCache; 079 080 public ExplicitMessageEncryptionElement(ExplicitMessageEncryptionProtocol protocol) { 081 this(protocol.getNamespace(), protocol.getName()); 082 } 083 084 public ExplicitMessageEncryptionElement(String encryptionNamespace) { 085 this(encryptionNamespace, null); 086 } 087 088 public ExplicitMessageEncryptionElement(String encryptionNamespace, String name) { 089 this.encryptionNamespace = StringUtils.requireNotNullOrEmpty(encryptionNamespace, 090 "encryptionNamespace must not be null"); 091 this.name = name; 092 } 093 094 public ExplicitMessageEncryptionProtocol getProtocol() { 095 if (protocolCache != null) { 096 return protocolCache; 097 } 098 099 if (isUnknownProtocol) { 100 return null; 101 } 102 103 ExplicitMessageEncryptionProtocol protocol = PROTOCOL_LUT.get(encryptionNamespace); 104 if (protocol == null) { 105 isUnknownProtocol = true; 106 return null; 107 } 108 109 protocolCache = protocol; 110 return protocol; 111 } 112 113 public String getEncryptionNamespace() { 114 return encryptionNamespace; 115 } 116 117 /** 118 * Get the optional name of the encryption method. 119 * 120 * @return the name of the encryption method or <code>null</code>. 121 */ 122 public String getName() { 123 return name; 124 } 125 126 @Override 127 public String getElementName() { 128 return ELEMENT; 129 } 130 131 @Override 132 public String getNamespace() { 133 return NAMESPACE; 134 } 135 136 @Override 137 public XmlStringBuilder toXML(String enclosingNamespace) { 138 XmlStringBuilder xml = new XmlStringBuilder(this); 139 xml.attribute("namespace", getEncryptionNamespace()); 140 xml.optAttribute("name", getName()); 141 xml.closeEmptyElement(); 142 return xml; 143 } 144 145 public static ExplicitMessageEncryptionElement from(Message message) { 146 return message.getExtension(ELEMENT, NAMESPACE); 147 } 148}