1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.pkix.params.x509certificate.extension

File X509IpAddress.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

10
29
8
1
144
74
17
0.59
3.62
8
2.12

Classes

Class Line # Actions
X509IpAddress 36 29 0% 17 7
0.8510638585.1%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.crypto.pkix.params.x509certificate.extension;
21   
22    import java.net.InetAddress;
23    import java.net.UnknownHostException;
24    import java.util.Arrays;
25   
26    import org.bouncycastle.asn1.DEROctetString;
27    import org.bouncycastle.asn1.x509.GeneralName;
28    import org.xwiki.crypto.pkix.internal.extension.BcGeneralName;
29   
30    /**
31    * IP address general name.
32    *
33    * @version $Id: 13ebca4e9e268304d6c88e258e1ab60916b4ca05 $
34    * @since 5.4
35    */
 
36    public class X509IpAddress implements X509StringGeneralName, BcGeneralName
37    {
38    private final byte[] ipAddress;
39   
40    /**
41    * Construct a IP address general name from an ip address.
42    *
43    * @param ipAddress the ip address.
44    */
 
45  5 toggle public X509IpAddress(String ipAddress)
46    {
47  5 GeneralName name = new GeneralName(GeneralName.iPAddress, ipAddress);
48  5 this.ipAddress = DEROctetString.getInstance(name.getName()).getOctets();
49    }
50   
51    /**
52    * Construct a IP address general name from an ip address.
53    *
54    * @param ipAddress the ip address.
55    */
 
56  2 toggle public X509IpAddress(InetAddress ipAddress)
57    {
58  2 this.ipAddress = ipAddress.getAddress();
59    }
60   
61    /**
62    * Construct a IP address general name from an ip address.
63    *
64    * @param ipAddress the ip address.
65    * @param ipMask the ip mask.
66    */
 
67  2 toggle public X509IpAddress(InetAddress ipAddress, InetAddress ipMask)
68    {
69  2 byte[] ip = ipAddress.getAddress();
70  2 byte[] mask = ipMask.getAddress();
71   
72  2 if (ip.length != mask.length) {
73  0 throw new IllegalArgumentException("Incompatible ip address (" + ip.length
74    + ") and ip mask (" + mask.length + ")");
75    }
76  2 this.ipAddress = new byte[ip.length + mask.length];
77  2 System.arraycopy(ip, 0, this.ipAddress, 0, ip.length);
78  2 System.arraycopy(mask, 0, this.ipAddress, ip.length, mask.length);
79    }
80   
81    /**
82    * Create a new instance from a Bouncy Castle general name.
83    *
84    * @param name the Bouncy Castle general name.
85    */
 
86  9 toggle public X509IpAddress(GeneralName name)
87    {
88  9 if (name.getTagNo() != GeneralName.iPAddress) {
89  0 throw new IllegalArgumentException("Incompatible general name: " + name.getTagNo());
90    }
91  9 this.ipAddress = DEROctetString.getInstance(name.getName()).getOctets();
92    }
93   
94    /**
95    * @return the ip net address represented by this general name.
96    * @throws UnknownHostException if the ip address is malformed.
97    */
 
98  23 toggle public InetAddress getIpAddress() throws UnknownHostException
99    {
100  23 byte[] ip = this.ipAddress;
101   
102  23 if (ip.length == 8 || ip.length == 32) {
103  15 ip = new byte[ip.length / 2];
104  15 System.arraycopy(this.ipAddress, 0, ip, 0, ip.length);
105    }
106   
107  23 return InetAddress.getByAddress(ip);
108    }
109   
110    /**
111    * @return the ip net mask represented by this general name, or null if no mask was given.
112    * @throws UnknownHostException if the ip mask is malformed.
113    */
 
114  15 toggle public InetAddress getIpMask() throws UnknownHostException
115    {
116  15 if (this.ipAddress.length != 8 && this.ipAddress.length != 32) {
117  0 return null;
118    }
119   
120  15 byte[] mask = new byte[this.ipAddress.length / 2];
121  15 System.arraycopy(this.ipAddress, mask.length, mask, 0, mask.length);
122   
123  15 return InetAddress.getByAddress(mask);
124    }
125   
 
126  23 toggle @Override
127    public String getName()
128    {
129  23 try {
130  23 if (this.ipAddress.length != 8 && this.ipAddress.length != 32) {
131  8 return getIpAddress().getHostAddress();
132    }
133  15 return getIpAddress().getHostAddress() + "/" + getIpMask().getHostAddress();
134    } catch (UnknownHostException e) {
135  0 return Arrays.toString(this.ipAddress);
136    }
137    }
138   
 
139  9 toggle @Override
140    public GeneralName getGeneralName()
141    {
142  9 return new GeneralName(GeneralName.iPAddress, new DEROctetString(this.ipAddress));
143    }
144    }