]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/cyassl/ctaocrypt/pkcs7.h
Final commit before tagging - cosmetic changes only.
[freertos] / FreeRTOS-Plus / Source / CyaSSL / cyassl / ctaocrypt / pkcs7.h
1 /* pkcs7.h
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22
23 #ifdef HAVE_PKCS7
24
25 #ifndef CTAO_CRYPT_PKCS7_H
26 #define CTAO_CRYPT_PKCS7_H
27
28 #include <cyassl/ctaocrypt/types.h>
29 #include <cyassl/ctaocrypt/asn.h>
30 #include <cyassl/ctaocrypt/asn_public.h>
31 #include <cyassl/ctaocrypt/random.h>
32 #include <cyassl/ctaocrypt/des3.h>
33
34 #ifdef __cplusplus
35     extern "C" {
36 #endif
37
38 /* PKCS#7 content types, ref RFC 2315 (Section 14) */
39 enum PKCS7_TYPES {
40     PKCS7_MSG                 = 650,   /* 1.2.840.113549.1.7   */
41     DATA                      = 651,   /* 1.2.840.113549.1.7.1 */
42     SIGNED_DATA               = 652,   /* 1.2.840.113549.1.7.2 */
43     ENVELOPED_DATA            = 653,   /* 1.2.840.113549.1.7.3 */
44     SIGNED_AND_ENVELOPED_DATA = 654,   /* 1.2.840.113549.1.7.4 */
45     DIGESTED_DATA             = 655,   /* 1.2.840.113549.1.7.5 */
46     ENCRYPTED_DATA            = 656    /* 1.2.840.113549.1.7.6 */
47 };
48
49 enum Pkcs7_Misc {
50     PKCS7_NONCE_SZ       = 16,
51     MAX_ENCRYPTED_KEY_SZ = 512,           /* max enc. key size, RSA <= 4096 */
52     MAX_CONTENT_KEY_LEN  = DES3_KEYLEN,   /* highest current cipher is 3DES */
53     MAX_RECIP_SZ         = MAX_VERSION_SZ +
54                            MAX_SEQ_SZ + ASN_NAME_MAX + MAX_SN_SZ +
55                            MAX_SEQ_SZ + MAX_ALGO_SZ + 1 + MAX_ENCRYPTED_KEY_SZ
56 };
57
58
59 typedef struct PKCS7Attrib {
60     byte* oid;
61     word32 oidSz;
62     byte* value;
63     word32 valueSz;
64 } PKCS7Attrib;
65
66
67 typedef struct PKCS7 {
68     byte* content;                /* inner content, not owner             */
69     word32 contentSz;             /* content size                         */
70     int contentOID;               /* PKCS#7 content type OID sum          */
71
72     RNG* rng;
73
74     int hashOID;
75     int encryptOID;               /* key encryption algorithm OID         */
76
77     byte*  singleCert;            /* recipient cert, DER, not owner       */
78     word32 singleCertSz;          /* size of recipient cert buffer, bytes */
79     byte issuerHash[SHA_SIZE];    /* hash of all alt Names                */
80     byte*  issuer;                /* issuer name of singleCert            */
81     word32 issuerSz;              /* length of issuer name                */
82     byte issuerSn[MAX_SN_SZ];     /* singleCert's serial number           */
83     word32 issuerSnSz;            /* length of serial number              */
84     byte publicKey[512];
85     word32 publicKeySz;
86     byte*  privateKey;            /* private key, DER, not owner          */
87     word32 privateKeySz;          /* size of private key buffer, bytes    */
88     
89     PKCS7Attrib* signedAttribs;
90     word32 signedAttribsSz;
91 } PKCS7;
92
93
94 CYASSL_LOCAL int SetContentType(int pkcs7TypeOID, byte* output);
95 CYASSL_LOCAL int GetContentType(const byte* input, word32* inOutIdx,
96                                 word32* oid, word32 maxIdx);
97 CYASSL_LOCAL int CreateRecipientInfo(const byte* cert, word32 certSz,
98                                      int keyEncAlgo, int blockKeySz,
99                                      RNG* rng, byte* contentKeyPlain,
100                                      byte* contentKeyEnc,
101                                      int* keyEncSz, byte* out, word32 outSz);
102
103 CYASSL_API int  PKCS7_InitWithCert(PKCS7* pkcs7, byte* cert, word32 certSz);
104 CYASSL_API void PKCS7_Free(PKCS7* pkcs7);
105 CYASSL_API int  PKCS7_EncodeData(PKCS7* pkcs7, byte* output, word32 outputSz);
106 CYASSL_API int  PKCS7_EncodeSignedData(PKCS7* pkcs7,
107                                        byte* output, word32 outputSz);
108 CYASSL_API int  PKCS7_VerifySignedData(PKCS7* pkcs7,
109                                        byte* pkiMsg, word32 pkiMsgSz);
110 CYASSL_API int  PKCS7_EncodeEnvelopedData(PKCS7* pkcs7,
111                                           byte* output, word32 outputSz);
112 CYASSL_API int  PKCS7_DecodeEnvelopedData(PKCS7* pkcs7, byte* pkiMsg,
113                                           word32 pkiMsgSz, byte* output,
114                                           word32 outputSz);
115
116 #ifdef __cplusplus
117     } /* extern "C" */
118 #endif
119
120 #endif /* CTAO_CRYPT_PKCS7_H */
121
122 #endif /* HAVE_PKCS7 */
123