1 // Copyright 2015 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BSSL_DER_TAG_H_ 6 #define BSSL_DER_TAG_H_ 7 8 #include "fillins/openssl_util.h" 9 #include <stdint.h> 10 11 12 #include <openssl/bytestring.h> 13 14 namespace bssl::der { 15 16 // This Tag type represents the identifier for an ASN.1 tag as encoded with 17 // DER. It matches the BoringSSL CBS and CBB in-memory representation for a 18 // tag. 19 // 20 // Callers must not assume it matches the DER representation for small tag 21 // numbers. Instead, constants are provided for universal class types, and 22 // functions are provided for building context specific tags. Tags can also be 23 // built from the provided constants and bitmasks. 24 using Tag = unsigned; 25 26 // Universal class primitive types 27 const Tag kBool = CBS_ASN1_BOOLEAN; 28 const Tag kInteger = CBS_ASN1_INTEGER; 29 const Tag kBitString = CBS_ASN1_BITSTRING; 30 const Tag kOctetString = CBS_ASN1_OCTETSTRING; 31 const Tag kNull = CBS_ASN1_NULL; 32 const Tag kOid = CBS_ASN1_OBJECT; 33 const Tag kEnumerated = CBS_ASN1_ENUMERATED; 34 const Tag kUtf8String = CBS_ASN1_UTF8STRING; 35 const Tag kPrintableString = CBS_ASN1_PRINTABLESTRING; 36 const Tag kTeletexString = CBS_ASN1_T61STRING; 37 const Tag kIA5String = CBS_ASN1_IA5STRING; 38 const Tag kUtcTime = CBS_ASN1_UTCTIME; 39 const Tag kGeneralizedTime = CBS_ASN1_GENERALIZEDTIME; 40 const Tag kVisibleString = CBS_ASN1_VISIBLESTRING; 41 const Tag kUniversalString = CBS_ASN1_UNIVERSALSTRING; 42 const Tag kBmpString = CBS_ASN1_BMPSTRING; 43 44 // Universal class constructed types 45 const Tag kSequence = CBS_ASN1_SEQUENCE; 46 const Tag kSet = CBS_ASN1_SET; 47 48 // Primitive/constructed bits 49 const unsigned kTagPrimitive = 0x00; 50 const unsigned kTagConstructed = CBS_ASN1_CONSTRUCTED; 51 52 // Tag classes 53 const unsigned kTagUniversal = 0x00; 54 const unsigned kTagApplication = CBS_ASN1_APPLICATION; 55 const unsigned kTagContextSpecific = CBS_ASN1_CONTEXT_SPECIFIC; 56 const unsigned kTagPrivate = CBS_ASN1_PRIVATE; 57 58 // Masks for the 3 components of a tag (class, primitive/constructed, number) 59 const unsigned kTagNumberMask = CBS_ASN1_TAG_NUMBER_MASK; 60 const unsigned kTagConstructionMask = CBS_ASN1_CONSTRUCTED; 61 const unsigned kTagClassMask = CBS_ASN1_CLASS_MASK; 62 63 // Creates the value for the outer tag of an explicitly tagged type. 64 // 65 // The ASN.1 keyword for this is: 66 // [tag_number] EXPLICIT 67 // 68 // (Note, the EXPLICIT may be omitted if the entire schema is in 69 // EXPLICIT mode, the default) 70 OPENSSL_EXPORT Tag ContextSpecificConstructed(uint8_t tag_number); 71 72 OPENSSL_EXPORT Tag ContextSpecificPrimitive(uint8_t base); 73 74 OPENSSL_EXPORT bool IsConstructed(Tag tag); 75 76 } // namespace bssl::der 77 78 #endif // BSSL_DER_TAG_H_ 79