]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/CyaSSL/cyassl/ctaocrypt/asn.h
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS-Plus / CyaSSL / cyassl / ctaocrypt / asn.h
1 /* asn.h
2  *
3  * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
4  *
5  * This file is part of CyaSSL.
6  *
7  * CyaSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * CyaSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20  */
21
22
23 #ifndef CTAO_CRYPT_ASN_H
24 #define CTAO_CRYPT_ASN_H
25
26 #include <cyassl/ctaocrypt/types.h>
27 #include <cyassl/ctaocrypt/rsa.h>
28 #include <cyassl/ctaocrypt/dh.h>
29 #include <cyassl/ctaocrypt/dsa.h>
30 #include <cyassl/ctaocrypt/sha.h>
31 #include <cyassl/ctaocrypt/md5.h>
32 #include <cyassl/ctaocrypt/asn_public.h>   /* public interface */
33 #ifdef HAVE_ECC
34     #include <cyassl/ctaocrypt/ecc.h>
35 #endif
36
37 #ifdef __cplusplus
38     extern "C" {
39 #endif
40
41
42 enum {
43     ISSUER  = 0,
44     SUBJECT = 1,
45
46     EXTERNAL_SERIAL_SIZE = 32,
47
48     BEFORE  = 0,
49     AFTER   = 1
50 };
51
52 /* ASN Tags   */
53 enum ASN_Tags {        
54     ASN_BOOLEAN           = 0x01,
55     ASN_INTEGER           = 0x02,
56     ASN_BIT_STRING        = 0x03,
57     ASN_OCTET_STRING      = 0x04,
58     ASN_TAG_NULL          = 0x05,
59     ASN_OBJECT_ID         = 0x06,
60     ASN_ENUMERATED        = 0x0a,
61     ASN_SEQUENCE          = 0x10,
62     ASN_SET               = 0x11,
63     ASN_UTC_TIME          = 0x17,
64     ASN_GENERALIZED_TIME  = 0x18,
65     CRL_EXTENSIONS        = 0xa0,
66     ASN_EXTENSIONS        = 0xa3,
67     ASN_LONG_LENGTH       = 0x80
68 };
69
70 enum  ASN_Flags{
71     ASN_CONSTRUCTED       = 0x20,
72     ASN_CONTEXT_SPECIFIC  = 0x80
73 };
74
75 enum DN_Tags {
76     ASN_COMMON_NAME   = 0x03,   /* CN */
77     ASN_SUR_NAME      = 0x04,   /* SN */
78     ASN_COUNTRY_NAME  = 0x06,   /* C  */
79     ASN_LOCALITY_NAME = 0x07,   /* L  */
80     ASN_STATE_NAME    = 0x08,   /* ST */
81     ASN_ORG_NAME      = 0x0a,   /* O  */
82     ASN_ORGUNIT_NAME  = 0x0b    /* OU */
83 };
84
85 enum PBES {
86     PBE_MD5_DES      = 0,
87     PBE_SHA1_DES     = 1,
88     PBE_SHA1_DES3    = 2,
89     PBE_SHA1_RC4_128 = 3,
90     PBES2            = 13       /* algo ID */
91 };
92
93 enum ENCRYPTION_TYPES {
94     DES_TYPE  = 0,
95     DES3_TYPE = 1,
96     RC4_TYPE  = 2
97 };
98
99 enum ECC_TYPES {
100     ECC_PREFIX_0 = 160,
101     ECC_PREFIX_1 = 161
102 };
103
104 enum Misc_ASN { 
105     ASN_NAME_MAX        = 256,
106     MAX_SALT_SIZE       =  64,     /* MAX PKCS Salt length */
107     MAX_IV_SIZE         =  64,     /* MAX PKCS Iv length */
108     MAX_KEY_SIZE        =  64,     /* MAX PKCS Key  length */
109     PKCS5               =   5,     /* PKCS oid tag */
110     PKCS5v2             =   6,     /* PKCS #5 v2.0 */
111     PKCS12              =  12,     /* PKCS #12 */
112     MAX_UNICODE_SZ      = 256,
113     ASN_BOOL_SIZE       =   2,     /* including type */
114     SHA_SIZE            =  20,
115     RSA_INTS            =   8,     /* RSA ints in private key */
116     MIN_DATE_SIZE       =  13,
117     MAX_DATE_SIZE       =  32,
118     ASN_GEN_TIME_SZ     =  15,     /* 7 numbers * 2 + Zulu tag */
119     MAX_ENCODED_SIG_SZ  = 512,
120     MAX_SIG_SZ          = 256,
121     MAX_ALGO_SZ         =  20,
122     MAX_SEQ_SZ          =   5,     /* enum(seq | con) + length(4) */  
123     MAX_SET_SZ          =   5,     /* enum(set | con) + length(4) */  
124     MAX_VERSION_SZ      =   5,     /* enum + id + version(byte) + (header(2))*/
125     MAX_ENCODED_DIG_SZ  =  73,     /* sha512 + enum(bit or octet) + legnth(4) */
126     MAX_RSA_INT_SZ      = 517,     /* RSA raw sz 4096 for bits + tag + len(4) */
127     MAX_NTRU_KEY_SZ     = 610,     /* NTRU 112 bit public key */
128     MAX_NTRU_ENC_SZ     = 628,     /* NTRU 112 bit DER public encoding */
129     MAX_LENGTH_SZ       =   4,     /* Max length size for DER encoding */
130     MAX_RSA_E_SZ        =  16,     /* Max RSA public e size */
131     MAX_CA_SZ           =  32,     /* Max encoded CA basic constraint length */
132     MAX_SN_SZ           =  35,     /* Max encoded serial number (INT) length */
133 #ifdef CYASSL_CERT_GEN
134     #ifdef CYASSL_ALT_NAMES
135         MAX_EXTENSIONS_SZ   = 1 + MAX_LENGTH_SZ + CTC_MAX_ALT_SIZE,
136     #else
137         MAX_EXTENSIONS_SZ   = 1 + MAX_LENGTH_SZ + MAX_CA_SZ,
138     #endif
139                                    /* Max total extensions, id + len + others */
140 #endif
141     MAX_PUBLIC_KEY_SZ   = MAX_NTRU_ENC_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ * 2
142                                    /* use bigger NTRU size */
143 };
144
145
146 enum Oid_Types {
147     hashType = 0,
148     sigType  = 1,
149     keyType  = 2
150 };
151
152
153 enum Hash_Sum  {
154     MD2h    = 646,
155     MD5h    = 649,
156     SHAh    =  88,
157     SHA256h = 414,
158     SHA384h = 415,
159     SHA512h = 416
160 };
161
162
163 enum Key_Sum {
164     DSAk   = 515,
165     RSAk   = 645,
166     NTRUk  = 364,
167     ECDSAk = 518
168 };
169
170
171 enum Ecc_Sum {
172     ECC_256R1 = 526,
173     ECC_384R1 = 210,
174     ECC_521R1 = 211,
175     ECC_160R1 = 184,
176     ECC_192R1 = 520,
177     ECC_224R1 = 209
178 };
179
180
181 enum KDF_Sum {
182     PBKDF2_OID = 660
183 };
184
185
186 enum Extensions_Sum {
187     BASIC_CA_OID  = 133,
188     ALT_NAMES_OID = 131,
189     CRL_DIST_OID  = 145,
190     AUTH_INFO_OID = 69,
191     CA_ISSUER_OID = 117
192 };
193
194
195 enum VerifyType {
196     NO_VERIFY = 0,
197     VERIFY    = 1
198 };
199
200
201 typedef struct DecodedCert DecodedCert;
202 typedef struct Signer      Signer;
203
204
205 struct DecodedCert {
206     byte*   publicKey;
207     word32  pubKeySize;
208     int     pubKeyStored;
209     word32  certBegin;               /* offset to start of cert          */
210     word32  sigIndex;                /* offset to start of signature     */
211     word32  sigLength;               /* length of signature              */
212     word32  signatureOID;            /* sum of algorithm object id       */
213     word32  keyOID;                  /* sum of key algo  object id       */
214     byte    subjectHash[SHA_SIZE];   /* hash of all Names                */
215     byte    issuerHash[SHA_SIZE];    /* hash of all Names                */
216 #ifdef HAVE_OCSP
217     byte    issuerKeyHash[SHA_SIZE]; /* hash of the public Key           */
218 #endif /* HAVE_OCSP */
219     byte*   signature;               /* not owned, points into raw cert  */
220     char*   subjectCN;               /* CommonName                       */
221     int     subjectCNLen;
222     char    issuer[ASN_NAME_MAX];    /* full name including common name  */
223     char    subject[ASN_NAME_MAX];   /* full name including common name  */
224     int     verify;                  /* Default to yes, but could be off */
225     byte*   source;                  /* byte buffer holder cert, NOT owner */
226     word32  srcIdx;                  /* current offset into buffer       */
227     word32  maxIdx;                  /* max offset based on init size    */
228     void*   heap;                    /* for user memory overrides        */
229     byte    serial[EXTERNAL_SERIAL_SIZE];  /* raw serial number          */
230     int     serialSz;                /* raw serial bytes stored */
231     byte*   extensions;              /* not owned, points into raw cert  */
232     int     extensionsSz;            /* length of cert extensions */
233     word32  extensionsIdx;           /* if want to go back and parse later */
234     byte*   extAuthInfo;             /* Authority Information Access URI */
235     int     extAuthInfoSz;           /* length of the URI                */
236     byte*   extCrlInfo;              /* CRL Distribution Points          */
237     int     extCrlInfoSz;            /* length of the URI                */
238     byte    isCA;                    /* CA basic constraint true */
239 #ifdef CYASSL_CERT_GEN
240     /* easy access to subject info for other sign */
241     char*   subjectSN;
242     int     subjectSNLen;
243     char*   subjectC;
244     int     subjectCLen;
245     char*   subjectL;
246     int     subjectLLen;
247     char*   subjectST;
248     int     subjectSTLen;
249     char*   subjectO;
250     int     subjectOLen;
251     char*   subjectOU;
252     int     subjectOULen;
253     char*   subjectEmail;
254     int     subjectEmailLen;
255     byte*   beforeDate;
256     int     beforeDateLen;
257     byte*   afterDate;
258     int     afterDateLen;
259 #endif /* CYASSL_CERT_GEN */
260 };
261
262
263 /* CA Signers */
264 struct Signer {
265     byte*   publicKey;
266     word32  pubKeySize;
267     word32  keyOID;                  /* key type */
268     char*   name;                    /* common name */
269     byte    hash[SHA_DIGEST_SIZE];   /* sha hash of names in certificate */
270     Signer* next;
271 };
272
273
274 /* not for public consumption but may use for testing sometimes */
275 #ifdef CYASSL_TEST_CERT
276     #define CYASSL_TEST_API CYASSL_API
277 #else
278     #define CYASSL_TEST_API CYASSL_LOCAL
279 #endif
280
281 CYASSL_TEST_API void InitDecodedCert(DecodedCert*, byte*, word32, void*);
282 CYASSL_TEST_API void FreeDecodedCert(DecodedCert*);
283 CYASSL_TEST_API int  ParseCert(DecodedCert*, int type, int verify, void* cm);
284
285 CYASSL_LOCAL int ParseCertRelative(DecodedCert*, int type, int verify,void* cm);
286 CYASSL_LOCAL int DecodeToKey(DecodedCert*, int verify);
287
288 CYASSL_LOCAL word32 EncodeSignature(byte* out, const byte* digest, word32 digSz,
289                                     int hashOID);
290
291 CYASSL_LOCAL Signer* MakeSigner(void*);
292 CYASSL_LOCAL void    FreeSigners(Signer*, void*);
293
294
295 CYASSL_LOCAL int ToTraditional(byte* buffer, word32 length);
296 CYASSL_LOCAL int ToTraditionalEnc(byte* buffer, word32 length,const char*, int);
297
298
299 #ifdef HAVE_ECC
300     /* ASN sig helpers */
301     CYASSL_LOCAL int StoreECC_DSA_Sig(byte* out, word32* outLen, mp_int* r,
302                                       mp_int* s);
303     CYASSL_LOCAL int DecodeECC_DSA_Sig(const byte* sig, word32 sigLen,
304                                        mp_int* r, mp_int* s);
305     /* private key helpers */
306     CYASSL_LOCAL int EccPrivateKeyDecode(const byte* input,word32* inOutIdx,
307                                          ecc_key*,word32);
308 #endif
309
310 #ifdef CYASSL_CERT_GEN
311
312 enum cert_enums {
313     NAME_ENTRIES    =  8,
314     JOINT_LEN       =  2,
315     EMAIL_JOINT_LEN =  9,
316     RSA_KEY         = 10,
317     NTRU_KEY        = 11
318 };
319
320
321 #endif /* CYASSL_CERT_GEN */
322
323
324 #ifdef HAVE_OCSP
325
326 enum Ocsp_Response_Status {
327     OCSP_SUCCESSFUL        = 0, /* Response has valid confirmations */
328     OCSP_MALFORMED_REQUEST = 1, /* Illegal confirmation request */
329     OCSP_INTERNAL_ERROR    = 2, /* Internal error in issuer */
330     OCSP_TRY_LATER         = 3, /* Try again later */
331     OCSP_SIG_REQUIRED      = 5, /* Must sign the request (4 is skipped) */
332     OCSP_UNAUTHROIZED      = 6  /* Request unauthorized */
333 };
334
335
336 enum Ocsp_Cert_Status {
337     CERT_GOOD    = 0,
338     CERT_REVOKED = 1,
339     CERT_UNKNOWN = 2
340 };
341
342
343 enum Ocsp_Sums {
344     OCSP_BASIC_OID = 117
345 };
346
347
348 #define STATUS_LIST_SIZE 5
349
350
351 typedef struct OcspResponse OcspResponse;
352
353
354 struct OcspResponse {
355     int     responseStatus;  /* return code from Responder */
356
357     word32  respBegin;       /* index to beginning of OCSP Response */
358     word32  respLength;      /* length of the OCSP Response */
359
360     int     version;         /* Response version number */
361
362     word32  sigIndex;        /* Index into source for start of sig */
363     word32  sigLength;       /* Length in octets for the sig */
364     word32  sigOID;          /* OID for hash used for sig */
365
366     int     certStatusCount; /* Count of certificate statuses, Note
367                               * 1:1 correspondence between certStatus
368                               * and certSerialNumber */
369     byte    certSN[STATUS_LIST_SIZE][EXTERNAL_SERIAL_SIZE];
370     int     certSNsz[STATUS_LIST_SIZE];
371                              /* Certificate serial number array. */
372     word32  certStatus[STATUS_LIST_SIZE];
373                              /* Certificate status array */
374
375     byte*   source;          /* pointer to source buffer, not owned */
376     word32  maxIdx;          /* max offset based on init size */
377     void*   heap;            /* for user memory overrides */
378 };
379
380
381 CYASSL_LOCAL void InitOcspResponse(OcspResponse*, byte*, word32, void*);
382 CYASSL_LOCAL void FreeOcspResponse(OcspResponse*);
383 CYASSL_LOCAL int  OcspResponseDecode(OcspResponse*);
384 CYASSL_LOCAL int  EncodeOcspRequest(DecodedCert*, byte*, word32);
385
386
387 #endif /* HAVE_OCSP */
388
389
390 /* for pointer use */
391 typedef struct RevokedCert RevokedCert;
392
393 #ifdef HAVE_CRL
394
395 struct RevokedCert {
396     byte         serialNumber[EXTERNAL_SERIAL_SIZE];
397     int          serialSz;
398     RevokedCert* next;
399 };
400
401 typedef struct DecodedCRL DecodedCRL;
402
403 struct DecodedCRL {
404     word32  certBegin;               /* offset to start of cert          */
405     word32  sigIndex;                /* offset to start of signature     */
406     word32  sigLength;               /* length of signature              */
407     word32  signatureOID;            /* sum of algorithm object id       */
408     byte*   signature;               /* pointer into raw source, not owned */
409     byte    issuerHash[SHA_DIGEST_SIZE];  /* issuer hash                 */ 
410     byte    crlHash[MD5_DIGEST_SIZE];     /* raw crl data hash           */ 
411     byte    lastDate[MAX_DATE_SIZE]; /* last date updated  */
412     byte    nextDate[MAX_DATE_SIZE]; /* next update date   */
413     RevokedCert* certs;              /* revoked cert list  */
414     int          totalCerts;         /* number on list     */
415 };
416
417 CYASSL_LOCAL void InitDecodedCRL(DecodedCRL*);
418 CYASSL_LOCAL int  ParseCRL(DecodedCRL*, const byte* buff, long sz);
419 CYASSL_LOCAL void FreeDecodedCRL(DecodedCRL*);
420
421
422 #endif /* HAVE_CRL */
423
424
425 #ifdef __cplusplus
426     } /* extern "C" */
427 #endif
428
429 #endif /* CTAO_CRYPT_ASN_H */
430