<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Solobear</title>
    <description>Notes as blogs</description>
    <link>https://solobearchn.github.io//</link>
    <atom:link href="https://solobearchn.github.io//feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sun, 06 Jun 2021 20:17:42 +0000</pubDate>
    <lastBuildDate>Sun, 06 Jun 2021 20:17:42 +0000</lastBuildDate>
    <generator>Jekyll v3.9.0</generator>
    
      <item>
        <title>椭圆曲线密码学在OpenSSL中的实现</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#有限域上的椭圆曲线&quot; id=&quot;markdown-toc-有限域上的椭圆曲线&quot;&gt;有限域上的椭圆曲线&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#openssl中的椭圆曲线表示&quot; id=&quot;markdown-toc-openssl中的椭圆曲线表示&quot;&gt;OpenSSL中的椭圆曲线表示&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#椭圆曲线的加密算法&quot; id=&quot;markdown-toc-椭圆曲线的加密算法&quot;&gt;椭圆曲线的加密算法&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ecdh-椭圆曲线diffie-hellman密钥交换&quot; id=&quot;markdown-toc-ecdh-椭圆曲线diffie-hellman密钥交换&quot;&gt;ECDH： 椭圆曲线Diffie-Hellman密钥交换&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#针对d-h交换的中间人攻击问题&quot; id=&quot;markdown-toc-针对d-h交换的中间人攻击问题&quot;&gt;针对D-H交换的中间人攻击问题&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#ecdsa-椭圆曲线签名算法&quot; id=&quot;markdown-toc-ecdsa-椭圆曲线签名算法&quot;&gt;ECDSA: 椭圆曲线签名算法&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;有限域上的椭圆曲线&quot;&gt;有限域上的椭圆曲线&lt;/h2&gt;
&lt;p&gt;这里略去有限域、射影几何等数学背景介绍。先给出实数域空间上椭圆曲线的一般形式：&lt;/p&gt;

&lt;p&gt;y^2&lt;em&gt;z + a_1&lt;/em&gt;x&lt;em&gt;y&lt;/em&gt;z + a_3&lt;em&gt;y&lt;/em&gt;z^2 = x^3 + a_2&lt;em&gt;x^2&lt;/em&gt;z + a_4&lt;em&gt;x&lt;/em&gt;z^2 + a_6*z^3
以上式子中，x,y,z均为变元。而令z=1, 则可以得到平面上的椭圆曲线Ep(x,y)。&lt;/p&gt;

&lt;p&gt;对平面上椭圆曲线上的点&lt;strong&gt;&lt;em&gt;P, Q, R&lt;/em&gt;&lt;/strong&gt;，以及关于x轴对称的点&lt;strong&gt;&lt;em&gt;P’, Q’, R’&lt;/em&gt;&lt;/strong&gt;, 定义点的加法:&lt;/p&gt;

&lt;p&gt;Add : P+Q = R’
&lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/a/ae/ECClines-2.svg/680px-ECClines-2.svg.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;从几何上看点的加法：P点和Q点之和R’，是P、Q两点连线与曲线的交点R关于x轴的对称点。当P、Q点重合时，二者的连线即点的切线。此外，图中点&lt;strong&gt;&lt;em&gt;0&lt;/em&gt;&lt;/strong&gt;即原点/无穷远点。&lt;/p&gt;

&lt;p&gt;加法定义完毕后，乘法和逆运算呼之欲出：
Mul : P + P + … P = nP
Inverse : P + (-P) =  O 
这样，椭圆曲线上点的加法、乘法、求逆等运算，就等同于平面上求直线与曲线交点、曲线上点的切线等初等解析几何问题，计算过程是简单的。&lt;/p&gt;

&lt;p&gt;密码学需要使用离散点，因此要将椭圆曲线上的点限定在一个有限域内，即参数(a,b,c)和变元(x,y)取值在有限域中。椭圆曲线主要使用的有限域有GF(p)素数域，以及GF(2^m)二元域。其中，素数域的椭圆曲线形式如下：&lt;/p&gt;

&lt;p&gt;y^2 = left (x^3 + ax + b)( mod p)&lt;/p&gt;

&lt;p&gt;4(a^3) + 27(b^2) != 0 ( mod p)
通常p是一个大素数。不同于实数域上点的运算有直观的几何形象，有限域中点P(x, y)的运算在等号两侧都需要取模（mod p）。此外一个显而易见的性质是：由于曲线的各个参数定义在有限域上，符合Abel群，因此上述乘法的因子也满足Abel群中的交换律和结合律：
n_1&lt;em&gt;(n_2&lt;/em&gt;P) = n_2&lt;em&gt;(n_1&lt;/em&gt;P) = (n_1&lt;em&gt;n_2)P
(n_1&lt;/em&gt;n_2)&lt;em&gt;n_3&lt;/em&gt;P = n_1&lt;em&gt;(n_2&lt;/em&gt;n_3)*P&lt;/p&gt;

&lt;p&gt;椭圆曲线之所以能用于公钥密码体系，是基于所谓&lt;strong&gt;椭圆曲线难题&lt;/strong&gt;:
Q = k*P&lt;/p&gt;
&lt;blockquote&gt;
  &lt;ul&gt;
    &lt;li&gt;已知点&lt;strong&gt;&lt;em&gt;P&lt;/em&gt;&lt;/strong&gt;，和因子&lt;em&gt;k&lt;/em&gt;，求点的乘积&lt;strong&gt;&lt;em&gt;Q&lt;/em&gt;&lt;/strong&gt;，在运算上是简单的（无非是求交点、取模运算等）；&lt;/li&gt;
    &lt;li&gt;反之，若已知点&lt;strong&gt;&lt;em&gt;P&lt;/em&gt;&lt;/strong&gt;,&lt;strong&gt;&lt;em&gt;Q&lt;/em&gt;&lt;/strong&gt;，求乘法因子k，在计算上是困难的，目前还没有多项式时间的解法。&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;这种&lt;strong&gt;单向门陷函数(Trapdoor function)&lt;/strong&gt;特性，与RSA一样，可以用于公钥密码体系。可以将{P, kP}公开作为公钥，乘法因子k作为私钥。&lt;/p&gt;

&lt;p&gt;由于参数和变元都定义在有限域中，在运算过程中都对大素数p取模，因此有限域中椭圆曲线上点的个数是有限的。对于点&lt;strong&gt;&lt;em&gt;P&lt;/em&gt;&lt;/strong&gt;进行乘法运算，乘数因子从0至p：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;P_0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;P_1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;P_2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;P_i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;P_p&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;P&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;在上述运算中产生了p+1个点，如果某个因子n满足：
P_n = n*P = 0
这样自n+1起之后的点都会和之前的重合。
P_n+1 = 0 + P =P, P_n+i = 0 + P_i =P_i
近世代数已经证明，对于有限域椭圆曲线，这样的n必然是存在的。使n*P = 0的最小的n，称之为椭圆曲线的&lt;strong&gt;阶（Order）&lt;/strong&gt;，相应的点P称之为&lt;strong&gt;基点(Genrator)&lt;/strong&gt;。这样&lt;strong&gt;Order&lt;/strong&gt;可以视作为曲线上对点P运算的&lt;strong&gt;周期&lt;/strong&gt;。此外，曲线上点的个数与基点阶的比值称之为&lt;strong&gt;co-factor&lt;/strong&gt;。&lt;/p&gt;

&lt;h2 id=&quot;openssl中的椭圆曲线表示&quot;&gt;OpenSSL中的椭圆曲线表示&lt;/h2&gt;

&lt;p&gt;再来看看素数域椭圆曲线——以&lt;strong&gt;secp256k1&lt;/strong&gt;为例，描述这个曲线的参数包括：&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;p : 大素数&lt;/li&gt;
  &lt;li&gt;a : 曲线方程系数&lt;/li&gt;
  &lt;li&gt;b : 曲线方程系数&lt;/li&gt;
  &lt;li&gt;x : 基点Generator的x坐标&lt;/li&gt;
  &lt;li&gt;y : 基点Generator的y坐标&lt;/li&gt;
  &lt;li&gt;order : 曲线的阶Order&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;取值如下：&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/* no seed */
/* p */
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFC, 0x2F,
/* a */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* b */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
/* x */
0x79, 0xBE, 0x66, 0x7E, 0xF9, 0xDC, 0xBB, 0xAC, 0x55, 0xA0, 0x62, 0x95,
0xCE, 0x87, 0x0B, 0x07, 0x02, 0x9B, 0xFC, 0xDB, 0x2D, 0xCE, 0x28, 0xD9,
0x59, 0xF2, 0x81, 0x5B, 0x16, 0xF8, 0x17, 0x98,
/* y */
0x48, 0x3a, 0xda, 0x77, 0x26, 0xa3, 0xc4, 0x65, 0x5d, 0xa4, 0xfb, 0xfc,
0x0e, 0x11, 0x08, 0xa8, 0xfd, 0x17, 0xb4, 0x48, 0xa6, 0x85, 0x54, 0x19,
0x9c, 0x47, 0xd0, 0x8f, 0xfb, 0x10, 0xd4, 0xb8,
/* order */
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFE, 0xBA, 0xAE, 0xDC, 0xE6, 0xAF, 0x48, 0xA0, 0x3B,
0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x41
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;椭圆曲线密码学中所使用的秘钥结构体&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ec_key_st&lt;/code&gt;如下：包含公钥、私钥、有限域、点运算函数等&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ec_key_st&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EC_KEY_METHOD&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;meth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//计算函数&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ENGINE&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;engine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;EC_GROUP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//群&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;EC_POINT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pub_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//公钥&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;priv_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;//私钥&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enc_flag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;point_conversion_form_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conv_form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;CRYPTO_REF_COUNT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;references&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;CRYPTO_EX_DATA&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ex_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;CRYPTO_RWLOCK&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;其中，字段中有限域EC_GROUP结构体如下：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ec_group_st&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EC_METHOD&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;meth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;      &lt;span class=&quot;cm&quot;&gt;/* 椭圆曲线运算函数 */&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;EC_POINT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;generator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;        &lt;span class=&quot;cm&quot;&gt;/* optional */&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cofactor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;poly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;椭圆曲线的加密算法&quot;&gt;椭圆曲线的加密算法&lt;/h2&gt;
&lt;p&gt;从公钥密码学角度来看，基于椭圆曲线难题，私钥为本地生成的大数k，公钥则为点k*G。
在椭圆曲线密码学中的数据通常涉及&lt;strong&gt;字符串、大整数、点&lt;/strong&gt;三类，其中，字符串和大整数可以相互转换：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BN_bin2bn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ret&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//字符串转换为大整数&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BN_bn2bin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;//大整数转换为字符串&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;而大整数和通常可以映射为曲线上某个点的x坐标：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;//获取曲线上点point的x坐标(大整数)&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EC_POINT_get_affine_coordinates_GFp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EC_GROUP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                        &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EC_POINT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                        &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BN_CTX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;//大整数映射至曲线上点&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EC_POINT_set_affine_coordinates_GFp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EC_GROUP&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                        &lt;span class=&quot;n&quot;&gt;EC_POINT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                        &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BN_CTX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这里打算只举一个最简单的例子，说明基于椭圆曲线加密过程。Alice的私钥为k_A，公钥为k_A*G。&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Bob将明文m映射至椭圆曲线上一点M。（如何映射？字符串-&amp;gt;BigNum-&amp;gt;P点x坐标即可）&lt;/li&gt;
  &lt;li&gt;Bob随机选取整数r，计算出点R=r*G&lt;/li&gt;
  &lt;li&gt;Bob利用Alice的公钥加密消息发往Alice，消息密文结构为C=(M+r&lt;em&gt;k_A&lt;/em&gt;G)，同时将点&lt;em&gt;R&lt;/em&gt;也发送给Alice&lt;/li&gt;
  &lt;li&gt;Alice收到密文C后，根据公开的点R和自己的私钥k_A进行解密：
M’=C-k_A&lt;em&gt;R=M+r&lt;/em&gt;k_A&lt;em&gt;R-k_A&lt;/em&gt;r*G=M&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;ecdh-椭圆曲线diffie-hellman密钥交换&quot;&gt;ECDH： 椭圆曲线Diffie-Hellman密钥交换&lt;/h2&gt;

&lt;p&gt;所谓D-H交换，即两个用户之间安全地交换彼此的密钥，并在后续的传输中使用该密钥对信息加密。
这里先从实际的应用角度出发，讨论一下D-H交换在通信中的一个典型应用场景——&lt;strong&gt;密钥协商&lt;/strong&gt;：&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;由于非对称加解密通常运算量都很大，主要用于低频的密钥交换，所生成的共享密钥通常是长期使用的。比如通信双方先使用ECDH交换，&lt;strong&gt;协商出共享密钥PK&lt;/strong&gt;，这种协商密钥的过程只需要一次。然后采用&lt;strong&gt;PK&lt;/strong&gt;作为密码，使用运算更快的AES、CR4、DES等传统的对称加密算法，对每次数据报文进行加解密。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;回到D-H交换的具体过程中来。在椭圆曲线加密过程中，曲线参数是公开的，包括基点G和阶Order。对于用户A和B：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A选取一个随机数&lt;strong&gt;rA&lt;/strong&gt;作为私钥，&lt;strong&gt;rA*G&lt;/strong&gt;作为公钥。其中G即曲线的基点。由椭圆曲线难题可知，其他用户很难反求私钥&lt;strong&gt;rA&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;B同理，其私钥为&lt;strong&gt;rB&lt;/strong&gt;，公钥为&lt;strong&gt;rB*G&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;A、B分别向对方发送公钥。&lt;/li&gt;
  &lt;li&gt;A、B收到的对方公钥后，乘以自己的私钥，本地计算出双方的共享密钥对。此时A计算得出PKA=rA&lt;em&gt;(rB&lt;/em&gt;G);B计算得出PKB=rB&lt;em&gt;(rA&lt;/em&gt;G)&lt;/li&gt;
  &lt;li&gt;根据Abel乘法运算的结合律，当基点G相同时，PKA=PKB。D-H交换通过。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ECDH密钥交换过程如下图所示
&lt;img src=&quot;https://github.com/solobearchn/solobearchn.github.io/raw/master/pics/ecdh_1.jpg&quot; alt=&quot;image&quot; /&gt;
最终，A、B双方所生成的共享密钥PK包含彼此的私钥信息，成功交换了密钥信息。A、B之间采用PK作为密钥，使用对称加解密技术发送报文，进行加密传输。&lt;/p&gt;

&lt;p&gt;其实D-H交换算法过程很简短，就是私钥之积乘以基点G，来看看共享密钥计算函数的代码：&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;openssl/crypto/ec/Ecdh_ossl.c&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ecdh_simple_compute_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;size_t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;poutlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EC_POINT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pub_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EC_KEY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ecdh&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;        &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EC_POINT_mul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tmp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pub_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;priv_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ECerr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EC_F_ECDH_SIMPLE_COMPUTE_KEY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EC_R_POINT_ARITHMETIC_FAILURE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;        
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;参数说明:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;pout: 输出，即最后的共享密钥字符串&lt;/li&gt;
  &lt;li&gt;poutlen: 输出长度&lt;/li&gt;
  &lt;li&gt;pub_key: 收到的对端用户公钥&lt;/li&gt;
  &lt;li&gt;ecdh: 本方的密钥结构体，包含公钥和私钥、有限域等&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;针对d-h交换的中间人攻击问题&quot;&gt;针对D-H交换的中间人攻击问题&lt;/h2&gt;
&lt;p&gt;从私钥保护的目的来看，D-H密钥交换是安全的，整个交换过程中不会泄露用户的私钥。但是D-H交换也存在局限性，比如要求整个交换过程的信道可信，同时交换双方必须在线，以保证消息可达。&lt;/p&gt;

&lt;p&gt;D-H交换最大的局限性在于不能防范&lt;strong&gt;中间人攻击&lt;/strong&gt;，这里仍然可以举一个例子。在不可靠信道上，假设A、B之间存在一个中间人C，可以窃听到A、B之间的信道消息，尽管信道上传输的都是公钥，但C确实能成功欺骗A、B双方:
&lt;img src=&quot;https://github.com/solobearchn/solobearchn.github.io/raw/master/pics/ecdh_attack.jpg&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A、B在D-H交换过程发送的公钥信息被用户C窃听。C分别与A、B交换了私钥并生成共享密钥对PKC_A和PKC_B。这样C就可以与A、B分别进行通信了。A、B对C的窃听一无所知，以为彼此协商出了共享密钥PK=rA*rB*G。而实际上A与C、B与C都分别协商出了PKC_A与PKC_B。&lt;/p&gt;

&lt;p&gt;D-H交换最大的问题，就在于无法鉴别通信的参与方，发送者对信息接受者的身份一无所知。当信道不可靠时，公钥以广播形式在网络中传播，传输路径中的第三方节点可以很容易窃听乃至篡改原有消息。&lt;/p&gt;

&lt;p&gt;为克服中间人攻击缺陷，需要引入数字签名和公钥证书机制。&lt;/p&gt;

&lt;h2 id=&quot;ecdsa-椭圆曲线签名算法&quot;&gt;ECDSA: 椭圆曲线签名算法&lt;/h2&gt;

&lt;p&gt;公钥密码体系中的签名机制是：发送方使用&lt;strong&gt;私钥&lt;/strong&gt;对消息进行签名，接收方使用&lt;strong&gt;公钥&lt;/strong&gt;验证签名。ECDSA的签名机制比RSA略复杂。这里仍然以消息发送方Alice和接收方Bob为例。&lt;/p&gt;

&lt;p&gt;先来看看Alice这边的签名生成过程：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Alice的私钥为k_A，公钥为k_A*G&lt;/li&gt;
  &lt;li&gt;Alice随机选取一个整数k，计算与基点的乘积K=k*G&lt;/li&gt;
  &lt;li&gt;K的横坐标为K_x，取其相对于曲线阶order的模结果r，即r=K_x(mod order)&lt;/li&gt;
  &lt;li&gt;计算k基于曲线阶order的乘法逆元k^(-1)&lt;/li&gt;
  &lt;li&gt;Alice对消息m进行Hash得到摘要h&lt;/li&gt;
  &lt;li&gt;最终的签名为{r,s}，r在第2步已经给出；而Alice根据自己的私钥k_A，得到s如下
s = k^(-1)(h+k_A&lt;em&gt;r)(mod order) 
根据模的乘法逆元性质，上式左右各乘以k，可得：
s k= (h+k_A&lt;/em&gt;r）(mod order)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Bob收到消息m和签名{r,s}后，进行签名验证：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Bob计算得出s基于曲线阶order的乘法逆元s^(-1)&lt;/li&gt;
  &lt;li&gt;Bob对所得消息进行Hash，得到h’&lt;/li&gt;
  &lt;li&gt;计算出u_1=h’*s^(-1)&lt;/li&gt;
  &lt;li&gt;计算出u_2=r*s^(-1)&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;根据Alice的公钥(k_A&lt;em&gt;G)，得到点P= u_1&lt;/em&gt;G+u_2&lt;em&gt;k_A&lt;/em&gt;G，展开得：
P=h’s^(-1)G+r&lt;em&gt;s^(-1)k_A&lt;/em&gt;G=(h’+k_Ar)s^(-1)G(mod order)&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;当Bob收到的消息未被篡改时，h’=h，则有:
P=s^(-1)(h+k_A&lt;em&gt;r)G=s^(-1)(s&lt;/em&gt;k)G=k*G(mod order)&lt;/li&gt;
  &lt;li&gt;取点P的x坐标P_x与签名中的r=K_x(mod order)相比 ，相等则签名验证通过。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;ECDSA的过程略长，但仍然遵循公钥密码体系中的签名原理。Alice生成的签名中包括自己的私钥，Bob验证签名则只需使用签名和公钥即可解开签名内容，验明正身。&lt;/p&gt;

&lt;p&gt;OpenSSL中ECDSA的签名生成过程如下.&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;openssl/crypto/ec/ecdsa_ossl.c&lt;/p&gt;
  &lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ecdsa_sign_setup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EC_KEY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eckey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BN_CTX&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ctx_in&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;kinvp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rp&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dgst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;  &lt;/div&gt;
  &lt;p&gt;此函数根据消息摘要，生成了签名所需的k^(-1)和r。&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;进函数内看看，首先这里使用了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BN_generate_dsa_nonce&lt;/code&gt; 进行伪随机数k生成。&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;cm&quot;&gt;/* get random k */&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dgst&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;cm&quot;&gt;/* 使用dgst和private key，应用于伪随机数生成器PRNG */&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BN_generate_dsa_nonce&lt;/span&gt;
                    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EC_KEY_get0_private_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eckey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dgst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                     &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                    &lt;span class=&quot;n&quot;&gt;ECerr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EC_F_ECDSA_SIGN_SETUP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                             &lt;span class=&quot;n&quot;&gt;EC_R_RANDOM_NUMBER_GENERATION_FAILED&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                    &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;k^{-1}和r 的生成如下：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EC_POINT_mul&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tmp_point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ECerr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EC_F_ECDSA_SIGN_SETUP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ERR_R_EC_LIB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EC_METHOD_get_field_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EC_GROUP_method_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;NID_X9_62_prime_field&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EC_POINT_get_affine_coordinates_GFp&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tmp_point&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;n&quot;&gt;ECerr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EC_F_ECDSA_SIGN_SETUP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ERR_R_EC_LIB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;cm&quot;&gt;/* r = X%order */&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BN_nnmod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ECerr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EC_F_ECDSA_SIGN_SETUP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ERR_R_BN_LIB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BN_is_zero&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

    &lt;span class=&quot;cm&quot;&gt;/* compute the inverse of k */&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EC_GROUP_get_mont_data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BN_mod_sub&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ECerr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EC_F_ECDSA_SIGN_SETUP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ERR_R_BN_LIB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;BN_set_flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BN_FLG_CONSTTIME&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
       &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BN_mod_inverse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ctx&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;ECerr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EC_F_ECDSA_SIGN_SETUP&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ERR_R_BN_LIB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;最终，生成签名的接口为：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;ECDSA_SIG&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ossl_ecdsa_sign_sig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dgst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dgst_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                               &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in_kinv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;in_r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                               &lt;span class=&quot;n&quot;&gt;EC_KEY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eckey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;此函数使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ecdsa_sign_setup&lt;/code&gt; 中的k^(-1)和r 产生了最终的签名，结构体为：&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ECDSA_SIG_st&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;BIGNUM&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;验证签名的过程在函数&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ossl_ecdsa_verify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dgst&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dgst_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;unsigned&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sigbuf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sig_len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;EC_KEY&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;eckey&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;与之前分析的一致。值得一提的是对于验证中的点P= u_1G+u_2(k_AG)，需要判断是否为平面上的零点/无穷远点，若是则验证失败，不能进行下一步取横坐标的操作。&lt;/p&gt;
</description>
        <pubDate>Sun, 18 Mar 2018 09:30:00 +0000</pubDate>
        <link>https://solobearchn.github.io//2018/03/18/ECC/</link>
        <guid isPermaLink="true">https://solobearchn.github.io//2018/03/18/ECC/</guid>
        
        <category>ECDSA</category>
        
        <category>椭圆曲线</category>
        
        <category>密码学</category>
        
        <category>网络安全</category>
        
        
        <category>Blogs</category>
        
      </item>
    
      <item>
        <title>公钥密码学中的RSA算法分析</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#公钥密码学应用场景&quot; id=&quot;markdown-toc-公钥密码学应用场景&quot;&gt;公钥密码学应用场景&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rsa中的数论知识&quot; id=&quot;markdown-toc-rsa中的数论知识&quot;&gt;RSA中的数论知识&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rsa密钥生成&quot; id=&quot;markdown-toc-rsa密钥生成&quot;&gt;RSA密钥生成&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rsa加密过程&quot; id=&quot;markdown-toc-rsa加密过程&quot;&gt;RSA加密过程&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rsa签名过程&quot; id=&quot;markdown-toc-rsa签名过程&quot;&gt;RSA签名过程&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;公钥密码学应用场景&quot;&gt;公钥密码学应用场景&lt;/h2&gt;
&lt;p&gt;公钥密码体系又称为非对称密码，即加密和解密算法使用不同的密钥：一个是公钥Pu、一个是私钥Pr。公钥密码学的应用场景有：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;加密和解密&lt;/strong&gt;
发送方使用接收方的公钥加密，接收方使用对应的私钥解密。这样只有私钥的拥有者能解密，这样就保证了消息的私密性&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;数字签名&lt;/strong&gt;
发送方使用私钥做签名。消息接收者根据对应的公钥进行签名验证。由于只有消息发送方拥有私钥、生成签名，因此签名验证成功后表明此消息确实由发送方发送过（注意，是发送过，而非消息来源是发送方）。数字签名解决了&lt;strong&gt;发送方否认问题&lt;/strong&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;密钥协商&lt;/strong&gt;
双方能够安全地交换彼此共享的秘密信息，作为后续对称加密所使用的密钥。常用的有基于离散对数的Diffie-Hellman交换和基于椭圆曲线算法的ECDH算法，这里暂时不涉及。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;rsa中的数论知识&quot;&gt;RSA中的数论知识&lt;/h2&gt;

&lt;p&gt;RSA依赖数论中的一些知识：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;乘法逆元&lt;/strong&gt;
对于互素的两个正整数a, n，根据&lt;strong&gt;扩展欧几里得算法&lt;/strong&gt;，总存在正整数b，使得
\(a*b=1(mod n)\)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;欧拉定理&lt;/strong&gt;
对于正整数a, n，phi(n)为n的欧拉函数（小于n且与之互素的所有正整数的个数），则有：
a^{phi(n)+1}=a(mod n)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;离散对数难题&lt;/strong&gt;：
对于正整数a, n, x, y，已知a, n, x，正向求幂y=a^x(mod n)是容易的，反之，求指数x在计算上是困难的；这个性质在RSA签名中得到应用。&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;大素数分解难题&lt;/strong&gt;，已知两个大素数p,q，求其乘积n=p*q是容易的，而已知n，求其素因子p,q是困难的。&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;其中计算乘法逆元时的欧几里得算法在计算上是容易的，通常将a对n取模的逆元写成b=a^(-1)(mod n).&lt;/p&gt;

&lt;p&gt;此外，根据欧拉定理，还可进一步得到推论：对于正整数a, n, k，phi(n)为n的欧拉函数，有：a^(k*phi(n)+1)=a(mod n)，这一推论可用于RSA加密和解密过程。&lt;/p&gt;

&lt;h2 id=&quot;rsa密钥生成&quot;&gt;RSA密钥生成&lt;/h2&gt;
&lt;p&gt;RSA算法是基于大素数分解难题，即已知两个大素数p,q，求其乘积n=p*q是容易的，而已知n求其素因子p,q是困难的。基于此，RSA算法按如下过程生成私钥和公钥：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;选定两个大素数p,q，这两个素数是私密的；&lt;/li&gt;
  &lt;li&gt;n = p*q，这个乘积n可以公开；&lt;/li&gt;
  &lt;li&gt;随机选定e满足gcd(phi(n), e) = 1 ，其中phi(n)=(p-1)*(q-1), 为n的欧拉函数。e的选取是公开的；&lt;/li&gt;
  &lt;li&gt;根据欧几里得算法求d = e^(-1) (mod phi(n))，d私密&lt;/li&gt;
  &lt;li&gt;最后一步，销毁大素数p,q&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;最终生成公钥Pu={e, n}，私钥Pr={d, n}。&lt;/p&gt;

&lt;h2 id=&quot;rsa加密过程&quot;&gt;RSA加密过程&lt;/h2&gt;

&lt;p&gt;加密和解密的场景为：对于明文M，Bob使用Alice的&lt;strong&gt;公钥加密&lt;/strong&gt;消息发往Alice；Alice根据自己的&lt;strong&gt;私钥解密&lt;/strong&gt;消息，得到明文。RSA中的过程如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;对于明文M，Bob根据Alice的公钥Pu={e, n}进行加密运算，得到密文C=M^d(\bmod n)&lt;/li&gt;
  &lt;li&gt;Alice收到密文后，根据自己的私钥Pr={d, n}进行解密运算，C’=C^d=M^{d*e}(\bmod n)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;由于d = e^(-1) (mod phi(n))，则必有d&lt;em&gt;e=k&lt;/em&gt;phi(n)+1，k为正整数。则根据之前的推论，有：
C’=M^(de)(mod n)=M^(k*phi(n)+1)=M(mod n)&lt;/p&gt;

&lt;p&gt;至此密文被解密为原来的明文M。值得一提的是，实际中的明文通常为字符串，在应用RSA算法时，需要将字符串映射为整数。&lt;/p&gt;

&lt;p&gt;攻击者想得到明文，则必须得到私钥d，这等价于求得phi(n)，无异于想分解n为两个大素数这一大素数分解难题。&lt;/p&gt;

&lt;h2 id=&quot;rsa签名过程&quot;&gt;RSA签名过程&lt;/h2&gt;

&lt;p&gt;签名的场景为发送方Alice使用私钥做签名，附带着消息原文M发送。所有的接收者都可以使用Alice公开的公钥对签名进行验证。&lt;/p&gt;

&lt;p&gt;Alice生成签名的过程如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Alice对消息原文M进行Hash运算，得到消息的摘要H=Hash(M)&lt;/li&gt;
  &lt;li&gt;Alice根据自己的私钥d对消息摘要进行签名S=H^d(mod n)&lt;/li&gt;
  &lt;li&gt;
    &lt;table&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td&gt;Alice将签名附在消息原文后，一起发送出去，消息为(M&lt;/td&gt;
          &lt;td&gt; &lt;/td&gt;
          &lt;td&gt;S)&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;接受者Bob收到消息后的签名验证过程如下：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Bob收到消息后根据Alice的公钥e对签名S进行验证，验证结果为H’=S^e(mod n)&lt;/li&gt;
  &lt;li&gt;Bob对接收的消息原文M进行Hash计算，得到消息的摘要H=Hash(M)&lt;/li&gt;
  &lt;li&gt;根据欧拉定理的推论，自然有H’=H，此时可以认定签名来自Alice，且消息M未经篡改&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;上面的签名过程，事实上解决了&lt;strong&gt;发送方否认问题&lt;/strong&gt;和&lt;strong&gt;消息完整性&lt;/strong&gt;两个问题：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;消息接受方Bob用Alice分发的公钥确实能够解开签名，Alice无法否认这条消息，这就保证了消息中所声称的身份（Alice）是真实可靠的。&lt;/li&gt;
  &lt;li&gt;Hash函数的引入，可以保证消息完整、不被篡改，通常使用&lt;strong&gt;HMAC&lt;/strong&gt;——基于MAC消息验证码的Hash函数&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;签名发送过程中S=H^d(mod n)和H是公开的，攻击者如果想获取私钥d，则必须解开离散对数难题，这在数学上是困难的。&lt;/p&gt;

&lt;p&gt;RSA签名仍然不能解决发送方身份可信问题，因为公钥的来源是没有保障的，这就需要引入CA证书机制。因为和RSA算法本身无关，这里就不再赘述。&lt;/p&gt;
</description>
        <pubDate>Sun, 18 Mar 2018 00:30:00 +0000</pubDate>
        <link>https://solobearchn.github.io//2018/03/18/RSA/</link>
        <guid isPermaLink="true">https://solobearchn.github.io//2018/03/18/RSA/</guid>
        
        <category>RSA</category>
        
        <category>密码学</category>
        
        <category>网络安全</category>
        
        
        <category>Blogs</category>
        
      </item>
    
      <item>
        <title>新的工作，新的一年</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#新的工作新的一年&quot; id=&quot;markdown-toc-新的工作新的一年&quot;&gt;新的工作，新的一年&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;新的工作新的一年&quot;&gt;新的工作，新的一年&lt;/h2&gt;

&lt;p&gt;到北京已经一个月有余了。初入公司被视为应届生，没有直接安排项目，而是参加了名目繁多的培训。和我一样，很多新同事也是半路出家，因此这些培训是有益的，不过应该浓缩一下。现在为止，大概弄清楚了部门和组里的工作内容，一部分是通信设备软件的特性开发，做链路层的通信协议，Linux C；另一部分是设备的云端管理平台，后台用Node.js实现。不是大数据云计算人工智能，也不是互联网+，和我此前涉足的Android、C++也没有关系。零基础的要求、从零开始的起点，可不就是应届生么？&lt;/p&gt;

&lt;p&gt;我很宅很抠门，因为穷、收入低，出远门要花钱，车票机票我都心疼；也因为一直对现状不满，没有心思游山玩水——工作以来我没有痛快地玩过，公司部门组织的活动我内心其实是抗拒的——我以为，我不该在这里。但是过去的一年我跑遍了北上广深杭，行程至少也上万公里。几个月没工作，后来又换工作、换城市、搬家。这两天交完一季的房租，发现自己两手空空，过去的一年，我压根没攒下几个钱。马上就26岁了，怎能不惶恐？&lt;/p&gt;

&lt;p&gt;工作和生活已经安顿下来了，告别了朝不知夕所在的2016年，接下来要怎么做，我得好好想想。毕竟下一次，再，也，不，能，打着应届生的名号，去找一个零基础的初级岗位了，我不仅要脸，还要活命。&lt;/p&gt;
</description>
        <pubDate>Sun, 08 Jan 2017 05:10:00 +0000</pubDate>
        <link>https://solobearchn.github.io//2017/01/08/%E6%96%B0%E7%9A%84%E4%B8%80%E5%B9%B4/</link>
        <guid isPermaLink="true">https://solobearchn.github.io//2017/01/08/%E6%96%B0%E7%9A%84%E4%B8%80%E5%B9%B4/</guid>
        
        <category>murmurs</category>
        
        
        <category>Blogs</category>
        
      </item>
    
      <item>
        <title>再见，深圳！</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#再见深圳&quot; id=&quot;markdown-toc-再见深圳&quot;&gt;再见，深圳！&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;再见深圳&quot;&gt;再见，深圳！&lt;/h2&gt;

&lt;p&gt;距离上次更新已经过去了一个半月。一个半月前，我初窥Android源码而入门不得，彼时已裸辞两月有余，眼看入冬前找工作几无可能，加之今年移动端形势之大乱，于是我放弃了。我终究是没有成功踏入互联网行业。&lt;/p&gt;

&lt;p&gt;在十月的一个难眠之夜，随手更新了简历，开始投工控软件开发等岗位。由于读研期间的一点儿背景，工作比想象中的好找，在科技园某自动化设备厂家写了3周的工控软件。期间又去北京面试了一家通信公司，几经权衡决定辞去深圳的工作北上。从深圳到北京，此番舍近求远，是好是坏，留待以后评价。&lt;/p&gt;

&lt;p&gt;在科技园工作三周，我没什么成果，只是维护旧系统的一丁点功能。制造业的软件开发技术更新换代极为缓慢，Borland VCL、MFC仍然没被淘汰，较新的技术也许就是UI框架改用Qt。不过看了Qt5.x的示例，这个框架越来越强大了。辞职之后有一周左右空闲时间，本打算就此机会看一看Qt，翻一翻用Qt写的qcad、libreCAD源码，以及Qt中的IPC机制——DBUS，看来只能再拖一拖了。&lt;/p&gt;

&lt;p&gt;深圳繁花似锦、四季常绿、无雾霾与寒冬，今将北上，还是难以割舍。难忘这里的红和绿，希望后会有期！&lt;/p&gt;
</description>
        <pubDate>Wed, 30 Nov 2016 05:10:00 +0000</pubDate>
        <link>https://solobearchn.github.io//2016/11/30/%E5%86%8D%E8%A7%81%E6%B7%B1%E5%9C%B3/</link>
        <guid isPermaLink="true">https://solobearchn.github.io//2016/11/30/%E5%86%8D%E8%A7%81%E6%B7%B1%E5%9C%B3/</guid>
        
        <category>murmurs</category>
        
        
        <category>Blogs</category>
        
      </item>
    
      <item>
        <title>Android Binder机制浅析</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#binder机制驱动与数据传输&quot; id=&quot;markdown-toc-binder机制驱动与数据传输&quot;&gt;Binder机制：驱动与数据传输&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#binderservicelibbinder中的接口与对象&quot; id=&quot;markdown-toc-binderservicelibbinder中的接口与对象&quot;&gt;Binder/Service：libbinder中的接口与对象&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#binder对象在驱动中的转换&quot; id=&quot;markdown-toc-binder对象在驱动中的转换&quot;&gt;Binder对象在驱动中的转换&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#servicemanager做了什么&quot; id=&quot;markdown-toc-servicemanager做了什么&quot;&gt;ServiceManager做了什么&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#服务的注册过程&quot; id=&quot;markdown-toc-服务的注册过程&quot;&gt;服务的注册过程&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#binder机制中的多线程&quot; id=&quot;markdown-toc-binder机制中的多线程&quot;&gt;Binder机制中的多线程&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#总结&quot; id=&quot;markdown-toc-总结&quot;&gt;总结&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#references&quot; id=&quot;markdown-toc-references&quot;&gt;References&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;笔记：简单、直观、透彻地理解Android Binder进程间通信的基本原理和基本流程：数据在进程间是怎样流动传输的，客户端是怎样拿到服务端的接口的。&lt;/p&gt;

&lt;h2 id=&quot;binder机制驱动与数据传输&quot;&gt;Binder机制：驱动与数据传输&lt;/h2&gt;

&lt;p&gt;Android中的Binder机制涉及Java应用层、Native应用层（libbinder、C++）和Linux内核层（binder driver，C），其中Java层是对C++ Native层的包装，而应用层进程之间的通信则最终通过内核中的驱动Binder dirver完成。这种跨进程调用，进程之间的数据必须放在内核空间然后等待。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://img.my.csdn.net/uploads/201610/14/1476404610_9170.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Binder机制作为Android的进程间通信方式(IPC)，采用mmap共享内存方式，应用层直接从与kernel共享的缓冲区读取，只产生一次读写提高了数据传输效率。其实质是在Client-Server之间利用虚拟的字符设备/dev/binder作为CS两端的数据传输中介。应用层（Client、Server）的通信是
&lt;strong&gt;通过与driver的IO&lt;/strong&gt;实现的：Client与Driver设备IO，Server则轮询Driver中的数据，并将结果反馈至Driver，Client再从Driver中读取结果。当然这里的Server通信依赖Binder机制，是一个Binder类型的Service。&lt;/p&gt;

&lt;p&gt;应用层与内核中的driver之间的IO操作使用统一的接口函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ioctl(fd,CMD,&amp;amp;bwr)&lt;/code&gt;，通过switch(CMD)进行相应的IO操作，所传输的数据则存放在binder_read_write结构体中。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;binder_write_read&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;binder_size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;binder_size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write_consumed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;binder_uintptr_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;write_buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;binder_size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;read_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;binder_size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;read_consumed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;binder_uintptr_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;read_buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;存放在bwr的buffer中的数据也有格式，即消息ID+binder_transaction_data&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;binder_transaction_data&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;k&quot;&gt;union&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;__u32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;binder_uintptr_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;binder_uintptr_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cookie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;__u32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;__u32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;pid_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sender_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;uid_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sender_euid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;binder_size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;binder_size_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;offsets_size&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;k&quot;&gt;union&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;binder_uintptr_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;binder_uintptr_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;offsets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;__u8&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;上面的binder_transact_data中包括了从应用层传输过来的Parcel类的数据data，以及Binder对象数据。驱动层只负责操作转换Binder相关的数据，对应用层Parcel中的普通数据并不关心。当然如果Parcel中打包了Binder对象，驱动也会进行处理。Binder对象在跨进程传输时，驱动会对其进行转换。&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;binderservicelibbinder中的接口与对象&quot;&gt;Binder/Service：libbinder中的接口与对象&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;http://gityuan.com/images/binder/addService/add_media_player_service.png&quot; alt=&quot;image&quot; /&gt;
&lt;em&gt;来源：Gityuan&lt;/em&gt; &lt;a href=&quot;http://gityuan.com/2015/11/14/binder-add-service/&quot;&gt;Binder系列5—注册服务(addService)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;IBinder与Service业务接口INTERFACE之间的关系如上图所示，图中没有指出的是BpMediaPlayerService类对BpBinder的聚合关系。&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Binder实体对象：BBinder，Binder服务的提供者，Binder机制中提供服务的Service必须继承自BBinder类；&lt;/li&gt;
  &lt;li&gt;Binder引用对象：BpBinder(handle)，存在于Client端，handle指向服务端。&lt;/li&gt;
  &lt;li&gt;Binder代理对象：BpINTERFACE(BpBinder(handle)):实现了Service服务中的业务接口INTERFACE的接口对象，通过代理对象，Client能够像使用本地对象一样使用远端的实体对象提供的服务。&lt;/li&gt;
  &lt;li&gt;IBinder对象：BBinder+BpBinder的统称。&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;图中以MediaPlayerService为例，其业务代码接口IMediaPlayerService提供给实体对象和代理对象相同的业务函数接口。BBinder实体对象MediaPlayerService继承了BnINTERFACE，并重写了onTransact()成员函数作为实际的业务处理过程。在CS模式中，Client获取了Service/Binder引用对象后，构造了BpMPS(BpBinder(handle))，在使用业务函数时，其参数经由驱动传递至Service进程，服务进程轮询driver中的数据、执行业务逻辑并reply。&lt;/p&gt;

&lt;p&gt;Client当然可以跳过代理对象，直接通过Binder引用对象BpBinder(handle)使用服务，但是代理对象具备了处理业务的接口，依靠native层的
&lt;strong&gt;libbinder框架带来业务逻辑接口的统一&lt;/strong&gt;，Client就像使用本地接口一样使用远程的服务接口。&lt;/p&gt;

&lt;p&gt;下面可以简单探讨一下Binder机制下服务类的写法。以ExmapleService为例，其业务逻辑接口由IExampleService定义，业务接口为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;virtual int getData();&lt;/code&gt;。建立的ExampleService类则实现了业务接口&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ExampleService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BnExampleService&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;public:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;}&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;private:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mData&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x123456&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;相应的，BnExmapleService中的onTransact()：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;BnExampleService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;onTransact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Parcel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Parcel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GET_DATA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;reply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writeInt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;......&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;代理类BpExmaple应当这样实现&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;BpExampleService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BpInterface&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IExampleService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;public:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;virtual&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getData&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;remote&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GET_DATA&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readInt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Client获得代理类BpExample，其引用对象remote=BpBinder(handle)。引用对象的transact()实际上交由IPCThreadState执行，其请求码（函数号）为GET_DATA。Service端收到请求码后，调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BBinder::transact()&lt;/code&gt;，继而调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExampleServic::onTransact()&lt;/code&gt;返回。&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;binder对象在驱动中的转换&quot;&gt;Binder对象在驱动中的转换&lt;/h2&gt;

&lt;p&gt;Binder对象在传输中是跨进程的，其生命周期的管理是一个重点。服务的实体BBinder死亡后，Client进程中的引用对象也应当删除。Client进程中的ProcessState管理本进程中的所有Binder引用类的创建和释放，而引用对象和实体对象之间的关联则由驱动负责管理。Driver驱动维护了一颗红黑树，每个进程中的binder_proc结构体都插入树中，每个进程中的binder_proc都保存了Binder对象的node节点表和node_ref引用表。Binder对象的插入和查询就是在这棵树中执行的，&lt;/p&gt;

&lt;p&gt;在传输过程中，Binder实体对象和引用对象均使用
&lt;strong&gt;flat_binder_object结构体来表示Binder对象&lt;/strong&gt;。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;flat_binder_object&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;__u32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;__u32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;k&quot;&gt;union&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;binder_uintptr_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;binder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;__u32&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
 &lt;span class=&quot;n&quot;&gt;binder_uintptr_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cookie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;以上type常用BINDER_TYPE_BINDER/BINDER_TYPE_HANDLE标识，flag表示传输方式，union则存储了binder的本地指针或者远程引用对象的句柄handle，cookie只在打包BBinder中使用，存放了BBinder的指针。&lt;/p&gt;

&lt;p&gt;Driver中的一个重要工作就是处理传递中的Binder对象，driver会将flat_binder_object结构体拆开并做相应操作。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BINDER_TYPE_BINDER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BINDER_TYPE_WEAK_BINDER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;......&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;binder_node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;binder_get_node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;binder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;......&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;binder_get_ref_for_node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target_proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BINDER_TYPE_BINDER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BINDER_TYPE_HANDLE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;......&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;handle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;desc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当类型为BINDER_TYPE_BINDER时，这是一个Binder实体对象，此时会使用binder_get_node在发送进程的Binder对象节点nodes中查找节点，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;binder_get_ref_for_node&lt;/code&gt;在目标进程中查找引用节点，若无则创建新节点。修改type为handle类型，将节点引用表中的序号赋给handle。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BINDER_TYPE_HANDLE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BINDER_TYPE__HANDLE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;binder_ref&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;binder_get_ref&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;target_proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BINDER_TYPE_BINDER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;binder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;fp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cookie&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cookie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;binder_ref&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_ref&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;binder_get_ref_for_node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target_proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ref&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;当类型为BINDER_TYPE_HANDLE时，这是一个Binder引用对象，首先&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;binder_get_ref(proc, fp-&amp;gt;handle)&lt;/code&gt;根据handle在发送进程的节点引用表中查找引用节点。当目标进程就是Binder实体对象所在进程时，修改type并设置字段，其中cookie存放了BBinder指针。如果不是Binder对象所在进程，则在目标进程中新建一个节点对象的引binder_ref，type不会改动。这个binder_ref数据结构如下：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;binder_ref&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;rb_node&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rb_node_desc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;rb_node&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rb_node_node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;hlist_node&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;node_entry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;binder_proc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;binder_node&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;uint32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;desc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strong&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weak&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;binder_ref_death&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;death&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;其中，数值&lt;strong&gt;uint32_desc&lt;/strong&gt;就是引用项在红黑树中的序号，即BpBinder中的mHandle。&lt;/p&gt;

&lt;p&gt;根据上面的过程，可以简要分析常用场景中Binder对象在整个传输过程中做了怎样的转换：&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://img.my.csdn.net/uploads/201610/14/1476408704_3527.png&quot; alt=&quot;image&quot; /&gt;
Binder机制中CS两端通信的数据单元是Parcel，Binder对象写入Parcel中时需要使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeStrongBinder&lt;/code&gt;函数，读取Binder对象则使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readStrongBinder&lt;/code&gt;,当然这个过程中Binder对象仍然要用flat_binder_object结构体表示。&lt;/p&gt;

&lt;p&gt;当某个Server/Service要向ServiceManager注册时，发送本地的Binder实体对象，写入Parcel中的Binder如下：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BINDER_TYPE_BINDER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;binder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;reinterpret_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uintptr_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getWeakRefs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cookie&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;reinterpret_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;uintptr_t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;local&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;此Binder对象数据经驱动转换后type=BINDER_TYPE_HANDLE，可以注册进ServiceManager了。而当某个第三方Client想要获取这个Service时，ServiceManager发送给驱动的Binder对象如下&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;obj-&amp;gt;type = BINDER_TYPE_HANDLE;
obj-&amp;gt;handle = handle;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;第三方Client进程中并没有这个Binder实体，因此Client进程中新建了一个节点引用这个Binder，拿到了mHandle，后续利用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;readStrongBinder&lt;/code&gt;构造了代理对象。&lt;/p&gt;

&lt;p&gt;而当这个第三方Client手握handle句柄值，想调用Service所在进程的业务逻辑时，向驱动发送的Binder对象其type=BINDER_TYPE_HANDLE。驱动根据根据handle找到了目标进程，发现Client所引用的Binder对象就在目标进程里，于是改动这个结构体内的type,target和cookie。文章后面会提到，
&lt;strong&gt;正是由于这种转换，Service拿到转换后的Binder对象，会执行相应的业务逻辑并返回结果&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;总而言之，CS模型中的Binder对象经由驱动往来穿梭，驱动通过对flat_binder_object结构的操作,实现了Binder对象的不同状态的转变。&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;servicemanager做了什么&quot;&gt;ServiceManager做了什么&lt;/h2&gt;

&lt;p&gt;Client和Service之间的通信，依赖Driver驱动，同时也要依赖ServiceManager在这个通信过程中起到名字查询作用。ServiceManager是一个守护进程，为各类Binder Service提供
&lt;strong&gt;名字查询功能，以及返回Binder Service的引用&lt;/strong&gt;。ServiceManager同样依赖Binder机制提供服务，其引用句柄handle=0。SM提供给外部的服务主要是注册服务&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;addService(sp&amp;lt;IBinder&amp;gt;)&lt;/code&gt;，查询和返回服务&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getService(String* name)&lt;/code&gt;等，其接口函数定义在IServiceManager.h中。&lt;/p&gt;

&lt;p&gt;ServiceManager的主函数没有依赖libbinder框架，而是自建了一个简单的类似原理的bind.c和主函数:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;frameworks\native\cmds\servicemanager\service_manager.c,&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;binder_open&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;128&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;binder_become_context_manager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ALOGE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;cannot become context manager (%s)&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;strerror&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;errno&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;selinux_enabled&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_selinux_enabled&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sehandle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;selinux_android_service_context_handle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;svcmgr_handle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BINDER_SERVICE_MANAGER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;binder_loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;svcmgr_handler&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;主要步骤是，对/dev/binder设备进行初始化后，将本进程设置为Binder管理进程，检查权限，最后进入消息循环
binder_loop(bs, svcmgr_handler)
中轮询Driver中的数据，使用消息处理函数svcmgr_handler处理请求。&lt;/p&gt;

&lt;p&gt;ServiceManager维护了一个服务列表svc_list，列表内是svcinfo结构体。注册服务时则将新的Service加入列表，查询/获取服务则将搜索列表返回相应服务的handle：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x7f&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FLAT_BINDER_FLAG_ACCEPTS_FDS&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BINDER_TYPE_HANDLE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;handle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;handle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cookie&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;上面的Binder对象obj，是一个flat_binder_object结构体，ServiceManager将obj传输至驱动。&lt;/p&gt;

&lt;p&gt;需要指出的是，Binder服务的句柄handle是一个u_int32数值，只对发起请求的Client端进程和驱动有意义，不同进程中指代相同Binder实体的句柄在数值上可能是不同的，ServiceManager记录了所有系统service对应的handle，当用户进程需要获取某个系统service的代理时，SMS就会在内部按service名查找到合适的句柄值handle，并“逻辑上”传递给用户进程，于是用户进程会得到一个新的合法句柄值，这个新句柄值可能在数值上和SMS所记录的句柄值不同，然而，它们指代的却是同一个Service实体，句柄的合法性是由Binder驱动保证的。&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;服务的注册过程&quot;&gt;服务的注册过程&lt;/h2&gt;

&lt;p&gt;这一段各种资料讲的比较多，常常以MediaPlayerService的注册过程入手分析，在注册过程中，MPS是一个Client，向ServiceManager这个Server注册自己。当注册完毕后，MPS充当一个服务的角色，轮询驱动中的消息。&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;frameworks\av\media\mediaserver\main_mediaserver.cpp&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argc&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__unused&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;argv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;sp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ProcessState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ProcessState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;    
    &lt;span class=&quot;n&quot;&gt;sp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IServiceManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sm&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;defaultServiceManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;      
    &lt;span class=&quot;n&quot;&gt;MediaPlayerService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instantiate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;     
    &lt;span class=&quot;n&quot;&gt;ProcessState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;startThreadPool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;    
    &lt;span class=&quot;n&quot;&gt;IPCThreadState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;joinThreadPool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
 &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;proc(ProcessState::self())
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;每个Client进程都要对本进程中Binder引用对象进行生命周期管理，这里打开了/dev/binder驱动,mmap了1MB空间用于binder事务。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;defaultServiceManager():获取ServiceManager，准确的说是获取ServiceManager的代理
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这个函数经过IIntergace.h的宏展开后，进入了ISM.asInterface()函数：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;n&quot;&gt;android&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IServiceManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IServiceManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asInterface&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;android&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;android&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IBinder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;android&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IServiceManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;intr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;intr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;static_cast&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IServiceManager&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;queryLocalInterface&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IServiceManager&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;descriptor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;intr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;intr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BpServiceManager&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;obj&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;intr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;注意到判断函数queryLocalInterface(),这是IBinder接口中的函数，默认返回null。BpBinder类并没有重写这个函数，intr=null，asInterface返回new BpServiceManager(BpBinder),这样就获取了ServiceManager的代理对象。也就是说，
&lt;strong&gt;Client拿到Binder引用对象后，就得到了Binder代理对象&lt;/strong&gt;，&lt;/p&gt;

&lt;p&gt;而如果是Binder实体对象，继承自BnInterface，而BnInterface重写了这个函数，判断对象的描述字符串descriptor是否和本类相同，如果符合就返回这个指针。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;template&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;typename&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;INTERFACE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;inline&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;IInterface&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BnInterface&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;INTERFACE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;queryLocalInterface&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;String16&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_descriptor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_descriptor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;INTERFACE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;descriptor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;NULL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;从CS两端的角度来看这个，Client端获取的必定是引用对象，因此使用BpBinder创建了代理对象。Service端收到实体对象后，如果类描述符合，则可以直接使用本进程中的对象，因此接返回对象指针。&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;MediaPlayerService::instantiate()    
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这一行也就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BpServiceManager(BpBinder(0)).addService(Str name,new MPS)&lt;/code&gt;，接着进入IServiceManager的业务逻辑，利用了指向ServiceManager的引用对象BpBinder(0)。接着进入BpBinder.transact过程，实质上还是将任务交给了IPCThreadState:&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IPC-&amp;gt;transact(mHandle, code, data, reply,flags)&lt;/code&gt;，对于Client来说，所需数据就通过&lt;strong&gt;reply&lt;/strong&gt;获取。&lt;/p&gt;

&lt;p&gt;每个线程都有一个IPCThreadState，成员变量mProcess保存了ProcessState变量(每个进程只有一个)，mIn用来接收来自Binder设备的数据，mOut用来存储发往Binder设备的数据，默认大小均为256字节。IPCThreadState进行transact事务处理分3部分：
&lt;strong&gt;errorCheck()数据错误检查，writeTransactionData()写入传输数据，waitForResponse()等待驱动数据响应&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;其中，waitForResponse()是一个死循环，利用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IPC.talkWithDriver()&lt;/code&gt;与驱动之间通信,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IPC.talkWithDriver()&lt;/code&gt;中又调用了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ioctl(mProcess-&amp;gt;mDriverFD, BINDER_WRITE_READ, &amp;amp;bwr)&lt;/code&gt;进入与内核，开始于驱动的通信,ioctl调用了内核中的binder.c。&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;kernel\drivers\staging\android\binder.c&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;进入ioctl后，经由&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;binder_thread_write()&lt;/code&gt;中的判断分支进入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;binder_transaction()&lt;/code&gt;过程。binder_transaction()内容比较多，主要步骤是：&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;找到代表目标进程的节点，handle=0则指向ServiceManager&lt;/li&gt;
  &lt;li&gt;搜寻目标线程，使用目标线程中的todo队列&lt;/li&gt;
  &lt;li&gt;对本次Binder调用事务过程创建binder_transaction结构体，设置相应的数据&lt;/li&gt;
  &lt;li&gt;在目标进程的缓冲区分配空间，并复制用户进程的数据到内核&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;处理传输的Binder对象&lt;/strong&gt;，前面已经分析了这个过程&lt;/li&gt;
  &lt;li&gt;将本次调用的binder_transaction结构体链接到线程的transaction_stack列表中.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;binder_thread_write()调用结束后，将继续binder_thread_read()处理当前线程和进程中的todo队列。&lt;/p&gt;

&lt;p&gt;下面离开内核，回到应用层。在waitForResponse()这个循环中，除了talkWithDriver()，还调用了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IPC.executeCommand(cmd)&lt;/code&gt;过程，用于响应驱动传来的指令，处理来自Driver的数据了。
&lt;strong&gt;对于Service来说，业务逻辑就在这里执行。&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;status_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IPCThreadState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;executeCommand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int32_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BR_TRANSACTION&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ptr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;sp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BBinder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BBinder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cookie&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;error&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;transact&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;buffer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;flags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt; 
            &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;......&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;    
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;在驱动对Binder对象的转换过程中提到，Client发送的Binder对象进入Service所在的目标进程时，其target、cookier都做了转换。对于经驱动处理后的Binder对象，由于Service进程自身就是Binder实体对象，因此执行了以上的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BBinder-&amp;gt;transact()&lt;/code&gt;函数。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BBinder::transact()&lt;/code&gt;函数又调用了Binder实体类的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onTransact()&lt;/code&gt;函数。前面分析过libbinder中的类和接口，Service类继承了BnExampleService并重写了onTransact()，这个onTransact中使用了ServiceManager中的函数体，&lt;strong&gt;实际服务的业务逻辑就在这里执行&lt;/strong&gt;。而这个onTransact字面意思也说明，服务本身不主动执行业务逻辑，而是通过响应客户端的调用执行。&lt;/p&gt;

&lt;p&gt;既然使用transaction描述过程，表明Binder调用过程是一个事务，需要符合ACID原则。下图中BC_TRANSACTION和BR_TRANSACTION是一个完整的事务过程，BC_REPLY和BR_REPLY是一个完整的事务过程。Client向Server请求的过程是一个同步过程，Client需要等待driver中的reply，在此期间线程阻塞。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://img.my.csdn.net/uploads/201610/14/1476404609_2669.png&quot; alt=&quot;image&quot; /&gt;&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;binder机制中的多线程&quot;&gt;Binder机制中的多线程&lt;/h2&gt;

&lt;p&gt;还有两行代码没有分析，下面来看看。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;ProcessState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;startThreadPool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;IPCThreadState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;joinThreadPool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;第一行看起来是启动了线程池，看看startThreadPool函数怎么实现的：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ProcessState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;startThreadPool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;AutoMutex&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mLock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mThreadPoolStarted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;mThreadPoolStarted&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;spawnPooledThread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;对mThreadPoolStarted的判断，表明这个startThreadPool只能启动一次，线程池启动后，这个值为true。接下来看看spawnPooledThread()：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ProcessState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spawnPooledThread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isMain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mThreadPoolStarted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;String8&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;makeBinderThreadName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;ALOGV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Spawning new pooled thread, name=%s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;sp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;PoolThread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isMain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;函数中创建了一个PoolThread类，PoolThread.run()则创建了新线程。isMain变量表示这个线程是否是线程池中的第一个。上面提到在Binder事务中还要执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IPCThreadState::executeCommand(cmd)&lt;/code&gt;响应驱动传来的消息，当驱动传来BR_SPAWN_LOOPER时，就要执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;spawnPooledThread(false)&lt;/code&gt;,这就是驱动传过来的消息，要创建新线程了。&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BR_SPAWN_LOOPER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;mProcess&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spawnPooledThread&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;PoolThread类的执行体是IPC.threadLoop()函数，函数里是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IPCThreadState::self()-&amp;gt;joinThreadPool(mIsMain)&lt;/code&gt;，也就是说线程池里的线程都是在执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;joinThreadPool(bool)&lt;/code&gt;。对于第一个线程，这是由应用层创建的，isMain=true；其他线程均是驱动告知目标进程创建的，isMain=false。这里也可以发现，Binder机制下的
&lt;strong&gt;所有线程都有一个关联的IPCThreadState对象，与驱动之间的通信实际上都是由这个IPCThreadState执行的&lt;/strong&gt;。&lt;/p&gt;

&lt;p&gt;该看看线程执行体了：&lt;/p&gt;

&lt;div class=&quot;language-c++ highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IPCThreadState&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;joinThreadPool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;bool&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isMain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mOut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writeInt32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isMain&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BC_ENTER_LOOPER&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BC_REGISTER_LOOPER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;set_sched_policy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mMyThreadId&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SP_FOREGROUND&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;status_t&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;processPendingDerefs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getAndExecuteCommand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TIMED_OUT&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;isMain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ECONNREFUSED&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EBADF&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mOut&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;writeInt32&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BC_EXIT_LOOPER&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;talkWithDriver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;这个执行体是一个循环体，使用了两个函数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;processPendingDerefs()&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getAndExecuteCommand()&lt;/code&gt;,前者用来处理IPCThreadState对象中Binder对象的引用计数。后者顾名思义就是读取驱动的指令并执行，内部就是调用了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;talkWithDriver()和executeCommand()&lt;/code&gt;。isMain变量写入mOut，告诉驱动本线程已经建立，同时便于驱动区分
&lt;strong&gt;应用层主动创建的“主”线程&lt;/strong&gt;和
&lt;strong&gt;受驱动通知创建的线程&lt;/strong&gt;，非主线程在空闲时会被驱动要求退出。&lt;/p&gt;

&lt;p&gt;应用层进程创建了多个线程与驱动通信，即有多个线程在调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ioctl(mProcess-&amp;gt;driverFD,cmd,&amp;amp;data)&lt;/code&gt;。而同一个进程中的多个线程共用一个驱动文件描述符driverFD，因此有多个线程在等待同一个驱动缓冲区域返回的数据。在这里，
&lt;strong&gt;驱动记录了每次Binder调用时的线程ID，唤醒对应的线程读取缓冲区数据&lt;/strong&gt;。在Binder机制中，&lt;strong&gt;由内核驱动来处理应用层线程的创建和唤醒&lt;/strong&gt;，这和一般的IO模型不同。&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;总结&quot;&gt;总结&lt;/h2&gt;

&lt;p&gt;以上内容主要是便于&lt;strong&gt;简单、粗暴、直观地&lt;/strong&gt;理解进程间通信的基本原理和基本流程，一般资料中翔实的过程代码细节就不涉及了。&lt;/p&gt;

&lt;p&gt;对于进程间通信，最想了解的不外乎最直观的两点：&lt;strong&gt;数据在进程间是怎样流动传输的，客户端是怎样拿到服务端的接口的&lt;/strong&gt;，这两点背后的机制在于Binder驱动层的&lt;strong&gt;mmap共享内存、红黑树节点和引用的维护&lt;/strong&gt;。理解了以上两点后，剩下的不过是梳理应用层libbinder构建的与底层驱动通信的模型，这其中涉及类与接口的设计、Binder调用事务过程以及多线程。&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;
&lt;p&gt;这是一篇笔记，参考了：&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://book.douban.com/subject/26377840/&quot;&gt;刘超 深入解析Android 5.0系统&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://weishu.me/2016/01/12/binder-index-for-newer/&quot;&gt;Weishu’s Notes :Binder学习指南&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://gityuan.com/tags/#binder&quot;&gt;Gityuan #binder&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://my.oschina.net/youranhongcha/blog/149578&quot;&gt;红茶一杯话Binder（ServiceManager篇）&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://blog.csdn.net/coding_glacier/article/details/7520199&quot;&gt;Glacier的专栏:Android Binder机制&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Mon, 17 Oct 2016 08:00:00 +0000</pubDate>
        <link>https://solobearchn.github.io//2016/10/17/Android-Binder%E6%9C%BA%E5%88%B6%E6%B5%85%E6%9E%90/</link>
        <guid isPermaLink="true">https://solobearchn.github.io//2016/10/17/Android-Binder%E6%9C%BA%E5%88%B6%E6%B5%85%E6%9E%90/</guid>
        
        <category>IPC</category>
        
        <category>Binder</category>
        
        <category>Service</category>
        
        <category>多线程</category>
        
        
        <category>Android</category>
        
      </item>
    
      <item>
        <title>Android消息机制与多线程</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#android中的消息机制&quot; id=&quot;markdown-toc-android中的消息机制&quot;&gt;android中的消息机制&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#asynctask异步框架&quot; id=&quot;markdown-toc-asynctask异步框架&quot;&gt;AsyncTask异步框架&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#看看源码api23&quot; id=&quot;markdown-toc-看看源码api23&quot;&gt;看看源码(API23)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;android中的消息机制&quot;&gt;android中的消息机制&lt;/h2&gt;

&lt;hr /&gt;

&lt;p&gt;Handler、Looper和MessageQueue:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Message被包装在Looper中，且Looper是线程相关的，每个线程只有一个Looper&lt;/li&gt;
  &lt;li&gt;UI主线程的启动参见framework中的ActivityThread.java，在main(String[] args)函数中启动了Looper.loop消息循环&lt;/li&gt;
  &lt;li&gt;新建一个Handler对象后，在Handler类的构造函数中会将Handler对象与所在线程关联，因此Handler对应了所在线程的Looper&lt;/li&gt;
  &lt;li&gt;Handler的sendMessage向所关联的消息队列发送message，message内则包含了对应的消息处理的target:Handler&lt;/li&gt;
  &lt;li&gt;Looper.loop循环中，message执行了回调message.target.dispatchMessage(message),进而调用了使用者所Overrdie的Handler.handleMessage()方法&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;asynctask异步框架&quot;&gt;AsyncTask异步框架&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    &lt;p&gt;构造函数中的几个泛型参数中：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Params...params&lt;/code&gt;是执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AsyncTask.execute(Params...)&lt;/code&gt;时使用的参数，实际上传入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doInBackground(Params...)&lt;/code&gt;作为参数;
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Progress... values&lt;/code&gt;表示进度，可在调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;publishProgress(Progress...)&lt;/code&gt;时将进度送至主消息循环中；进度同样会传入用户override的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onProgressUpdate(Progress)&lt;/code&gt;作为方法参数； &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Result result&lt;/code&gt;是后台任务方法&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doInBackgroun(Params...)&lt;/code&gt;的返回值，即任务结果,作为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onPostExecute(Result)&lt;/code&gt;方法的参数。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new AsyncTask().execute(Params...params)&lt;/code&gt;:UI线程中调用，触发异步任务AsyncTask&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Override onPreExecute()&lt;/code&gt;:在UI线程中执行，异步任务启动前的准备工作&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Override doInBackground(Params...)&lt;/code&gt;:在任务子线程中执行，通常用于理耗时任务。在此方法中用户可以自行调用
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;publishProgress(Progress...)&lt;/code&gt;，发送&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MESSAGE_POST_PROGRESS&lt;/code&gt;消息至主线程，进而触发主线程中的方法&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onProgressUpdate(Progress...)&lt;/code&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Override onProgressUpdate(Progress...)&lt;/code&gt;:后台任务调用了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;publishProgress()&lt;/code&gt;后，主线程对消息的回应处理，可用于更新UI&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Override onPostExecute(Result)&lt;/code&gt;:任务子线程执行完毕后触发结束消息的处理方法，收尾工作。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;看看源码api23&quot;&gt;看看源码(API23)&lt;/h2&gt;

&lt;p&gt;AsyncTask将Handler、Looper和MessageQueuq进行了包装，用线程池执行后台任务。因此主要来看后台任务
&lt;strong&gt;FutureTask&lt;/strong&gt;、消息处理
&lt;strong&gt;Handler&lt;/strong&gt;以及
&lt;strong&gt;ThreadPoolExecutor&lt;/strong&gt;是如何设计的：&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Handler：&lt;/p&gt;

    &lt;p&gt;构造了InternalHandler, 其构造函数获取MainLoop，即Handler是和主线程消息循环绑定的。消息处理方法&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;handleMessage&lt;/code&gt;则对&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MESSAGE_POST_RESULT&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MESSAGE_POST_PROGRESS&lt;/code&gt;消息做出了响应。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;FutureTask&lt;/p&gt;

    &lt;p&gt;AsyncTask构造函数创建了Callable类型的对象
&lt;strong&gt;mWorker&lt;/strong&gt;,call()方法则包装了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;doInBackGround()&lt;/code&gt;方法，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postResult()&lt;/code&gt;方法得到result并发送MESSAGE_POST_RESULT消息。当然，在doInBackground中则可以调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;publishProgress(Progress...)&lt;/code&gt;。mWorker用于构造FutureTask类型
&lt;strong&gt;mFuture&lt;/strong&gt;。mFuture就是任务执行体了。&lt;/p&gt;

    &lt;p&gt;比较了几个版本的源码之后，我发现API22之后，Handler的创建是在mWorker 的call()方法中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;postResult()&lt;/code&gt;方法中完成的, 采用了单例模式的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getHandler()&lt;/code&gt;创建/获取InternalHandler。早期的版本(Android5.0)则在类的静态成员中就已经创建此Handler对象。尽管这里Handler的创建在子线程，但是由于其构造函数与主线程Looper绑定，因此消息处理仍然在主线程完成。&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;executors相关&lt;/p&gt;

    &lt;p&gt;用户在主线程中调用execute(Params…parms)后，实际上执行了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;executeOnExecutor(sDefaultExecutor, params)&lt;/code&gt;，此方法内首先执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;onPreExecute()&lt;/code&gt;，随后则是&lt;strong&gt;执行任务过&lt;/strong&gt;程：&lt;strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sDefaultExecutor.execute(mFuture)&lt;/code&gt;&lt;/strong&gt;。&lt;/p&gt;

    &lt;p&gt;再来看看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sDefaultExecutors&lt;/code&gt;, 这是一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SerialExecutor&lt;/code&gt;类的实例，其&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;execute()&lt;/code&gt;方法用于维持一个任务队列，并将将Runnable放入mTasks队尾，随后Poll一个task送入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;THREAD_POOL_EXECUTOR&lt;/code&gt;执行。也就是说&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sDefaultExecutors&lt;/code&gt;只起到维护任务队列的过程，&lt;strong&gt;真正的执行体还是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;THREAD_POOL_EXECUTOR&lt;/code&gt;&lt;/strong&gt;，这是一个线程池&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new ThreadPoolExecutor(...)&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;值得注意的是，尽管将任务送入了线程池&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;THREAD_POOL_EXECUTOR&lt;/code&gt;，但是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SerialExecutor.execute()&lt;/code&gt;方法使用了同步锁，实质上是一个线性入队执行的单线程过程，从类名上也是可以看出来的：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;public synchronized void execute(final Runnable r)&lt;/code&gt;，即使多个线程提交任务，也得一个一个来。这也是android3.0之后的变化，旧版本的AsyncTask是支持在线程池中多任务并发的。&lt;/p&gt;

    &lt;p&gt;线程池的执行过程不再赘述，submit()和execute()最终都是调用了callable的call()方法。call()过程中发送的两类消息则可以被InternalHandler获取并得到相应处理。&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Mon, 17 Oct 2016 07:00:00 +0000</pubDate>
        <link>https://solobearchn.github.io//2016/10/17/Android%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%AC%94%E8%AE%B0/</link>
        <guid isPermaLink="true">https://solobearchn.github.io//2016/10/17/Android%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%AC%94%E8%AE%B0/</guid>
        
        <category>消息</category>
        
        <category>多线程</category>
        
        <category>AsyncTask</category>
        
        
        <category>Android</category>
        
      </item>
    
      <item>
        <title>第一篇博客</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#博客模板&quot; id=&quot;markdown-toc-博客模板&quot;&gt;博客模板&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#关于markdown&quot; id=&quot;markdown-toc-关于markdown&quot;&gt;关于MarkDown&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#试一试jekyll的md&quot; id=&quot;markdown-toc-试一试jekyll的md&quot;&gt;试一试Jekyll的md&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;博客模板&quot;&gt;博客模板&lt;/h2&gt;

&lt;p&gt;使用了&lt;a href=&quot;https://github.com/Gaohaoyang&quot;&gt;Gaohaoyang&lt;/a&gt;的Jekyll模板。说来也巧，高浩阳是我同一级的校友，当年在人人网上很出名，玩儿校园民谣的。&lt;/p&gt;

&lt;h2 id=&quot;关于markdown&quot;&gt;关于MarkDown&lt;/h2&gt;

&lt;p&gt;写博客中感觉最蛋疼的，还是MarkDown，因为没有统一的标准。我喜欢用有道云笔记，但是转到GitHub上代码缩进就一团糟，而且Github貌似还不支持流程图？关于所见即所得的编辑器，我还用过CmdMarkDown、CSDN、简书，但每家都不太一样，头大。现在只好用Sublime写然后预览了。&lt;/p&gt;

&lt;h2 id=&quot;试一试jekyll的md&quot;&gt;试一试Jekyll的md&lt;/h2&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;md&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;以及这样的呢&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;findViewById(R.id.textView)&lt;/code&gt; ，看看效果？&lt;/p&gt;

</description>
        <pubDate>Mon, 17 Oct 2016 06:00:00 +0000</pubDate>
        <link>https://solobearchn.github.io//2016/10/17/welcome/</link>
        <guid isPermaLink="true">https://solobearchn.github.io//2016/10/17/welcome/</guid>
        
        <category>murmurs</category>
        
        
        <category>Blogs</category>
        
      </item>
    
  </channel>
</rss>
