CryptoAPI. Labs. Daniil Leksin



Similar documents
FIPS Documentation: Security Policy 05/06/ :21 AM. Windows CE and Windows Mobile Operating System. Abstract

Windows Server 2003 Enhanced Cryptographic Provider (RSAENH)

Windows Server 2003 Enhanced Cryptographic Provider (RSAENH)

Guidelines for Developing Cryptographic Service Providers (CSPs) for Acrobat on Windows

CALIFORNIA SOFTWARE LABS

Intel EP80579 Software for Security Applications on Intel QuickAssist Technology Cryptographic API Reference

Microsoft Win32 Internet Functions

Apple Certificate Library Functional Specification

UM0586 User manual. STM32 Cryptographic Library. Introduction

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

Microsoft Windows 7 Kernel Mode Cryptographic Primitives Library (cng.sys) Security Policy Document

Illustration 1: Diagram of program function and data flow

Package PKI. July 28, 2015

Software Development Kit Manual

Everything is Terrible

A PKI case study: Implementing the Server-based Certificate Validation Protocol

Apple Cryptographic Service Provider Functional Specification

MatrixSSL Developer s Guide

Stacks. Linear data structures

Using etoken for SSL Web Authentication. SSL V3.0 Overview

[MS-RDPESC]: Remote Desktop Protocol: Smart Card Virtual Channel Extension

System Calls and Standard I/O

BlackBerry Enterprise Service 10. Secure Work Space for ios and Android Version: Security Note

Application Note. Introduction AN2471/D 3/2003. PC Master Software Communication Protocol Specification

Specific Simple Network Management Tools

TCP/IP Networking, Part 2: Web-Based Control

CS 241 Data Organization Coding Standards

MatrixSSL Porting Guide

Software Application Development. D2XX Programmer's Guide

MS Active Sync: Sync with External Memory Files

Sources: On the Web: Slides will be available on:

Common security requirements Basic security tools. Example. Secret-key cryptography Public-key cryptography. Online shopping with Amazon

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Microsoft Windows Server 2008 R2 Cryptographic Primitives Library (bcryptprimitives.dll) Security Policy Document

Overview of CSS SSL. SSL Cryptography Overview CHAPTER

... Protecting i5/os data with encryption. Kent Milligan and Beth Hagemeister ISV Business Strategy and Enablement October 2006

Package PKI. February 20, 2013

Format string exploitation on windows Using Immunity Debugger / Python. By Abysssec Inc

OpenSSL (lab notes) Definition: OpenSSL is an open-source library containing cryptographic tools.

HP ProtectTools Embedded Security Guide

MPLAB Harmony System Service Libraries Help

Introducing etoken. What is etoken?

X.509 Certificate Generator User Manual

File System Encryption in C#

TivaWare Utilities Library

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

FIPS Non- Proprietary Security Policy. McAfee SIEM Cryptographic Module, Version 1.0

Accellion Secure File Transfer Cryptographic Module Security Policy Document Version 1.0. Accellion, Inc.

Virtuozzo Virtualization SDK

ACTi SDK C Library Edition v1.2 SP1. API Reference Guide

Affdex SDK for Windows!

El Dorado Union High School District Educational Services

User Manual. 3-Heights PDF Producer API. Version 4.6

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

epass2003 User Guide V1.0 Feitian Technologies Co., Ltd. Website:

Ciphire Mail. Abstract

FINAL DoIT v.4 PAYMENT CARD INDUSTRY DATA SECURITY STANDARDS APPLICATION DEVELOPMENT AND MAINTENANCE PROCEDURES

RTI Monitoring Library Getting Started Guide

Introduction to Programming II Winter, 2014 Assignment 2

1 Step 1: Select... Files to Encrypt 2 Step 2: Confirm... Name of Archive 3 Step 3: Define... Pass Phrase

Overview. SSL Cryptography Overview CHAPTER 1

CS222: Systems Programming

Secure Network Communications FIPS Non Proprietary Security Policy

Security Technical. Overview. BlackBerry Enterprise Service 10. BlackBerry Device Service Solution Version: 10.2

Entrust Certificate Services. Java Code Signing. User Guide. Date of Issue: December Document issue: 2.0

Protecting IBM i data with encryption

Binary storage of graphs and related data

AWS Encryption SDK. Developer Guide

DocStore: Document Database for MySQL at Facebook. Peng Tian, Tian Xia 04/14/2015

SMF Digital Signatures in z/os 2.2. Anthony Sofia Software Engineer at IBM August 14 th 2015

System Security Fundamentals

Internet Payment Gateway. Win32 API Developer Guide

ncipher modules Integration Guide for Microsoft Windows Server 2008 Active Directory Certificate Services Windows Server bit and 64-bit

Expansion of a Framework for a Data- Intensive Wide-Area Application to the Java Language

Building a Multi-Threaded Web Server

Fast Arithmetic Coding (FastAC) Implementations

EC-Council CAST CENTER FOR ADVANCED SECURITY TRAINING. CAST 619 Advanced SQLi Attacks and Countermeasures. Make The Difference CAST.

Basic Data Types. typedef short SHORT; typedef unsigned long DWORD;

Paper-based Document Authentication using Digital Signature and QR Code

Windows CE 6.0 APEX ZF

CS3600 SYSTEMS AND NETWORKS

Using NSM for Event Notification. Abstract. with DM3, R4, and Win32 Devices

HTC Windows Phone 7 Arbitrary Read/Write of Kernel Memory 10/11/2011

SkyRecon Cryptographic Module (SCM)

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

Practice Questions. CS161 Computer Security, Fall 2008

Keywords Cloud Storage, Error Identification, Partitioning, Cloud Storage Integrity Checking, Digital Signature Extraction, Encryption, Decryption

Introduction...3 Terms in this Document...3 Conditions for Secure Operation...3 Requirements...3 Key Generation Requirements...

AN AES encryption and decryption software on LPC microcontrollers. Document information

SecureDoc Disk Encryption Cryptographic Engine

APPLICATION PROGRAMMING INTERFACE

Asymmetric Encryption

Lecture 3 Programming with OpenSSL

HP AppPulse Mobile. Adding HP AppPulse Mobile to Your Android App

Draft Middleware Specification. Version X.X MM/DD/YYYY

Client-server Sockets

Integrating Gestures in Windows Embedded Compact 7. Douglas Boling Boling Consulting Inc.

National Certification Authority Framework in Sri Lanka

Transcription:

CryptoAPI Labs Daniil Leksin

Structure CryptoAPI CSP (Cryptography Service Provider) CA Working with CryptoAPI, CSP, CA: algorithms, block-schemes and examples

CryptoAPI CryptoAPI (Cryptographic Application Programming Interface, Microsoft Cryptography API, MS-CAPI or simply CAPI) is an application programming interface included with Microsoft Windows operating systems that provides services to enable developers to secure Windows-based applications using cryptography. It is a set of dynamically linked libraries that provides an abstraction layer which isolates programmers from the code used to encrypt the data. (CryptoAPI supports both public-key and symmetric key cryptography) CAPI provides: 1. Secure data storing 2. Ability to transfer data 3. Validation from 3 rd party users 4. Work with EDS 5. Work with cryptographic standards 6. Extension CAPI functionality groups: 1. Basic cryptographic functions: 1.1 encoding / decoding 1.2 hash function, EDS 1.3 initializing CSP, working with context 1.4 key generation 1.5 key exchanging 2. Functions for working with certificates 3. High-level functions 4. Low-level functions

CSP CSP (Cryptography Service Provider) - is a software library that implements the Microsoft CryptoAPI (CAPI). CSPs implement encoding and decoding functions, which computer application programs may use. CSP provides: 1. implementation of the standard interface 2. work with encode / decode keys 3. inability to interference from third parties 2 function groups for working with CSP: 1. initialization of the context and getting CSP parameters 2. Key generation and function for work with them 3. encode / decode functions 4. Hash functions and getting EDS

CAPI & CSP & Apps CSP 1 CSP 2 CSP 3 OS Windows with CAPI Application 1 Application 2 Application 3

Find CSP on current machine HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography\Defaults\Provider\

CryptEnumProviderTypes( dwindex, 0, &dwtype, &cbname) enum.cpp _tprintf (TEXT(" %4.0d %s\n"), dwtype, pszname); The CryptEnumProviderTypes function retrieves the first or next types of cryptographic service provider (CSP) supported on the computer. Used in a loop, this function retrieves in sequence all of the CSP types available on a computer. BOOL WINAPI CryptEnumProviderTypes( _In_ DWORD dwindex, _In_ DWORD *pdwreserved, _In_ DWORD dwflags, _Out_ DWORD *pdwprovtype, _Out_ LPTSTR psztypename, _Inout_ DWORD ); *pcbtypename

CryptEnumProviders( dwindex++, 0, &dwtype, &cbname) enum.cpp _tprintf (TEXT(" %4.0d %s\n"), dwtype, pszname); The CryptEnumProviders function retrieves the first or next available cryptographic service providers (CSPs). Used in a loop, this function can retrieve in sequence all of the CSPs available on a computer. BOOL WINAPI CryptEnumProviders( _In_ DWORD dwindex, _In_ DWORD *pdwreserved, _In_ DWORD dwflags, _Out_ DWORD *pdwprovtype, _Out_ LPTSTR pszprovname, _Inout_ DWORD ); *pcbprovname

CryptGetDefaultProvider( PROV_RSA_FULL, CRYPT_MACHINE_DEFAULT, pbprovname, &cbprovname)) _tprintf(text("\nthe default provider name is \"%s\"\n"), pbprovname); enum.cpp The CryptGetDefaultProvider function finds the default cryptographic service provider (CSP) of a specified provider type for the local computer or current user. The name of the default CSP for the provider type specified in the dwprovtype parameter is returned in the pszprovname buffer. BOOL WINAPI CryptGetDefaultProvider( _In_ DWORD dwprovtype, _In_ DWORD *pdwreserved, _In_ DWORD dwflags, _Out_ LPTSTR pszprovname, _Inout_ DWORD *pcbprovname );

enum.cpp CryptAcquireContext( &hprov, PROV_RSA_FULL, NULL) CryptAcquireContext( &hprov, PROV_RSA_FULL, CRYPT_NEWKEYSET) The CryptAcquireContext function is used to acquire a handle to a particular key container within a particular cryptographic service provider (CSP). This returned handle is used in calls to CryptoAPI functions that use the selected CSP. BOOL WINAPI CryptAcquireContext( _Out_ HCRYPTPROV *phprov, _In_ LPCTSTR pszcontainer, _In_ LPCTSTR pszprovider, _In_ DWORD dwprovtype, _In_ DWORD dwflags ); phprov [out] A pointer to a handle of a CSP. CRYPT_NEWKEYSET Creates a new key container with the name specified by pszcontainer. If pszcontainer is a key container with the default name is created.

enum.cpp CryptGetProvParam( hprov, PP_ENUMALGS, pbdata, &cbdata, dwflags) BOOL WINAPI CryptGetProvParam( _In_ HCRYPTPROV hprov, _In_ DWORD dwparam, _Out_ BYTE *pbdata, _Inout_ DWORD *pdwdatalen, _In_ DWORD dwflags ); typedef struct _PROV_ENUMALGS ALG_ID aialgid; DWORD dwbitlen; DWORD dwnamelen; CHAR szname[20]; PROV_ENUMALGS; The CryptGetProvParam function retrieves parameters that govern the operations of a cryptographic service provider A PROV_ENUMALGS structure that contains information about one algorithm supported by the CSP being queried. The first time this value is read, the dwflags parameter must contain the CRYPT_FIRST flag. Doing so causes this function to retrieve the first element in the enumeration. The subsequent elements can then be retrieved by setting the CRYPT_NEXT flag in the dwflags parameter. When this function fails with the ERROR_NO_MORE_ITEMS error code, the end of the enumeration has been reached.

enum.cpp // Extract algorithm information from the pbdata buffer. dwflags = 0; ptr = pbdata; aialgid = *(ALG_ID *)ptr; ptr += sizeof(alg_id); dwbits = *(DWORD *)ptr; ptr += dwincrement; dwnamelen = *(DWORD *)ptr; ptr += dwincrement; strncpy(szname,(char *) ptr, dwnamelen); // Determine the algorithm type. switch(get_alg_class(aialgid)) case ALG_CLASS_DATA_ENCRYPT: pszalgtype = "Encrypt "; break; case ALG_CLASS_HASH: pszalgtype = "Hash "; break; case ALG_CLASS_KEY_EXCHANGE: pszalgtype = "Exchange "; break; case ALG_CLASS_SIGNATURE: pszalgtype = "Signature"; break; default: pszalgtype = "Unknown "; break; // Print information about the algorithm. printf(" %8.8xh %-4d %s %-2d %s\n", aialgid, dwbits, pszalgtype, dwnamelen, szname);

enum.cpp

c1.cpp // Define the name of the store where the needed certificate // can be found. // The message to be signed // Size of message. Note that the length set is one more than the // length returned by the strlen function in order to include // the NULL string termination character. // Pointer to a signer certificate // Create the MessageArray and the MessageSizeArray. // Begin processing. Display the original message. // Open a certificate store. // Get a pointer to the signer's certificate. // Initialize the signature structure. // With two calls to CryptSignMessage, sign the message. // First, get the size of the output signed BLOB. // Second, Get the SignedMessageBlob. // Verify the message signature. // With two calls to CryptVerifyMessageSignature, verify and // decode the signed message. // First, call CryptVerifyMessageSignature to get the length // of the buffer needed to hold the decoded message. // Allocate memory for the buffer. step1 step2 step3 step4 step5

step1 // Define the name of the store where the needed certificate // can be found. #define CERT_STORE_NAME L"labak_cert_store" step2 // The message to be signed // Size of message. Note that the length set is one more than the // length returned by the strlen function in order to include // the NULL string termination character. // Pointer to a signer certificate // Create the MessageArray and the MessageSizeArray. BYTE* pbmessage = (BYTE*)"CryptoAPI is a good way to handle security"; // DWORD cbmessage = strlen((char*) pbmessage)+1; // PCCERT_CONTEXT psignercert; // CRYPT_SIGN_MESSAGE_PARA SigParams; DWORD cbsignedmessageblob; BYTE *pbsignedmessageblob; DWORD cbdecodedmessageblob; BYTE *pbdecodedmessageblob; CRYPT_VERIFY_MESSAGE_PARA VerifyParams; // const BYTE* MessageArray[] = pbmessage; DWORD MessageSizeArray[1]; MessageSizeArray[0] = cbmessage;

step3 if (!( hstorehandle = CertOpenStore( CERT_STORE_PROV_SYSTEM, 0, CERT_SYSTEM_STORE_CURRENT_USER, CERT_STORE_NAME))) MyHandleError("The MY store could not be opened."); if(psignercert = CertFindCertificateInStore( hstorehandle, MY_TYPE, 0, CERT_FIND_SUBJECT_STR, SIGNER_NAME, NULL)) printf("the signer's certificate was found.\n"); else MyHandleError( "Signer certificate not found."); // Begin processing. Display the original message. // Open a certificate store. // Get a pointer to the signer's certificate. // Initialize the signature structure. The CertOpenStore function opens a certificate store by using a specified store provider type. While this function can open a certificate store for most purposes. HCERTSTORE WINAPI CertOpenStore( _In_ LPCSTR lpszstoreprovider, _In_ DWORD dwmsgandcertencodingtype, _In_ HCRYPTPROV_LEGACY hcryptprov, _In_ DWORD dwflags, _In_ const void *pvpara ); This function finds the first or next certificate context in a certificate store that matches search criteria established by the dwfindtype parameter and its associated pvfindpara parameter. PCCERT_CONTEXT WINAPI CertFindCertificateInStore( HCERTSTORE hcertstore, DWORD dwcertencodingtype, DWORD dwfindflags, DWORD dwfindtype, const void* pvfindpara, PCCERT_CONTEXT pprevcertcontext );

SigParams.cbSize = sizeof(crypt_sign_message_para); SigParams.dwMsgEncodingType = MY_TYPE; SigParams.pSigningCert = psignercert; SigParams.HashAlgorithm.pszObjId = szoid_rsa_md5; SigParams.HashAlgorithm.Parameters.cbData = NULL; SigParams.cMsgCert = 1; SigParams.rgpMsgCert = &psignercert; SigParams.cAuthAttr = 0; SigParams.dwInnerContentType = 0; SigParams.cMsgCrl = 0; SigParams.cUnauthAttr = 0; SigParams.dwFlags = 0; SigParams.pvHashAuxInfo = NULL; SigParams.rgAuthAttr = NULL; typedef struct _CRYPT_SIGN_MESSAGE_PARA DWORD cbsize; DWORD dwmsgencodingtype; PCCERT_CONTEXT psigningcert; CRYPT_ALGORITHM_IDENTIFIER HashAlgorithm; void *pvhashauxinfo; DWORD cmsgcert; PCCERT_CONTEXT *rgpmsgcert; DWORD cmsgcrl; PCCRL_CONTEXT *rgpmsgcrl; DWORD cauthattr; PCRYPT_ATTRIBUTE rgauthattr; DWORD cunauthattr; PCRYPT_ATTRIBUTE rgunauthattr; DWORD dwflags; DWORD dwinnercontenttype; CRYPT_ALGORITHM_IDENTIFIER HashEncryptionAlgorithm; void pvhashencryptionauxinfo; CRYPT_SIGN_MESSAGE_PARA, *PCRYPT_SIGN_MESSAGE_PARA;

step4 // With two calls to CryptSignMessage, sign the message. // First, get the size of the output signed BLOB. // Second, Get the SignedMessageBlob. if(cryptsignmessage( &SigParams, FALSE, 1, MessageArray, MessageSizeArray, &cbsignedmessageblob)) printf("the size of the BLOB is %d.\n",cbsignedmessageblob); else MyHandleError("Getting signed BLOB size failed"); if(cryptsignmessage( &SigParams, FALSE, 1, MessageArray, MessageSizeArray, pbsignedmessageblob, &cbsignedmessageblob)) printf("the message was signed successfully. \n"); else MyHandleError("Error getting signed BLOB"); if(!(pbsignedmessageblob = (BYTE*)malloc(cbSignedMessageBlob))) MyHandleError("Memory allocation error while signing."); The CryptSignMessage function creates a hash of the specified content, signs the hash, and then encodes both the original message content and the signed hash.

step5 // Verify the message signature. // With two calls to CryptVerifyMessageSignature, verify and // decode the signed message. // First, call CryptVerifyMessageSignature to get the length // of the buffer needed to hold the decoded message. // Allocate memory for the buffer. VerifyParams.cbSize = sizeof(crypt_verify_message_para); VerifyParams.dwMsgAndCertEncodingType = MY_TYPE; VerifyParams.hCryptProv = 0; VerifyParams.pfnGetSignerCertificate = NULL; VerifyParams.pvGetArg = NULL; typedef struct _CRYPT_VERIFY_MESSAGE_PARA DWORD cbsize; DWORD dwmsgandcertencodingtype; HCRYPTPROV_LEGACY hcryptprov; PFN_CRYPT_GET_SIGNER_CERTIFICATE pfngetsignercertificate; void *pvgetarg; PCCERT_STRONG_SIGN_PARA pstrongsignpara; CRYPT_VERIFY_MESSAGE_PARA, *PCRYPT_VERIFY_MESSAGE_PARA;

if(cryptverifymessagesignature( &VerifyParams, 0, pbsignedmessageblob, cbsignedmessageblob, &cbdecodedmessageblob, NULL)) printf("%d bytes need for the buffer.\n", cbdecodedmessageblob); else printf("verification message failed. \n"); if(cryptverifymessagesignature( &VerifyParams, 0, pbsignedmessageblob, cbsignedmessageblob, pbdecodedmessageblob &cbdecodedmessageblob, NULL)) printf("the verified message is \n-> %s \n", pbdecodedmessageblob); else printf("verification message failed. \n"); if(!(pbdecodedmessageblob = (BYTE*)malloc(cbDecodedMessageBlob))) MyHandleError("Memory allocation error allocating decode BLOB."); The CryptVerifyMessageSignature function verifies a signed message's signature.