remove prefix .well-known uri & change from 'ocf' to 'oic' uri
[iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / resources / certificate / CertificateResource.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2016 Samsung Electronics All Rights Reserved.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
8  * // Licensed under the Apache License, Version 2.0 (the "License");
9  * // you may not use this file except in compliance with the License.
10  * // You may obtain a copy of the License at
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
14  * // Unless required by applicable law or agreed to in writing, software
15  * // distributed under the License is distributed on an "AS IS" BASIS,
16  * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * // See the License for the specific language governing permissions and
18  * // limitations under the License.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.accountserver.resources.certificate;
23
24 import java.security.PublicKey;
25 import java.util.Arrays;
26 import java.util.Date;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import org.iotivity.cloud.accountserver.Constants;
31 import org.iotivity.cloud.accountserver.x509.cert.CSRParser;
32 import org.iotivity.cloud.accountserver.x509.cert.CertificateBuilder;
33 import org.iotivity.cloud.accountserver.x509.cert.CertificateIssuerConfig;
34 import org.iotivity.cloud.accountserver.x509.cert.CertificatePrivateKeyPair;
35 import org.iotivity.cloud.base.device.Device;
36 import org.iotivity.cloud.base.exception.ServerException;
37 import org.iotivity.cloud.base.exception.ServerException.BadRequestException;
38 import org.iotivity.cloud.base.protocols.IRequest;
39 import org.iotivity.cloud.base.protocols.IResponse;
40 import org.iotivity.cloud.base.protocols.MessageBuilder;
41 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
42 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
43 import org.iotivity.cloud.base.resource.Resource;
44 import org.iotivity.cloud.util.Cbor;
45
46 public class CertificateResource extends Resource {
47     /* resource uri for certificate issuer */
48     private Cbor<HashMap<String, Object>> mCbor               = new Cbor<>();
49     private CertificateManager            mCertificateManager = new CertificateManager();
50     private static String                 DEVICE_OU           = "OCF Device";
51
52     public CertificateResource() {
53         super(Arrays.asList(Constants.PREFIX_OIC, Constants.CREDPROV_URI,
54                 Constants.CERT_URI));
55     }
56
57     @Override
58     public void onDefaultRequestReceived(Device srcDevice, IRequest request)
59             throws ServerException {
60         IResponse response = null;
61
62         switch (request.getMethod()) {
63             case POST:
64                 response = handlePutRequest(request);
65                 break;
66
67             default:
68                 throw new BadRequestException(
69                         request.getMethod() + " request type is not support");
70         }
71
72         srcDevice.sendResponse(response);
73     }
74
75     private IResponse handlePutRequest(IRequest request)
76             throws ServerException {
77
78         Map<String, Object> payloadData = mCbor
79                 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
80
81         Map<String, Object> responsePayload = null;
82
83         if (payloadData.containsKey(Constants.REQ_CSR)) {
84
85             byte[] csrDer = (byte[]) payloadData.get(Constants.REQ_CSR);
86             CSRParser parser = null;
87             PublicKey pubKey = null;
88
89             try {
90                 parser = new CSRParser(csrDer);
91                 if (!parser.verify()) {
92                     throw new Exception();
93                 }
94                 pubKey = parser.getPublicKey();
95             } catch (Exception e) {
96                 throw new BadRequestException("Bad CSR");
97             }
98
99             CertificateIssuerConfig ciConfig = CertificateIssuerConfig
100                     .getInstance();
101
102             Date notBefore = ciConfig.getNotBeforeDate();
103             Date notAfter = ciConfig.getNotAfterDate();
104
105             CertificateBuilder certBuilder = new CertificateBuilder(
106                     parser.getCommonName(), pubKey, notBefore, notAfter,
107                     ciConfig.getNextSerialNumber(),
108                     ciConfig.getRootCertificate());
109
110             certBuilder.setSubjectC(parser.getCountry());
111
112             certBuilder.setSubjectO(parser.getOrganizational());
113
114             certBuilder.setSubjectOU(DEVICE_OU);
115
116             CertificatePrivateKeyPair devCert = null;
117
118             try {
119                 devCert = certBuilder.build();
120                 // System.out.println("============================== DEV CERT
121                 // ===========================");
122                 // System.out.println();
123                 // System.out.println(devCert.getCertificate().toString());
124                 // System.out.println("===================================================================");
125
126                 if (payloadData.containsKey(Constants.REQ_DEVICE_ID)) {
127                     mCertificateManager.addDeviceId(
128                             (String) payloadData.get(Constants.RESP_DEVICE_ID));
129                 }
130
131                 responsePayload = mCertificateManager.createPayload(
132                         devCert.getCertificate().getEncoded(),
133                         ciConfig.getRootCertificate().getCertificate()
134                                 .getEncoded());
135
136             } catch (Exception e) {
137                 throw new BadRequestException("Certificate generation error");
138             }
139
140         } else {
141             throw new BadRequestException("CSR is null");
142         }
143
144         return MessageBuilder.createResponse(request, ResponseStatus.CONTENT,
145                 ContentFormat.APPLICATION_CBOR,
146                 mCbor.encodingPayloadToCbor(responsePayload));
147     }
148 }