javaextensions/satsa/pki/src.s60/tstsdistinguishednameconverter.cpp
branchRCL_3
changeset 19 04becd199f91
child 23 98ccebc37403
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "tstsdistinguishednameconverter.h"
       
    22 #include "tstscharactersetconverter.h"
       
    23 #include "stscommon.h" // for CleanupResetAndDestroy
       
    24 #include "stspkiconstants.h" // for error constants
       
    25 #include <x500dn.h>
       
    26 #include <charconv.h>
       
    27 
       
    28 
       
    29 
       
    30 
       
    31 // CONSTANTS
       
    32 const TInt KSTSGeneratedNameLength = 23;
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // TSTSDistinguishedNameConverter::TSTSDistinguishedNameConverter
       
    38 // C++ default constructor can NOT contain any code, that
       
    39 // might leave.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 TSTSDistinguishedNameConverter::TSTSDistinguishedNameConverter()
       
    43 {
       
    44 }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // TSTSDistinguishedNameConverter::CreateDNL
       
    48 // Create CX500DistinguishedName
       
    49 // -----------------------------------------------------------------------------
       
    50 CX500DistinguishedName* TSTSDistinguishedNameConverter::CreateDNL(
       
    51     const TDesC& aNameInfo)
       
    52 {
       
    53 
       
    54     CArrayPtrFlat< CX520AttributeTypeAndValue>* elements =
       
    55         new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue> (1);
       
    56     CleanupStack::PushL(elements);
       
    57     CleanupResetAndDestroyPushL(*elements);
       
    58     TInt pos(0);
       
    59     TInt nameLength(aNameInfo.Length());
       
    60     TInt elementStart(0);
       
    61 
       
    62     while (pos < nameLength)
       
    63     {
       
    64         if (aNameInfo[ pos ] == '\\')
       
    65         {
       
    66             // next character is escaped, so we jump over it
       
    67             pos++;
       
    68         }
       
    69         else if ((aNameInfo[ pos ] == ',') || (pos == (nameLength - 1)))
       
    70         {
       
    71             // found the end of single element, parse it
       
    72             TInt elementLength = pos - elementStart;
       
    73             if (pos == (nameLength - 1))
       
    74             {
       
    75                 elementLength++;
       
    76             }
       
    77             TPtrC elementDes(aNameInfo.Mid(elementStart, elementLength));
       
    78             CX520AttributeTypeAndValue* element =
       
    79                 ParseDNElementL(elementDes);
       
    80             if (element)
       
    81             {
       
    82                 CleanupStack::PushL(element);
       
    83                 elements->AppendL(element);
       
    84                 CleanupStack::Pop();  // element
       
    85             }
       
    86             elementStart = pos + 1;
       
    87         }
       
    88         pos++;
       
    89     }
       
    90 
       
    91     TInt elementCount = elements->Count();
       
    92 
       
    93     if (elementCount == 0)
       
    94     {
       
    95         CX520AttributeTypeAndValue* element = GenerateDNElementLC();
       
    96         elements->AppendL(element);
       
    97         CleanupStack::Pop();  // element
       
    98         elementCount++;
       
    99     }
       
   100 
       
   101     // In certificates the element order is reversed
       
   102     CArrayPtrFlat< CX520AttributeTypeAndValue>* reversedElements =
       
   103         new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue> (elementCount);
       
   104     CleanupStack::PushL(reversedElements);
       
   105 
       
   106     for (TInt i = elementCount - 1; i >= 0; i--)
       
   107     {
       
   108         reversedElements->AppendL(elements->At(i));
       
   109     }
       
   110 
       
   111     CX500DistinguishedName* dn =
       
   112         CX500DistinguishedName::NewL(*reversedElements);
       
   113 
       
   114     CleanupStack::PopAndDestroy(3);   // elements, reversedElements
       
   115 
       
   116 
       
   117     return dn;
       
   118 }
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // TSTSDistinguishedNameConverter::GenerateDNElementL
       
   122 // Generates a pseudo random distinguished name element
       
   123 // -----------------------------------------------------------------------------
       
   124 CX520AttributeTypeAndValue*
       
   125 TSTSDistinguishedNameConverter::GenerateDNElementLC()
       
   126 {
       
   127     TTime currentTime;
       
   128     currentTime.UniversalTime();
       
   129     TBuf< KSTSGeneratedNameLength > value;
       
   130     currentTime.FormatL(value, KSTSDistinguishedNameFormat());
       
   131 
       
   132     TSTSCharacterSetConverter csConverter;
       
   133     HBufC8* byteValue = csConverter.EscapedUnicodeToPrintableLC(value);
       
   134 
       
   135     CX520AttributeTypeAndValue* retVal =
       
   136         CX520AttributeTypeAndValue::NewL(ECommonName, *byteValue);
       
   137 
       
   138     CleanupStack::PopAndDestroy(byteValue);
       
   139     CleanupStack::PushL(retVal);
       
   140 
       
   141     return retVal;
       
   142 }
       
   143 
       
   144 // -----------------------------------------------------------------------------
       
   145 // TSTSDistinguishedNameConverter::ParseDNElementLC
       
   146 // Parse single element from distinguished name
       
   147 // -----------------------------------------------------------------------------
       
   148 CX520AttributeTypeAndValue* TSTSDistinguishedNameConverter::ParseDNElementL(
       
   149     TDesC& aSingleElement)
       
   150 {
       
   151 
       
   152     TInt elementLength = aSingleElement.Length();
       
   153     TInt pos(0);
       
   154 
       
   155     CX520AttributeTypeAndValue* retVal = NULL;
       
   156 
       
   157     while (pos < elementLength)
       
   158     {
       
   159         if (aSingleElement[ pos ] == '=')
       
   160         {
       
   161             TPtrC type = aSingleElement.Left(pos);
       
   162             TPtrC value = aSingleElement.Mid(pos + 1);
       
   163             retVal = CreateDNElementL(type, value);
       
   164             // break in order to get quickly out of the while loop
       
   165             break;
       
   166         }
       
   167         pos++;
       
   168     }
       
   169     if (pos == elementLength)
       
   170     {
       
   171         User::Leave(KSTSErrInvalidCAName);
       
   172     }
       
   173 
       
   174     return retVal;
       
   175 }
       
   176 
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // TSTSDistinguishedNameConverter::CreateDNElementLC
       
   180 // Create a single DN element from given parameters
       
   181 // -----------------------------------------------------------------------------
       
   182 CX520AttributeTypeAndValue* TSTSDistinguishedNameConverter::CreateDNElementL(
       
   183     TDesC& aType, TDesC& aValue)
       
   184 {
       
   185 
       
   186     TLex typeLexer(aType);
       
   187     typeLexer.SkipSpace();
       
   188     TPtrC16 lexedType = typeLexer.Remainder();
       
   189 
       
   190     TSTSCharacterSetConverter csConverter;
       
   191 
       
   192     HBufC8* value = csConverter.EscapedUnicodeToPrintableLC(aValue);
       
   193 
       
   194     CX520AttributeTypeAndValue* retVal = NULL;
       
   195     if (lexedType.Compare(KSTSX500AttributeCN()) == 0)
       
   196     {
       
   197         retVal = CX520AttributeTypeAndValue::NewL(ECommonName, *value);
       
   198     }
       
   199     else if (lexedType.Compare(KSTSX500AttributeL()) == 0)
       
   200     {
       
   201         retVal = CX520AttributeTypeAndValue::NewL(ELocalityName, *value);
       
   202     }
       
   203     else if (lexedType.Compare(KSTSX500AttributeST()) == 0)
       
   204     {
       
   205         retVal = CX520AttributeTypeAndValue::NewL(EStateOrProvinceName,
       
   206                  *value);
       
   207     }
       
   208     else if (lexedType.Compare(KSTSX500AttributeO()) == 0)
       
   209     {
       
   210         retVal = CX520AttributeTypeAndValue::NewL(EOrganizationName, *value);
       
   211     }
       
   212     else if (lexedType.Compare(KSTSX500AttributeOU()) == 0)
       
   213     {
       
   214         retVal = CX520AttributeTypeAndValue::NewL(EOrganizationalUnitName,
       
   215                  *value);
       
   216     }
       
   217     else if (lexedType.Compare(KSTSX500AttributeC()) == 0)
       
   218     {
       
   219         retVal = CX520AttributeTypeAndValue::NewL(ECountryName, *value);
       
   220     }
       
   221     else if (lexedType.Compare(KSTSX500AttributeSTREET()) == 0)
       
   222     {
       
   223         retVal = CX520AttributeTypeAndValue::NewL(ERFC2256Street, *value);
       
   224     }
       
   225     else if (lexedType.Compare(KSTSX500AttributeDC()) == 0)
       
   226     {
       
   227         retVal = CX520AttributeTypeAndValue::NewL(ERFC2247DomainComponent,
       
   228                  *value);
       
   229     }
       
   230     // UID is mentioned in RFC2253, but its not supported by native
       
   231     // implementation
       
   232     else if (lexedType.Compare(KSTSX500AttributeUID()) != 0)
       
   233     {
       
   234         User::Leave(KErrArgument);
       
   235     }
       
   236     CleanupStack::PopAndDestroy(value);
       
   237 
       
   238 
       
   239     return retVal;
       
   240 }
       
   241 
       
   242 
       
   243 //  End of File