ssh lib
wolfcrypt/pkcs7.h@1:e4ea39eba2fb, 2019-11-25 (annotated)
- Committer:
- sPymbed
- Date:
- Mon Nov 25 14:23:49 2019 +0000
- Revision:
- 1:e4ea39eba2fb
- Parent:
- 0:1387ff3eed4a
improved
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sPymbed | 0:1387ff3eed4a | 1 | /* pkcs7.h |
sPymbed | 0:1387ff3eed4a | 2 | * |
sPymbed | 0:1387ff3eed4a | 3 | * Copyright (C) 2006-2017 wolfSSL Inc. |
sPymbed | 0:1387ff3eed4a | 4 | * |
sPymbed | 0:1387ff3eed4a | 5 | * This file is part of wolfSSL. |
sPymbed | 0:1387ff3eed4a | 6 | * |
sPymbed | 0:1387ff3eed4a | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
sPymbed | 0:1387ff3eed4a | 8 | * it under the terms of the GNU General Public License as published by |
sPymbed | 0:1387ff3eed4a | 9 | * the Free Software Foundation; either version 2 of the License, or |
sPymbed | 0:1387ff3eed4a | 10 | * (at your option) any later version. |
sPymbed | 0:1387ff3eed4a | 11 | * |
sPymbed | 0:1387ff3eed4a | 12 | * wolfSSL is distributed in the hope that it will be useful, |
sPymbed | 0:1387ff3eed4a | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
sPymbed | 0:1387ff3eed4a | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
sPymbed | 0:1387ff3eed4a | 15 | * GNU General Public License for more details. |
sPymbed | 0:1387ff3eed4a | 16 | * |
sPymbed | 0:1387ff3eed4a | 17 | * You should have received a copy of the GNU General Public License |
sPymbed | 0:1387ff3eed4a | 18 | * along with this program; if not, write to the Free Software |
sPymbed | 0:1387ff3eed4a | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
sPymbed | 0:1387ff3eed4a | 20 | */ |
sPymbed | 0:1387ff3eed4a | 21 | |
sPymbed | 0:1387ff3eed4a | 22 | /*! |
sPymbed | 0:1387ff3eed4a | 23 | \file wolfssl/wolfcrypt/pkcs7.h |
sPymbed | 0:1387ff3eed4a | 24 | */ |
sPymbed | 0:1387ff3eed4a | 25 | |
sPymbed | 0:1387ff3eed4a | 26 | #ifndef WOLF_CRYPT_PKCS7_H |
sPymbed | 0:1387ff3eed4a | 27 | #define WOLF_CRYPT_PKCS7_H |
sPymbed | 0:1387ff3eed4a | 28 | |
sPymbed | 0:1387ff3eed4a | 29 | #include <wolfcrypt/types.h> |
sPymbed | 0:1387ff3eed4a | 30 | |
sPymbed | 0:1387ff3eed4a | 31 | #ifdef HAVE_PKCS7 |
sPymbed | 0:1387ff3eed4a | 32 | |
sPymbed | 0:1387ff3eed4a | 33 | #ifndef NO_ASN |
sPymbed | 0:1387ff3eed4a | 34 | #include <wolfcrypt/asn.h> |
sPymbed | 0:1387ff3eed4a | 35 | #endif |
sPymbed | 0:1387ff3eed4a | 36 | #include <wolfcrypt/asn_public.h> |
sPymbed | 0:1387ff3eed4a | 37 | #include <wolfcrypt/random.h> |
sPymbed | 0:1387ff3eed4a | 38 | #ifndef NO_AES |
sPymbed | 0:1387ff3eed4a | 39 | #include <wolfcrypt/aes.h> |
sPymbed | 0:1387ff3eed4a | 40 | #endif |
sPymbed | 0:1387ff3eed4a | 41 | #ifndef NO_DES3 |
sPymbed | 0:1387ff3eed4a | 42 | #include <wolfcrypt/des3.h> |
sPymbed | 0:1387ff3eed4a | 43 | #endif |
sPymbed | 0:1387ff3eed4a | 44 | |
sPymbed | 0:1387ff3eed4a | 45 | #ifdef __cplusplus |
sPymbed | 0:1387ff3eed4a | 46 | extern "C" { |
sPymbed | 0:1387ff3eed4a | 47 | #endif |
sPymbed | 0:1387ff3eed4a | 48 | |
sPymbed | 0:1387ff3eed4a | 49 | /* Max number of certificates that PKCS7 structure can parse */ |
sPymbed | 0:1387ff3eed4a | 50 | #ifndef MAX_PKCS7_CERTS |
sPymbed | 0:1387ff3eed4a | 51 | #define MAX_PKCS7_CERTS 4 |
sPymbed | 0:1387ff3eed4a | 52 | #endif |
sPymbed | 0:1387ff3eed4a | 53 | |
sPymbed | 0:1387ff3eed4a | 54 | /* PKCS#7 content types, ref RFC 2315 (Section 14) */ |
sPymbed | 0:1387ff3eed4a | 55 | enum PKCS7_TYPES { |
sPymbed | 0:1387ff3eed4a | 56 | PKCS7_MSG = 650, /* 1.2.840.113549.1.7 */ |
sPymbed | 0:1387ff3eed4a | 57 | DATA = 651, /* 1.2.840.113549.1.7.1 */ |
sPymbed | 0:1387ff3eed4a | 58 | SIGNED_DATA = 652, /* 1.2.840.113549.1.7.2 */ |
sPymbed | 0:1387ff3eed4a | 59 | ENVELOPED_DATA = 653, /* 1.2.840.113549.1.7.3 */ |
sPymbed | 0:1387ff3eed4a | 60 | SIGNED_AND_ENVELOPED_DATA = 654, /* 1.2.840.113549.1.7.4 */ |
sPymbed | 0:1387ff3eed4a | 61 | DIGESTED_DATA = 655, /* 1.2.840.113549.1.7.5 */ |
sPymbed | 0:1387ff3eed4a | 62 | ENCRYPTED_DATA = 656 /* 1.2.840.113549.1.7.6 */ |
sPymbed | 0:1387ff3eed4a | 63 | }; |
sPymbed | 0:1387ff3eed4a | 64 | |
sPymbed | 0:1387ff3eed4a | 65 | enum Pkcs7_Misc { |
sPymbed | 0:1387ff3eed4a | 66 | PKCS7_NONCE_SZ = 16, |
sPymbed | 0:1387ff3eed4a | 67 | MAX_ENCRYPTED_KEY_SZ = 512, /* max enc. key size, RSA <= 4096 */ |
sPymbed | 0:1387ff3eed4a | 68 | MAX_CONTENT_KEY_LEN = 32, /* highest current cipher is AES-256-CBC */ |
sPymbed | 0:1387ff3eed4a | 69 | MAX_CONTENT_IV_SIZE = 16, /* highest current is AES128 */ |
sPymbed | 0:1387ff3eed4a | 70 | #ifndef NO_AES |
sPymbed | 0:1387ff3eed4a | 71 | MAX_CONTENT_BLOCK_LEN = AES_BLOCK_SIZE, |
sPymbed | 0:1387ff3eed4a | 72 | #else |
sPymbed | 0:1387ff3eed4a | 73 | MAX_CONTENT_BLOCK_LEN = DES_BLOCK_SIZE, |
sPymbed | 0:1387ff3eed4a | 74 | #endif |
sPymbed | 0:1387ff3eed4a | 75 | MAX_RECIP_SZ = MAX_VERSION_SZ + |
sPymbed | 0:1387ff3eed4a | 76 | MAX_SEQ_SZ + ASN_NAME_MAX + MAX_SN_SZ + |
sPymbed | 0:1387ff3eed4a | 77 | MAX_SEQ_SZ + MAX_ALGO_SZ + 1 + MAX_ENCRYPTED_KEY_SZ |
sPymbed | 0:1387ff3eed4a | 78 | }; |
sPymbed | 0:1387ff3eed4a | 79 | |
sPymbed | 0:1387ff3eed4a | 80 | |
sPymbed | 0:1387ff3eed4a | 81 | typedef struct PKCS7Attrib { |
sPymbed | 0:1387ff3eed4a | 82 | byte* oid; |
sPymbed | 0:1387ff3eed4a | 83 | word32 oidSz; |
sPymbed | 0:1387ff3eed4a | 84 | byte* value; |
sPymbed | 0:1387ff3eed4a | 85 | word32 valueSz; |
sPymbed | 0:1387ff3eed4a | 86 | } PKCS7Attrib; |
sPymbed | 0:1387ff3eed4a | 87 | |
sPymbed | 0:1387ff3eed4a | 88 | |
sPymbed | 0:1387ff3eed4a | 89 | typedef struct PKCS7DecodedAttrib { |
sPymbed | 0:1387ff3eed4a | 90 | struct PKCS7DecodedAttrib* next; |
sPymbed | 0:1387ff3eed4a | 91 | byte* oid; |
sPymbed | 0:1387ff3eed4a | 92 | word32 oidSz; |
sPymbed | 0:1387ff3eed4a | 93 | byte* value; |
sPymbed | 0:1387ff3eed4a | 94 | word32 valueSz; |
sPymbed | 0:1387ff3eed4a | 95 | } PKCS7DecodedAttrib; |
sPymbed | 0:1387ff3eed4a | 96 | |
sPymbed | 0:1387ff3eed4a | 97 | |
sPymbed | 0:1387ff3eed4a | 98 | /* Public Structure Warning: |
sPymbed | 0:1387ff3eed4a | 99 | * Existing members must not be changed to maintain backwards compatibility! |
sPymbed | 0:1387ff3eed4a | 100 | */ |
sPymbed | 0:1387ff3eed4a | 101 | typedef struct PKCS7 { |
sPymbed | 0:1387ff3eed4a | 102 | WC_RNG* rng; |
sPymbed | 0:1387ff3eed4a | 103 | PKCS7Attrib* signedAttribs; |
sPymbed | 0:1387ff3eed4a | 104 | byte* content; /* inner content, not owner */ |
sPymbed | 0:1387ff3eed4a | 105 | byte* singleCert; /* recipient cert, DER, not owner */ |
sPymbed | 0:1387ff3eed4a | 106 | byte* issuer; /* issuer name of singleCert */ |
sPymbed | 0:1387ff3eed4a | 107 | byte* privateKey; /* private key, DER, not owner */ |
sPymbed | 0:1387ff3eed4a | 108 | void* heap; /* heap hint for dynamic memory */ |
sPymbed | 0:1387ff3eed4a | 109 | #ifdef ASN_BER_TO_DER |
sPymbed | 0:1387ff3eed4a | 110 | byte* der; /* DER encoded version of message */ |
sPymbed | 0:1387ff3eed4a | 111 | #endif |
sPymbed | 0:1387ff3eed4a | 112 | byte* cert[MAX_PKCS7_CERTS]; |
sPymbed | 0:1387ff3eed4a | 113 | |
sPymbed | 0:1387ff3eed4a | 114 | /* Encrypted-data Content Type */ |
sPymbed | 0:1387ff3eed4a | 115 | byte* encryptionKey; /* block cipher encryption key */ |
sPymbed | 0:1387ff3eed4a | 116 | PKCS7Attrib* unprotectedAttribs; /* optional */ |
sPymbed | 0:1387ff3eed4a | 117 | PKCS7DecodedAttrib* decodedAttrib; /* linked list of decoded attribs */ |
sPymbed | 0:1387ff3eed4a | 118 | |
sPymbed | 0:1387ff3eed4a | 119 | /* Enveloped-data optional ukm, not owner */ |
sPymbed | 0:1387ff3eed4a | 120 | byte* ukm; |
sPymbed | 0:1387ff3eed4a | 121 | word32 ukmSz; |
sPymbed | 0:1387ff3eed4a | 122 | |
sPymbed | 0:1387ff3eed4a | 123 | word32 encryptionKeySz; /* size of key buffer, bytes */ |
sPymbed | 0:1387ff3eed4a | 124 | word32 unprotectedAttribsSz; |
sPymbed | 0:1387ff3eed4a | 125 | word32 contentSz; /* content size */ |
sPymbed | 0:1387ff3eed4a | 126 | word32 singleCertSz; /* size of recipient cert buffer, bytes */ |
sPymbed | 0:1387ff3eed4a | 127 | word32 issuerSz; /* length of issuer name */ |
sPymbed | 0:1387ff3eed4a | 128 | word32 issuerSnSz; /* length of serial number */ |
sPymbed | 0:1387ff3eed4a | 129 | |
sPymbed | 0:1387ff3eed4a | 130 | word32 publicKeySz; |
sPymbed | 0:1387ff3eed4a | 131 | word32 publicKeyOID; /* key OID (RSAk, ECDSAk, etc) */ |
sPymbed | 0:1387ff3eed4a | 132 | word32 privateKeySz; /* size of private key buffer, bytes */ |
sPymbed | 0:1387ff3eed4a | 133 | word32 signedAttribsSz; |
sPymbed | 0:1387ff3eed4a | 134 | int contentOID; /* PKCS#7 content type OID sum */ |
sPymbed | 0:1387ff3eed4a | 135 | int hashOID; |
sPymbed | 0:1387ff3eed4a | 136 | int encryptOID; /* key encryption algorithm OID */ |
sPymbed | 0:1387ff3eed4a | 137 | int keyWrapOID; /* key wrap algorithm OID */ |
sPymbed | 0:1387ff3eed4a | 138 | int keyAgreeOID; /* key agreement algorithm OID */ |
sPymbed | 0:1387ff3eed4a | 139 | int devId; /* device ID for HW based private key */ |
sPymbed | 0:1387ff3eed4a | 140 | byte issuerHash[KEYID_SIZE]; /* hash of all alt Names */ |
sPymbed | 0:1387ff3eed4a | 141 | byte issuerSn[MAX_SN_SZ]; /* singleCert's serial number */ |
sPymbed | 0:1387ff3eed4a | 142 | byte publicKey[MAX_RSA_INT_SZ + MAX_RSA_E_SZ]; /* MAX RSA key size (m + e)*/ |
sPymbed | 0:1387ff3eed4a | 143 | word32 certSz[MAX_PKCS7_CERTS]; |
sPymbed | 0:1387ff3eed4a | 144 | |
sPymbed | 0:1387ff3eed4a | 145 | /* flags - up to 16-bits */ |
sPymbed | 0:1387ff3eed4a | 146 | word16 isDynamic:1; |
sPymbed | 0:1387ff3eed4a | 147 | |
sPymbed | 0:1387ff3eed4a | 148 | /* !! NEW DATA MEMBERS MUST BE ADDED AT END !! */ |
sPymbed | 0:1387ff3eed4a | 149 | } PKCS7; |
sPymbed | 0:1387ff3eed4a | 150 | |
sPymbed | 0:1387ff3eed4a | 151 | |
sPymbed | 0:1387ff3eed4a | 152 | WOLFSSL_API PKCS7* wc_PKCS7_New(void* heap, int devId); |
sPymbed | 0:1387ff3eed4a | 153 | WOLFSSL_API int wc_PKCS7_Init(PKCS7* pkcs7, void* heap, int devId); |
sPymbed | 0:1387ff3eed4a | 154 | WOLFSSL_API int wc_PKCS7_InitWithCert(PKCS7* pkcs7, byte* cert, word32 certSz); |
sPymbed | 0:1387ff3eed4a | 155 | WOLFSSL_API void wc_PKCS7_Free(PKCS7* pkcs7); |
sPymbed | 0:1387ff3eed4a | 156 | |
sPymbed | 0:1387ff3eed4a | 157 | WOLFSSL_API int wc_PKCS7_GetAttributeValue(PKCS7* pkcs7, const byte* oid, |
sPymbed | 0:1387ff3eed4a | 158 | word32 oidSz, byte* out, word32* outSz); |
sPymbed | 0:1387ff3eed4a | 159 | WOLFSSL_API int wc_PKCS7_EncodeData(PKCS7* pkcs7, byte* output, |
sPymbed | 0:1387ff3eed4a | 160 | word32 outputSz); |
sPymbed | 0:1387ff3eed4a | 161 | WOLFSSL_API int wc_PKCS7_EncodeSignedData(PKCS7* pkcs7, |
sPymbed | 0:1387ff3eed4a | 162 | byte* output, word32 outputSz); |
sPymbed | 0:1387ff3eed4a | 163 | WOLFSSL_API int wc_PKCS7_VerifySignedData(PKCS7* pkcs7, |
sPymbed | 0:1387ff3eed4a | 164 | byte* pkiMsg, word32 pkiMsgSz); |
sPymbed | 0:1387ff3eed4a | 165 | WOLFSSL_API int wc_PKCS7_EncodeEnvelopedData(PKCS7* pkcs7, |
sPymbed | 0:1387ff3eed4a | 166 | byte* output, word32 outputSz); |
sPymbed | 0:1387ff3eed4a | 167 | WOLFSSL_API int wc_PKCS7_DecodeEnvelopedData(PKCS7* pkcs7, byte* pkiMsg, |
sPymbed | 0:1387ff3eed4a | 168 | word32 pkiMsgSz, byte* output, |
sPymbed | 0:1387ff3eed4a | 169 | word32 outputSz); |
sPymbed | 0:1387ff3eed4a | 170 | |
sPymbed | 0:1387ff3eed4a | 171 | WOLFSSL_API int wc_PKCS7_GetPadSize(word32 inputSz, word32 blockSz); |
sPymbed | 0:1387ff3eed4a | 172 | WOLFSSL_API int wc_PKCS7_PadData(byte* in, word32 inSz, byte* out, word32 outSz, |
sPymbed | 0:1387ff3eed4a | 173 | word32 blockSz); |
sPymbed | 0:1387ff3eed4a | 174 | |
sPymbed | 0:1387ff3eed4a | 175 | #ifndef NO_PKCS7_ENCRYPTED_DATA |
sPymbed | 0:1387ff3eed4a | 176 | WOLFSSL_API int wc_PKCS7_EncodeEncryptedData(PKCS7* pkcs7, |
sPymbed | 0:1387ff3eed4a | 177 | byte* output, word32 outputSz); |
sPymbed | 0:1387ff3eed4a | 178 | WOLFSSL_API int wc_PKCS7_DecodeEncryptedData(PKCS7* pkcs7, byte* pkiMsg, |
sPymbed | 0:1387ff3eed4a | 179 | word32 pkiMsgSz, byte* output, |
sPymbed | 0:1387ff3eed4a | 180 | word32 outputSz); |
sPymbed | 0:1387ff3eed4a | 181 | #endif /* NO_PKCS7_ENCRYPTED_DATA */ |
sPymbed | 0:1387ff3eed4a | 182 | |
sPymbed | 0:1387ff3eed4a | 183 | #ifdef __cplusplus |
sPymbed | 0:1387ff3eed4a | 184 | } /* extern "C" */ |
sPymbed | 0:1387ff3eed4a | 185 | #endif |
sPymbed | 0:1387ff3eed4a | 186 | |
sPymbed | 0:1387ff3eed4a | 187 | #endif /* HAVE_PKCS7 */ |
sPymbed | 0:1387ff3eed4a | 188 | #endif /* WOLF_CRYPT_PKCS7_H */ |
sPymbed | 0:1387ff3eed4a | 189 | |
sPymbed | 0:1387ff3eed4a | 190 |