2012-10-22

PKCS#12 to GnuPG: Extract and import RSA key

X509 p12 (pfx) file và GnuPG (OpenPGP) không tương thích nhau. Tuy nhiên có thể export RSA key từ p12 file, convert rồi import vào gpg. Theo lý thuyết cả 2 đều thao tác trên public/private cryptography có dùng RSA.


Thực ra nếu xem xét ASCII-armored và PEM chúng ta sẽ thấy khá là giống nhau ngoại trừ BEGIN/END markers thì PGP còn có thêm 1 dòng (line cuối cùng) là checksum. Mình không rõ nhưng theo RFC 4880 là CRC24.

Ví dụ xem xét public key PGP ASCII-armored và RSA PEM

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.11 (GNU/Linux)

mQINBE9NBIQBEADMSzN6b0FaPP0rGiLDWKfH4ehN66Z0SAIynXm6lBHjmO69pNsm
....
5NsttvIOclBY5A==
=Zqph
-----END PGP PUBLIC KEY BLOCK-----




-----BEGIN RSA PUBLIC KEY-----
mQINBE9NBIQBEADMSzN6b0FaPP0rGiLDWKfH4ehN66Z0SAIynXm6lBHjmO69pNsm
...
5NsttvIOclBY5A==
-----END RSA PUBLIC KEY-----

Tại Sysmic.org có chỉ cách convert từ OpenSSL sang GnuPG tuy nhiên là gpgsm (with S/MIME)

Có thể thực hiện như sau:
sudo apt-get install dirmngr
GPG_TTY="tty"
gpgsm --import -v file.p12

Tuy nhiên không thể dùng với gpg. Nếu làm manual thì có lẽ rất vất vả, phải nói là không khả thi. Để import RSA key từ OpenSSL sang GPG mình thực hiện như sau:

Extract private key with passphrase
openssl pkcs12 -in file.p12 -nocerts -out private.pem

Remove passphrase
openssl rsa -in private.pem -out rsa-private.key

Dùng pem2openpgp để convert sang OpenPGP với một string là $USERID (lưu ý install monkeysphere trước để có pem2openpgp)

PEM2OPENPGP_USAGE_FLAGS=authenticate,certify,sign pem2openpgp "Some desc for User ID <any@gmail.com>" < rsa-private.key | gpg --import

Lưu ý: biến môi trường PEM2OPENPGP_USAGE_FLAGS xác định usage là SCA, mặc định pem2openpgp sẽ convert với usage là certify.

gpg: key AC37D3E3: secret key imported

gpg: key AC37D3E3: public key "Some desc for User ID <any@gmail.com>" imported


gpg: Total number processed: 1

gpg:               imported: 1  (RSA: 1)
gpg:       secret keys read: 1
gpg:   secret keys imported: 1

$ gpg --list-secret-keys
/home/xxxx/.gnupg/secring.gpg
------------------------------
sec   2048R/AC37D3E3 2012-10-22
uid                 

Some desc for User ID

<
any@gmail.com

>

Sau khi import xong nhớ thực hiện --edit-key với ID eg 
AC37D3E3 thực hiện enable và trust


2012-09-06

Linux - Embedding a file in an executable

Original http://www.linuxjournal.com/content/embedding-file-executable-aka-hello-world-version-5967


Embedding a File in an Executable, aka Hello World, Version 5967


Jun 12, 2008  By Mitch Frazier

I recently had the need to embed a file in an executable. Since I'm working at the command line with gcc, et al and not with a fancy RAD tool that makes it all happen magically it wasn't immediately obvious to me how to make this happen. A bit of searching on the net found a hack to essentially cat it onto the end of the executable and then decipher where it was based on a bunch of information I didn't want to know about. Seemed like there ought to be a better way...
And there is, it's objcopy to the rescue. objcopy converts object files or executables from one format to another. One of the formats it understands is "binary", which is basicly any file that's not in one of the other formats that it understands. So you've probably envisioned the idea: convert the file that we want to embed into an object file, then it can simply be linked in with the rest of our code.
Let's say we have a file name data.txt that we want to embed in our executable:
  # cat data.txt
  Hello world
To convert this into an object file that we can link with our program we just useobjcopy to produce a ".o" file:
  # objcopy --input binary \
            --output elf32-i386 \
            --binary-architecture i386 data.txt data.o
This tells objcopy that our input file is in the "binary" format, that our output file should be in the "elf32-i386" format (object files on the x86). The --binary-architecture option tells objcopy that the output file is meant to "run" on an x86. This is needed so that ld will accept the file for linking with other files for the x86. One would think that specifying the output format as "elf32-i386" would imply this, but it does not.
Now that we have an object file we only need to include it when we run the linker:
  # gcc main.c data.o
When we run the result we get the prayed for output:
  # ./a.out
  Hello world
Of course, I haven't told the whole story yet, nor shown you main.c. When objcopydoes the above conversion it adds some "linker" symbols to the converted object file:
   _binary_data_txt_start
   _binary_data_txt_end
After linking, these symbols specify the start and end of the embedded file. The symbol names are formed by prepending _binary_ and appending _start or _end to the file name. If the file name contains any characters that would be invalid in a symbol name they are converted to underscores (eg data.txt becomes data_txt). If you get unresolved names when linking using these symbols, do a hexdump -C on the object file and look at the end of the dump for the names that objcopy chose.
The code to actually use the embedded file should now be reasonably obvious:
#include 

extern char _binary_data_txt_start;
extern char _binary_data_txt_end;

main()
{
    char*  p = &_binary_data_txt_start;

    while ( p != &_binary_data_txt_end ) putchar(*p++);
}
One important and subtle thing to note is that the symbols added to the object file aren't "variables". They don't contain any data, rather, their address is their value. I declare them as type char because it's convenient for this example: the embedded data is character data. However, you could declare them as anything, as int if the data is an array of integers, or as struct foo_bar_t if the data were any array of foo bars. If the embedded data is not uniform, then char is probably the most convenient: take its address and cast the pointer to the proper type as you traverse the data.
Mitch Frazier is an Associate Editor for Linux Journal.

2012-07-13

Compile OpenSSL with Visual Studio 2010


Download Perl, nasm, source OpenSSL -> chỉnh lại path trong file bat (copy trong link trên)

@rem @echo off
@cls

@rem 1. Download and unzip Netwide Assembler (http://nasm.us/) to some folder
@rem 2. Download and uzip Strawberry Perl (http://strawberryperl.com/)
@rem    or ActivePerl (http://www.activestate.com/activeperl/downloads)
@rem 3. Download and uzip (xxx.tar.gz) latest version of OpenSSL (http://www.openssl.org/)

@rem NOTE: For more info, please read INSTALL.W32 file in OpenSSL folder

@rem ***************************************************************
@rem Setup paths
@rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@set strPathNetwideAssembler=D:\workspace\nasm-2.09.10
@set strPathStrawberryPerl=D:\workspace\strawberry
@set strPathActivePerl=D:\workspace\Perl64\bin
@rem set strPathActivePerl=D:\workspace\Perl64\bin
@set strPathOpenSSLDir=D:
@set strPathOpenSSL=%strPathOpenSSLDir%\download\openssl-1.0.1c
@set strPathTargetOpenSSLDir=D:\workspace\openssl-1.0.1c
@set bEnableStaticEngine=True
@rem True -> the shared library build will compile the engines into libeay32.dll
@rem ***************************************************************

@IF NOT "%strPathNetwideAssembler%" == "" set PATH=%strPathNetwideAssembler%;%PATH%
@IF NOT "%strPathStrawberryPerl%" == "" set PATH=%strPathStrawberryPerl%\c\bin;

%strPathStrawberryPerl%\perl\site\bin;%strPathStrawberryPerl%\perl\bin;%PATH%
@IF NOT "%strPathActivePerl%" == "" set PATH=%strPathActivePerl%;%PATH%
@set strEnableStaticEngine=
@IF "%bEnableStaticEngine%"=="True" set strEnableStaticEngine=enable-static-engine

@rem START

@rem Create OpenSSL installation dir
@rmdir /S /Q %strPathTargetOpenSSLDir%
@mkdir %strPathTargetOpenSSLDir%
@echo OpenSSL will be installed into %strPathTargetOpenSSLDir%

@%strPathOpenSSLDir%
@cd %strPathOpenSSL%

@IF NOT "%strPathNetwideAssembler%" == "" call :ConfigureMakefileWithASM
@IF "%strPathNetwideAssembler%" == ""     call :ConfigureMakefileWithoutASM

@if "%VS100COMNTOOLS%" == "" goto ErrorNoVS2010
@call "%VS100COMNTOOLS%vsvars32.bat"

@cls
@echo ***************************************************************
@echo Compile OpenSSL
@echo ***************************************************************
@pause
@nmake -f ms\ntdll.mak
@echo ***************************************************************
@echo Compile OpenSSL DONE
@echo ***************************************************************
@echo .
@echo ***************************************************************
@echo Test OpenSSL
@echo ***************************************************************
@pause
@nmake -f ms\ntdll.mak test
@echo ***************************************************************
@echo Test OpenSSL DONE
@echo ***************************************************************
@echo .
@echo ***************************************************************
@echo install OpenSSL to the specified location (%strPathTargetOpenSSLDir%)
@echo ***************************************************************
@pause
@nmake -f ms\ntdll.mak install
@echo ***************************************************************
@echo install OpenSSL to %strPathTargetOpenSSLDir% DONE
@echo ***************************************************************
@pause

goto EOF



:ConfigureMakefileWithoutASM
@rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@rem Configure with platform VC-WIN32 but without Assembler and Build Makefiles
@rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@echo ***************************************************************
@echo Configure with platform VC-WIN32 but without Assembler
@echo ***************************************************************
@perl Configure VC-WIN32 no-asm %strEnableStaticEngine% --prefix=%strPathTargetOpenSSLDir%
@echo ***************************************************************
@echo Configure with platform VC-WIN32 but without Assembler. DONE
@echo ***************************************************************
@echo .
@echo ***************************************************************
@echo Build the Makefiles
@echo ***************************************************************
@call ms\do_ms.bat
@echo ***************************************************************
@echo Build the Makefiles DONE
@echo ***************************************************************
@pause
@cls
goto EOF

:ConfigureMakefileWithASM
@rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@rem Configure with platform VC-WIN32 and with Assembler, and Build Makefiles
@rem ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@echo ***************************************************************
@echo Configure with platform VC-WIN32 and Assembler
@echo ***************************************************************
perl Configure VC-WIN32 %strEnableStaticEngine% --prefix=%strPathTargetOpenSSLDir%
@echo ***************************************************************
@echo Configure with platform VC-WIN32 and Assembler. DONE
@echo ***************************************************************
@echo .
@echo ***************************************************************
@echo Build the Makefiles and optionally the assembly language files:
@echo ***************************************************************
@call ms\do_nasm.bat
@echo ***************************************************************
@echo Build the Makefiles DONE
@echo ***************************************************************
@pause
@cls
goto EOF

rem Errors
rem ***********************************************************
:ErrorNoVS2010
echo Visual Studio 2010 is not installed on your computer. Please install it and run this file again.
goto EOF

:EOF

LNK1181: cannot open input file ‘rpcndr.lib’

Mở đám code cũ COM ra, convert xong build bị cái lỗi này.

Note lại
Vào Linker bỏ rpcndr.lib tất nhiên vẫn giữ lại rpcrt4.lib
http://henbo.wordpress.com/2006/11/22/lnk1181-cannot-open-input-file-rpcndr-lib/

Compiling gcc on Mac OSX


Link hướng dẫn ở đây

Tạo link cho g++, gcc, cpp
cd /usr/bin
sudo ln -s /usr/local/g++ g++-4.2

Thêm -m32 hay -m64 vào export, không sẽ bị lỗi
checking for __gmpz_init in -lgmp... no
configure: error: libgmp not found or uses a different ABI.
Please read the INSTALL file -- see "In case of problem".

Thay export CXX như sau:
export CXX=/usr/bin/g++-4.2
chuyển thành
export CXX="/usr/bin/g++-4.2 -m32"

Nhanh hơn thì dùng macports
sudo port install gmp
sudo port install mpfr
sudo port install mpc

macports install vào /opt/local

2012-04-28

Một bản "cực kỳ" nháp syncing file (file storage)


Theo cách liệt kê thì tất cả các trường hợp có thể conflict:

Đối với file
Local
Remote
Notes
Created
Created
Type 1
Created
Modified
Hạn chế *
Created
Renamed
Hạn chế *
Created
Deleted
Hạn chế *
Created
Deleted container folder
Có thể không coi là conflict **
Modified
Created
logically
Modified
Modified
Type 2
Modified
Renamed
Type 2
Modified
Deleted
Type 3
Modified
Deleted container folder
Type 4
Renamed
Created
logically
Renamed
Modified
Type 2
Renamed
Renamed
Type 2
Renamed
Deleted
Type 3
Renamed
Deleted container folder
Type 4
Deleted
Created
logically
Deleted
Modified
Type 3
Deleted
Renamed
Type 3
Deleted
Deleted
Type 5
Deleted
Deleted container folder
Type 6

* hạn chế là trường hợp ít khả năng xảy ra, ví dụ Created – Modified là ít khả năng xảy ra, khi local tạo file mà không biết rằng remote đã tạo file, và không biết cả việc thay đổi nội dung file sau đó.
** trường hợp Created file vs Deleted parent folderCreated folder vs Deleted parent folder có thể không coi là conflict vì chúng ta sẽ phải luôn giữ các file mới tạo tại remote dù đã bị xóa tại local vì vấn đề an toàn (do local không biết nội dung file/folder mới tạo).
Notes: trường hợp loại bỏ là liệt kê theo logic và không tính là conflict à dạng Deleted vs CreatedModified (Renamed) vs Created à sẽ phải rơi vào trường hợp Created vs Created và phải giải quyết trước khi có thể xảy ra, implementation sẽ không để xảy ra những trường hợp này.

Đối với folder tương tự nhưng không có modified, chỉ còn trường hợp renamed
Local
Remote
Notes
Created
Created
Type 7
Created
Renamed
Hạn chế *
Created
Deleted
Hạn chế *
Created
Deleted container folder
Có thể không coi là conflict
Renamed
Created
logically
Renamed
Renamed
Type 8
Renamed
Deleted
Type 9
Renamed
Deleted container folder
Type 10
Deleted
Created
logically
Deleted
Renamed
Type 9
Deleted
Deleted
Type 11
Deleted
Deleted container folder
Type 12


Gom lại có tất cả 7 trường hợp conflict (cả case conflict)


1.       Created  file – Created file conflict

Tạo file khi không biết thông tin về file đã được tạo

2.       Modified file – Modified file conflict

Thay đổi nội dung file (kể cả đổi tên) khi không biết file đã bị thay đổi

3.       Modified file – Deleted file conflict

Thay đổi nội dung file (kể cả đổi tên) khi không biết chính file đó bị xóa

4.       Modified file – Deleted parent folder conflict

Thay đổi nội dung file (kể cả đổi tên) khi không biết folder chứa file (parent hoặc ancestor đã bị xóa)

5.       Deleted file – Deleted file conflict

Xóa file khi không biết chính file đã bị xóa

6.       Deleted file – Deleted parent folder conflict

Xóa file khi không biết folder chứa file (parent hoặc ancestor đã bị xóa)

7.       Case conflict

Trên Mac và Windows không phân biệt hoa thường trong khi trên Linux có phân biệt à case conflict giải quyết à force rename?
Dựa trên các trường hợp có thể detect conflict phân thành theo file và theo folder
DetectFileConflict(…) kiểm tra conflict từ 1 đến 6
DetectFolderConflict(…) kiểm tra conflict đối với folder

Phát hiện conflict

ü  Khi download notice changes từ server, pause sync, kiểm tra giữa 2 file list để xác định conflict về file với Type từ 1 đến 6.
ü  Khi thực hiện sync up lên server à phát hiện conflict do thực hiện thất bại và theo error code trả về.
Xét với từng loại
ü  Type 1: xác định cùng file name nhưng khác file size, modified date à conflict
ü  Type 2 & Type 3: Modified file vs Modified file và Modified file vs Deleted file
ü  Type 4: là conflict

ü  Type 5 & 6: có thể không coi là conflict

Startup syncing

Download server snapshot

Thực hiện download snapshot file và folder list trên server insert vào table snapshot tại client. Table snapshot giữ thông tin mới nhất về file và folder, chỉ download 1 lần khi start app. Table snapshot cấu trúc giống table signstore.
Table signstore dung để keep track thay đổi khi app running, mọi thay đổi nếu đã sync với server sẽ được update vào signstore.

Có thể dùng thuật toán Eugene Myers?

Tìm danh sách file chỉ có tại local và danh sách file chỉ có tại server?

Coi thông tin trong stored là chuẩn à tính diff của local files và server files để có thông tin thay đổi của local và server đối với stored.

Tính diff giữa local và stored à local deleted files, no changes và local added files.
Tính diff giữa server và stored à server deleted files, no changes và server added files.

Xử lý các trường hợp xảy ra như sau:



Giải thích:
1 + 4: local added
2 + 5: local both
3 + 6: local deleted
4 + 7: server added
5 + 6: server both
2 + 3: server deleted

1.      Local added (1 + 4)  và server added (4 + 7)

Local add bắt buộc sync up, server added bắt buộc sync down. Local va server cùng added à conflict.
1: created sync up
7: created sync down
4: conflict

2.      Local added (1 + 4) và server both (5 + 6) à không intersect

3.      Local added (1 + 4)  và server deleted (2 + 3) à không intersect

4.      Local both (2 + 5)  và server added (4 + 7) à không intersect

5.      Local both (2 + 5)  và server both (5 + 6) à không chọn vì cả 2 list cùng có nhiều items

Nếu có cả server và local kiểm tra modified cho phần 5: có cả tại local, stored và server
5: check modified

6.      Local both (2 + 5)  và server deleted (2 + 3)

2: deleted sync down
3: delete khỏi stored (có trong stored nhưng không có tại local và server)
5: check modified à làm cho case 5

7.      Local deleted (3 + 6)  và server added (4 + 7) à không intersect

8.      Local deleted (3 + 6)  và server both (5 + 6) à server both nhiều items

6: deleted sync up

9.      Local deleted (3 + 6)  và server deleted (2 + 3)

2: deleted sync down
3: không cần sync deleted à xóa khỏi stored
6: deleted sync up à làm cho case 8