e60a3c25c779870f7fe01c5c8f3c05d6657fefd6
[platform/upstream/connectedhomeip.git] / src / lib / asn1 / ASN1OID.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2013-2017 Nest Labs, Inc.
5  *    All rights reserved.
6  *
7  *    Licensed under the Apache License, Version 2.0 (the "License");
8  *    you may not use this file except in compliance with the License.
9  *    You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *    Unless required by applicable law or agreed to in writing, software
14  *    distributed under the License is distributed on an "AS IS" BASIS,
15  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *    See the License for the specific language governing permissions and
17  *    limitations under the License.
18  */
19
20 /**
21  *    @file
22  *      This file implements methods for manipulating for writing
23  *      Abstract Syntax Notation One (ASN.1) Object Identifiers
24  *      (OIDs).
25  *
26  */
27
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include <support/DLLUtil.h>
33
34 #define ASN1_DEFINE_OID_TABLE
35 #define ASN1_DEFINE_OID_NAME_TABLE
36 #include <asn1/ASN1.h>
37
38 namespace chip {
39 namespace ASN1 {
40
41 DLL_EXPORT OID ParseObjectID(const uint8_t * encodedOID, uint16_t encodedOIDLen)
42 {
43     if (encodedOID == nullptr or encodedOIDLen == 0)
44         return kOID_NotSpecified;
45
46     for (uint32_t i = 0; i < sOIDTableSize; i++)
47         if (encodedOIDLen == sOIDTable[i].EncodedOIDLen && memcmp(encodedOID, sOIDTable[i].EncodedOID, encodedOIDLen) == 0)
48             return sOIDTable[i].EnumVal;
49
50     return kOID_Unknown;
51 }
52
53 bool GetEncodedObjectID(OID oid, const uint8_t *& encodedOID, uint16_t & encodedOIDLen)
54 {
55     for (uint32_t i = 0; i < sOIDTableSize; i++)
56         if (oid == sOIDTable[i].EnumVal)
57         {
58             encodedOID    = sOIDTable[i].EncodedOID;
59             encodedOIDLen = sOIDTable[i].EncodedOIDLen;
60             return true;
61         }
62
63     return false;
64 }
65
66 OIDCategory GetOIDCategory(OID oid)
67 {
68     if (oid == kOID_Unknown)
69         return kOIDCategory_Unknown;
70     if (oid == kOID_NotSpecified)
71         return kOIDCategory_NotSpecified;
72     return (OIDCategory)(oid & kOIDCategory_Mask);
73 }
74
75 const char * GetOIDName(OID oid)
76 {
77     if (oid == kOID_Unknown)
78         return "Unknown";
79     if (oid == kOID_NotSpecified)
80         return "NotSpecified";
81     for (uint32_t i = 0; i < sOIDTableSize; i++)
82         if (oid == sOIDNameTable[i].EnumVal)
83             return sOIDNameTable[i].Name;
84     return "Unknown";
85 }
86
87 ASN1_ERROR ASN1Reader::GetObjectId(OID & oid)
88 {
89     if (Value == nullptr)
90         return ASN1_ERROR_INVALID_STATE;
91     if (ValueLen < 1)
92         return ASN1_ERROR_INVALID_ENCODING;
93     if (mElemStart + mHeadLen + ValueLen > mContainerEnd)
94         return ASN1_ERROR_UNDERRUN;
95     oid = ParseObjectID(Value, ValueLen);
96     return ASN1_NO_ERROR;
97 }
98
99 ASN1_ERROR ASN1Writer::PutObjectId(OID oid)
100 {
101     const uint8_t * encodedOID;
102     uint16_t encodedOIDLen;
103
104     if (!GetEncodedObjectID(oid, encodedOID, encodedOIDLen))
105         return ASN1_ERROR_UNKNOWN_OBJECT_ID;
106
107     return PutObjectId(encodedOID, encodedOIDLen);
108 }
109
110 } // namespace ASN1
111 } // namespace chip