[AAF-21] Initial code import
[aaf/cadi.git] / core / src / main / java / com / att / cadi / principal / X509Principal.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi.principal;\r
25 \r
26 import java.io.IOException;\r
27 import java.security.cert.CertificateEncodingException;\r
28 import java.security.cert.X509Certificate;\r
29 import java.util.regex.Pattern;\r
30 \r
31 import com.att.cadi.GetCred;\r
32 \r
33 public class X509Principal extends BearerPrincipal implements GetCred {\r
34         private static final Pattern pattern = Pattern.compile("[a-zA-Z0-9]*\\@[a-zA-Z0-9.]*");\r
35         private byte[] content;  \r
36         private X509Certificate cert;\r
37         private String name;\r
38 \r
39         public X509Principal(String identity, X509Certificate cert, byte[] content) {\r
40                 name = identity;\r
41                 this.content = content;\r
42                 this.cert = cert;\r
43         }\r
44         \r
45         public X509Principal(X509Certificate cert, byte[] content) throws IOException {\r
46                 this.content=content;\r
47                 this.cert = cert;\r
48                 String subj = cert.getSubjectDN().getName();\r
49                 int cn = subj.indexOf("OU=");\r
50                 if(cn>=0) {\r
51                         cn+=3;\r
52                         int space = subj.indexOf(',',cn);\r
53                         if(space>=0) {\r
54                                 String id = subj.substring(cn, space);\r
55                                 if(pattern.matcher(id).matches()) {\r
56                                         name = id;\r
57                                 }\r
58                         }\r
59                 }\r
60                 if(name==null)\r
61                         throw new IOException("X509 does not have Identity as CN");\r
62                 \r
63         }\r
64         \r
65         \r
66         public String getAsHeader() throws IOException {\r
67                 try {\r
68                         if(content==null) \r
69                                 content=cert.getEncoded();\r
70                 } catch (CertificateEncodingException e) {\r
71                         throw new IOException(e);\r
72                 }\r
73                 return "X509 " + content;\r
74         }\r
75         \r
76         public String toString() {\r
77                 return "X509 Authentication for " + name;\r
78         }\r
79 \r
80 \r
81         public byte[] getCred() {\r
82                 try {\r
83                         return content==null?(content=cert.getEncoded()):content;\r
84                 } catch (CertificateEncodingException e) {\r
85                         return null;\r
86                 }\r
87         }\r
88 \r
89 \r
90         public String getName() {\r
91                 return name;\r
92         }\r
93 }\r