]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/CyaSSL/cyassl/ctaocrypt/asn.h
Prepare for V7.2.0 release.
[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_DNS_TYPE          = 0x02,
65     ASN_GENERALIZED_TIME  = 0x18,
66     CRL_EXTENSIONS        = 0xa0,
67     ASN_EXTENSIONS        = 0xa3,
68     ASN_LONG_LENGTH       = 0x80
69 };
70
71 enum  ASN_Flags{
72     ASN_CONSTRUCTED       = 0x20,
73     ASN_CONTEXT_SPECIFIC  = 0x80
74 };
75
76 enum DN_Tags {
77     ASN_COMMON_NAME   = 0x03,   /* CN */
78     ASN_SUR_NAME      = 0x04,   /* SN */
79     ASN_COUNTRY_NAME  = 0x06,   /* C  */
80     ASN_LOCALITY_NAME = 0x07,   /* L  */
81     ASN_STATE_NAME    = 0x08,   /* ST */
82     ASN_ORG_NAME      = 0x0a,   /* O  */
83     ASN_ORGUNIT_NAME  = 0x0b    /* OU */
84 };
85
86 enum PBES {
87     PBE_MD5_DES      = 0,
88     PBE_SHA1_DES     = 1,
89     PBE_SHA1_DES3    = 2,
90     PBE_SHA1_RC4_128 = 3,
91     PBES2            = 13       /* algo ID */
92 };
93
94 enum ENCRYPTION_TYPES {
95     DES_TYPE  = 0,
96     DES3_TYPE = 1,
97     RC4_TYPE  = 2
98 };
99
100 enum ECC_TYPES {
101     ECC_PREFIX_0 = 160,
102     ECC_PREFIX_1 = 161
103 };
104
105 enum Misc_ASN { 
106     ASN_NAME_MAX        = 256,
107     MAX_SALT_SIZE       =  64,     /* MAX PKCS Salt length */
108     MAX_IV_SIZE         =  64,     /* MAX PKCS Iv length */
109     MAX_KEY_SIZE        =  64,     /* MAX PKCS Key  length */
110     PKCS5               =   5,     /* PKCS oid tag */
111     PKCS5v2             =   6,     /* PKCS #5 v2.0 */
112     PKCS12              =  12,     /* PKCS #12 */
113     MAX_UNICODE_SZ      = 256,
114     ASN_BOOL_SIZE       =   2,     /* including type */
115     SHA_SIZE            =  20,
116     RSA_INTS            =   8,     /* RSA ints in private key */
117     MIN_DATE_SIZE       =  13,
118     MAX_DATE_SIZE       =  32,
119     ASN_GEN_TIME_SZ     =  15,     /* 7 numbers * 2 + Zulu tag */
120     MAX_ENCODED_SIG_SZ  = 512,
121     MAX_SIG_SZ          = 256,
122     MAX_ALGO_SZ         =  20,
123     MAX_SEQ_SZ          =   5,     /* enum(seq | con) + length(4) */  
124     MAX_SET_SZ          =   5,     /* enum(set | con) + length(4) */  
125     MAX_VERSION_SZ      =   5,     /* enum + id + version(byte) + (header(2))*/
126     MAX_ENCODED_DIG_SZ  =  73,     /* sha512 + enum(bit or octet) + legnth(4) */
127     MAX_RSA_INT_SZ      = 517,     /* RSA raw sz 4096 for bits + tag + len(4) */
128     MAX_NTRU_KEY_SZ     = 610,     /* NTRU 112 bit public key */
129     MAX_NTRU_ENC_SZ     = 628,     /* NTRU 112 bit DER public encoding */
130     MAX_LENGTH_SZ       =   4,     /* Max length size for DER encoding */
131     MAX_RSA_E_SZ        =  16,     /* Max RSA public e size */
132     MAX_CA_SZ           =  32,     /* Max encoded CA basic constraint length */
133     MAX_SN_SZ           =  35,     /* Max encoded serial number (INT) length */
134 #ifdef CYASSL_CERT_GEN
135     #ifdef CYASSL_ALT_NAMES
136         MAX_EXTENSIONS_SZ   = 1 + MAX_LENGTH_SZ + CTC_MAX_ALT_SIZE,
137     #else
138         MAX_EXTENSIONS_SZ   = 1 + MAX_LENGTH_SZ + MAX_CA_SZ,
139     #endif
140                                    /* Max total extensions, id + len + others */
141 #endif
142     MAX_OCSP_EXT_SZ     = 58,      /* Max OCSP Extension length */
143     MAX_OCSP_NONCE_SZ   = 18,      /* OCSP Nonce size           */
144     MAX_PUBLIC_KEY_SZ   = MAX_NTRU_ENC_SZ + MAX_ALGO_SZ + MAX_SEQ_SZ * 2
145                                    /* use bigger NTRU size */
146 };
147
148
149 enum Oid_Types {
150     hashType = 0,
151     sigType  = 1,
152     keyType  = 2
153 };
154
155
156 enum Hash_Sum  {
157     MD2h    = 646,
158     MD5h    = 649,
159     SHAh    =  88,
160     SHA256h = 414,
161     SHA384h = 415,
162     SHA512h = 416
163 };
164
165
166 enum Key_Sum {
167     DSAk   = 515,
168     RSAk   = 645,
169     NTRUk  = 364,
170     ECDSAk = 518
171 };
172
173
174 enum Ecc_Sum {
175     ECC_256R1 = 526,
176     ECC_384R1 = 210,
177     ECC_521R1 = 211,
178     ECC_160R1 = 184,
179     ECC_192R1 = 520,
180     ECC_224R1 = 209
181 };
182
183
184 enum KDF_Sum {
185     PBKDF2_OID = 660
186 };
187
188
189 enum Extensions_Sum {
190     BASIC_CA_OID  = 133,
191     ALT_NAMES_OID = 131,
192     CRL_DIST_OID  = 145,
193     AUTH_INFO_OID = 69,
194     CA_ISSUER_OID = 117
195 };
196
197
198 enum VerifyType {
199     NO_VERIFY = 0,
200     VERIFY    = 1
201 };
202
203
204 typedef struct DNS_entry   DNS_entry;
205
206 struct DNS_entry {
207     DNS_entry* next;   /* next on DNS list */
208     char*      name;   /* actual DNS name */
209 };
210
211 typedef struct DecodedCert DecodedCert;
212 typedef struct Signer      Signer;
213
214
215 struct DecodedCert {
216     byte*   publicKey;
217     word32  pubKeySize;
218     int     pubKeyStored;
219     word32  certBegin;               /* offset to start of cert          */
220     word32  sigIndex;                /* offset to start of signature     */
221     word32  sigLength;               /* length of signature              */
222     word32  signatureOID;            /* sum of algorithm object id       */
223     word32  keyOID;                  /* sum of key algo  object id       */
224     DNS_entry* altNames;             /* alt names list of dns entries    */
225     byte    subjectHash[SHA_SIZE];   /* hash of all Names                */
226     byte    issuerHash[SHA_SIZE];    /* hash of all Names                */
227 #ifdef HAVE_OCSP
228     byte    issuerKeyHash[SHA_SIZE]; /* hash of the public Key           */
229 #endif /* HAVE_OCSP */
230     byte*   signature;               /* not owned, points into raw cert  */
231     char*   subjectCN;               /* CommonName                       */
232     int     subjectCNLen;
233     int     subjectCNStored;         /* have we saved a copy we own      */
234     char    issuer[ASN_NAME_MAX];    /* full name including common name  */
235     char    subject[ASN_NAME_MAX];   /* full name including common name  */
236     int     verify;                  /* Default to yes, but could be off */
237     byte*   source;                  /* byte buffer holder cert, NOT owner */
238     word32  srcIdx;                  /* current offset into buffer       */
239     word32  maxIdx;                  /* max offset based on init size    */
240     void*   heap;                    /* for user memory overrides        */
241     byte    serial[EXTERNAL_SERIAL_SIZE];  /* raw serial number          */
242     int     serialSz;                /* raw serial bytes stored */
243     byte*   extensions;              /* not owned, points into raw cert  */
244     int     extensionsSz;            /* length of cert extensions */
245     word32  extensionsIdx;           /* if want to go back and parse later */
246     byte*   extAuthInfo;             /* Authority Information Access URI */
247     int     extAuthInfoSz;           /* length of the URI                */
248     byte*   extCrlInfo;              /* CRL Distribution Points          */
249     int     extCrlInfoSz;            /* length of the URI                */
250     byte    isCA;                    /* CA basic constraint true */
251 #ifdef CYASSL_CERT_GEN
252     /* easy access to subject info for other sign */
253     char*   subjectSN;
254     int     subjectSNLen;
255     char*   subjectC;
256     int     subjectCLen;
257     char*   subjectL;
258     int     subjectLLen;
259     char*   subjectST;
260     int     subjectSTLen;
261     char*   subjectO;
262     int     subjectOLen;
263     char*   subjectOU;
264     int     subjectOULen;
265     char*   subjectEmail;
266     int     subjectEmailLen;
267     byte*   beforeDate;
268     int     beforeDateLen;
269     byte*   afterDate;
270     int     afterDateLen;
271 #endif /* CYASSL_CERT_GEN */
272 };
273
274
275 /* CA Signers */
276 struct Signer {
277     byte*   publicKey;
278     word32  pubKeySize;
279     word32  keyOID;                  /* key type */
280     char*   name;                    /* common name */
281     byte    hash[SHA_DIGEST_SIZE];   /* sha hash of names in certificate */
282     Signer* next;
283 };
284
285
286 /* not for public consumption but may use for testing sometimes */
287 #ifdef CYASSL_TEST_CERT
288     #define CYASSL_TEST_API CYASSL_API
289 #else
290     #define CYASSL_TEST_API CYASSL_LOCAL
291 #endif
292
293 CYASSL_TEST_API void FreeAltNames(DNS_entry*, void*);
294 CYASSL_TEST_API void InitDecodedCert(DecodedCert*, byte*, word32, void*);
295 CYASSL_TEST_API void FreeDecodedCert(DecodedCert*);
296 CYASSL_TEST_API int  ParseCert(DecodedCert*, int type, int verify, void* cm);
297
298 CYASSL_LOCAL int ParseCertRelative(DecodedCert*, int type, int verify,void* cm);
299 CYASSL_LOCAL int DecodeToKey(DecodedCert*, int verify);
300
301 CYASSL_LOCAL word32 EncodeSignature(byte* out, const byte* digest, word32 digSz,
302                                     int hashOID);
303
304 CYASSL_LOCAL Signer* MakeSigner(void*);
305 CYASSL_LOCAL void    FreeSigners(Signer*, void*);
306
307
308 CYASSL_LOCAL int ToTraditional(byte* buffer, word32 length);
309 CYASSL_LOCAL int ToTraditionalEnc(byte* buffer, word32 length,const char*, int);
310
311 CYASSL_LOCAL int ValidateDate(const byte* date, byte format, int dateType);
312
313 #ifdef HAVE_ECC
314     /* ASN sig helpers */
315     CYASSL_LOCAL int StoreECC_DSA_Sig(byte* out, word32* outLen, mp_int* r,
316                                       mp_int* s);
317     CYASSL_LOCAL int DecodeECC_DSA_Sig(const byte* sig, word32 sigLen,
318                                        mp_int* r, mp_int* s);
319     /* private key helpers */
320     CYASSL_LOCAL int EccPrivateKeyDecode(const byte* input,word32* inOutIdx,
321                                          ecc_key*,word32);
322 #endif
323
324 #ifdef CYASSL_CERT_GEN
325
326 enum cert_enums {
327     NAME_ENTRIES    =  8,
328     JOINT_LEN       =  2,
329     EMAIL_JOINT_LEN =  9,
330     RSA_KEY         = 10,
331     NTRU_KEY        = 11
332 };
333
334
335 #endif /* CYASSL_CERT_GEN */
336
337
338
339 /* for pointer use */
340 typedef struct CertStatus CertStatus;
341
342 #ifdef HAVE_OCSP
343
344 enum Ocsp_Response_Status {
345     OCSP_SUCCESSFUL        = 0, /* Response has valid confirmations */
346     OCSP_MALFORMED_REQUEST = 1, /* Illegal confirmation request */
347     OCSP_INTERNAL_ERROR    = 2, /* Internal error in issuer */
348     OCSP_TRY_LATER         = 3, /* Try again later */
349     OCSP_SIG_REQUIRED      = 5, /* Must sign the request (4 is skipped) */
350     OCSP_UNAUTHROIZED      = 6  /* Request unauthorized */
351 };
352
353
354 enum Ocsp_Cert_Status {
355     CERT_GOOD    = 0,
356     CERT_REVOKED = 1,
357     CERT_UNKNOWN = 2
358 };
359
360
361 enum Ocsp_Sums {
362     OCSP_BASIC_OID = 117,
363     OCSP_NONCE_OID = 118
364 };
365
366
367 typedef struct OcspRequest  OcspRequest;
368 typedef struct OcspResponse OcspResponse;
369
370
371 struct CertStatus {
372     CertStatus* next;
373
374     byte serial[EXTERNAL_SERIAL_SIZE];
375     int serialSz;
376
377     int status;
378
379     byte thisDate[MAX_DATE_SIZE];
380     byte nextDate[MAX_DATE_SIZE];
381     byte thisDateFormat;
382     byte nextDateFormat;
383 };
384
385
386 struct OcspResponse {
387     int     responseStatus;  /* return code from Responder */
388
389     byte*   response;        /* Pointer to beginning of OCSP Response */
390     word32  responseSz;      /* length of the OCSP Response */
391
392     byte    producedDate[MAX_DATE_SIZE];
393                                                          /* Date at which this response was signed */
394     byte    producedDateFormat; /* format of the producedDate */
395     byte*   issuerHash;
396     byte*   issuerKeyHash;
397
398     byte*   cert;
399     word32  certSz;
400
401     byte*   sig;             /* Pointer to sig in source */
402     word32  sigSz;           /* Length in octets for the sig */
403     word32  sigOID;          /* OID for hash used for sig */
404
405     CertStatus* status;      /* certificate status to fill out */
406
407     byte*   nonce;           /* pointer to nonce inside ASN.1 response */
408     int     nonceSz;         /* length of the nonce string */
409
410     byte*   source;          /* pointer to source buffer, not owned */
411     word32  maxIdx;          /* max offset based on init size */
412 };
413
414
415 struct OcspRequest {
416     DecodedCert* cert;
417
418     byte    nonce[MAX_OCSP_NONCE_SZ];
419     int     nonceSz;
420
421     byte*   issuerHash;      /* pointer to issuerHash in source cert */
422     byte*   issuerKeyHash;   /* pointer to issuerKeyHash in source cert */
423     byte*   serial;          /* pointer to serial number in source cert */
424     int     serialSz;        /* length of the serial number */
425
426     byte*   dest;            /* pointer to the destination ASN.1 buffer */
427     word32  destSz;          /* length of the destination buffer */
428 };
429
430
431 CYASSL_LOCAL void InitOcspResponse(OcspResponse*, CertStatus*, byte*, word32);
432 CYASSL_LOCAL int  OcspResponseDecode(OcspResponse*);
433
434 CYASSL_LOCAL void InitOcspRequest(OcspRequest*, DecodedCert*, byte*, word32);
435 CYASSL_LOCAL int  EncodeOcspRequest(OcspRequest*);
436
437 CYASSL_LOCAL int  CompareOcspReqResp(OcspRequest*, OcspResponse*);
438
439
440 #endif /* HAVE_OCSP */
441
442
443 /* for pointer use */
444 typedef struct RevokedCert RevokedCert;
445
446 #ifdef HAVE_CRL
447
448 struct RevokedCert {
449     byte         serialNumber[EXTERNAL_SERIAL_SIZE];
450     int          serialSz;
451     RevokedCert* next;
452 };
453
454 typedef struct DecodedCRL DecodedCRL;
455
456 struct DecodedCRL {
457     word32  certBegin;               /* offset to start of cert          */
458     word32  sigIndex;                /* offset to start of signature     */
459     word32  sigLength;               /* length of signature              */
460     word32  signatureOID;            /* sum of algorithm object id       */
461     byte*   signature;               /* pointer into raw source, not owned */
462     byte    issuerHash[SHA_DIGEST_SIZE];  /* issuer hash                 */ 
463     byte    crlHash[MD5_DIGEST_SIZE];     /* raw crl data hash           */ 
464     byte    lastDate[MAX_DATE_SIZE]; /* last date updated  */
465     byte    nextDate[MAX_DATE_SIZE]; /* next update date   */
466     byte    lastDateFormat;          /* format of last date */
467     byte    nextDateFormat;          /* format of next date */
468     RevokedCert* certs;              /* revoked cert list  */
469     int          totalCerts;         /* number on list     */
470 };
471
472 CYASSL_LOCAL void InitDecodedCRL(DecodedCRL*);
473 CYASSL_LOCAL int  ParseCRL(DecodedCRL*, const byte* buff, long sz, void* cm);
474 CYASSL_LOCAL void FreeDecodedCRL(DecodedCRL*);
475
476
477 #endif /* HAVE_CRL */
478
479
480 #ifdef __cplusplus
481     } /* extern "C" */
482 #endif
483
484 #endif /* CTAO_CRYPT_ASN_H */
485