본문 바로가기

카테고리 없음

Keystore SoftKeymaster USER_PKEY 포맷 분석

<초기 헤더 정보>

- [0-3] : 메타정보(version, type, flags, info)

- [4-19] : AES 복호화를 위한 IV값 (128bit)

- [20-35] : MD5 Hash 값

- [36-39] : Blob의 길이(Integer 타입)

- [40~  ] : Key blob (SoftwareKeyblob, Qcom blob 등등)

------------------------------------------------------------------------------

<SoftwareKeyblob 구조>

[00-03] MAGIC NUM : PK#8

[04-07] KEY TYPE : EVP_PKEY_EC, EVP_PKEY_DSA, EVP_PKEY_RSA

[08-11] Public key length(RSA)  // 항상 0, 이건 대체 왜 넣는지 모르겠음

[12-15] Private key length(RSA)

[16~  ] Private key data


소스에서 확인 (헤더(4byte) + 키타입(4byte) + 공개키길이(4byte) + 개인키길이(4byte) + 개인키)

* https://android.googlesource.com/platform/system/security/+/master/softkeymaster/keymaster_openssl.cpp




<16 byte 이후로는 ASN.1 구조로 구성되어 있음>

308204A4

020100

02820101

00B1CF6BDC79B~~~



[00-00] 0x30 : 00110000 => Class : 00, P/C : 1, 10000 : 16(SEQUENCE)

Octet 1Octet 2 onwards
8765432187654321
Tag classP/CTag number (0–30)N/A
31MoreTag number

[01-03] 0x820276

0x82 : 10000010 =>  Form : 1, Number : 0000010 

0x0276 : Length : 1166

FormBits
87654321
Definite, short0Length (0–127)
Indefinite10
Definite, long1Number of following octets (1–126)
Reserved1127


[04-06] 0x020100

0x02 : INTEGER

0x01 : Length - 1 byte

0x00 : Value - 0

Types, universal class
NameValue
encodings
Tag number
DecimalHexadecimal
End-of-Content (EOC)Primitive00
BOOLEANPrimitive11
INTEGERPrimitive22
BIT STRINGBoth33
OCTET STRINGBoth44
NULLPrimitive55
OBJECT IDENTIFIERPrimitive66
Object DescriptorBoth77
EXTERNALConstructed88
REAL (float)Primitive99
ENUMERATEDPrimitive10A
EMBEDDED PDVConstructed11B
UTF8StringBoth12C
RELATIVE-OIDPrimitive13D
Reserved14E
Reserved15F
SEQUENCE and SEQUENCE OFConstructed1610
SET and SET OFConstructed1711
NumericStringBoth1812
PrintableStringBoth1913
T61StringBoth2014
VideotexStringBoth2115
IA5StringBoth2216
UTCTimeBoth2317
GeneralizedTimeBoth2418
GraphicStringBoth2519
VisibleStringBoth261A
GeneralStringBoth271B
UniversalStringBoth281C
CHARACTER STRINGBoth291D
BMPStringBoth301E

[07-10] 0x02820101

0x02 : INTEGER

0x82 : Length - 2 byte

0x0101 : Value - 257


[11-267] : Private Key

00B1CF6BDC79B~~~


-----------------------------------------------------------------------------


openssl 을 이용하여서 RSA private key 의 ASN.1 포맷을 출력하여 분석한 예제는 다음과 같습니다.

 

# openssl.exe asn1parse -in key.pem -inform PEM
    0:d=0  hl=4 l= 603 cons: SEQUENCE
    4:d=1  hl=2 l=   1 prim: INTEGER           :00                                                                                 <- Version
    7:d=1  hl=3 l= 129 prim: INTEGER           :C6ACA7101D8D0E8D08C5AAA3947E2DE0                        <- Modulus
C708DA88017AD7276372B7745C0CB9ACD2A22E45438C472DBC81F62AAEE0521EE6DC283768D9C20C
C523B52478AF2B2ED9BCC7717BB25560E63FBA603A1C0E0351367CECFE45DB67EB50014031B4D652
4C95EA9B71819C175F17FCCAAB8DCE1B1F7D7BD24C211E210E1D5C2D91066163
  139:d=1  hl=2 l=   3 prim: INTEGER           :010001                                                                         <- Public Exponent
  144:d=1  hl=3 l= 128 prim: INTEGER           :2BACB2C1292DE41FFEE8AD3D6E9C5A6B                       <- Private Exponent
8A7598CCC33E84E3C4C9E8286D3FE2E56A1C7AFD80FEEA7AD470E173BAA3D789B1887E52C7FAEEDC
ADCDCEB968CA613C74EE6135AFA7EEAD72362A6DE12B2E2AD70208CFBDC2C8E87B472E93A725ECF0
027341BB873F0A14AA0DA5625373E529927E54E510D1E7A436CB68D1EA6AC781
  275:d=1  hl=2 l=  65 prim: INTEGER           :F3CEAEA8E9B862D9C19845E700EDC445                         <- Prime 1
0F9FF33A6274FFE527F5F6A4120E736BFC98CA4CD39AB3A9D418EFBBC8B9C995886E739CE66B09E8
6987474E6F262A41
  342:d=1  hl=2 l=  65 prim: INTEGER           :D09C292A5B2EE02EF4F7F92C33877724                          <- Prime 2
9A742865DA8388344F1CAB5EC58CD53121A462451C564BFE60781F45A2BB21439576118336A42D61
5821E0D1ECF6FAA3
  409:d=1  hl=2 l=  64 prim: INTEGER           :72949FE12276557EA14D3F0CE192DCBA                         <- Exponent 1
D82D03D25C933FF02267A61255AA14F49B31F36147D15CBE63BF946B5C5A7432A27BC639E6D04747
C2D0A0E26BBA3401
  475:d=1  hl=2 l=  64 prim: INTEGER           :794F8684D217B558B92E06E194699F78                          <- Exponent 2
52B55FD4B338B019333449D7C7EBB902643CF2C49BD0F74A4B112FA65016A0266C2107D8150B6E34
608AFAF138EAFB1F
  541:d=1  hl=2 l=  64 prim: INTEGER           :3D8D967868A0FA34D0622D33981C97C9                        <- Coefficient
493CD82CF234F587D398CF005BA7E9970D39AC3CFA6B161192A996377CD6AD7F2D28AFD5085114C1
15D34705A84447EE


<PKCS#1 개인키 구조>

-----BEGIN RSA PRIVATE KEY-----
RSAPrivateKey ::= SEQUENCE {
  version           Version,
  modulus           INTEGER,  -- n
  publicExponent    INTEGER,  -- e
  privateExponent   INTEGER,  -- d
  prime1            INTEGER,  -- p
  prime2            INTEGER,  -- q
  exponent1         INTEGER,  -- d mod (p-1)
  exponent2         INTEGER,  -- d mod (q-1)
  coefficient       INTEGER,  -- (inverse of q) mod p
  otherPrimeInfos   OtherPrimeInfos OPTIONAL
}
-----END RSA PRIVATE KEY-----

<참고자료>

https://etherhack.co.uk/asymmetric/docs/rsa_key_breakdown.html