3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2013-2017 Nest Labs, Inc.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * This file implements methods for manipulating for writing
23 * Abstract Syntax Notation One (ASN.1) Object Identifiers
32 #include <support/DLLUtil.h>
34 #define ASN1_DEFINE_OID_TABLE
35 #define ASN1_DEFINE_OID_NAME_TABLE
36 #include <asn1/ASN1.h>
41 DLL_EXPORT OID ParseObjectID(const uint8_t * encodedOID, uint16_t encodedOIDLen)
43 if (encodedOID == nullptr or encodedOIDLen == 0)
44 return kOID_NotSpecified;
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;
53 bool GetEncodedObjectID(OID oid, const uint8_t *& encodedOID, uint16_t & encodedOIDLen)
55 for (uint32_t i = 0; i < sOIDTableSize; i++)
56 if (oid == sOIDTable[i].EnumVal)
58 encodedOID = sOIDTable[i].EncodedOID;
59 encodedOIDLen = sOIDTable[i].EncodedOIDLen;
66 OIDCategory GetOIDCategory(OID oid)
68 if (oid == kOID_Unknown)
69 return kOIDCategory_Unknown;
70 if (oid == kOID_NotSpecified)
71 return kOIDCategory_NotSpecified;
72 return (OIDCategory)(oid & kOIDCategory_Mask);
75 const char * GetOIDName(OID oid)
77 if (oid == kOID_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;
87 ASN1_ERROR ASN1Reader::GetObjectId(OID & oid)
90 return ASN1_ERROR_INVALID_STATE;
92 return ASN1_ERROR_INVALID_ENCODING;
93 if (mElemStart + mHeadLen + ValueLen > mContainerEnd)
94 return ASN1_ERROR_UNDERRUN;
95 oid = ParseObjectID(Value, ValueLen);
99 ASN1_ERROR ASN1Writer::PutObjectId(OID oid)
101 const uint8_t * encodedOID;
102 uint16_t encodedOIDLen;
104 if (!GetEncodedObjectID(oid, encodedOID, encodedOIDLen))
105 return ASN1_ERROR_UNKNOWN_OBJECT_ID;
107 return PutObjectId(encodedOID, encodedOIDLen);